Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/loadmore recyclerview #132

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flowbinding-recyclerview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ fun RecyclerView.childAttachStateChangeEvents(): Flow<RecyclerViewChildAttachSta
fun RecyclerView.flingEvents(handled: (FlingEvent) -> Boolean = { true }): Flow<FlingEvent>
fun RecyclerView.scrollEvents(): Flow<RecyclerViewScrollEvent>
fun RecyclerView.scrollStateChanges(): Flow<Int>
fun RecyclerView.loadMoreFlowVertically(): Flow<Boolean>
fun RecyclerView.loadMoreFlowHorizontally(): Flow<Boolean>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package reactivecircus.flowbinding.recyclerview

import androidx.annotation.CheckResult
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import reactivecircus.flowbinding.common.checkMainThread

/**
* Create a [Flow] of scroll events on the [RecyclerView] instance.
*
* Note: create a extension for loading more in different directions
*
* Example of usage:
*
* ```
* recyclerView.loadMoreFlowVertically()
* .onEach { loadMore ->
* // handle data changed
* }
* .launchIn(uiScope)
* ```
*/
@CheckResult
@OptIn(ExperimentalCoroutinesApi::class)
public fun RecyclerView.loadMoreFlowVertically(): Flow<Boolean> = callbackFlow {
checkMainThread()
val listener = object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (!recyclerView.canScrollVertically(1) && newState == RecyclerView.SCROLL_STATE_IDLE) {
trySend(true)
} else {
trySend(false)
}
}
}
addOnScrollListener(listener)
awaitClose { removeOnScrollListener(listener) }
}.conflate()


@CheckResult
@OptIn(ExperimentalCoroutinesApi::class)
public fun RecyclerView.loadMoreFlowHorizontally(): Flow<Boolean> = callbackFlow {
checkMainThread()
val listener = object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (!recyclerView.canScrollHorizontally(1) && newState == RecyclerView.SCROLL_STATE_IDLE) {
trySend(true)
} else {
trySend(false)
}
}
}
addOnScrollListener(listener)
awaitClose { removeOnScrollListener(listener) }
}.conflate()