From 629884a396fc892d4b9d29261ccd9c7ad7a84a73 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 23 Nov 2020 00:36:37 +0600 Subject: [PATCH] update left items --- CHANGELOG.md | 2 ++ .../micro_utils/android/recyclerview/LeftItems.kt | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b19566594cb..db3fc88a875 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ * `Versions`: * `Klock`: `1.12.1` -> `2.0.0` +* `Commons`: + * Update left items functionality to include work with `GridLayoutManager` * `Repos`: * Add interface `VersionsRepo` * Add default realization of `VersionsRepo` named `StandardVersionsRepo` which use `StandardVersionsRepoProxy` diff --git a/android/recyclerview/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/LeftItems.kt b/android/recyclerview/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/LeftItems.kt index fc6d51b864e..d37fc919591 100644 --- a/android/recyclerview/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/LeftItems.kt +++ b/android/recyclerview/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/LeftItems.kt @@ -1,14 +1,20 @@ package dev.inmo.micro_utils.android.recyclerview -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.* import kotlinx.coroutines.CoroutineScope 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( completingScope: CoroutineScope ): Flow { - 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()) addOnScrollListener( object : RecyclerView.OnScrollListener() {