From cf777d989339a5190966b537aa493c5984d36a93 Mon Sep 17 00:00:00 2001 From: Two-Ai <81279822+Two-Ai@users.noreply.github.com> Date: Fri, 9 Jun 2023 23:11:02 -0400 Subject: [PATCH] Cleanup MangaScreenModel successState usage (#9582) * Refactor updateSuccessState - Convert to inline function - Use when for type safety if we add other MangaScreenState types * Replace equivalent expressions with updateSuccessState * Replace safe cast in MangaScreen --- .../kanade/tachiyomi/ui/manga/MangaScreen.kt | 2 +- .../tachiyomi/ui/manga/MangaScreenModel.kt | 72 ++++++------------- 2 files changed, 21 insertions(+), 53 deletions(-) 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 49f064da82..dce1c8f461 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 @@ -134,7 +134,7 @@ class MangaScreen( ) val onDismissRequest = { screenModel.dismissDialog() } - when (val dialog = (state as? MangaScreenState.Success)?.dialog) { + when (val dialog = successState.dialog) { null -> {} is MangaInfoScreenModel.Dialog.ChangeCategory -> { ChangeCategoryDialog( 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 e6715dd60a..a731e48a55 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 @@ -135,8 +135,13 @@ class MangaInfoScreenModel( /** * Helper function to update the UI state only if it's currently in success state */ - private fun updateSuccessState(func: (MangaScreenState.Success) -> MangaScreenState.Success) { - mutableState.update { if (it is MangaScreenState.Success) func(it) else it } + private inline fun updateSuccessState(func: (MangaScreenState.Success) -> MangaScreenState.Success) { + mutableState.update { + when (it) { + MangaScreenState.Loading -> it + is MangaScreenState.Success -> func(it) + } + } } init { @@ -278,12 +283,7 @@ class MangaInfoScreenModel( val duplicate = getDuplicateLibraryManga.await(manga.title) if (duplicate != null) { - mutableState.update { state -> - when (state) { - MangaScreenState.Loading -> state - is MangaScreenState.Success -> state.copy(dialog = Dialog.DuplicateManga(manga, duplicate)) - } - } + updateSuccessState { it.copy(dialog = Dialog.DuplicateManga(manga, duplicate)) } return@launchIO } } @@ -340,16 +340,13 @@ class MangaInfoScreenModel( coroutineScope.launch { val categories = getCategories() val selection = getMangaCategoryIds(manga) - mutableState.update { state -> - when (state) { - MangaScreenState.Loading -> state - is MangaScreenState.Success -> state.copy( - dialog = Dialog.ChangeCategory( - manga = manga, - initialSelection = categories.mapAsCheckboxState { it.id in selection }, - ), - ) - } + updateSuccessState { successState -> + successState.copy( + dialog = Dialog.ChangeCategory( + manga = manga, + initialSelection = categories.mapAsCheckboxState { it.id in selection }, + ), + ) } } } @@ -954,52 +951,23 @@ class MangaInfoScreenModel( } fun dismissDialog() { - mutableState.update { state -> - when (state) { - MangaScreenState.Loading -> state - is MangaScreenState.Success -> state.copy(dialog = null) - } - } + updateSuccessState { it.copy(dialog = null) } } fun showDeleteChapterDialog(chapters: List) { - mutableState.update { state -> - when (state) { - MangaScreenState.Loading -> state - is MangaScreenState.Success -> state.copy(dialog = Dialog.DeleteChapters(chapters)) - } - } + updateSuccessState { it.copy(dialog = Dialog.DeleteChapters(chapters)) } } fun showSettingsDialog() { - mutableState.update { state -> - when (state) { - MangaScreenState.Loading -> state - is MangaScreenState.Success -> state.copy(dialog = Dialog.SettingsSheet) - } - } + updateSuccessState { it.copy(dialog = Dialog.SettingsSheet) } } fun showTrackDialog() { - mutableState.update { state -> - when (state) { - MangaScreenState.Loading -> state - is MangaScreenState.Success -> { - state.copy(dialog = Dialog.TrackSheet) - } - } - } + updateSuccessState { it.copy(dialog = Dialog.TrackSheet) } } fun showCoverDialog() { - mutableState.update { state -> - when (state) { - MangaScreenState.Loading -> state - is MangaScreenState.Success -> { - state.copy(dialog = Dialog.FullCover) - } - } - } + updateSuccessState { it.copy(dialog = Dialog.FullCover) } } }