Last active
October 9, 2019 13:38
Revisions
-
danielgomezrico renamed this gist
Oct 9, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
danielgomezrico revised this gist
Oct 9, 2019 . 1 changed file with 13 additions and 0 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,10 @@ package com.lahaus.utils.extensions import androidx.paging.DataSource import androidx.paging.PagedList import androidx.paging.PositionalDataSource import androidx.paging.RxPagedListBuilder import io.reactivex.Observable fun <T> List<T>.toObservablePagedList(): Observable<PagedList<T>> { val defaultConfig = PagedList.Config.Builder() @@ -11,12 +18,18 @@ fun <T> List<T>.toObservablePagedList(): Observable<PagedList<T>> { .buildObservable() } /** * src: https://gist.github.com/JoseAlcerreca/1e9ee05dcdd6a6a6fa1cbfc125559bba */ private fun <T> dataSourceFactory(itemList: List<T>): DataSource.Factory<Int, T> = object : DataSource.Factory<Int, T>() { override fun create(): DataSource<Int, T> = ListDataSource(itemList) } /** * src: https://gist.github.com/JoseAlcerreca/1e9ee05dcdd6a6a6fa1cbfc125559bba */ private class ListDataSource<T>(private val itemList: List<T>) : PositionalDataSource<T>() { override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<T>) { -
danielgomezrico created this gist
Oct 9, 2019 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ fun <T> List<T>.toObservablePagedList(): Observable<PagedList<T>> { val defaultConfig = PagedList.Config.Builder() .setEnablePlaceholders(false) .setPageSize(size) .setMaxSize(size + 2) .setPrefetchDistance(1) .build() return RxPagedListBuilder(dataSourceFactory(this), defaultConfig) .buildObservable() } private fun <T> dataSourceFactory(itemList: List<T>): DataSource.Factory<Int, T> = object : DataSource.Factory<Int, T>() { override fun create(): DataSource<Int, T> = ListDataSource(itemList) } private class ListDataSource<T>(private val itemList: List<T>) : PositionalDataSource<T>() { override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<T>) { itemList.subList(params.startPosition, params.startPosition + params.loadSize) .toMutableList() } override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<T>) { callback.onResult(itemList, 0) } }