Save the scroll state for sources

This commit is contained in:
Syer10
2021-05-28 19:06:20 -04:00
parent db5f95ff48
commit 798e16eab0
3 changed files with 37 additions and 11 deletions

View File

@@ -33,6 +33,7 @@ import ca.gosyer.ui.sources.components.SourceHomeScreen
import ca.gosyer.ui.sources.components.SourceScreen
import ca.gosyer.util.compose.ThemedWindow
import com.github.zsoltk.compose.savedinstancestate.Bundle
import com.github.zsoltk.compose.savedinstancestate.BundleScope
import com.github.zsoltk.compose.savedinstancestate.LocalSavedInstanceState
fun openSourcesMenu() {
@@ -85,10 +86,12 @@ fun SourcesMenu(bundle: Bundle, onMangaClick: (Long) -> Unit) {
}
val selectedSource: Source? = selectedSourceTab
if (selectedSource != null) {
SourceScreen(selectedSource, onMangaClick)
} else {
SourceHomeScreen(isLoading, sources, serverUrl, vm::addTab)
BundleScope(selectedSource?.id.toString(), autoDispose = false) {
if (selectedSource != null) {
SourceScreen(selectedSource, onMangaClick)
} else {
SourceHomeScreen(isLoading, sources, serverUrl, vm::addTab)
}
}
}
}

View File

@@ -26,6 +26,7 @@ import ca.gosyer.data.models.Source
import ca.gosyer.ui.base.components.LoadingScreen
import ca.gosyer.ui.base.components.MangaGridItem
import ca.gosyer.ui.base.vm.viewModel
import ca.gosyer.util.compose.persistentLazyListState
import com.github.zsoltk.compose.savedinstancestate.Bundle
import com.github.zsoltk.compose.savedinstancestate.LocalSavedInstanceState
@@ -34,13 +35,8 @@ fun SourceScreen(
source: Source,
onMangaClick: (Long) -> Unit
) {
val upstream = LocalSavedInstanceState.current
val bundle = LocalSavedInstanceState.current
val vm = viewModel<SourceScreenViewModel>()
val bundle = remember(source.id) {
upstream.getBundle(source.id.toString())
?: Bundle().also { upstream.putBundle(source.id.toString(), it) }
}
remember(source.id) {
vm.init(source, bundle)
}
@@ -51,6 +47,7 @@ fun SourceScreen(
val serverUrl by vm.serverUrl.collectAsState()
MangaTable(
bundle,
mangas,
loading,
hasNextPage,
@@ -64,6 +61,7 @@ fun SourceScreen(
@Composable
private fun MangaTable(
bundle: Bundle,
mangas: List<Manga>,
isLoading: Boolean = false,
hasNextPage: Boolean = false,
@@ -95,7 +93,8 @@ private fun MangaTable(
}
}
LazyVerticalGrid(GridCells.Adaptive(160.dp)) {
val persistentState = persistentLazyListState(bundle)
LazyVerticalGrid(GridCells.Adaptive(160.dp), state = persistentState) {
items(mangas) { manga ->
MangaGridItem(
title = manga.title,

View File

@@ -6,11 +6,35 @@
package ca.gosyer.util.compose
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import com.github.zsoltk.compose.savedinstancestate.Bundle
import com.github.zsoltk.compose.savedinstancestate.LocalSavedInstanceState
@Composable
fun <T> State<T>.persistent(key: String) {
val bundle = LocalSavedInstanceState.current
}
const val LAZY_LIST_ITEM = "lazy_list_item"
const val LAZY_LIST_OFFSET = "lazy_list_offset"
@Composable
fun persistentLazyListState(bundle: Bundle = LocalSavedInstanceState.current): LazyListState {
val state = rememberLazyListState(
remember { bundle.getInt(LAZY_LIST_ITEM, 0) },
remember { bundle.getInt(LAZY_LIST_OFFSET, 0) }
)
DisposableEffect(Unit) {
onDispose {
bundle.putInt(LAZY_LIST_ITEM, state.firstVisibleItemIndex)
bundle.putInt(LAZY_LIST_OFFSET, state.firstVisibleItemScrollOffset)
}
}
return state
}