Skip to content

Commit 34ff3be

Browse files
authored
Restore webview scroll position by percent scroll (#1498)
Given a a view height of 500px and current scroll position of 100px, the scroll state will save a float value of 0.2. On rotate, if the viewport height decreases slightly given a different aspect ratio, say 480px, then the restore position will be 96px, or 20% * 480px. This replaces the absolute scroll position restore which did not account for the slight change in max height.
1 parent 929505f commit 34ff3be

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

app/src/main/java/com/capyreader/app/ui/articles/detail/ArticleReader.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import androidx.compose.runtime.LaunchedEffect
1212
import androidx.compose.runtime.collectAsState
1313
import androidx.compose.runtime.getValue
1414
import androidx.compose.runtime.mutableFloatStateOf
15-
import androidx.compose.runtime.mutableIntStateOf
1615
import androidx.compose.runtime.remember
1716
import androidx.compose.runtime.saveable.rememberSaveable
1817
import androidx.compose.runtime.setValue
@@ -32,6 +31,7 @@ import com.capyreader.app.ui.components.rememberSaveableShareLink
3231
import com.capyreader.app.ui.components.rememberWebViewState
3332
import com.jocmp.capy.Article
3433
import org.koin.compose.koinInject
34+
import kotlin.math.roundToInt
3535

3636
@Composable
3737
fun ArticleReader(
@@ -93,7 +93,7 @@ fun ScrollableWebView(webViewState: WebViewState) {
9393
ScrollState(initial = 0)
9494
}
9595

96-
var lastScrollY by rememberSaveable { mutableIntStateOf(0) }
96+
var lastScrollYPercent by rememberSaveable { mutableFloatStateOf(0f) }
9797

9898
CornerTapGestureScroll(
9999
maxArticleHeight = maxHeight,
@@ -118,14 +118,14 @@ fun ScrollableWebView(webViewState: WebViewState) {
118118
}
119119
}
120120

121-
LaunchedEffect(scrollState.value) {
122-
if (scrollState.value > 0) {
123-
lastScrollY = scrollState.value
121+
LaunchedEffect(scrollState.value, maxHeight) {
122+
if (scrollState.value > 0 && maxHeight > 0) {
123+
lastScrollYPercent = scrollState.value / maxHeight
124124
}
125125
}
126-
LaunchedEffect(scrollState.maxValue) {
127-
if (scrollState.maxValue > 0) {
128-
scrollState.scrollTo(lastScrollY)
126+
LaunchedEffect(scrollState.maxValue, maxHeight) {
127+
if (scrollState.maxValue > 0 && maxHeight > 0) {
128+
scrollState.scrollTo((lastScrollYPercent * maxHeight).roundToInt())
129129
}
130130
}
131131
}

0 commit comments

Comments
 (0)