From aef1dc6eaffd96a5ea14cf897f78b9f26db48167 Mon Sep 17 00:00:00 2001 From: FourTOne5 <59261191+FourTOne5@users.noreply.github.com> Date: Mon, 2 May 2022 08:35:34 +0600 Subject: [PATCH] Make `MigrateSourceState` similar to `MigrateState` (#7054) --- .../source/MigrateSourceScreen.kt | 17 +++++++---- .../sources/MigrationSourcesPresenter.kt | 28 +++++++------------ 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/source/MigrateSourceScreen.kt b/app/src/main/java/eu/kanade/presentation/source/MigrateSourceScreen.kt index dcfcffea3c..2554417bcd 100644 --- a/app/src/main/java/eu/kanade/presentation/source/MigrateSourceScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/source/MigrateSourceScreen.kt @@ -26,6 +26,7 @@ import eu.kanade.presentation.source.components.BaseSourceItem import eu.kanade.presentation.theme.header import eu.kanade.presentation.util.horizontalPadding import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.ui.browse.migration.sources.MigrateSourceState import eu.kanade.tachiyomi.ui.browse.migration.sources.MigrationSourcesPresenter @Composable @@ -36,17 +37,16 @@ fun MigrateSourceScreen( onLongClickItem: (Source) -> Unit, ) { val state by presenter.state.collectAsState() - when { - state.isLoading -> LoadingScreen() - state.isEmpty -> EmptyScreen(textResource = R.string.information_empty_library) - else -> { + when (state) { + is MigrateSourceState.Loading -> LoadingScreen() + is MigrateSourceState.Error -> Text(text = (state as MigrateSourceState.Error).error.message!!) + is MigrateSourceState.Success -> MigrateSourceList( nestedScrollInterop = nestedScrollInterop, - list = state.sources!!, + list = (state as MigrateSourceState.Success).sources, onClickItem = onClickItem, onLongClickItem = onLongClickItem, ) - } } } @@ -57,6 +57,11 @@ fun MigrateSourceList( onClickItem: (Source) -> Unit, onLongClickItem: (Source) -> Unit, ) { + if (list.isEmpty()) { + EmptyScreen(textResource = R.string.information_empty_library) + return + } + LazyColumn( modifier = Modifier.nestedScroll(nestedScrollInterop), contentPadding = WindowInsets.navigationBars.asPaddingValues(), diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesPresenter.kt index 1d4ca46c3e..37a2c10695 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesPresenter.kt @@ -9,8 +9,8 @@ import eu.kanade.tachiyomi.util.lang.launchIO import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.collectLatest -import kotlinx.coroutines.flow.update import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -19,7 +19,7 @@ class MigrationSourcesPresenter( private val setMigrateSorting: SetMigrateSorting = Injekt.get() ) : BasePresenter() { - private val _state: MutableStateFlow = MutableStateFlow(MigrateSourceState.EMPTY) + private val _state: MutableStateFlow = MutableStateFlow(MigrateSourceState.Loading) val state: StateFlow = _state.asStateFlow() override fun onCreate(savedState: Bundle?) { @@ -27,10 +27,11 @@ class MigrationSourcesPresenter( presenterScope.launchIO { getSourcesWithFavoriteCount.subscribe() + .catch { exception -> + _state.emit(MigrateSourceState.Error(exception)) + } .collectLatest { sources -> - _state.update { state -> - state.copy(sources = sources) - } + _state.emit(MigrateSourceState.Success(sources)) } } } @@ -44,17 +45,8 @@ class MigrationSourcesPresenter( } } -data class MigrateSourceState( - val sources: List>? -) { - - val isLoading: Boolean - get() = sources == null - - val isEmpty: Boolean - get() = sources.isNullOrEmpty() - - companion object { - val EMPTY = MigrateSourceState(null) - } +sealed class MigrateSourceState { + object Loading : MigrateSourceState() + data class Error(val error: Throwable) : MigrateSourceState() + data class Success(val sources: List>) : MigrateSourceState() }