Skip to content

Commit

Permalink
refactor(ui): decouple ReaderNestedScrollConnection & PullToLoadState
Browse files Browse the repository at this point in the history
  • Loading branch information
JunkFood02 committed Feb 19, 2024
1 parent 8b89644 commit 2438b27
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
37 changes: 12 additions & 25 deletions app/src/main/java/me/ash/reader/ui/page/home/reading/PullToLoad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ private const val TAG = "PullRelease"
* And you should manually handle the offset of components
* with [PullToLoadState.progress] or [PullToLoadState.offsetFraction]
*
* @param state The [PullToLoadState] associated with this pull-to-load component.
* The state will be updated by this connection.
* @param enabled If not enabled, all scroll delta and fling velocity will be ignored.
* @param onScroll Used for detecting if the reader is scrolling down
*/
class ReaderNestedScrollConnection(
private val state: PullToLoadState,
private val enabled: Boolean,
private val onPreScroll: (Float) -> Float,
private val onPostScroll: (Float) -> Float,
private val onRelease: (Float) -> Unit,
private val onScroll: (Float) -> Unit
) : NestedScrollConnection {

Expand All @@ -61,8 +61,8 @@ class ReaderNestedScrollConnection(
!enabled || available.y == 0f -> Offset.Zero

// Scroll down to reduce the progress when the offset is currently pulled up, same for the opposite
source == Drag && state.offsetFraction.signOpposites(available.y) -> {
Offset(0f, state.onPull(available.y))
source == Drag -> {
Offset(0f, onPreScroll(available.y))
}

else -> Offset.Zero
Expand All @@ -74,18 +74,13 @@ class ReaderNestedScrollConnection(
consumed: Offset, available: Offset, source: NestedScrollSource
): Offset = when {
!enabled -> Offset.Zero
source == Drag -> Offset(0f, state.onPull(available.y)) // Pull to load
source == Drag -> Offset(0f, onPostScroll(available.y)) // Pull to load
else -> Offset.Zero
}

override suspend fun onPreFling(available: Velocity): Velocity {
return if (abs(state.progress) > 1f) {
state.onRelease(available.y)
Velocity.Zero
} else {
state.animateDistanceTo(0f)
Velocity.Zero
}
onRelease(available.y)
return Velocity.Zero
}
}

Expand Down Expand Up @@ -212,23 +207,15 @@ class PullToLoadState internal constructor(
TAG,
"onPull: currentOffset = $offsetPulled, pullDelta = $pullDelta, consumed = $consumed"
)*/


offsetPulled += consumed
return consumed
}

internal fun onPullBack(pullDelta: Float): Float {
return if (offsetPulled.signOpposites(pullDelta)) onPull(pullDelta) else 0f
}

internal fun onRelease(velocity: Float): Float {
// val consumed = when {
// // We are flinging without having dragged the pull refresh (for example a fling inside
// // a list) - don't consume
// distancePulled == 0f -> 0f
// // If the velocity is negative, the fling is upwards, and we don't want to prevent the
// // the list from scrolling
// velocity < 0f -> 0f
// // We are showing the indicator, and the fling is downwards - consume everything
// else -> velocity
// }
when (status) {
// We don't change the pull offset here because the animation for loading another content
// should be handled outside, and this state will be soon disposed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ fun ReadingPage(
modifier = Modifier
.nestedScroll(
ReaderNestedScrollConnection(
state = state,
enabled = true,
onPreScroll = state::onPullBack,
onPostScroll = state::onPull,
onRelease = state::onRelease,
onScroll = { f ->
if (abs(f) > 2f)
isReaderScrollingDown = f < 0f
Expand Down

0 comments on commit 2438b27

Please sign in to comment.