|
1 | 1 | # MutableDataSource |
| 2 | + |
| 3 | +A wrapper around Android [Paging Library](https://developer.android.com/topic/libraries/architecture/paging)'s DataSources to allow the free mutation of their resulting items. |
| 4 | + |
| 5 | +#### 100% Kotlin ❤️ |
| 6 | +[ ](https://bintray.com/sarquella/MutableDataSource/dev.sarquella.mutabledatasource/_latestVersion) [](https://android-arsenal.com/api?level=16) [](https://opensource.org/licenses/Apache-2.0) |
| 7 | + |
| 8 | +### Follow |
| 9 | +<a href='https://ko-fi.com/S6S8RENM' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a> |
| 10 | + |
| 11 | +[](https://github.com/Sarquella) [](https://twitter.com/AdriSarquella) |
| 12 | + |
| 13 | +## Download |
| 14 | +```groovy |
| 15 | +dependencies { |
| 16 | + //... |
| 17 | + implementation 'dev.sarquella.mutabledatasource:mutabledatasource:0.1.0' |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +1. Apply the mutating function |
| 24 | + |
| 25 | +```kotlin |
| 26 | +val mutableDataSourceFactory = dataSourceFactory.mutateByPage { original -> |
| 27 | + //Mutate |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +> **Important** |
| 32 | +> 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. |
| 33 | +
|
| 34 | +2. Build the LiveData PagedList |
| 35 | + |
| 36 | +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: |
| 37 | + |
| 38 | +```kotlin |
| 39 | +val liveDataPagedList = MutableLivePagedListBuilder(mutableDataSourceFactory, pageSize) |
| 40 | +``` |
| 41 | + |
| 42 | +Alternatively, the `toLiveData` extension function can also be used: |
| 43 | + |
| 44 | +```kotlin |
| 45 | +val liveDataPagedList = mutableDataSourceFactory.toLiveData(pageSize) |
| 46 | +``` |
| 47 | + |
| 48 | +## License |
| 49 | +[LICENSE](https://github.com/Sarquella/MutableDataSource/blob/master/LICENSE) |
| 50 | + |
| 51 | +``` |
| 52 | +Copyright 2020 Adrià Sarquella Farrés |
| 53 | +
|
| 54 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 55 | +you may not use this file except in compliance with the License. |
| 56 | +You may obtain a copy of the License at |
| 57 | +
|
| 58 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 59 | +
|
| 60 | +Unless required by applicable law or agreed to in writing, software |
| 61 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 62 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 63 | +See the License for the specific language governing permissions and |
| 64 | +limitations under the License. |
| 65 | +``` |
0 commit comments