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.LocalHapticFeedback
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.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.Navigator
@ -109,10 +111,11 @@ class MangaScreen(
val context = LocalContext.current
val haptic = LocalHapticFeedback.current
val scope = rememberCoroutineScope()
val lifecycleOwner = LocalLifecycleOwner.current
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) {
LoadingScreen()

View File

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