update left items

This commit is contained in:
InsanusMokrassar 2020-11-23 00:36:37 +06:00
parent e7a0fa4e8f
commit 629884a396
2 changed files with 11 additions and 3 deletions

View File

@ -4,6 +4,8 @@
* `Versions`: * `Versions`:
* `Klock`: `1.12.1` -> `2.0.0` * `Klock`: `1.12.1` -> `2.0.0`
* `Commons`:
* Update left items functionality to include work with `GridLayoutManager`
* `Repos`: * `Repos`:
* Add interface `VersionsRepo` * Add interface `VersionsRepo`
* Add default realization of `VersionsRepo` named `StandardVersionsRepo` which use `StandardVersionsRepoProxy` * Add default realization of `VersionsRepo` named `StandardVersionsRepo` which use `StandardVersionsRepoProxy`

View File

@ -1,14 +1,20 @@
package dev.inmo.micro_utils.android.recyclerview package dev.inmo.micro_utils.android.recyclerview
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.*
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
@Suppress("NOTHING_TO_INLINE")
private inline fun RecyclerView.LayoutManager.findLastVisibleItemPositionGetter(): (() -> Int)? = when (this) {
is LinearLayoutManager -> ::findLastVisibleItemPosition
is GridLayoutManager -> ::findLastVisibleItemPosition
else -> null
}
fun RecyclerView.lastVisibleItemFlow( fun RecyclerView.lastVisibleItemFlow(
completingScope: CoroutineScope completingScope: CoroutineScope
): Flow<Int> { ): Flow<Int> {
val lastVisibleElementFun: () -> Int = (layoutManager as? LinearLayoutManager) ?.let { it::findLastVisibleItemPosition } ?: error("Currently supported only linear layout manager") val lastVisibleElementFun: () -> Int = layoutManager ?.findLastVisibleItemPositionGetter() ?: error("Currently supported only linear layout manager")
val lastVisibleFlow = MutableStateFlow(lastVisibleElementFun()) val lastVisibleFlow = MutableStateFlow(lastVisibleElementFun())
addOnScrollListener( addOnScrollListener(
object : RecyclerView.OnScrollListener() { object : RecyclerView.OnScrollListener() {