mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-21 20:47:03 -05:00
Cleanup backup/restore related code
This commit is contained in:
parent
56fb4f62a1
commit
c201b341a7
9 changed files with 32 additions and 34 deletions
|
@ -51,24 +51,22 @@ class BackupCreator(
|
|||
suspend fun backup(uri: Uri, options: BackupOptions): String {
|
||||
var file: UniFile? = null
|
||||
try {
|
||||
file = (
|
||||
if (isAutoBackup) {
|
||||
// Get dir of file and create
|
||||
val dir = UniFile.fromUri(context, uri)
|
||||
file = if (isAutoBackup) {
|
||||
// Get dir of file and create
|
||||
val dir = UniFile.fromUri(context, uri)
|
||||
|
||||
// Delete older backups
|
||||
dir?.listFiles { _, filename -> FILENAME_REGEX.matches(filename) }
|
||||
.orEmpty()
|
||||
.sortedByDescending { it.name }
|
||||
.drop(MAX_AUTO_BACKUPS - 1)
|
||||
.forEach { it.delete() }
|
||||
// Delete older backups
|
||||
dir?.listFiles { _, filename -> FILENAME_REGEX.matches(filename) }
|
||||
.orEmpty()
|
||||
.sortedByDescending { it.name }
|
||||
.drop(MAX_AUTO_BACKUPS - 1)
|
||||
.forEach { it.delete() }
|
||||
|
||||
// Create new file to place backup
|
||||
dir?.createFile(getFilename())
|
||||
} else {
|
||||
UniFile.fromUri(context, uri)
|
||||
}
|
||||
)
|
||||
// Create new file to place backup
|
||||
dir?.createFile(getFilename())
|
||||
} else {
|
||||
UniFile.fromUri(context, uri)
|
||||
}
|
||||
|
||||
if (file == null || !file.isFile) {
|
||||
throw IllegalStateException(context.stringResource(MR.strings.create_backup_file_error))
|
||||
|
@ -116,29 +114,29 @@ class BackupCreator(
|
|||
private suspend fun backupCategories(options: BackupOptions): List<BackupCategory> {
|
||||
if (!options.categories) return emptyList()
|
||||
|
||||
return categoriesBackupCreator.backupCategories()
|
||||
return categoriesBackupCreator()
|
||||
}
|
||||
|
||||
private suspend fun backupMangas(mangas: List<Manga>, options: BackupOptions): List<BackupManga> {
|
||||
if (!options.libraryEntries) return emptyList()
|
||||
|
||||
return mangaBackupCreator.backupMangas(mangas, options)
|
||||
return mangaBackupCreator(mangas, options)
|
||||
}
|
||||
|
||||
private fun backupSources(mangas: List<BackupManga>): List<BackupSource> {
|
||||
return sourcesBackupCreator.backupSources(mangas)
|
||||
return sourcesBackupCreator(mangas)
|
||||
}
|
||||
|
||||
private fun backupAppPreferences(options: BackupOptions): List<BackupPreference> {
|
||||
if (!options.appSettings) return emptyList()
|
||||
|
||||
return preferenceBackupCreator.backupAppPreferences(includePrivatePreferences = options.privateSettings)
|
||||
return preferenceBackupCreator.createApp(includePrivatePreferences = options.privateSettings)
|
||||
}
|
||||
|
||||
private fun backupSourcePreferences(options: BackupOptions): List<BackupSourcePreferences> {
|
||||
if (!options.sourceSettings) return emptyList()
|
||||
|
||||
return preferenceBackupCreator.backupSourcePreferences(includePrivatePreferences = options.privateSettings)
|
||||
return preferenceBackupCreator.createSource(includePrivatePreferences = options.privateSettings)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
|
|
@ -11,7 +11,7 @@ class CategoriesBackupCreator(
|
|||
private val getCategories: GetCategories = Injekt.get(),
|
||||
) {
|
||||
|
||||
suspend fun backupCategories(): List<BackupCategory> {
|
||||
suspend operator fun invoke(): List<BackupCategory> {
|
||||
return getCategories.await()
|
||||
.filterNot(Category::isSystemCategory)
|
||||
.map(backupCategoryMapper)
|
||||
|
|
|
@ -20,7 +20,7 @@ class MangaBackupCreator(
|
|||
private val getHistory: GetHistory = Injekt.get(),
|
||||
) {
|
||||
|
||||
suspend fun backupMangas(mangas: List<Manga>, options: BackupOptions): List<BackupManga> {
|
||||
suspend operator fun invoke(mangas: List<Manga>, options: BackupOptions): List<BackupManga> {
|
||||
return mangas.map {
|
||||
backupManga(it, options)
|
||||
}
|
||||
|
|
|
@ -22,12 +22,12 @@ class PreferenceBackupCreator(
|
|||
private val preferenceStore: PreferenceStore = Injekt.get(),
|
||||
) {
|
||||
|
||||
fun backupAppPreferences(includePrivatePreferences: Boolean): List<BackupPreference> {
|
||||
fun createApp(includePrivatePreferences: Boolean): List<BackupPreference> {
|
||||
return preferenceStore.getAll().toBackupPreferences()
|
||||
.withPrivatePreferences(includePrivatePreferences)
|
||||
}
|
||||
|
||||
fun backupSourcePreferences(includePrivatePreferences: Boolean): List<BackupSourcePreferences> {
|
||||
fun createSource(includePrivatePreferences: Boolean): List<BackupSourcePreferences> {
|
||||
return sourceManager.getCatalogueSources()
|
||||
.filterIsInstance<ConfigurableSource>()
|
||||
.map {
|
||||
|
|
|
@ -11,7 +11,7 @@ class SourcesBackupCreator(
|
|||
private val sourceManager: SourceManager = Injekt.get(),
|
||||
) {
|
||||
|
||||
fun backupSources(mangas: List<BackupManga>): List<BackupSource> {
|
||||
operator fun invoke(mangas: List<BackupManga>): List<BackupSource> {
|
||||
return mangas
|
||||
.asSequence()
|
||||
.map(BackupManga::source)
|
||||
|
|
|
@ -100,7 +100,7 @@ class BackupRestorer(
|
|||
|
||||
private fun CoroutineScope.restoreCategories(backupCategories: List<BackupCategory>) = launch {
|
||||
ensureActive()
|
||||
categoriesRestorer.restoreCategories(backupCategories)
|
||||
categoriesRestorer(backupCategories)
|
||||
|
||||
restoreProgress += 1
|
||||
notifier.showRestoreProgress(
|
||||
|
@ -120,7 +120,7 @@ class BackupRestorer(
|
|||
ensureActive()
|
||||
|
||||
try {
|
||||
mangaRestorer.restoreManga(it, backupCategories)
|
||||
mangaRestorer.restore(it, backupCategories)
|
||||
} catch (e: Exception) {
|
||||
val sourceName = sourceMapping[it.source] ?: it.source.toString()
|
||||
errors.add(Date() to "${it.title} [$sourceName]: ${e.message}")
|
||||
|
@ -133,7 +133,7 @@ class BackupRestorer(
|
|||
|
||||
private fun CoroutineScope.restoreAppPreferences(preferences: List<BackupPreference>) = launch {
|
||||
ensureActive()
|
||||
preferenceRestorer.restoreAppPreferences(preferences)
|
||||
preferenceRestorer.restoreApp(preferences)
|
||||
|
||||
restoreProgress += 1
|
||||
notifier.showRestoreProgress(
|
||||
|
@ -146,7 +146,7 @@ class BackupRestorer(
|
|||
|
||||
private fun CoroutineScope.restoreSourcePreferences(preferences: List<BackupSourcePreferences>) = launch {
|
||||
ensureActive()
|
||||
preferenceRestorer.restoreSourcePreferences(preferences)
|
||||
preferenceRestorer.restoreSource(preferences)
|
||||
|
||||
restoreProgress += 1
|
||||
notifier.showRestoreProgress(
|
||||
|
|
|
@ -13,7 +13,7 @@ class CategoriesRestorer(
|
|||
private val libraryPreferences: LibraryPreferences = Injekt.get(),
|
||||
) {
|
||||
|
||||
suspend fun restoreCategories(backupCategories: List<BackupCategory>) {
|
||||
suspend operator fun invoke(backupCategories: List<BackupCategory>) {
|
||||
if (backupCategories.isNotEmpty()) {
|
||||
val dbCategories = getCategories.await()
|
||||
val dbCategoriesByName = dbCategories.associateBy { it.name }
|
||||
|
|
|
@ -53,7 +53,7 @@ class MangaRestorer(
|
|||
)
|
||||
}
|
||||
|
||||
suspend fun restoreManga(
|
||||
suspend fun restore(
|
||||
backupManga: BackupManga,
|
||||
backupCategories: List<BackupCategory>,
|
||||
) {
|
||||
|
|
|
@ -22,14 +22,14 @@ class PreferenceRestorer(
|
|||
private val preferenceStore: PreferenceStore = Injekt.get(),
|
||||
) {
|
||||
|
||||
fun restoreAppPreferences(preferences: List<BackupPreference>) {
|
||||
fun restoreApp(preferences: List<BackupPreference>) {
|
||||
restorePreferences(preferences, preferenceStore)
|
||||
|
||||
LibraryUpdateJob.setupTask(context)
|
||||
BackupCreateJob.setupTask(context)
|
||||
}
|
||||
|
||||
fun restoreSourcePreferences(preferences: List<BackupSourcePreferences>) {
|
||||
fun restoreSource(preferences: List<BackupSourcePreferences>) {
|
||||
preferences.forEach {
|
||||
val sourcePrefs = AndroidPreferenceStore(context, sourcePreferences(it.sourceKey))
|
||||
restorePreferences(it.prefs, sourcePrefs)
|
||||
|
|
Loading…
Reference in a new issue