fix(reader): incorrect scroll position

Resolves: #237
This commit is contained in:
Acclorite 2025-06-28 11:39:54 +03:00
parent 19db53473d
commit 6878ecac86
No known key found for this signature in database
GPG key ID: 6E54C611F6EE8593

View file

@ -493,36 +493,40 @@ class ReaderModel @Inject constructor(
} }
@OptIn(FlowPreview::class) @OptIn(FlowPreview::class)
fun updateProgress(listState: LazyListState) { suspend fun updateProgress(listState: LazyListState) {
viewModelScope.launch(Dispatchers.Main) { snapshotFlow {
snapshotFlow { listState.firstVisibleItemIndex to listState.firstVisibleItemScrollOffset
listState.firstVisibleItemIndex to listState.firstVisibleItemScrollOffset }.distinctUntilChanged().debounce(300).collectLatest { (index, offset) ->
}.distinctUntilChanged().debounce(300).collectLatest { (index, offset) -> if (
val progress = calculateProgress(index) _state.value.isLoading ||
if (progress == _state.value.book.progress) return@collectLatest listState.layoutInfo.totalItemsCount == 0 ||
val (currentChapter, currentChapterProgress) = calculateCurrentChapter(index) _state.value.text.isEmpty() ||
_state.value.errorMessage != null
) return@collectLatest
Log.i( val progress = calculateProgress(index)
READER, val (currentChapter, currentChapterProgress) = calculateCurrentChapter(index)
"Changed progress|currentChapter: $progress; ${currentChapter?.title}"
Log.i(
READER,
"Changed progress|currentChapter: $progress; ${currentChapter?.title}"
)
_state.update {
it.copy(
book = it.book.copy(
progress = progress,
scrollIndex = index,
scrollOffset = offset
),
currentChapter = currentChapter,
currentChapterProgress = currentChapterProgress
) )
_state.update {
it.copy(
book = it.book.copy(
progress = progress,
scrollIndex = index,
scrollOffset = offset
),
currentChapter = currentChapter,
currentChapterProgress = currentChapterProgress
)
}
updateBookUseCase(_state.value.book)
LibraryScreen.refreshListChannel.trySend(0)
HistoryScreen.refreshListChannel.trySend(0)
} }
updateBookUseCase(_state.value.book)
LibraryScreen.refreshListChannel.trySend(0)
HistoryScreen.refreshListChannel.trySend(0)
} }
} }