diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreen.kt index e500a04d3..2ac921507 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreen.kt @@ -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() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt index 248342e59..287176b00 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt @@ -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 {