Skip to content

Commit

Permalink
Version 3.1.1
Browse files Browse the repository at this point in the history
- improved pagination helper
  • Loading branch information
kroegerama committed Jul 19, 2020
1 parent 7377723 commit afa1089
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/ProjectInfo.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
object P {
const val projectVersion = "3.1.0"
const val projectVersion = "3.1.1"

const val projectGroupId = "com.kroegerama.android-kaiteki"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class Listing<T>(
fun cancel() = cancelFun.invoke()
}

fun <T> LiveData<Listing<T>>.result() = switchMap { it.result }
fun <T> LiveData<Listing<T>>.isRunning() = switchMap { it.state }.map { it.isRunning }
fun LiveData<Listing<*>>.update() = value?.update()
fun LiveData<Listing<*>>.cancel() = value?.cancel()
fun <T> LiveData<out Listing<T>>.result() = switchMap { it.result }
fun <T> LiveData<out Listing<T>>.isRunning() = switchMap { it.state }.map { it.isRunning }
fun LiveData<out Listing<*>>.update() {
value?.update()
}

fun LiveData<out Listing<*>>.cancel() {
value?.cancel()
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,23 @@ class RetrofitPageKeyedDataSource<T>(
val page = pageProvider.firstPage
val size = params.requestedLoadSize
makeLoadRequest(true, page, size) { data ->
callback.onResult(data, pageProvider.getPreviousPage(page), pageProvider.getNextPage(page))
callback.onResult(data, pageProvider.getPreviousPage(page, data.size, size), pageProvider.getNextPage(page, data.size, size))
}
}

override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, T>) {
val page = params.key
val size = params.requestedLoadSize
makeLoadRequest(false, page, size) { data ->
callback.onResult(data, pageProvider.getNextPage(page))
callback.onResult(data, pageProvider.getNextPage(page, data.size, size))
}
}

override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, T>) {
val page = params.key
val size = params.requestedLoadSize
makeLoadRequest(false, page, size) { data ->
callback.onResult(data, pageProvider.getPreviousPage(page))
callback.onResult(data, pageProvider.getPreviousPage(page, data.size, size))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ internal typealias RetryFun = () -> Unit

interface PageProvider {
val firstPage: Int
fun getNextPage(currentPage: Int): Int?
fun getPreviousPage(currentPage: Int): Int?
fun getNextPage(currentPage: Int, currentSize: Int, requestedSize: Int): Int?
fun getPreviousPage(currentPage: Int, currentSize: Int, requestedSize: Int): Int?
}

object DefaultPageProvider : PageProvider {
override val firstPage = 0
override fun getNextPage(currentPage: Int) = currentPage + 1
override fun getPreviousPage(currentPage: Int): Int? = null

override fun getNextPage(currentPage: Int, currentSize: Int, requestedSize: Int) =
if (currentSize < requestedSize) null else currentPage + 1

override fun getPreviousPage(currentPage: Int, currentSize: Int, requestedSize: Int): Int? = null
}

val DefaultPageConfig by lazy { PagedList.Config.Builder().setPageSize(10).setPrefetchDistance(20).build() }
Expand Down Expand Up @@ -45,12 +48,23 @@ class PagedListing<T>(
}
}

fun <T> LiveData<PagedListing<T>>.pagedList() = switchMap { it.pagedList }
fun <T> LiveData<PagedListing<T>>.initialRunning() = switchMap { it.initialState }.map { it.isRunning }
fun <T> LiveData<PagedListing<T>>.loadRunning() = switchMap { it.loadState }.map { it.isRunning }
fun <T> LiveData<PagedListing<T>>.initialResult() = switchMap { it.initialResponse }
fun <T> LiveData<PagedListing<T>>.loadResult() = switchMap { it.loadResponse }
fun LiveData<PagedListing<*>>.refresh() = value?.refresh()
fun LiveData<PagedListing<*>>.retryInitial() = value?.retryInitial()
fun LiveData<PagedListing<*>>.retryLoad() = value?.retryLoad()
fun LiveData<PagedListing<*>>.cancel() = value?.cancel()
fun <T> LiveData<out PagedListing<T>>.pagedList() = switchMap { it.pagedList }
fun <T> LiveData<out PagedListing<T>>.initialRunning() = switchMap { it.initialState }.map { it.isRunning }
fun <T> LiveData<out PagedListing<T>>.loadRunning() = switchMap { it.loadState }.map { it.isRunning }
fun <T> LiveData<out PagedListing<T>>.initialResult() = switchMap { it.initialResponse }
fun <T> LiveData<out PagedListing<T>>.loadResult() = switchMap { it.loadResponse }
fun LiveData<out PagedListing<*>>.refresh() {
value?.refresh()
}

fun LiveData<out PagedListing<*>>.retryInitial() {
value?.retryInitial()
}

fun LiveData<out PagedListing<*>>.retryLoad() {
value?.retryLoad()
}

fun LiveData<out PagedListing<*>>.cancel() {
value?.cancel()
}

0 comments on commit afa1089

Please sign in to comment.