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.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
@ -85,9 +87,10 @@ 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 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) { 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
@ -83,8 +85,9 @@ import uy.kohesive.injekt.api.get
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,
private val libraryPreferences: LibraryPreferences = Injekt.get(), private val libraryPreferences: LibraryPreferences = Injekt.get(),
readerPreferences: ReaderPreferences = Injekt.get(), readerPreferences: ReaderPreferences = Injekt.get(),
@ -159,6 +162,7 @@ class MangaScreenModel(
downloadCache.changes, downloadCache.changes,
downloadManager.queueState, downloadManager.queueState,
) { mangaAndChapters, _, _ -> mangaAndChapters } ) { mangaAndChapters, _, _ -> mangaAndChapters }
.flowWithLifecycle(lifecycle)
.collectLatest { (manga, chapters) -> .collectLatest { (manga, chapters) ->
updateSuccessState { updateSuccessState {
it.copy( it.copy(
@ -171,6 +175,7 @@ class MangaScreenModel(
screenModelScope.launchIO { screenModelScope.launchIO {
getExcludedScanlators.subscribe(mangaId) getExcludedScanlators.subscribe(mangaId)
.flowWithLifecycle(lifecycle)
.distinctUntilChanged() .distinctUntilChanged()
.collectLatest { excludedScanlators -> .collectLatest { excludedScanlators ->
updateSuccessState { updateSuccessState {
@ -181,6 +186,7 @@ class MangaScreenModel(
screenModelScope.launchIO { screenModelScope.launchIO {
getAvailableScanlators.subscribe(mangaId) getAvailableScanlators.subscribe(mangaId)
.flowWithLifecycle(lifecycle)
.distinctUntilChanged() .distinctUntilChanged()
.collectLatest { availableScanlators -> .collectLatest { availableScanlators ->
updateSuccessState { updateSuccessState {
@ -464,6 +470,7 @@ class MangaScreenModel(
downloadManager.statusFlow() downloadManager.statusFlow()
.filter { it.manga.id == successState?.manga?.id } .filter { it.manga.id == successState?.manga?.id }
.catch { error -> logcat(LogPriority.ERROR, error) } .catch { error -> logcat(LogPriority.ERROR, error) }
.flowWithLifecycle(lifecycle)
.collect { .collect {
withUIContext { withUIContext {
updateDownloadState(it) updateDownloadState(it)
@ -475,6 +482,7 @@ class MangaScreenModel(
downloadManager.progressFlow() downloadManager.progressFlow()
.filter { it.manga.id == successState?.manga?.id } .filter { it.manga.id == successState?.manga?.id }
.catch { error -> logcat(LogPriority.ERROR, error) } .catch { error -> logcat(LogPriority.ERROR, error) }
.flowWithLifecycle(lifecycle)
.collect { .collect {
withUIContext { withUIContext {
updateDownloadState(it) updateDownloadState(it)
@ -979,6 +987,7 @@ class MangaScreenModel(
val supportedTrackerTracks = mangaTracks.filter { it.trackerId in supportedTrackerIds } val supportedTrackerTracks = mangaTracks.filter { it.trackerId in supportedTrackerIds }
supportedTrackerTracks.size to supportedTrackers.isNotEmpty() supportedTrackerTracks.size to supportedTrackers.isNotEmpty()
} }
.flowWithLifecycle(lifecycle)
.distinctUntilChanged() .distinctUntilChanged()
.collectLatest { (trackingCount, hasLoggedInTrackers) -> .collectLatest { (trackingCount, hasLoggedInTrackers) ->
updateSuccessState { updateSuccessState {