Make MangaCoverViewModel collect manga only while subscribed (#3716)

Assisted-by: Claude:claude-opus-5
This commit is contained in:
AntsyLich
2026-08-08 11:57:55 +06:00
committed by GitHub
parent ed274e26fd
commit 0834884335
5 changed files with 16 additions and 15 deletions
@@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.manga
import android.content.Context
import android.net.Uri
import androidx.compose.material3.SnackbarHostState
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.initializer
@@ -19,10 +20,14 @@ import eu.kanade.tachiyomi.data.saver.Location
import eu.kanade.tachiyomi.util.editCover
import eu.kanade.tachiyomi.util.system.getBitmapOrNull
import eu.kanade.tachiyomi.util.system.toShareIntent
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.WhileSubscribed
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import logcat.LogPriority
import mihon.core.viewmodel.StateViewModel
import tachiyomi.core.common.i18n.stringResource
import tachiyomi.core.common.util.lang.launchIO
import tachiyomi.core.common.util.lang.withIOContext
@@ -33,6 +38,7 @@ import tachiyomi.domain.manga.model.Manga
import tachiyomi.i18n.MR
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import kotlin.time.Duration.Companion.seconds
class MangaCoverViewModel(
private val mangaId: Long,
@@ -42,7 +48,7 @@ class MangaCoverViewModel(
private val updateManga: UpdateManga = Injekt.get(),
val snackbarHostState: SnackbarHostState = SnackbarHostState(),
) : StateViewModel<Manga?>(null) {
) : ViewModel() {
companion object {
val MANGA_ID_KEY = CreationExtras.Key<Long>()
@@ -56,12 +62,9 @@ class MangaCoverViewModel(
}
}
init {
viewModelScope.launchIO {
getManga.subscribe(mangaId)
.collect { newManga -> mutableState.update { newManga } }
}
}
val state: StateFlow<Manga?> = getManga.subscribe(mangaId)
.flowOn(Dispatchers.IO)
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5.seconds), null)
fun saveCover(context: Context) {
viewModelScope.launch {
@@ -147,7 +150,6 @@ class MangaCoverViewModel(
}
fun deleteCustomCover(context: Context) {
val mangaId = state.value?.id ?: return
viewModelScope.launchIO {
try {
coverCache.deleteCustomCover(mangaId)
@@ -6,7 +6,6 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -250,7 +249,7 @@ class MangaScreen(
set(MangaCoverViewModel.MANGA_ID_KEY, successState.manga.id)
},
)
val manga by sm.state.collectAsState()
val manga by sm.state.collectAsStateWithLifecycle()
if (manga != null) {
val getContent = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) {
if (it == null) return@rememberLauncherForActivityResult