Add LazyListState::smoothScrollToTop extension function

This commit is contained in:
vfsfitvnm 2022-09-29 20:24:28 +02:00
parent b4fb9cf6d9
commit db44969038
3 changed files with 14 additions and 21 deletions

View file

@ -9,7 +9,6 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
@ -24,6 +23,7 @@ import androidx.compose.ui.unit.dp
import it.vfsfitvnm.vimusic.LocalPlayerAwarePaddingValues
import it.vfsfitvnm.vimusic.R
import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance
import it.vfsfitvnm.vimusic.utils.smoothScrollToTop
import kotlinx.coroutines.launch
@Composable
@ -39,25 +39,7 @@ fun ScrollToTop(
ScrollToTop(
isVisible = showScrollTopButton,
onClick = { lazyListState.animateScrollToItem(0) },
modifier = modifier
)
}
@Composable
fun ScrollToTop(
lazyGridState: LazyGridState,
modifier: Modifier = Modifier,
) {
val showScrollTopButton by remember {
derivedStateOf {
lazyGridState.firstVisibleItemIndex > lazyGridState.layoutInfo.visibleItemsInfo.size
}
}
ScrollToTop(
isVisible = showScrollTopButton,
onClick = { lazyGridState.animateScrollToItem(0) },
onClick = lazyListState::smoothScrollToTop,
modifier = modifier
)
}

View file

@ -64,6 +64,7 @@ import it.vfsfitvnm.vimusic.utils.rememberMediaItemIndex
import it.vfsfitvnm.vimusic.utils.rememberShouldBePlaying
import it.vfsfitvnm.vimusic.utils.rememberWindows
import it.vfsfitvnm.vimusic.utils.shuffleQueue
import it.vfsfitvnm.vimusic.utils.smoothScrollToTop
import kotlinx.coroutines.launch
@ExperimentalFoundationApi
@ -285,7 +286,7 @@ fun PlayerBottomSheet(
.padding(end = 2.dp)
.clickable {
reorderingState.coroutineScope.launch {
reorderingState.lazyListState.animateScrollToItem(0)
reorderingState.lazyListState.smoothScrollToTop()
}.invokeOnCompletion {
binder.player.shuffleQueue()
}

View file

@ -0,0 +1,10 @@
package it.vfsfitvnm.vimusic.utils
import androidx.compose.foundation.lazy.LazyListState
suspend fun LazyListState.smoothScrollToTop() {
if (firstVisibleItemIndex > layoutInfo.visibleItemsInfo.size) {
scrollToItem(layoutInfo.visibleItemsInfo.size)
}
animateScrollToItem(0)
}