Collect MangaScreen state with lifecycle

Co-authored-by: ivan <12537387+ivaniskandar@users.noreply.github.com>
This commit is contained in:
AntsyLich 2024-09-02 21:22:21 +06:00
parent a45eb5e528
commit 03eb756ecb
No known key found for this signature in database
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
@ -85,9 +87,10 @@ class MangaScreen(
val context = LocalContext.current
val haptic = LocalHapticFeedback.current
val scope = rememberCoroutineScope()
val screenModel = rememberScreenModel { MangaScreenModel(context, mangaId, fromSource) }
val lifecycleOwner = LocalLifecycleOwner.current
val screenModel = rememberScreenModel { MangaScreenModel(context, lifecycleOwner.lifecycle, mangaId, fromSource) }
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
@ -83,8 +85,9 @@ import uy.kohesive.injekt.api.get
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,
private val libraryPreferences: LibraryPreferences = Injekt.get(),
readerPreferences: ReaderPreferences = Injekt.get(),
@ -159,6 +162,7 @@ class MangaScreenModel(
downloadCache.changes,
downloadManager.queueState,
) { mangaAndChapters, _, _ -> mangaAndChapters }
.flowWithLifecycle(lifecycle)
.collectLatest { (manga, chapters) ->
updateSuccessState {
it.copy(
@ -171,6 +175,7 @@ class MangaScreenModel(
screenModelScope.launchIO {
getExcludedScanlators.subscribe(mangaId)
.flowWithLifecycle(lifecycle)
.distinctUntilChanged()
.collectLatest { excludedScanlators ->
updateSuccessState {
@ -181,6 +186,7 @@ class MangaScreenModel(
screenModelScope.launchIO {
getAvailableScanlators.subscribe(mangaId)
.flowWithLifecycle(lifecycle)
.distinctUntilChanged()
.collectLatest { availableScanlators ->
updateSuccessState {
@ -464,6 +470,7 @@ class MangaScreenModel(
downloadManager.statusFlow()
.filter { it.manga.id == successState?.manga?.id }
.catch { error -> logcat(LogPriority.ERROR, error) }
.flowWithLifecycle(lifecycle)
.collect {
withUIContext {
updateDownloadState(it)
@ -475,6 +482,7 @@ class MangaScreenModel(
downloadManager.progressFlow()
.filter { it.manga.id == successState?.manga?.id }
.catch { error -> logcat(LogPriority.ERROR, error) }
.flowWithLifecycle(lifecycle)
.collect {
withUIContext {
updateDownloadState(it)
@ -979,6 +987,7 @@ class MangaScreenModel(
val supportedTrackerTracks = mangaTracks.filter { it.trackerId in supportedTrackerIds }
supportedTrackerTracks.size to supportedTrackers.isNotEmpty()
}
.flowWithLifecycle(lifecycle)
.distinctUntilChanged()
.collectLatest { (trackingCount, hasLoggedInTrackers) ->
updateSuccessState {