Collect MangaScreen state with lifecycle

Co-authored-by: ivan <12537387+ivaniskandar@users.noreply.github.com>
(cherry picked from commit 03eb756ecba0692d88d3a76254afc4c157fa225b)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreen.kt
#	app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt
This commit is contained in:
AntsyLich 2024-09-02 21:22:21 +06:00 committed by Jobobby04
parent efbaf1a4ca
commit f36906df45
2 changed files with 16 additions and 4 deletions

View File

@ -18,6 +18,8 @@ import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import cafe.adriel.voyager.core.model.rememberScreenModel import cafe.adriel.voyager.core.model.rememberScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.Navigator import cafe.adriel.voyager.navigator.Navigator
@ -109,10 +111,11 @@ class MangaScreen(
val context = LocalContext.current val context = LocalContext.current
val haptic = LocalHapticFeedback.current val haptic = LocalHapticFeedback.current
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val lifecycleOwner = LocalLifecycleOwner.current
val screenModel = val screenModel =
rememberScreenModel { MangaScreenModel(context, mangaId, fromSource, smartSearchConfig != null) } rememberScreenModel { MangaScreenModel(context, lifecycleOwner.lifecycle, mangaId, fromSource, smartSearchConfig != null) }
val state by screenModel.state.collectAsState() val state by screenModel.state.collectAsStateWithLifecycle()
if (state is MangaScreenModel.State.Loading) { if (state is MangaScreenModel.State.Loading) {
LoadingScreen() LoadingScreen()

View File

@ -6,6 +6,8 @@ import androidx.compose.material3.SnackbarResult
import androidx.compose.runtime.Immutable import androidx.compose.runtime.Immutable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.ui.util.fastAny import androidx.compose.ui.util.fastAny
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import cafe.adriel.voyager.core.model.StateScreenModel import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.screenModelScope import cafe.adriel.voyager.core.model.screenModelScope
import eu.kanade.core.preference.asState import eu.kanade.core.preference.asState
@ -135,8 +137,9 @@ import uy.kohesive.injekt.injectLazy
import kotlin.math.floor import kotlin.math.floor
class MangaScreenModel( class MangaScreenModel(
val context: Context, private val context: Context,
val mangaId: Long, private val lifecycle: Lifecycle,
private val mangaId: Long,
private val isFromSource: Boolean, private val isFromSource: Boolean,
val smartSearched: Boolean, val smartSearched: Boolean,
private val libraryPreferences: LibraryPreferences = Injekt.get(), private val libraryPreferences: LibraryPreferences = Injekt.get(),
@ -313,6 +316,7 @@ class MangaScreenModel(
.combine(downloadCache.changes) { state, _ -> state } .combine(downloadCache.changes) { state, _ -> state }
.combine(downloadManager.queueState) { state, _ -> state } .combine(downloadManager.queueState) { state, _ -> state }
// SY <-- // SY <--
.flowWithLifecycle(lifecycle)
.collectLatest { (manga, chapters /* SY --> */, flatMetadata, mergedData /* SY <-- */) -> .collectLatest { (manga, chapters /* SY --> */, flatMetadata, mergedData /* SY <-- */) ->
val chapterItems = chapters.toChapterListItems(manga /* SY --> */, mergedData /* SY <-- */) val chapterItems = chapters.toChapterListItems(manga /* SY --> */, mergedData /* SY <-- */)
updateSuccessState { updateSuccessState {
@ -330,6 +334,7 @@ class MangaScreenModel(
screenModelScope.launchIO { screenModelScope.launchIO {
getExcludedScanlators.subscribe(mangaId) getExcludedScanlators.subscribe(mangaId)
.flowWithLifecycle(lifecycle)
.distinctUntilChanged() .distinctUntilChanged()
.collectLatest { excludedScanlators -> .collectLatest { excludedScanlators ->
updateSuccessState { updateSuccessState {
@ -340,6 +345,7 @@ class MangaScreenModel(
screenModelScope.launchIO { screenModelScope.launchIO {
getAvailableScanlators.subscribe(mangaId) getAvailableScanlators.subscribe(mangaId)
.flowWithLifecycle(lifecycle)
.distinctUntilChanged() .distinctUntilChanged()
// SY --> // SY -->
.combine( .combine(
@ -938,6 +944,7 @@ class MangaScreenModel(
} }
} }
.catch { error -> logcat(LogPriority.ERROR, error) } .catch { error -> logcat(LogPriority.ERROR, error) }
.flowWithLifecycle(lifecycle)
.collect { .collect {
withUIContext { withUIContext {
updateDownloadState(it) updateDownloadState(it)
@ -956,6 +963,7 @@ class MangaScreenModel(
} }
} }
.catch { error -> logcat(LogPriority.ERROR, error) } .catch { error -> logcat(LogPriority.ERROR, error) }
.flowWithLifecycle(lifecycle)
.collect { .collect {
withUIContext { withUIContext {
updateDownloadState(it) updateDownloadState(it)
@ -1548,6 +1556,7 @@ class MangaScreenModel(
trackingCount to supportedTrackers.isNotEmpty() trackingCount to supportedTrackers.isNotEmpty()
// SY <-- // SY <--
} }
.flowWithLifecycle(lifecycle)
.distinctUntilChanged() .distinctUntilChanged()
.collectLatest { (trackingCount, hasLoggedInTrackers) -> .collectLatest { (trackingCount, hasLoggedInTrackers) ->
updateSuccessState { updateSuccessState {