From 6433b5a212fa97efe76d34e055d0aeb4dbf07543 Mon Sep 17 00:00:00 2001 From: Jobobby04 Date: Tue, 9 May 2023 22:14:23 -0400 Subject: [PATCH] Cleanup favorites sync status --- .../java/exh/favorites/FavoritesSyncHelper.kt | 86 ++++++++++++------- 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/exh/favorites/FavoritesSyncHelper.kt b/app/src/main/java/exh/favorites/FavoritesSyncHelper.kt index 657287a0d..67cc0537c 100644 --- a/app/src/main/java/exh/favorites/FavoritesSyncHelper.kt +++ b/app/src/main/java/exh/favorites/FavoritesSyncHelper.kt @@ -94,7 +94,7 @@ class FavoritesSyncHelper(val context: Context) { } // Validate library state - status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_verifying_library), context = context) + status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_verifying_library)) val libraryManga = getLibraryManga.await() val seenManga = HashSet(libraryManga.size) libraryManga.forEach { (manga) -> @@ -113,7 +113,7 @@ class FavoritesSyncHelper(val context: Context) { // Download remote favorites val favorites = try { - status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_downloading), context = context) + status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_downloading)) exh.fetchFavorites() } catch (e: Exception) { status.value = FavoritesSyncStatus.Error(context.getString(R.string.favorites_sync_failed_to_featch)) @@ -143,17 +143,17 @@ class FavoritesSyncHelper(val context: Context) { // Do not update galleries while syncing favorites EHentaiUpdateWorker.cancelBackground(context) - status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_calculating_remote_changes), context = context) + status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_calculating_remote_changes)) val remoteChanges = storage.getChangedRemoteEntries(favorites.first) val localChanges = if (prefs.exhReadOnlySync().get()) { null // Do not build local changes if they are not going to be applied } else { - status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_calculating_local_changes), context = context) + status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_calculating_local_changes)) storage.getChangedDbEntries() } // Apply remote categories - status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_syncing_category_names), context = context) + status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_syncing_category_names)) applyRemoteCategories(favorites.second) // Apply change sets @@ -162,7 +162,7 @@ class FavoritesSyncHelper(val context: Context) { applyChangeSetToRemote(errorList, localChanges) } - status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_cleaning_up), context = context) + status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_cleaning_up)) storage.snapshotEntries() withUIContext { @@ -273,7 +273,7 @@ class FavoritesSyncHelper(val context: Context) { private suspend fun applyChangeSetToRemote(errorList: MutableList, changeSet: ChangeSet) { // Apply removals if (changeSet.removed.isNotEmpty()) { - status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_removing_galleries, changeSet.removed.size), context = context) + status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_removing_galleries, changeSet.removed.size)) val formBody = FormBody.Builder() .add("ddact", "delete") @@ -305,9 +305,10 @@ class FavoritesSyncHelper(val context: Context) { throttleManager.resetThrottle() changeSet.added.forEachIndexed { index, it -> status.value = FavoritesSyncStatus.Processing( - context.getString(R.string.favorites_sync_adding_to_remote, index + 1, changeSet.added.size), - needWarnThrottle(), - context, + message = context.getString(R.string.favorites_sync_adding_to_remote, index + 1, changeSet.added.size), + isThrottle = needWarnThrottle(), + context = context, + title = it.title, ) throttleManager.throttle() @@ -321,7 +322,7 @@ class FavoritesSyncHelper(val context: Context) { // Apply removals changeSet.removed.forEachIndexed { index, it -> - status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_remove_from_local, index + 1, changeSet.removed.size), context = context) + status.value = FavoritesSyncStatus.Processing(context.getString(R.string.favorites_sync_remove_from_local, index + 1, changeSet.removed.size), title = it.title) val url = it.getUrl() // Consider both EX and EH sources @@ -351,10 +352,10 @@ class FavoritesSyncHelper(val context: Context) { throttleManager.resetThrottle() changeSet.added.forEachIndexed { index, it -> status.value = FavoritesSyncStatus.Processing( - context.getString(R.string.favorites_sync_add_to_local, index + 1, changeSet.added.size), - needWarnThrottle(), - context, - it.title, + message = context.getString(R.string.favorites_sync_add_to_local, index + 1, changeSet.added.size), + isThrottle = needWarnThrottle(), + context = context, + title = it.title, ) throttleManager.throttle() @@ -409,26 +410,47 @@ class FavoritesSyncHelper(val context: Context) { } } -sealed class FavoritesSyncStatus(val message: String) { - class Error(message: String) : FavoritesSyncStatus(message) - class Idle(context: Context) : FavoritesSyncStatus(context.getString(R.string.favorites_sync_waiting_for_start)) - sealed class BadLibraryState(message: String) : FavoritesSyncStatus(message) { - class MangaInMultipleCategories( +sealed class FavoritesSyncStatus() { + abstract val message: String + + data class Error(override val message: String) : FavoritesSyncStatus() + data class Idle(override val message: String) : FavoritesSyncStatus() { + constructor(context: Context) : this(context.getString(R.string.favorites_sync_waiting_for_start)) + } + sealed class BadLibraryState : FavoritesSyncStatus() { + data class MangaInMultipleCategories( val manga: Manga, val categories: List, - context: Context, - ) : - BadLibraryState(context.getString(R.string.favorites_sync_gallery_in_multiple_categories, manga.title, categories.joinToString { it.name })) + override val message: String, + ) : BadLibraryState() { + constructor(manga: Manga, categories: List, context: Context) : + this( + manga = manga, + categories = categories, + message = context.getString(R.string.favorites_sync_gallery_in_multiple_categories, manga.title, categories.joinToString { it.name }), + ) + } } - class Initializing(context: Context) : FavoritesSyncStatus(context.getString(R.string.favorites_sync_initializing)) - class Processing(message: String, isThrottle: Boolean = false, context: Context, val title: String? = null) : FavoritesSyncStatus( - if (isThrottle) { - context.getString(R.string.favorites_sync_processing_throttle, message) - } else { - message - }, - ) { + data class Initializing(override val message: String) : FavoritesSyncStatus() { + constructor(context: Context) : this(context.getString(R.string.favorites_sync_initializing)) + } + data class Processing( + override val message: String, + val title: String? = null, + ) : FavoritesSyncStatus() { + constructor(message: String, isThrottle: Boolean, context: Context, title: String?) : + this( + if (isThrottle) { + context.getString(R.string.favorites_sync_processing_throttle, message) + } else { + message + }, + title, + ) + val delayedMessage get() = if (title != null) this.message + "\n\n" + title else null } - class CompleteWithErrors(messages: List) : FavoritesSyncStatus(messages.joinToString("\n")) + data class CompleteWithErrors(val messages: List) : FavoritesSyncStatus() { + override val message: String = messages.joinToString("\n") + } }