diff --git a/composeApp/src/commonMain/kotlin/net/sergeych/toread/App.kt b/composeApp/src/commonMain/kotlin/net/sergeych/toread/App.kt index 45f682b..7e0fae7 100644 --- a/composeApp/src/commonMain/kotlin/net/sergeych/toread/App.kt +++ b/composeApp/src/commonMain/kotlin/net/sergeych/toread/App.kt @@ -232,9 +232,11 @@ private fun BookReaderApp( is AppState.Error, AppState.LoadingStartup -> defaultLibraryScanPath().orEmpty() } val nextState = runCatching { + val book = Fb2Format.parse(request.bytes, request.displayName) + saveActiveReadingFileId(request.id) AppState.Reader( fileId = request.id, - book = Fb2Format.parse(request.bytes, request.displayName), + book = book, libraryItems = emptyList(), scanPath = scanPath, ) diff --git a/composeApp/src/commonMain/kotlin/net/sergeych/toread/AppState.kt b/composeApp/src/commonMain/kotlin/net/sergeych/toread/AppState.kt index 2474675..e16f37b 100644 --- a/composeApp/src/commonMain/kotlin/net/sergeych/toread/AppState.kt +++ b/composeApp/src/commonMain/kotlin/net/sergeych/toread/AppState.kt @@ -47,9 +47,11 @@ internal suspend fun loadStartupState(): AppState { } platformRequest?.let { request -> return runCatching { + val book = Fb2Format.parse(request.bytes, request.displayName) + saveActiveReadingFileId(request.id) AppState.Reader( fileId = request.id, - book = Fb2Format.parse(request.bytes, request.displayName), + book = book, libraryItems = emptyList(), scanPath = scanPath, ) diff --git a/composeApp/src/commonMain/kotlin/net/sergeych/toread/ReaderScreen.kt b/composeApp/src/commonMain/kotlin/net/sergeych/toread/ReaderScreen.kt index 77b8ca7..480390e 100644 --- a/composeApp/src/commonMain/kotlin/net/sergeych/toread/ReaderScreen.kt +++ b/composeApp/src/commonMain/kotlin/net/sergeych/toread/ReaderScreen.kt @@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.layout.Row @@ -67,6 +68,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.setValue import androidx.compose.runtime.snapshotFlow import androidx.compose.ui.Alignment @@ -105,7 +107,7 @@ internal fun BookView( ) { val stats = remember(book) { BookStats.from(book) } val contentPlan = remember(book) { buildReaderContentPlan(book) } - val listState = rememberLazyListState() + val listState = remember(fileId) { LazyListState() } val scope = rememberCoroutineScope() val snackbarHostState = remember { SnackbarHostState() } var restored by remember(fileId) { mutableStateOf(false) } @@ -162,6 +164,7 @@ internal fun BookView( readAloudResumeSentenceIndex, backStack, ) + val currentReadingPositionProvider by rememberUpdatedState { currentReadingPosition() } fun openPosition(position: ReadingPosition, backStack: List) { scope.launch { @@ -226,6 +229,11 @@ internal fun BookView( DisposableEffect(fileId) { onDispose { + if (restored) { + scope.launch { + saveLibraryReadingPosition(fileId, currentReadingPositionProvider()) + } + } ReadAloudPlatform.stop() } }