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
This commit is contained in:
Two-Ai 2023-06-09 23:11:02 -04:00 committed by GitHub
parent 0d9f8e8743
commit cf777d9893
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 53 deletions

View file

@ -134,7 +134,7 @@ class MangaScreen(
) )
val onDismissRequest = { screenModel.dismissDialog() } val onDismissRequest = { screenModel.dismissDialog() }
when (val dialog = (state as? MangaScreenState.Success)?.dialog) { when (val dialog = successState.dialog) {
null -> {} null -> {}
is MangaInfoScreenModel.Dialog.ChangeCategory -> { is MangaInfoScreenModel.Dialog.ChangeCategory -> {
ChangeCategoryDialog( ChangeCategoryDialog(

View file

@ -135,8 +135,13 @@ class MangaInfoScreenModel(
/** /**
* Helper function to update the UI state only if it's currently in success state * Helper function to update the UI state only if it's currently in success state
*/ */
private fun updateSuccessState(func: (MangaScreenState.Success) -> MangaScreenState.Success) { private inline fun updateSuccessState(func: (MangaScreenState.Success) -> MangaScreenState.Success) {
mutableState.update { if (it is MangaScreenState.Success) func(it) else it } mutableState.update {
when (it) {
MangaScreenState.Loading -> it
is MangaScreenState.Success -> func(it)
}
}
} }
init { init {
@ -278,12 +283,7 @@ class MangaInfoScreenModel(
val duplicate = getDuplicateLibraryManga.await(manga.title) val duplicate = getDuplicateLibraryManga.await(manga.title)
if (duplicate != null) { if (duplicate != null) {
mutableState.update { state -> updateSuccessState { it.copy(dialog = Dialog.DuplicateManga(manga, duplicate)) }
when (state) {
MangaScreenState.Loading -> state
is MangaScreenState.Success -> state.copy(dialog = Dialog.DuplicateManga(manga, duplicate))
}
}
return@launchIO return@launchIO
} }
} }
@ -340,10 +340,8 @@ class MangaInfoScreenModel(
coroutineScope.launch { coroutineScope.launch {
val categories = getCategories() val categories = getCategories()
val selection = getMangaCategoryIds(manga) val selection = getMangaCategoryIds(manga)
mutableState.update { state -> updateSuccessState { successState ->
when (state) { successState.copy(
MangaScreenState.Loading -> state
is MangaScreenState.Success -> state.copy(
dialog = Dialog.ChangeCategory( dialog = Dialog.ChangeCategory(
manga = manga, manga = manga,
initialSelection = categories.mapAsCheckboxState { it.id in selection }, initialSelection = categories.mapAsCheckboxState { it.id in selection },
@ -352,7 +350,6 @@ class MangaInfoScreenModel(
} }
} }
} }
}
/** /**
* Returns true if the manga has any downloads. * Returns true if the manga has any downloads.
@ -954,52 +951,23 @@ class MangaInfoScreenModel(
} }
fun dismissDialog() { fun dismissDialog() {
mutableState.update { state -> updateSuccessState { it.copy(dialog = null) }
when (state) {
MangaScreenState.Loading -> state
is MangaScreenState.Success -> state.copy(dialog = null)
}
}
} }
fun showDeleteChapterDialog(chapters: List<Chapter>) { fun showDeleteChapterDialog(chapters: List<Chapter>) {
mutableState.update { state -> updateSuccessState { it.copy(dialog = Dialog.DeleteChapters(chapters)) }
when (state) {
MangaScreenState.Loading -> state
is MangaScreenState.Success -> state.copy(dialog = Dialog.DeleteChapters(chapters))
}
}
} }
fun showSettingsDialog() { fun showSettingsDialog() {
mutableState.update { state -> updateSuccessState { it.copy(dialog = Dialog.SettingsSheet) }
when (state) {
MangaScreenState.Loading -> state
is MangaScreenState.Success -> state.copy(dialog = Dialog.SettingsSheet)
}
}
} }
fun showTrackDialog() { fun showTrackDialog() {
mutableState.update { state -> updateSuccessState { it.copy(dialog = Dialog.TrackSheet) }
when (state) {
MangaScreenState.Loading -> state
is MangaScreenState.Success -> {
state.copy(dialog = Dialog.TrackSheet)
}
}
}
} }
fun showCoverDialog() { fun showCoverDialog() {
mutableState.update { state -> updateSuccessState { it.copy(dialog = Dialog.FullCover) }
when (state) {
MangaScreenState.Loading -> state
is MangaScreenState.Success -> {
state.copy(dialog = Dialog.FullCover)
}
}
}
} }
} }