Cleanup favorites sync status

This commit is contained in:
Jobobby04 2023-05-09 22:14:23 -04:00
parent 8d83384fb7
commit 6433b5a212

View File

@ -94,7 +94,7 @@ class FavoritesSyncHelper(val context: Context) {
} }
// Validate library state // 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 libraryManga = getLibraryManga.await()
val seenManga = HashSet<Long>(libraryManga.size) val seenManga = HashSet<Long>(libraryManga.size)
libraryManga.forEach { (manga) -> libraryManga.forEach { (manga) ->
@ -113,7 +113,7 @@ class FavoritesSyncHelper(val context: Context) {
// Download remote favorites // Download remote favorites
val favorites = try { 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() exh.fetchFavorites()
} catch (e: Exception) { } catch (e: Exception) {
status.value = FavoritesSyncStatus.Error(context.getString(R.string.favorites_sync_failed_to_featch)) 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 // Do not update galleries while syncing favorites
EHentaiUpdateWorker.cancelBackground(context) 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 remoteChanges = storage.getChangedRemoteEntries(favorites.first)
val localChanges = if (prefs.exhReadOnlySync().get()) { val localChanges = if (prefs.exhReadOnlySync().get()) {
null // Do not build local changes if they are not going to be applied null // Do not build local changes if they are not going to be applied
} else { } 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() storage.getChangedDbEntries()
} }
// Apply remote categories // 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) applyRemoteCategories(favorites.second)
// Apply change sets // Apply change sets
@ -162,7 +162,7 @@ class FavoritesSyncHelper(val context: Context) {
applyChangeSetToRemote(errorList, localChanges) 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() storage.snapshotEntries()
withUIContext { withUIContext {
@ -273,7 +273,7 @@ class FavoritesSyncHelper(val context: Context) {
private suspend fun applyChangeSetToRemote(errorList: MutableList<String>, changeSet: ChangeSet) { private suspend fun applyChangeSetToRemote(errorList: MutableList<String>, changeSet: ChangeSet) {
// Apply removals // Apply removals
if (changeSet.removed.isNotEmpty()) { 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() val formBody = FormBody.Builder()
.add("ddact", "delete") .add("ddact", "delete")
@ -305,9 +305,10 @@ class FavoritesSyncHelper(val context: Context) {
throttleManager.resetThrottle() throttleManager.resetThrottle()
changeSet.added.forEachIndexed { index, it -> changeSet.added.forEachIndexed { index, it ->
status.value = FavoritesSyncStatus.Processing( status.value = FavoritesSyncStatus.Processing(
context.getString(R.string.favorites_sync_adding_to_remote, index + 1, changeSet.added.size), message = context.getString(R.string.favorites_sync_adding_to_remote, index + 1, changeSet.added.size),
needWarnThrottle(), isThrottle = needWarnThrottle(),
context, context = context,
title = it.title,
) )
throttleManager.throttle() throttleManager.throttle()
@ -321,7 +322,7 @@ class FavoritesSyncHelper(val context: Context) {
// Apply removals // Apply removals
changeSet.removed.forEachIndexed { index, it -> 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() val url = it.getUrl()
// Consider both EX and EH sources // Consider both EX and EH sources
@ -351,10 +352,10 @@ class FavoritesSyncHelper(val context: Context) {
throttleManager.resetThrottle() throttleManager.resetThrottle()
changeSet.added.forEachIndexed { index, it -> changeSet.added.forEachIndexed { index, it ->
status.value = FavoritesSyncStatus.Processing( status.value = FavoritesSyncStatus.Processing(
context.getString(R.string.favorites_sync_add_to_local, index + 1, changeSet.added.size), message = context.getString(R.string.favorites_sync_add_to_local, index + 1, changeSet.added.size),
needWarnThrottle(), isThrottle = needWarnThrottle(),
context, context = context,
it.title, title = it.title,
) )
throttleManager.throttle() throttleManager.throttle()
@ -409,26 +410,47 @@ class FavoritesSyncHelper(val context: Context) {
} }
} }
sealed class FavoritesSyncStatus(val message: String) { sealed class FavoritesSyncStatus() {
class Error(message: String) : FavoritesSyncStatus(message) abstract val message: String
class Idle(context: Context) : FavoritesSyncStatus(context.getString(R.string.favorites_sync_waiting_for_start))
sealed class BadLibraryState(message: String) : FavoritesSyncStatus(message) { data class Error(override val message: String) : FavoritesSyncStatus()
class MangaInMultipleCategories( 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 manga: Manga,
val categories: List<Category>, val categories: List<Category>,
context: Context, override val message: String,
) : ) : BadLibraryState() {
BadLibraryState(context.getString(R.string.favorites_sync_gallery_in_multiple_categories, manga.title, categories.joinToString { it.name })) constructor(manga: Manga, categories: List<Category>, 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( 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) { if (isThrottle) {
context.getString(R.string.favorites_sync_processing_throttle, message) context.getString(R.string.favorites_sync_processing_throttle, message)
} else { } else {
message message
}, },
) { title,
)
val delayedMessage get() = if (title != null) this.message + "\n\n" + title else null val delayedMessage get() = if (title != null) this.message + "\n\n" + title else null
} }
class CompleteWithErrors(messages: List<String>) : FavoritesSyncStatus(messages.joinToString("\n")) data class CompleteWithErrors(val messages: List<String>) : FavoritesSyncStatus() {
override val message: String = messages.joinToString("\n")
}
} }