Toast errors where possible

This commit is contained in:
Syer10
2022-11-05 11:56:58 -04:00
parent a054f262ab
commit 94cf9d77b5
15 changed files with 66 additions and 52 deletions

View File

@@ -47,7 +47,7 @@ class CategoriesScreenViewModel @Inject constructor(
private suspend fun getCategories() {
_categories.value = persistentListOf()
val categories = getCategories.await(true)
val categories = getCategories.await(true, onError = { toast(it.message.orEmpty()) })
if (categories != null) {
_categories.value = categories
.sortedBy { it.order }
@@ -61,24 +61,24 @@ class CategoriesScreenViewModel @Inject constructor(
val categories = _categories.value
val newCategories = categories.filter { it.id == null }
newCategories.forEach {
createCategory.await(it.name)
createCategory.await(it.name, onError = { toast(it.message.orEmpty()) })
}
originalCategories.forEach { originalCategory ->
val category = categories.find { it.id == originalCategory.id }
if (category == null) {
deleteCategory.await(originalCategory)
deleteCategory.await(originalCategory, onError = { toast(it.message.orEmpty()) })
} else if (category.name != originalCategory.name) {
modifyCategory.await(originalCategory, category.name)
modifyCategory.await(originalCategory, category.name, onError = { toast(it.message.orEmpty()) })
}
}
var updatedCategories = getCategories.await(true)
var updatedCategories = getCategories.await(true, onError = { toast(it.message.orEmpty()) })
categories.forEach { category ->
val updatedCategory = updatedCategories?.find { it.id == category.id || it.name == category.name } ?: return@forEach
if (category.order != updatedCategory.order) {
log.debug { "${category.name}: ${updatedCategory.order} to ${category.order}" }
reorderCategory.await(category.order, updatedCategory.order)
reorderCategory.await(category.order, updatedCategory.order, onError = { toast(it.message.orEmpty()) })
}
updatedCategories = getCategories.await(true)
updatedCategories = getCategories.await(true, onError = { toast(it.message.orEmpty()) })
}
if (manualUpdate) {

View File

@@ -54,25 +54,25 @@ class DownloadsScreenViewModel @Inject constructor(
.stateIn(scope, SharingStarted.Eagerly, persistentListOf())
fun start() {
scope.launch { startDownloading.await() }
scope.launch { startDownloading.await(onError = { toast(it.message.orEmpty()) }) }
}
fun pause() {
scope.launch { stopDownloading.await() }
scope.launch { stopDownloading.await(onError = { toast(it.message.orEmpty()) }) }
}
fun clear() {
scope.launch { clearDownloadQueue.await() }
scope.launch { clearDownloadQueue.await(onError = { toast(it.message.orEmpty()) }) }
}
fun stopDownload(chapter: Chapter) {
scope.launch { stopChapterDownload.await(chapter) }
scope.launch { stopChapterDownload.await(chapter, onError = { toast(it.message.orEmpty()) }) }
}
fun moveToBottom(chapter: Chapter) {
scope.launch {
stopChapterDownload.await(chapter)
queueChapterDownload.await(chapter)
stopChapterDownload.await(chapter, onError = { toast(it.message.orEmpty()) })
queueChapterDownload.await(chapter, onError = { toast(it.message.orEmpty()) })
}
}

View File

@@ -80,7 +80,7 @@ class ExtensionsScreenViewModel @Inject constructor(
}
private suspend fun getExtensions() {
extensionList.value = getExtensionList.await().orEmpty()
extensionList.value = getExtensionList.await(onError = { toast(it.message.orEmpty()) }).orEmpty()
_isLoading.value = false
}
@@ -93,7 +93,7 @@ class ExtensionsScreenViewModel @Inject constructor(
.also { file ->
source.saveTo(file)
}
installExtensionFile.await(file)
installExtensionFile.await(file, onError = { toast(it.message.orEmpty()) })
} catch (e: Exception) {
log.warn(e) { "Error creating apk file" }
// todo toast if error
@@ -107,7 +107,7 @@ class ExtensionsScreenViewModel @Inject constructor(
fun install(extension: Extension) {
log.info { "Install clicked" }
scope.launch {
installExtension.await(extension)
installExtension.await(extension, onError = { toast(it.message.orEmpty()) })
getExtensions()
}
}
@@ -115,7 +115,7 @@ class ExtensionsScreenViewModel @Inject constructor(
fun update(extension: Extension) {
log.info { "Update clicked" }
scope.launch {
updateExtension.await(extension)
updateExtension.await(extension, onError = { toast(it.message.orEmpty()) })
getExtensions()
}
}
@@ -123,7 +123,7 @@ class ExtensionsScreenViewModel @Inject constructor(
fun uninstall(extension: Extension) {
log.info { "Uninstall clicked" }
scope.launch {
uninstallExtension.await(extension)
uninstallExtension.await(extension, onError = { toast(it.message.orEmpty()) })
getExtensions()
}
}

View File

@@ -288,7 +288,7 @@ class LibraryScreenViewModel @Inject constructor(
fun removeManga(mangaId: Long) {
scope.launch {
removeMangaFromLibrary.await(mangaId)
removeMangaFromLibrary.await(mangaId, onError = { toast(it.message.orEmpty()) })
}
}
@@ -297,11 +297,11 @@ class LibraryScreenViewModel @Inject constructor(
}
fun updateLibrary() {
scope.launch { updateLibrary.await() }
scope.launch { updateLibrary.await(onError = { toast(it.message.orEmpty()) }) }
}
fun updateCategory(category: Category) {
scope.launch { updateCategory.await(category) }
scope.launch { updateCategory.await(category, onError = { toast(it.message.orEmpty()) }) }
}
private companion object {

View File

@@ -9,7 +9,6 @@ package ca.gosyer.jui.ui.main
import ca.gosyer.jui.domain.ui.service.UiPreferences
import ca.gosyer.jui.i18n.MR
import ca.gosyer.jui.uicore.vm.ContextWrapper
import ca.gosyer.jui.uicore.vm.Length
import ca.gosyer.jui.uicore.vm.ViewModel
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
@@ -30,6 +29,6 @@ class MainViewModel @Inject constructor(
}
fun confirmExitToast() {
toast(MR.strings.confirm_exit_toast.toPlatformString(), Length.SHORT)
toast(MR.strings.confirm_exit_toast.toPlatformString())
}
}

View File

@@ -49,13 +49,13 @@ class AboutViewModel @Inject constructor(
private fun getAbout() {
scope.launch {
_aboutHolder.value = aboutServer.await()
_aboutHolder.value = aboutServer.await(onError = { toast(it.message.orEmpty()) })
}
}
fun checkForUpdates() {
scope.launch {
when (val update = updateChecker.await(true)) {
when (val update = updateChecker.await(true, onError = { toast(it.message.orEmpty()) })) {
is Update.UpdateFound -> _updates.emit(update)
else -> Unit
}

View File

@@ -84,7 +84,10 @@ class MangaScreenViewModel @Inject constructor(
val categories = getCategories.asFlow(true)
.map { it.toImmutableList() }
.catch { log.warn(it) { "Failed to get categories" } }
.catch {
toast(it.message.orEmpty())
log.warn(it) { "Failed to get categories" }
}
.stateIn(scope, SharingStarted.Eagerly, persistentListOf())
private val _mangaCategories = MutableStateFlow<ImmutableList<Category>>(persistentListOf())
@@ -169,14 +172,12 @@ class MangaScreenViewModel @Inject constructor(
private suspend fun refreshChaptersAsync(mangaId: Long, refresh: Boolean = false) = withIOContext {
async {
val chapters = if (refresh) {
refreshChapters.await(mangaId)
refreshChapters.await(mangaId, onError = { toast(it.message.orEmpty()) })
} else {
getChapters.await(mangaId)
getChapters.await(mangaId, onError = { toast(it.message.orEmpty()) })
}
if (chapters != null) {
_chapters.value = chapters.toDownloadChapters()
} else {
// TODO: 2022-07-01 Error toast
}
}
}
@@ -185,7 +186,7 @@ class MangaScreenViewModel @Inject constructor(
scope.launch {
manga.value?.let { manga ->
if (manga.inLibrary) {
removeMangaFromLibrary.await(manga)
removeMangaFromLibrary.await(manga, onError = { toast(it.message.orEmpty()) })
refreshMangaAsync(manga.id).await()
} else {
if (categories.value.isEmpty()) {
@@ -203,13 +204,13 @@ class MangaScreenViewModel @Inject constructor(
manga.value?.let { manga ->
if (manga.inLibrary) {
oldCategories.filterNot { it in categories }.forEach {
removeMangaFromCategory.await(manga, it)
removeMangaFromCategory.await(manga, it, onError = { toast(it.message.orEmpty()) })
}
} else {
addMangaToLibrary.await(manga)
addMangaToLibrary.await(manga, onError = { toast(it.message.orEmpty()) })
}
categories.filterNot { it in oldCategories }.forEach {
addMangaToCategory.await(manga, it)
addMangaToCategory.await(manga, it, onError = { toast(it.message.orEmpty()) })
}
refreshMangaAsync(manga.id).await()
}
@@ -222,7 +223,7 @@ class MangaScreenViewModel @Inject constructor(
val chapter = findChapter(index) ?: return
scope.launch {
manga.value?.let { manga ->
updateChapterRead.await(manga, index, read = chapter.read.not())
updateChapterRead.await(manga, index, read = chapter.read.not(), onError = { toast(it.message.orEmpty()) })
refreshChaptersAsync(manga.id).await()
}
}
@@ -232,7 +233,7 @@ class MangaScreenViewModel @Inject constructor(
val chapter = findChapter(index) ?: return
scope.launch {
manga.value?.let { manga ->
updateChapterBookmarked.await(manga, index, bookmarked = chapter.bookmarked.not())
updateChapterBookmarked.await(manga, index, bookmarked = chapter.bookmarked.not(), onError = { toast(it.message.orEmpty()) })
refreshChaptersAsync(manga.id).await()
}
}
@@ -241,7 +242,7 @@ class MangaScreenViewModel @Inject constructor(
fun markPreviousRead(index: Int) {
scope.launch {
manga.value?.let { manga ->
updateChapterMarkPreviousRead.await(manga, index)
updateChapterMarkPreviousRead.await(manga, index, onError = { toast(it.message.orEmpty()) })
refreshChaptersAsync(manga.id).await()
}
}
@@ -249,7 +250,7 @@ class MangaScreenViewModel @Inject constructor(
fun downloadChapter(index: Int) {
manga.value?.let { manga ->
scope.launch { queueChapterDownload.await(manga, index) }
scope.launch { queueChapterDownload.await(manga, index, onError = { toast(it.message.orEmpty()) }) }
}
}

View File

@@ -191,7 +191,7 @@ class ReaderMenuViewModel @Inject constructor(
fun setMangaReaderMode(mode: String) {
scope.launchDefault {
_manga.value?.let {
updateMangaMeta.await(it, mode)
updateMangaMeta.await(it, mode, onError = { toast(it.message.orEmpty()) })
}
initManga(params.mangaId)
}
@@ -307,14 +307,14 @@ class ReaderMenuViewModel @Inject constructor(
}
private fun markChapterRead(chapter: ReaderChapter) {
scope.launch { updateChapterRead.await(chapter.chapter, read = true) }
scope.launch { updateChapterRead.await(chapter.chapter, read = true, onError = { toast(it.message.orEmpty()) }) }
}
@OptIn(DelicateCoroutinesApi::class)
fun sendProgress(chapter: Chapter? = this.chapter.value?.chapter, lastPageRead: Int = currentPage.value) {
chapter ?: return
if (chapter.read) return
GlobalScope.launch { updateChapterLastPageRead.await(chapter, lastPageRead = lastPageRead) }
GlobalScope.launch { updateChapterLastPageRead.await(chapter, lastPageRead = lastPageRead, onError = { toast(it.message.orEmpty()) }) }
}
fun updateLastPageReadOffset(offset: Int) {
@@ -323,7 +323,7 @@ class ReaderMenuViewModel @Inject constructor(
@OptIn(DelicateCoroutinesApi::class)
private fun updateLastPageReadOffset(chapter: Chapter, offset: Int) {
GlobalScope.launch { updateChapterMeta.await(chapter, offset) }
GlobalScope.launch { updateChapterMeta.await(chapter, offset, onError = { toast(it.message.orEmpty()) }) }
}
override fun onDispose() {

View File

@@ -157,6 +157,7 @@ class SettingsBackupViewModel @Inject constructor(
}
}
.catch {
toast(it.message.orEmpty())
log.warn(it) { "Error importing backup" }
_restoreStatus.value = Status.Error
}
@@ -181,6 +182,7 @@ class SettingsBackupViewModel @Inject constructor(
_restoreStatus.value = Status.Success
}
.catch {
toast(it.message.orEmpty())
log.warn(it) { "Error importing backup" }
_restoreStatus.value = Status.Error
}
@@ -228,6 +230,7 @@ class SettingsBackupViewModel @Inject constructor(
_createFlow.emit(filename)
}
.catch {
toast(it.message.orEmpty())
log.warn(it) { "Error exporting backup" }
_creatingStatus.value = Status.Error
}

View File

@@ -114,7 +114,7 @@ class SettingsLibraryViewModel @Inject constructor(
fun refreshCategoryCount() {
scope.launch {
_categories.value = getCategories.await(true)?.size ?: 0
_categories.value = getCategories.await(true, onError = { toast(it.message.orEmpty()) })?.size ?: 0
}
}

View File

@@ -76,10 +76,11 @@ class SourceFiltersViewModel(
.filterNotNull()
.onEach {
setSourceFilter.await(
sourceId,
filter.index,
childFilter.index,
it
sourceId = sourceId,
filterIndex = filter.index,
childFilterIndex = childFilter.index,
filter = it,
onError = { toast(it.message.orEmpty()) }
)
getFilters()
}
@@ -89,9 +90,10 @@ class SourceFiltersViewModel(
filter.state.drop(1).filterNotNull()
.onEach {
setSourceFilter.await(
sourceId,
filter.index,
it
sourceId = sourceId,
filterIndex = filter.index,
filter = it,
onError = { toast(it.message.orEmpty()) }
)
getFilters()
}
@@ -117,6 +119,7 @@ class SourceFiltersViewModel(
_loading.value = false
}
.catch {
toast(it.message.orEmpty())
log.warn(it) { "Error getting filters" }
_loading.value = false
}

View File

@@ -87,6 +87,7 @@ class GlobalSearchViewModel @Inject constructor(
_isLoading.value = false
}
.catch {
toast(it.message.orEmpty())
log.warn(it) { "Error getting sources" }
_isLoading.value = false
}

View File

@@ -104,6 +104,7 @@ class SourceHomeScreenViewModel @Inject constructor(
_isLoading.value = false
}
.catch {
toast(it.message.orEmpty())
log.warn(it) { "Error getting sources" }
_isLoading.value = false
}

View File

@@ -47,7 +47,11 @@ class SourceSettingsScreenViewModel @Inject constructor(
setting.state.drop(1)
.filterNotNull()
.onEach {
setSourceSetting.await(params.sourceId, setting.index, it)
setSourceSetting.await(
sourceId = params.sourceId,
settingIndex = setting.index,
setting = it,
onError = { toast(it.message.orEmpty()) })
getSourceSettings()
}
.launchIn(this)
@@ -63,6 +67,7 @@ class SourceSettingsScreenViewModel @Inject constructor(
_loading.value = false
}
.catch {
toast(it.message.orEmpty())
log.warn(it) { "Error setting source setting" }
_loading.value = false
}

View File

@@ -121,6 +121,7 @@ class UpdatesScreenViewModel @Inject constructor(
_isLoading.value = false
}
.catch {
toast(it.message.orEmpty())
log.warn(it) { "Failed to get updates for page $page" }
if (page > 1) {
currentPage.value = page - 1
@@ -131,7 +132,7 @@ class UpdatesScreenViewModel @Inject constructor(
}
fun downloadChapter(chapter: Chapter) {
scope.launch { queueChapterDownload.await(chapter) }
scope.launch { queueChapterDownload.await(chapter, onError = { toast(it.message.orEmpty()) }) }
}
fun deleteDownloadedChapter(chapter: Chapter) {