fix: scrollToItem does not work correctly
This commit is contained in:
parent
5a943185d8
commit
b0a21c116f
7 changed files with 23 additions and 68 deletions
|
|
@ -16,11 +16,6 @@ sealed class ReaderEffect {
|
||||||
|
|
||||||
data object OnResetBrightness : ReaderEffect()
|
data object OnResetBrightness : ReaderEffect()
|
||||||
|
|
||||||
data class OnScroll(
|
|
||||||
val scrollIndex: Int,
|
|
||||||
val scrollOffset: Int
|
|
||||||
) : ReaderEffect()
|
|
||||||
|
|
||||||
data class OnOpenTranslator(
|
data class OnOpenTranslator(
|
||||||
val textToTranslate: String,
|
val textToTranslate: String,
|
||||||
val translateWholeParagraph: Boolean
|
val translateWholeParagraph: Boolean
|
||||||
|
|
|
||||||
|
|
@ -111,11 +111,9 @@ class ReaderModel @Inject constructor(
|
||||||
is ReaderEvent.OnRestoreScroll -> {
|
is ReaderEvent.OnRestoreScroll -> {
|
||||||
snapshotFlow { _state.value.listState.layoutInfo.totalItemsCount }.first { it > 0 }
|
snapshotFlow { _state.value.listState.layoutInfo.totalItemsCount }.first { it > 0 }
|
||||||
|
|
||||||
_effects.emit(
|
_state.value.listState.requestScrollToItem(
|
||||||
ReaderEffect.OnScroll(
|
index = _state.value.book.scrollIndex,
|
||||||
scrollIndex = _state.value.book.scrollIndex,
|
scrollOffset = _state.value.book.scrollOffset
|
||||||
scrollOffset = _state.value.book.scrollOffset
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
_state.update {
|
_state.update {
|
||||||
|
|
@ -205,11 +203,9 @@ class ReaderModel @Inject constructor(
|
||||||
.takeIf { it != -1 }
|
.takeIf { it != -1 }
|
||||||
if (chapterIndex == null) return@withContext
|
if (chapterIndex == null) return@withContext
|
||||||
|
|
||||||
_effects.emit(
|
_state.value.listState.requestScrollToItem(
|
||||||
ReaderEffect.OnScroll(
|
index = chapterIndex,
|
||||||
scrollIndex = chapterIndex,
|
scrollOffset = 0
|
||||||
scrollOffset = 0
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
onEvent(ReaderEvent.OnUpdateChapter(chapterIndex))
|
onEvent(ReaderEvent.OnUpdateChapter(chapterIndex))
|
||||||
|
|
@ -230,11 +226,9 @@ class ReaderModel @Inject constructor(
|
||||||
|
|
||||||
val scrollTo = (_state.value.text.lastIndex * event.progress).roundToInt()
|
val scrollTo = (_state.value.text.lastIndex * event.progress).roundToInt()
|
||||||
|
|
||||||
_effects.emit(
|
_state.value.listState.requestScrollToItem(
|
||||||
ReaderEffect.OnScroll(
|
index = scrollTo,
|
||||||
scrollIndex = scrollTo,
|
scrollOffset = 0
|
||||||
scrollOffset = 0
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
onEvent(ReaderEvent.OnUpdateChapter(scrollTo))
|
onEvent(ReaderEvent.OnUpdateChapter(scrollTo))
|
||||||
|
|
@ -252,11 +246,9 @@ class ReaderModel @Inject constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_effects.emit(
|
_state.value.listState.requestScrollToItem(
|
||||||
ReaderEffect.OnScroll(
|
index = event.checkpoint.index,
|
||||||
scrollIndex = event.checkpoint.index,
|
scrollOffset = event.checkpoint.offset
|
||||||
scrollOffset = event.checkpoint.offset
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
onEvent(ReaderEvent.OnUpdateChapter(event.checkpoint.index))
|
onEvent(ReaderEvent.OnUpdateChapter(event.checkpoint.index))
|
||||||
|
|
|
||||||
|
|
@ -380,7 +380,6 @@ data class ReaderScreen(val bookId: Int) : Screen, Parcelable {
|
||||||
ReaderEffects(
|
ReaderEffects(
|
||||||
effects = screenModel.effects,
|
effects = screenModel.effects,
|
||||||
book = state.value.book,
|
book = state.value.book,
|
||||||
listState = listState,
|
|
||||||
fullscreen = settings.fullscreen.value
|
fullscreen = settings.fullscreen.value
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,18 +6,11 @@
|
||||||
|
|
||||||
package ua.acclorite.book_story.presentation.settings
|
package ua.acclorite.book_story.presentation.settings
|
||||||
|
|
||||||
import androidx.compose.foundation.lazy.LazyListState
|
|
||||||
import androidx.compose.runtime.Immutable
|
import androidx.compose.runtime.Immutable
|
||||||
import ua.acclorite.book_story.domain.model.reader.ColorPreset
|
import ua.acclorite.book_story.domain.model.reader.ColorPreset
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
sealed class SettingsEffect {
|
sealed class SettingsEffect {
|
||||||
data class OnScroll(
|
|
||||||
val listState: LazyListState,
|
|
||||||
val index: Int,
|
|
||||||
val offset: Int
|
|
||||||
) : SettingsEffect()
|
|
||||||
|
|
||||||
data class OnSwitchedColorPreset(
|
data class OnSwitchedColorPreset(
|
||||||
val newColorPreset: ColorPreset
|
val newColorPreset: ColorPreset
|
||||||
) : SettingsEffect()
|
) : SettingsEffect()
|
||||||
|
|
|
||||||
|
|
@ -80,12 +80,9 @@ class SettingsModel @Inject constructor(
|
||||||
|
|
||||||
val scrollIndex = colorPresets.indexOfFirst { it.isSelected }
|
val scrollIndex = colorPresets.indexOfFirst { it.isSelected }
|
||||||
if (scrollIndex != -1) {
|
if (scrollIndex != -1) {
|
||||||
_effects.emit(
|
_state.value.colorPresetListState.requestScrollToItem(
|
||||||
SettingsEffect.OnScroll(
|
index = scrollIndex,
|
||||||
listState = _state.value.colorPresetListState,
|
scrollOffset = 0
|
||||||
index = scrollIndex,
|
|
||||||
offset = 0
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -258,12 +255,9 @@ class SettingsModel @Inject constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_effects.emit(
|
_state.value.colorPresetListState.requestScrollToItem(
|
||||||
SettingsEffect.OnScroll(
|
index = nextPosition,
|
||||||
listState = _state.value.colorPresetListState,
|
scrollOffset = 0
|
||||||
index = nextPosition,
|
|
||||||
offset = 0
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -348,12 +342,9 @@ class SettingsModel @Inject constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_effects.emit(
|
_state.value.colorPresetListState.requestScrollToItem(
|
||||||
SettingsEffect.OnScroll(
|
index = colorPresets.lastIndex,
|
||||||
listState = _state.value.colorPresetListState,
|
scrollOffset = 0
|
||||||
index = colorPresets.lastIndex,
|
|
||||||
offset = 0
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ package ua.acclorite.book_story.ui.reader
|
||||||
|
|
||||||
import android.app.SearchManager
|
import android.app.SearchManager
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import androidx.compose.foundation.lazy.LazyListState
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
|
|
@ -30,13 +29,12 @@ import ua.acclorite.book_story.ui.navigator.LocalNavigator
|
||||||
fun ReaderEffects(
|
fun ReaderEffects(
|
||||||
effects: SharedFlow<ReaderEffect>,
|
effects: SharedFlow<ReaderEffect>,
|
||||||
book: Book,
|
book: Book,
|
||||||
listState: LazyListState,
|
|
||||||
fullscreen: Boolean
|
fullscreen: Boolean
|
||||||
) {
|
) {
|
||||||
val navigator = LocalNavigator.current
|
val navigator = LocalNavigator.current
|
||||||
val activity = LocalActivity.current
|
val activity = LocalActivity.current
|
||||||
|
|
||||||
LaunchedEffect(effects, book, listState, fullscreen) {
|
LaunchedEffect(effects, book, fullscreen) {
|
||||||
effects.collect { effect ->
|
effects.collect { effect ->
|
||||||
when (effect) {
|
when (effect) {
|
||||||
is ReaderEffect.OnSystemBarsVisibility -> {
|
is ReaderEffect.OnSystemBarsVisibility -> {
|
||||||
|
|
@ -55,13 +53,6 @@ fun ReaderEffects(
|
||||||
activity.setBrightness(brightness = null)
|
activity.setBrightness(brightness = null)
|
||||||
}
|
}
|
||||||
|
|
||||||
is ReaderEffect.OnScroll -> {
|
|
||||||
listState.requestScrollToItem(
|
|
||||||
effect.scrollIndex,
|
|
||||||
effect.scrollOffset
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
is ReaderEffect.OnOpenTranslator -> {
|
is ReaderEffect.OnOpenTranslator -> {
|
||||||
val translatorIntent = Intent()
|
val translatorIntent = Intent()
|
||||||
val browserIntent = Intent()
|
val browserIntent = Intent()
|
||||||
|
|
|
||||||
|
|
@ -21,20 +21,14 @@ fun SettingsEffects(effects: SharedFlow<SettingsEffect>) {
|
||||||
LaunchedEffect(effects) {
|
LaunchedEffect(effects) {
|
||||||
effects.collect { effect ->
|
effects.collect { effect ->
|
||||||
when (effect) {
|
when (effect) {
|
||||||
is SettingsEffect.OnScroll -> {
|
|
||||||
effect.listState.requestScrollToItem(effect.index, effect.offset)
|
|
||||||
}
|
|
||||||
|
|
||||||
is SettingsEffect.OnSwitchedColorPreset -> {
|
is SettingsEffect.OnSwitchedColorPreset -> {
|
||||||
context.getString(
|
context.getString(
|
||||||
R.string.color_preset_selected_query,
|
R.string.color_preset_selected_query,
|
||||||
if (effect.newColorPreset.name.isBlank()) {
|
effect.newColorPreset.name.ifBlank {
|
||||||
context.getString(
|
context.getString(
|
||||||
R.string.color_preset_query,
|
R.string.color_preset_query,
|
||||||
effect.newColorPreset.id.toString()
|
effect.newColorPreset.id.toString()
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
effect.newColorPreset.name
|
|
||||||
}.trim()
|
}.trim()
|
||||||
).showToast(context, longToast = false)
|
).showToast(context, longToast = false)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue