From 4b594badb71631ac38801414bffe80b439b866fa Mon Sep 17 00:00:00 2001 From: junkfood <69683722+JunkFood02@users.noreply.github.com> Date: Tue, 20 Feb 2024 02:27:26 +0800 Subject: [PATCH] fix(ui): avoid overhead by hoisting states --- .../me/ash/reader/ui/page/home/reading/Content.kt | 5 ++--- .../ash/reader/ui/page/home/reading/ReadingPage.kt | 13 ++++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/me/ash/reader/ui/page/home/reading/Content.kt b/app/src/main/java/me/ash/reader/ui/page/home/reading/Content.kt index 029facc2e..ee10dd0e5 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/reading/Content.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/reading/Content.kt @@ -1,5 +1,6 @@ package me.ash.reader.ui.page.home.reading +import android.util.Log import androidx.compose.animation.AnimatedContent import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.core.tween @@ -50,7 +51,6 @@ fun Content( publishedDate: Date, listState: LazyListState, isLoading: Boolean, - pullToLoadState: PullToLoadState, onImageClick: ((imgUrl: String, altText: String) -> Unit)? = null, ) { val context = LocalContext.current @@ -72,8 +72,7 @@ fun Content( LazyColumn( modifier = modifier .fillMaxSize() - .drawVerticalScrollbar(listState) - .offset(x = 0.dp, y = (pullToLoadState.offsetFraction * 80).dp), + .drawVerticalScrollbar(listState), state = listState, ) { item { diff --git a/app/src/main/java/me/ash/reader/ui/page/home/reading/ReadingPage.kt b/app/src/main/java/me/ash/reader/ui/page/home/reading/ReadingPage.kt index 87cf0dbf3..72aaee386 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/reading/ReadingPage.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/reading/ReadingPage.kt @@ -7,6 +7,7 @@ import androidx.compose.foundation.LocalOverscrollConfiguration import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyListState import androidx.compose.material.ExperimentalMaterialApi @@ -26,6 +27,7 @@ import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.nestedscroll.nestedScroll import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalHapticFeedback +import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel import androidx.navigation.NavHostController import androidx.paging.compose.collectAsLazyPagingItems @@ -129,11 +131,13 @@ fun ReadingPage( } materialSharedAxisY( initialOffsetY = { (it * 0.1f * direction).toInt() }, - targetOffsetY = { (it * -0.1f * direction).toInt() }) + targetOffsetY = { (it * -0.1f * direction).toInt() }, + durationMillis = 400 + ) }, label = "" ) { - it.run { + remember { it }.run { val state = rememberPullToLoadState( key = content, @@ -177,8 +181,8 @@ fun ReadingPage( isReaderScrollingDown = f < 0f }) ) - - .padding(paddings), + .padding(paddings) + .offset(x = 0.dp, y = (state.offsetFraction * 80).dp), content = content.text ?: "", feedName = feedName, title = title.toString(), @@ -187,7 +191,6 @@ fun ReadingPage( publishedDate = publishedDate, isLoading = content is ReaderState.Loading, listState = listState, - pullToLoadState = state, onImageClick = { imgUrl, altText -> currentImageData = ImageData(imgUrl, altText) showFullScreenImageViewer = true