From fe8301830ebc9680b79298ad5d57a3c417f450c2 Mon Sep 17 00:00:00 2001 From: Esmaeel Moustafa Date: Fri, 5 Jul 2024 09:26:35 +0200 Subject: [PATCH] scrolls to the top of the screen Fixes scroll to the top by - using the device actual screen height instead of a hardcoded value --- .../zeapp/zeui/LazyListScrollDirections.kt | 5 ++++ .../zeapp/zeui/ZeFloatingScroller.kt | 16 +++++++++--- .../berlindroid/zeapp/zeui/ZeNavigationPad.kt | 25 ++++++++++--------- 3 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/LazyListScrollDirections.kt diff --git a/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/LazyListScrollDirections.kt b/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/LazyListScrollDirections.kt new file mode 100644 index 00000000..f9c61c43 --- /dev/null +++ b/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/LazyListScrollDirections.kt @@ -0,0 +1,5 @@ +package de.berlindroid.zeapp.zeui + +enum class LazyListScrollDirections { + UP, DOWN +} diff --git a/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/ZeFloatingScroller.kt b/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/ZeFloatingScroller.kt index 25df397f..328c892f 100644 --- a/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/ZeFloatingScroller.kt +++ b/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/ZeFloatingScroller.kt @@ -1,7 +1,6 @@ package de.berlindroid.zeapp.zeui import androidx.compose.foundation.border -import androidx.compose.foundation.gestures.animateScrollBy import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.shape.RoundedCornerShape @@ -21,9 +20,13 @@ import androidx.compose.material3.Text as ZeText fun ZeFloatingScroller( coroutineScope: CoroutineScope, lazyListState: LazyListState, - scrollLength: Float, - text: String, + direction: LazyListScrollDirections, ) { + val text = when (direction) { + LazyListScrollDirections.UP -> "↑" + LazyListScrollDirections.DOWN -> "↓" + } + FloatingActionButton( containerColor = ZeBlack, modifier = Modifier @@ -35,7 +38,12 @@ fun ZeFloatingScroller( ), onClick = { coroutineScope.launch { - lazyListState.animateScrollBy(scrollLength) + when (direction) { + LazyListScrollDirections.UP -> lazyListState.animateScrollToItem(0) + LazyListScrollDirections.DOWN -> lazyListState.animateScrollToItem( + lazyListState.layoutInfo.totalItemsCount - 1, + ) + } } }, ) { diff --git a/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/ZeNavigationPad.kt b/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/ZeNavigationPad.kt index 7dc8a6a2..3f1b0703 100644 --- a/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/ZeNavigationPad.kt +++ b/zeapp/android/src/main/java/de/berlindroid/zeapp/zeui/ZeNavigationPad.kt @@ -20,12 +20,10 @@ fun ZeNavigationPad( lazyListState: LazyListState, ) { val coroutineScope = rememberCoroutineScope() - val scrollLength = 425f - val topReached by remember { derivedStateOf { lazyListState.layoutInfo.visibleItemsInfo.firstOrNull()?.offset == 0 } } - val bottomReached by remember { + + val topReached by remember { derivedStateOf { - lazyListState.layoutInfo.visibleItemsInfo - .lastOrNull()?.index == lazyListState.layoutInfo.totalItemsCount - 1 + lazyListState.firstVisibleItemIndex == 0 && lazyListState.firstVisibleItemScrollOffset == 0 } } @@ -35,12 +33,15 @@ fun ZeNavigationPad( .padding(24.dp), horizontalAlignment = Alignment.End, ) { - if (!topReached) { - ZeFloatingScroller(coroutineScope, lazyListState, -scrollLength, "↑") - } - Spacer(modifier = Modifier.size(10.dp)) - if (!bottomReached) { - ZeFloatingScroller(coroutineScope, lazyListState, scrollLength, "↓") - } + ZeFloatingScroller( + coroutineScope = coroutineScope, + lazyListState = lazyListState, + direction = if (topReached) { + LazyListScrollDirections.DOWN + } else { + LazyListScrollDirections.UP + }, + ) + Spacer(modifier = Modifier.size(16.dp)) } }