From db35ba53b193eb16b8ace646b9e96b62979351ab Mon Sep 17 00:00:00 2001 From: Andreas Date: Sat, 26 Nov 2022 15:14:11 +0100 Subject: [PATCH] Use Voyager between supported screens in Extension package (#8616) - Minor state behavior changes --- .../extension/ExtensionFilterController.kt | 17 ------------ .../browse/extension/ExtensionFilterScreen.kt | 6 ++--- .../ui/browse/extension/ExtensionsTab.kt | 11 ++++---- .../details/ExtensionDetailsController.kt | 26 ------------------- .../details/ExtensionDetailsScreen.kt | 8 +++--- .../details/ExtensionDetailsScreenModel.kt | 24 +++++++++++------ 6 files changed, 29 insertions(+), 63 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterController.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterController.kt deleted file mode 100644 index ccebb88212..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterController.kt +++ /dev/null @@ -1,17 +0,0 @@ -package eu.kanade.tachiyomi.ui.browse.extension - -import androidx.compose.runtime.Composable -import androidx.compose.runtime.CompositionLocalProvider -import cafe.adriel.voyager.navigator.Navigator -import eu.kanade.presentation.util.LocalRouter -import eu.kanade.tachiyomi.ui.base.controller.BasicFullComposeController - -class ExtensionFilterController : BasicFullComposeController() { - - @Composable - override fun ComposeContent() { - CompositionLocalProvider(LocalRouter provides router) { - Navigator(screen = ExtensionFilterScreen()) - } - } -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterScreen.kt index 928d47929d..d968155d1b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterScreen.kt @@ -7,10 +7,10 @@ import androidx.compose.runtime.getValue import androidx.compose.ui.platform.LocalContext import cafe.adriel.voyager.core.model.rememberScreenModel import cafe.adriel.voyager.core.screen.Screen +import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.currentOrThrow import eu.kanade.presentation.browse.ExtensionFilterScreen import eu.kanade.presentation.components.LoadingScreen -import eu.kanade.presentation.util.LocalRouter import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.util.system.toast import kotlinx.coroutines.flow.collectLatest @@ -20,7 +20,7 @@ class ExtensionFilterScreen : Screen { @Composable override fun Content() { val context = LocalContext.current - val router = LocalRouter.currentOrThrow + val navigator = LocalNavigator.currentOrThrow val screenModel = rememberScreenModel { ExtensionFilterScreenModel() } val state by screenModel.state.collectAsState() @@ -32,7 +32,7 @@ class ExtensionFilterScreen : Screen { val successState = state as ExtensionFilterState.Success ExtensionFilterScreen( - navigateUp = router::popCurrentController, + navigateUp = navigator::pop, state = successState, onClickToggle = { screenModel.toggle(it) }, ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsTab.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsTab.kt index 9e9dd6d81e..7c98756a78 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsTab.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsTab.kt @@ -6,21 +6,20 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.res.stringResource +import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.currentOrThrow import eu.kanade.presentation.browse.ExtensionScreen import eu.kanade.presentation.components.AppBar import eu.kanade.presentation.components.TabContent -import eu.kanade.presentation.util.LocalRouter import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.extension.model.Extension -import eu.kanade.tachiyomi.ui.base.controller.pushController -import eu.kanade.tachiyomi.ui.browse.extension.details.ExtensionDetailsController +import eu.kanade.tachiyomi.ui.browse.extension.details.ExtensionDetailsScreen @Composable fun extensionsTab( extensionsScreenModel: ExtensionsScreenModel, ): TabContent { - val router = LocalRouter.currentOrThrow + val navigator = LocalNavigator.currentOrThrow val state by extensionsScreenModel.state.collectAsState() return TabContent( @@ -31,7 +30,7 @@ fun extensionsTab( AppBar.Action( title = stringResource(R.string.action_filter), icon = Icons.Outlined.Translate, - onClick = { router.pushController(ExtensionFilterController()) }, + onClick = { navigator.push(ExtensionFilterScreen()) }, ), ), content = { contentPadding, _ -> @@ -47,7 +46,7 @@ fun extensionsTab( onClickItemCancel = extensionsScreenModel::cancelInstallUpdateExtension, onClickUpdateAll = extensionsScreenModel::updateAllExtensions, onInstallExtension = extensionsScreenModel::installExtension, - onOpenExtension = { router.pushController(ExtensionDetailsController(it.pkgName)) }, + onOpenExtension = { navigator.push(ExtensionDetailsScreen(it.pkgName)) }, onTrustExtension = { extensionsScreenModel.trustSignature(it.signatureHash) }, onUninstallExtension = { extensionsScreenModel.uninstallExtension(it.pkgName) }, onUpdateExtension = extensionsScreenModel::updateExtension, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt deleted file mode 100644 index fa136ec4b5..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt +++ /dev/null @@ -1,26 +0,0 @@ -package eu.kanade.tachiyomi.ui.browse.extension.details - -import android.os.Bundle -import androidx.compose.runtime.Composable -import androidx.core.os.bundleOf -import cafe.adriel.voyager.navigator.Navigator -import eu.kanade.presentation.browse.ExtensionDetailsScreen -import eu.kanade.tachiyomi.ui.base.controller.BasicFullComposeController - -private const val PKGNAME_KEY = "pkg_name" - -class ExtensionDetailsController : BasicFullComposeController { - - @Suppress("unused") - constructor(bundle: Bundle) : this(bundle.getString(PKGNAME_KEY)!!) - - constructor(pkgName: String) : super(bundleOf(PKGNAME_KEY to pkgName)) - - val pkgName: String - get() = args.getString(PKGNAME_KEY)!! - - @Composable - override fun ComposeContent() { - Navigator(screen = ExtensionDetailsScreen(pkgName = pkgName)) - } -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsScreen.kt index b5aca286d9..e1e82b1d6b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsScreen.kt @@ -8,6 +8,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalUriHandler import cafe.adriel.voyager.core.model.rememberScreenModel import cafe.adriel.voyager.core.screen.Screen +import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.currentOrThrow import eu.kanade.presentation.browse.ExtensionDetailsScreen import eu.kanade.presentation.components.LoadingScreen @@ -15,7 +16,7 @@ import eu.kanade.presentation.util.LocalRouter import eu.kanade.tachiyomi.ui.base.controller.pushController import kotlinx.coroutines.flow.collectLatest -class ExtensionDetailsScreen( +data class ExtensionDetailsScreen( private val pkgName: String, ) : Screen { @@ -30,11 +31,12 @@ class ExtensionDetailsScreen( return } + val navigator = LocalNavigator.currentOrThrow val router = LocalRouter.currentOrThrow val uriHandler = LocalUriHandler.current ExtensionDetailsScreen( - navigateUp = router::popCurrentController, + navigateUp = navigator::pop, state = state, onClickSourcePreferences = { router.pushController(SourcePreferencesController(it)) }, onClickWhatsNew = { uriHandler.openUri(screenModel.getChangelogUrl()) }, @@ -49,7 +51,7 @@ class ExtensionDetailsScreen( LaunchedEffect(Unit) { screenModel.events.collectLatest { event -> if (event is ExtensionDetailsEvent.Uninstalled) { - router.popCurrentController() + navigator.pop() } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsScreenModel.kt index 26c907d8f3..32c5f4552c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsScreenModel.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.browse.extension.details import android.content.Context +import androidx.compose.runtime.Immutable import cafe.adriel.voyager.core.model.StateScreenModel import cafe.adriel.voyager.core.model.coroutineScope import eu.kanade.domain.extension.interactor.ExtensionSourceItem @@ -14,11 +15,13 @@ import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.logcat import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import logcat.LogPriority import okhttp3.HttpUrl.Companion.toHttpUrl import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -69,12 +72,13 @@ class ExtensionDetailsScreenModel( }, ), ) - }.collectLatest { sources -> - mutableState.update { - it.copy( - sources = sources, - ) - } + } + .catch { throwable -> + logcat(LogPriority.ERROR, throwable) + mutableState.update { it.copy(_sources = emptyList()) } + } + .collectLatest { sources -> + mutableState.update { it.copy(_sources = sources) } } } } @@ -157,11 +161,15 @@ sealed class ExtensionDetailsEvent { object Uninstalled : ExtensionDetailsEvent() } +@Immutable data class ExtensionDetailsState( val extension: Extension.Installed? = null, - val sources: List = emptyList(), + private val _sources: List? = null, ) { + val sources: List + get() = _sources ?: emptyList() + val isLoading: Boolean - get() = sources.isEmpty() + get() = extension == null || _sources == null }