mutabledatasource

A wrapper around Android Paging Library's DataSources to allow the free mutation of their resulting items.

License

License

Categories

Categories

Data
GroupId

GroupId

dev.sarquella.mutabledatasource
ArtifactId

ArtifactId

mutabledatasource
Last Version

Last Version

0.1.1
Release Date

Release Date

Type

Type

aar
Description

Description

mutabledatasource
A wrapper around Android Paging Library's DataSources to allow the free mutation of their resulting items.
Project URL

Project URL

https://github.com/Sarquella/MutableDataSource
Source Code Management

Source Code Management

https://github.com/Sarquella/MutableDataSource

Download mutabledatasource

How to add to project

<!-- https://jarcasting.com/artifacts/dev.sarquella.mutabledatasource/mutabledatasource/ -->
<dependency>
    <groupId>dev.sarquella.mutabledatasource</groupId>
    <artifactId>mutabledatasource</artifactId>
    <version>0.1.1</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/dev.sarquella.mutabledatasource/mutabledatasource/
implementation 'dev.sarquella.mutabledatasource:mutabledatasource:0.1.1'
// https://jarcasting.com/artifacts/dev.sarquella.mutabledatasource/mutabledatasource/
implementation ("dev.sarquella.mutabledatasource:mutabledatasource:0.1.1")
'dev.sarquella.mutabledatasource:mutabledatasource:aar:0.1.1'
<dependency org="dev.sarquella.mutabledatasource" name="mutabledatasource" rev="0.1.1">
  <artifact name="mutabledatasource" type="aar" />
</dependency>
@Grapes(
@Grab(group='dev.sarquella.mutabledatasource', module='mutabledatasource', version='0.1.1')
)
libraryDependencies += "dev.sarquella.mutabledatasource" % "mutabledatasource" % "0.1.1"
[dev.sarquella.mutabledatasource/mutabledatasource "0.1.1"]

Dependencies

compile (5)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-android-extensions-runtime jar 1.3.61
org.jetbrains.kotlin : kotlin-stdlib-jdk7 jar 1.3.61
androidx.appcompat » appcompat jar 1.1.0
androidx.core » core-ktx jar 1.3.0
androidx.paging » paging-runtime-ktx jar 2.1.2

Project Modules

There are no modules declared in this project.

MutableDataSource

A wrapper around Android Paging Library's DataSources to allow the free mutation of their resulting items.

100% Kotlin ❤️

Maven Central API License

Follow

Buy Me a Coffee at ko-fi.com

GitHub Follow Twitter Follow

Download

dependencies {
    //...
    implementation 'dev.sarquella.mutabledatasource:mutabledatasource:0.1.1'
}

Usage

1- Apply the mutating function

Java (Java 8+ required)

MutableDataSource.Factory<Key, Value> mutableDataSourceFactory = DataSourceTransformation.mutateByPage(dataSourceFactory, original -> {
	//Mutate
});

Kotlin

val mutableDataSourceFactory = dataSourceFactory.mutateByPage { original ->
  //Mutate
}

Important The mutating function is applied per page. You will need to be careful in cases such as adding items at every page without taking into account that the original list has already loaded all items (page is empty), as it could lead to a never ending list.

2- Build the LiveData PagedList

Generating the LiveData<PagedList> instance from the MutableDataSource.Factory is the same as with the regular DataSource.Factory. However, instead of using LivePagedListBuilder to do so, MutableLivePagedListBuilder should be used:

val liveDataPagedList = MutableLivePagedListBuilder(mutableDataSourceFactory, pageSize)

Alternatively, the toLiveData extension function can also be used:

val liveDataPagedList = mutableDataSourceFactory.toLiveData(pageSize)

Placeholders [Optional]

In order to be able to use placeholders in the Paging Library, it should be capable of computing the total number of items even before loading all of them. When applying the mutation function, this total count might change due to modifying the number of items per page.

In case the total number of items after mutating is already known, it can be specified when applying the mutating function to continue making use of the placeholders:

Java

... = DataSourceTransformation.mutateByPage(dataSourceFactory, totalCount, original -> {
	//Mutate
});

Kotlin

... = dataSourceFactory.mutateByPage(totalCount) { original ->
  //Mutate
}

License

LICENSE

Copyright 2020 Adrià Sarquella Farrés

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Versions

Version
0.1.1
0.1.0