Cleanup favorites sync status
This commit is contained in:
parent
8d83384fb7
commit
6433b5a212
@ -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<Long>(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<String>, 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<Category>,
|
||||
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<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(
|
||||
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<String>) : FavoritesSyncStatus(messages.joinToString("\n"))
|
||||
data class CompleteWithErrors(val messages: List<String>) : FavoritesSyncStatus() {
|
||||
override val message: String = messages.joinToString("\n")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user