From f4105025c81947b4bf45460eb3cca69f955d6517 Mon Sep 17 00:00:00 2001 From: Cuong-Tran Date: Thu, 24 Oct 2024 13:10:58 +0700 Subject: [PATCH] Using screenModel to load categories instead of runBlocking --- .../settings/screen/SettingsDownloadScreen.kt | 16 ++++++++++++---- .../settings/screen/SettingsLibraryScreen.kt | 17 +++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDownloadScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDownloadScreen.kt index 27f0504ce..df2349b4c 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDownloadScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDownloadScreen.kt @@ -9,14 +9,15 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.util.fastMap +import cafe.adriel.voyager.core.model.rememberScreenModel import eu.kanade.presentation.category.visualName import eu.kanade.presentation.more.settings.Preference import eu.kanade.presentation.more.settings.widget.TriStateListDialog +import eu.kanade.tachiyomi.ui.category.CategoryScreenModel +import eu.kanade.tachiyomi.ui.category.CategoryScreenState import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentMapOf import kotlinx.collections.immutable.toImmutableMap -import kotlinx.coroutines.runBlocking -import tachiyomi.domain.category.interactor.GetCategories import tachiyomi.domain.category.model.Category import tachiyomi.domain.download.service.DownloadPreferences import tachiyomi.i18n.MR @@ -34,8 +35,15 @@ object SettingsDownloadScreen : SearchableSettings { @Composable override fun getPreferences(): List { - val getCategories = remember { Injekt.get() } - val allCategories by getCategories.subscribe().collectAsState(initial = runBlocking { getCategories.await() }) + val screenModel = rememberScreenModel { CategoryScreenModel() } + val state by screenModel.state.collectAsState() + val allCategories = when (state) { + is CategoryScreenState.Loading -> emptyList() + else -> { + val successState = state as CategoryScreenState.Success + successState.categories + } + } val downloadPreferences = remember { Injekt.get() } return listOf( diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsLibraryScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsLibraryScreen.kt index c3ef07d42..00728bed1 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsLibraryScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsLibraryScreen.kt @@ -12,6 +12,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.util.fastMap import androidx.core.content.ContextCompat +import cafe.adriel.voyager.core.model.rememberScreenModel import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.Navigator import cafe.adriel.voyager.navigator.currentOrThrow @@ -20,12 +21,12 @@ import eu.kanade.presentation.more.settings.Preference import eu.kanade.presentation.more.settings.widget.TriStateListDialog import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.ui.category.CategoryScreen +import eu.kanade.tachiyomi.ui.category.CategoryScreenModel +import eu.kanade.tachiyomi.ui.category.CategoryScreenState import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentMapOf import kotlinx.collections.immutable.toImmutableMap import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking -import tachiyomi.domain.category.interactor.GetCategories import tachiyomi.domain.category.interactor.ResetCategoryFlags import tachiyomi.domain.category.model.Category import tachiyomi.domain.library.service.LibraryPreferences @@ -51,9 +52,17 @@ object SettingsLibraryScreen : SearchableSettings { @Composable override fun getPreferences(): List { - val getCategories = remember { Injekt.get() } val libraryPreferences = remember { Injekt.get() } - val allCategories by getCategories.subscribe().collectAsState(initial = runBlocking { getCategories.await() }) + + val screenModel = rememberScreenModel { CategoryScreenModel() } + val state by screenModel.state.collectAsState() + val allCategories = when (state) { + is CategoryScreenState.Loading -> emptyList() + else -> { + val successState = state as CategoryScreenState.Success + successState.categories + } + } return listOf( getCategoriesGroup(LocalNavigator.currentOrThrow, allCategories, libraryPreferences),