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

Support fling for nested scroll on TimetableGrid. #1273

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.droidkaigi.confsched2023.sessions.section

import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationState
import androidx.compose.animation.core.animateDecay
import androidx.compose.animation.core.exponentialDecay
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.focusGroup
Expand Down Expand Up @@ -35,8 +37,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollDispatcher
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.PointerInputScope
import androidx.compose.ui.input.pointer.pointerInput
Expand Down Expand Up @@ -84,14 +88,12 @@ data class TimetableGridUiState(val timetable: Timetable)
@Composable
fun TimetableGrid(
uiState: TimetableGridUiState,
nestedScrollDispatcher: NestedScrollDispatcher,
onTimetableItemClick: (TimetableItem) -> Unit,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
) {
TimetableGrid(
timetable = uiState.timetable,
nestedScrollDispatcher = nestedScrollDispatcher,
onTimetableItemClick = onTimetableItemClick,
modifier = modifier,
contentPadding = contentPadding,
Expand All @@ -101,7 +103,6 @@ fun TimetableGrid(
@Composable
fun TimetableGrid(
timetable: Timetable,
nestedScrollDispatcher: NestedScrollDispatcher,
onTimetableItemClick: (TimetableItem) -> Unit,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
Expand Down Expand Up @@ -133,7 +134,6 @@ fun TimetableGrid(
TimetableGrid(
timetable = timetable,
timetableState = timetableGridState,
nestedScrollDispatcher = nestedScrollDispatcher,
modifier = modifier,
contentPadding = PaddingValues(
top = 16.dp + contentPadding.calculateTopPadding(),
Expand All @@ -157,7 +157,6 @@ fun TimetableGrid(
fun TimetableGrid(
timetable: Timetable,
timetableState: TimetableState,
nestedScrollDispatcher: NestedScrollDispatcher,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
content: @Composable (TimetableItem, Int) -> Unit,
Expand Down Expand Up @@ -186,10 +185,14 @@ fun TimetableGrid(
content(timetableItemWithFavorite.timetableItem, itemHeightPx)
}

val nestedScrollConnection = remember { object : NestedScrollConnection {} }
val nestedScrollDispatcher = remember { NestedScrollDispatcher() }

LazyLayout(
modifier = modifier
.focusGroup()
.clipToBounds()
.nestedScroll(nestedScrollConnection, nestedScrollDispatcher)
.drawBehind {
timetableScreen.timeHorizontalLines.value.forEach {
drawLine(
Expand Down Expand Up @@ -245,7 +248,7 @@ fun TimetableGrid(
},
onDragEnd = {
coroutineScope.launch {
scrollState.flingIfPossible()
scrollState.flingIfPossible(nestedScrollDispatcher)
}
},
)
Expand Down Expand Up @@ -322,7 +325,6 @@ fun TimetableGrid(
fun TimetablePreview() {
TimetableGrid(
timetable = Timetable.fake(),
nestedScrollDispatcher = remember { NestedScrollDispatcher() },
onTimetableItemClick = {},
modifier = Modifier.fillMaxSize(),
)
Expand Down Expand Up @@ -510,19 +512,41 @@ class ScreenScrollState(
}
}

suspend fun flingIfPossible() = coroutineScope {
suspend fun flingIfPossible(nestedScrollDispatcher: NestedScrollDispatcher) = coroutineScope {
val velocity = velocityTracker.calculateVelocity()
launch {
_scrollX.animateDecay(
velocity.x / 2f,
exponentialDecay(),
)
}
launch {
_scrollY.animateDecay(
velocity.y / 2f,
exponentialDecay(),
)

var lastValue = 0f
AnimationState(
initialValue = 0f,
initialVelocity = velocity.y,
).animateDecay(
exponentialDecay(),
) {
launch {
val delta = Offset(0f, value - lastValue)
lastValue = value
val preConsumed = nestedScrollDispatcher.dispatchPreScroll(
available = delta,
source = NestedScrollSource.Fling,
)

val weAvailable = delta - preConsumed
val previousY = _scrollY.value
_scrollY.snapTo(_scrollY.value + weAvailable.y)
val weConsumed = Offset(0f, _scrollY.value - previousY)

nestedScrollDispatcher.dispatchPostScroll(
consumed = preConsumed + weConsumed,
available = weAvailable - weConsumed,
source = NestedScrollSource.Fling,
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollDispatcher
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalLayoutDirection
Expand Down Expand Up @@ -128,17 +127,11 @@ fun TimetableSheet(
}

is GridTimetable -> {
val nestedScrollDispatcher = remember { NestedScrollDispatcher() }
Copy link
Contributor

@swimmy-reo swimmy-reo Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the great PR!
I would like to comment on a point of concern.
Is there any reason why you originally defined the NestedScrollDispatcher and applied Modifier nestedScroll here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I was misunderstanding that I needed to pass a nestedScrollDispatcher and a timetableSheetContentScrollState.nestedScrollConnection to one Modifier.nestedScroll() argument.
But in this case, the TimetableGrid's nestedScrollDispatcher is independent of the TimetableSheet. Therefore I moved the dispatcher into TimetableGrid class.

Copy link
Contributor

@swimmy-reo swimmy-reo Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much. I am convinced.

TimetableGrid(
uiState = requireNotNull(uiState.timetableGridUiState[selectedDay]),
nestedScrollDispatcher = nestedScrollDispatcher,
onTimetableItemClick = onTimetableItemClick,
modifier = Modifier
.fillMaxSize()
.nestedScroll(
timetableSheetContentScrollState.nestedScrollConnection,
nestedScrollDispatcher,
)
.weight(1f),
contentPadding = PaddingValues(
bottom = contentPadding.calculateBottomPadding(),
Expand Down
Loading