MutableDataSource
A wrapper around Android Paging Library's DataSources to allow the free mutation of their resulting items.
100% Kotlin
❤️
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
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.