Fix library is backed up when disabled and make categories backup/restore independent

This commit is contained in:
AntsyLich 2024-07-30 04:47:57 +06:00
parent 0af90999c8
commit 56fb4f62a1
No known key found for this signature in database
7 changed files with 42 additions and 31 deletions

View File

@ -6,7 +6,6 @@ import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable import androidx.compose.runtime.Immutable
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
@ -68,7 +67,7 @@ class CreateBackupScreen : Screen() {
LazyColumnWithAction( LazyColumnWithAction(
contentPadding = contentPadding, contentPadding = contentPadding,
actionLabel = stringResource(MR.strings.action_create), actionLabel = stringResource(MR.strings.action_create),
actionEnabled = state.options.anyEnabled(), actionEnabled = state.options.canCreate(),
onClickAction = { onClickAction = {
if (!BackupCreateJob.isManualJobRunning(context)) { if (!BackupCreateJob.isManualJobRunning(context)) {
try { try {
@ -103,7 +102,7 @@ class CreateBackupScreen : Screen() {
} }
@Composable @Composable
private fun ColumnScope.Options( private fun Options(
options: ImmutableList<BackupOptions.Entry>, options: ImmutableList<BackupOptions.Entry>,
state: CreateBackupScreenModel.State, state: CreateBackupScreenModel.State,
model: CreateBackupScreenModel, model: CreateBackupScreenModel,

View File

@ -63,7 +63,7 @@ class RestoreBackupScreen(
LazyColumnWithAction( LazyColumnWithAction(
contentPadding = contentPadding, contentPadding = contentPadding,
actionLabel = stringResource(MR.strings.action_restore), actionLabel = stringResource(MR.strings.action_restore),
actionEnabled = state.canRestore && state.options.anyEnabled(), actionEnabled = state.canRestore && state.options.canRestore(),
onClickAction = { onClickAction = {
model.startRestore() model.startRestore()
navigator.pop() navigator.pop()

View File

@ -74,11 +74,11 @@ class BackupCreator(
throw IllegalStateException(context.stringResource(MR.strings.create_backup_file_error)) throw IllegalStateException(context.stringResource(MR.strings.create_backup_file_error))
} }
val databaseManga = getFavorites.await() val backupManga = backupMangas(getFavorites.await(), options)
val backup = Backup( val backup = Backup(
backupManga = backupMangas(databaseManga, options), backupManga = backupManga,
backupCategories = backupCategories(options), backupCategories = backupCategories(options),
backupSources = backupSources(databaseManga), backupSources = backupSources(backupManga),
backupPreferences = backupAppPreferences(options), backupPreferences = backupAppPreferences(options),
backupSourcePreferences = backupSourcePreferences(options), backupSourcePreferences = backupSourcePreferences(options),
) )
@ -120,10 +120,12 @@ class BackupCreator(
} }
private suspend fun backupMangas(mangas: List<Manga>, options: BackupOptions): List<BackupManga> { private suspend fun backupMangas(mangas: List<Manga>, options: BackupOptions): List<BackupManga> {
if (!options.libraryEntries) return emptyList()
return mangaBackupCreator.backupMangas(mangas, options) return mangaBackupCreator.backupMangas(mangas, options)
} }
private fun backupSources(mangas: List<Manga>): List<BackupSource> { private fun backupSources(mangas: List<BackupManga>): List<BackupSource> {
return sourcesBackupCreator.backupSources(mangas) return sourcesBackupCreator.backupSources(mangas)
} }

View File

@ -26,7 +26,7 @@ data class BackupOptions(
privateSettings, privateSettings,
) )
fun anyEnabled() = libraryEntries || appSettings || sourceSettings fun canCreate() = libraryEntries || categories || appSettings || sourceSettings
companion object { companion object {
val libraryOptions = persistentListOf( val libraryOptions = persistentListOf(
@ -35,12 +35,6 @@ data class BackupOptions(
getter = BackupOptions::libraryEntries, getter = BackupOptions::libraryEntries,
setter = { options, enabled -> options.copy(libraryEntries = enabled) }, setter = { options, enabled -> options.copy(libraryEntries = enabled) },
), ),
Entry(
label = MR.strings.categories,
getter = BackupOptions::categories,
setter = { options, enabled -> options.copy(categories = enabled) },
enabled = { it.libraryEntries },
),
Entry( Entry(
label = MR.strings.chapters, label = MR.strings.chapters,
getter = BackupOptions::chapters, getter = BackupOptions::chapters,
@ -59,6 +53,11 @@ data class BackupOptions(
setter = { options, enabled -> options.copy(history = enabled) }, setter = { options, enabled -> options.copy(history = enabled) },
enabled = { it.libraryEntries }, enabled = { it.libraryEntries },
), ),
Entry(
label = MR.strings.categories,
getter = BackupOptions::categories,
setter = { options, enabled -> options.copy(categories = enabled) },
),
) )
val settingsOptions = persistentListOf( val settingsOptions = persistentListOf(

View File

@ -1,8 +1,8 @@
package eu.kanade.tachiyomi.data.backup.create.creators package eu.kanade.tachiyomi.data.backup.create.creators
import eu.kanade.tachiyomi.data.backup.models.BackupManga
import eu.kanade.tachiyomi.data.backup.models.BackupSource import eu.kanade.tachiyomi.data.backup.models.BackupSource
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.source.service.SourceManager import tachiyomi.domain.source.service.SourceManager
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -11,10 +11,10 @@ class SourcesBackupCreator(
private val sourceManager: SourceManager = Injekt.get(), private val sourceManager: SourceManager = Injekt.get(),
) { ) {
fun backupSources(mangas: List<Manga>): List<BackupSource> { fun backupSources(mangas: List<BackupManga>): List<BackupSource> {
return mangas return mangas
.asSequence() .asSequence()
.map(Manga::source) .map(BackupManga::source)
.distinct() .distinct()
.map(sourceManager::getOrStub) .map(sourceManager::getOrStub)
.map { it.toBackupSource() } .map { it.toBackupSource() }

View File

@ -67,8 +67,11 @@ class BackupRestorer(
val backupMaps = backup.backupSources + backup.backupBrokenSources.map { it.toBackupSource() } val backupMaps = backup.backupSources + backup.backupBrokenSources.map { it.toBackupSource() }
sourceMapping = backupMaps.associate { it.sourceId to it.name } sourceMapping = backupMaps.associate { it.sourceId to it.name }
if (options.library) { if (options.libraryEntries) {
restoreAmount += backup.backupManga.size + 1 // +1 for categories restoreAmount += backup.backupManga.size
}
if (options.categories) {
restoreAmount += 1
} }
if (options.appSettings) { if (options.appSettings) {
restoreAmount += 1 restoreAmount += 1
@ -78,7 +81,7 @@ class BackupRestorer(
} }
coroutineScope { coroutineScope {
if (options.library) { if (options.categories) {
restoreCategories(backup.backupCategories) restoreCategories(backup.backupCategories)
} }
if (options.appSettings) { if (options.appSettings) {
@ -87,8 +90,8 @@ class BackupRestorer(
if (options.sourceSettings) { if (options.sourceSettings) {
restoreSourcePreferences(backup.backupSourcePreferences) restoreSourcePreferences(backup.backupSourcePreferences)
} }
if (options.library) { if (options.libraryEntries) {
restoreManga(backup.backupManga, backup.backupCategories) restoreManga(backup.backupManga, if (options.categories) backup.backupCategories else emptyList())
} }
// TODO: optionally trigger online library + tracker update // TODO: optionally trigger online library + tracker update

View File

@ -5,25 +5,32 @@ import kotlinx.collections.immutable.persistentListOf
import tachiyomi.i18n.MR import tachiyomi.i18n.MR
data class RestoreOptions( data class RestoreOptions(
val library: Boolean = true, val libraryEntries: Boolean = true,
val categories: Boolean = true,
val appSettings: Boolean = true, val appSettings: Boolean = true,
val sourceSettings: Boolean = true, val sourceSettings: Boolean = true,
) { ) {
fun asBooleanArray() = booleanArrayOf( fun asBooleanArray() = booleanArrayOf(
library, libraryEntries,
categories,
appSettings, appSettings,
sourceSettings, sourceSettings,
) )
fun anyEnabled() = library || appSettings || sourceSettings fun canRestore() = libraryEntries || categories || appSettings || sourceSettings
companion object { companion object {
val options = persistentListOf( val options = persistentListOf(
Entry( Entry(
label = MR.strings.label_library, label = MR.strings.label_library,
getter = RestoreOptions::library, getter = RestoreOptions::libraryEntries,
setter = { options, enabled -> options.copy(library = enabled) }, setter = { options, enabled -> options.copy(libraryEntries = enabled) },
),
Entry(
label = MR.strings.categories,
getter = RestoreOptions::categories,
setter = { options, enabled -> options.copy(categories = enabled) },
), ),
Entry( Entry(
label = MR.strings.app_settings, label = MR.strings.app_settings,
@ -38,9 +45,10 @@ data class RestoreOptions(
) )
fun fromBooleanArray(array: BooleanArray) = RestoreOptions( fun fromBooleanArray(array: BooleanArray) = RestoreOptions(
library = array[0], libraryEntries = array[0],
appSettings = array[1], categories = array[1],
sourceSettings = array[2], appSettings = array[2],
sourceSettings = array[3],
) )
} }