Ver código fonte

Some more code cleanup

arkon 4 anos atrás
pai
commit
858664bfd7

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/data/backup/models/AbstractBackupManager.kt → app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt

@@ -1,4 +1,4 @@
-package eu.kanade.tachiyomi.data.backup.models
+package eu.kanade.tachiyomi.data.backup
 
 import android.content.Context
 import android.net.Uri

+ 1 - 2
app/src/main/java/eu/kanade/tachiyomi/data/backup/models/AbstractBackupRestore.kt → app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestore.kt

@@ -1,8 +1,7 @@
-package eu.kanade.tachiyomi.data.backup.models
+package eu.kanade.tachiyomi.data.backup
 
 import android.content.Context
 import android.net.Uri
-import eu.kanade.tachiyomi.data.backup.BackupNotifier
 import eu.kanade.tachiyomi.data.database.DatabaseHelper
 import eu.kanade.tachiyomi.data.track.TrackManager
 import kotlinx.coroutines.Job

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/data/backup/models/AbstractBackupRestoreValidator.kt → app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestoreValidator.kt

@@ -1,4 +1,4 @@
-package eu.kanade.tachiyomi.data.backup.models
+package eu.kanade.tachiyomi.data.backup
 
 import android.content.Context
 import android.net.Uri

+ 0 - 1
app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt

@@ -10,7 +10,6 @@ import androidx.core.content.ContextCompat
 import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.data.backup.full.FullBackupRestore
 import eu.kanade.tachiyomi.data.backup.legacy.LegacyBackupRestore
-import eu.kanade.tachiyomi.data.backup.models.AbstractBackupRestore
 import eu.kanade.tachiyomi.data.notification.Notifications
 import eu.kanade.tachiyomi.util.system.acquireWakeLock
 import eu.kanade.tachiyomi.util.system.isServiceRunning

+ 27 - 35
app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt

@@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.data.backup.full
 import android.content.Context
 import android.net.Uri
 import com.hippo.unifile.UniFile
+import eu.kanade.tachiyomi.data.backup.AbstractBackupManager
 import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CATEGORY
 import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CATEGORY_MASK
 import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CHAPTER
@@ -20,7 +21,6 @@ import eu.kanade.tachiyomi.data.backup.full.models.BackupManga
 import eu.kanade.tachiyomi.data.backup.full.models.BackupSerializer
 import eu.kanade.tachiyomi.data.backup.full.models.BackupSource
 import eu.kanade.tachiyomi.data.backup.full.models.BackupTracking
-import eu.kanade.tachiyomi.data.backup.models.AbstractBackupManager
 import eu.kanade.tachiyomi.data.database.models.Chapter
 import eu.kanade.tachiyomi.data.database.models.History
 import eu.kanade.tachiyomi.data.database.models.Manga
@@ -53,8 +53,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
         var backup: Backup? = null
 
         databaseHelper.inTransaction {
-            // Get manga from database
-            val databaseManga = getDatabaseManga()
+            val databaseManga = getFavoriteManga()
 
             backup = Backup(
                 backupManga(databaseManga, flags),
@@ -64,45 +63,38 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
         }
 
         try {
-            // When BackupCreatorJob
-            if (isJob) {
-                // Get dir of file and create
-                var dir = UniFile.fromUri(context, uri)
-                dir = dir.createDirectory("automatic")
-
-                // Delete older backups
-                val numberOfBackups = numberOfBackups()
-                val backupRegex = Regex("""tachiyomi_\d+-\d+-\d+_\d+-\d+.proto.gz""")
-                dir.listFiles { _, filename -> backupRegex.matches(filename) }
-                    .orEmpty()
-                    .sortedByDescending { it.name }
-                    .drop(numberOfBackups - 1)
-                    .forEach { it.delete() }
-
-                // Create new file to place backup
-                val newFile = dir.createFile(BackupFull.getDefaultFilename())
-                    ?: throw Exception("Couldn't create backup file")
-
-                val byteArray = parser.encodeToByteArray(BackupSerializer, backup!!)
-                newFile.openOutputStream().sink().gzip().buffer().use { it.write(byteArray) }
-
-                return newFile.uri.toString()
-            } else {
-                val file = UniFile.fromUri(context, uri)
-                    ?: throw Exception("Couldn't create backup file")
-                val byteArray = parser.encodeToByteArray(BackupSerializer, backup!!)
-                file.openOutputStream().sink().gzip().buffer().use { it.write(byteArray) }
+            val file: UniFile = (
+                if (isJob) {
+                    // Get dir of file and create
+                    var dir = UniFile.fromUri(context, uri)
+                    dir = dir.createDirectory("automatic")
+
+                    // Delete older backups
+                    val numberOfBackups = numberOfBackups()
+                    val backupRegex = Regex("""tachiyomi_\d+-\d+-\d+_\d+-\d+.proto.gz""")
+                    dir.listFiles { _, filename -> backupRegex.matches(filename) }
+                        .orEmpty()
+                        .sortedByDescending { it.name }
+                        .drop(numberOfBackups - 1)
+                        .forEach { it.delete() }
+
+                    // Create new file to place backup
+                    dir.createFile(BackupFull.getDefaultFilename())
+                } else {
+                    UniFile.fromUri(context, uri)
+                }
+                )
+                ?: throw Exception("Couldn't create backup file")
 
-                return file.uri.toString()
-            }
+            val byteArray = parser.encodeToByteArray(BackupSerializer, backup!!)
+            file.openOutputStream().sink().gzip().buffer().use { it.write(byteArray) }
+            return file.uri.toString()
         } catch (e: Exception) {
             Timber.e(e)
             throw e
         }
     }
 
-    private fun getDatabaseManga() = getFavoriteManga()
-
     private fun backupManga(mangas: List<Manga>, flags: Int): List<BackupManga> {
         return mangas.map {
             backupMangaObject(it, flags)

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestore.kt

@@ -3,12 +3,12 @@ package eu.kanade.tachiyomi.data.backup.full
 import android.content.Context
 import android.net.Uri
 import eu.kanade.tachiyomi.R
+import eu.kanade.tachiyomi.data.backup.AbstractBackupRestore
 import eu.kanade.tachiyomi.data.backup.BackupNotifier
 import eu.kanade.tachiyomi.data.backup.full.models.BackupCategory
 import eu.kanade.tachiyomi.data.backup.full.models.BackupHistory
 import eu.kanade.tachiyomi.data.backup.full.models.BackupManga
 import eu.kanade.tachiyomi.data.backup.full.models.BackupSerializer
-import eu.kanade.tachiyomi.data.backup.models.AbstractBackupRestore
 import eu.kanade.tachiyomi.data.database.models.Chapter
 import eu.kanade.tachiyomi.data.database.models.Manga
 import eu.kanade.tachiyomi.data.database.models.Track

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestoreValidator.kt

@@ -3,8 +3,8 @@ package eu.kanade.tachiyomi.data.backup.full
 import android.content.Context
 import android.net.Uri
 import eu.kanade.tachiyomi.R
+import eu.kanade.tachiyomi.data.backup.AbstractBackupRestoreValidator
 import eu.kanade.tachiyomi.data.backup.full.models.BackupSerializer
-import eu.kanade.tachiyomi.data.backup.models.AbstractBackupRestoreValidator
 import kotlinx.serialization.ExperimentalSerializationApi
 import okio.buffer
 import okio.gzip

+ 25 - 32
app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupManager.kt

@@ -12,6 +12,7 @@ import com.google.gson.JsonArray
 import com.google.gson.JsonElement
 import com.google.gson.JsonObject
 import com.hippo.unifile.UniFile
+import eu.kanade.tachiyomi.data.backup.AbstractBackupManager
 import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CATEGORY
 import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CATEGORY_MASK
 import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CHAPTER
@@ -34,7 +35,6 @@ import eu.kanade.tachiyomi.data.backup.legacy.serializer.ChapterTypeAdapter
 import eu.kanade.tachiyomi.data.backup.legacy.serializer.HistoryTypeAdapter
 import eu.kanade.tachiyomi.data.backup.legacy.serializer.MangaTypeAdapter
 import eu.kanade.tachiyomi.data.backup.legacy.serializer.TrackTypeAdapter
-import eu.kanade.tachiyomi.data.backup.models.AbstractBackupManager
 import eu.kanade.tachiyomi.data.database.models.CategoryImpl
 import eu.kanade.tachiyomi.data.database.models.Chapter
 import eu.kanade.tachiyomi.data.database.models.ChapterImpl
@@ -108,7 +108,6 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
         root[EXTENSIONS] = extensionEntries
 
         databaseHelper.inTransaction {
-            // Get manga from database
             val mangas = getFavoriteManga()
 
             val extensions: MutableSet<String> = mutableSetOf()
@@ -135,39 +134,33 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
         }
 
         try {
-            // When BackupCreatorJob
-            if (isJob) {
-                // Get dir of file and create
-                var dir = UniFile.fromUri(context, uri)
-                dir = dir.createDirectory("automatic")
-
-                // Delete older backups
-                val numberOfBackups = numberOfBackups()
-                val backupRegex = Regex("""tachiyomi_\d+-\d+-\d+_\d+-\d+.json""")
-                dir.listFiles { _, filename -> backupRegex.matches(filename) }
-                    .orEmpty()
-                    .sortedByDescending { it.name }
-                    .drop(numberOfBackups - 1)
-                    .forEach { it.delete() }
-
-                // Create new file to place backup
-                val newFile = dir.createFile(Backup.getDefaultFilename())
-                    ?: throw Exception("Couldn't create backup file")
-
-                newFile.openOutputStream().bufferedWriter().use {
-                    parser.toJson(root, it)
+            val file: UniFile = (
+                if (isJob) {
+                    // Get dir of file and create
+                    var dir = UniFile.fromUri(context, uri)
+                    dir = dir.createDirectory("automatic")
+
+                    // Delete older backups
+                    val numberOfBackups = numberOfBackups()
+                    val backupRegex = Regex("""tachiyomi_\d+-\d+-\d+_\d+-\d+.json""")
+                    dir.listFiles { _, filename -> backupRegex.matches(filename) }
+                        .orEmpty()
+                        .sortedByDescending { it.name }
+                        .drop(numberOfBackups - 1)
+                        .forEach { it.delete() }
+
+                    // Create new file to place backup
+                    dir.createFile(Backup.getDefaultFilename())
+                } else {
+                    UniFile.fromUri(context, uri)
                 }
+                )
+                ?: throw Exception("Couldn't create backup file")
 
-                return newFile.uri.toString()
-            } else {
-                val file = UniFile.fromUri(context, uri)
-                    ?: throw Exception("Couldn't create backup file")
-                file.openOutputStream().bufferedWriter().use {
-                    parser.toJson(root, it)
-                }
-
-                return file.uri.toString()
+            file.openOutputStream().bufferedWriter().use {
+                parser.toJson(root, it)
             }
+            return file.uri.toString()
         } catch (e: Exception) {
             Timber.e(e)
             throw e

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestore.kt

@@ -9,12 +9,12 @@ import com.google.gson.JsonObject
 import com.google.gson.JsonParser
 import com.google.gson.stream.JsonReader
 import eu.kanade.tachiyomi.R
+import eu.kanade.tachiyomi.data.backup.AbstractBackupRestore
 import eu.kanade.tachiyomi.data.backup.BackupConst
 import eu.kanade.tachiyomi.data.backup.BackupNotifier
 import eu.kanade.tachiyomi.data.backup.legacy.models.Backup
 import eu.kanade.tachiyomi.data.backup.legacy.models.Backup.MANGAS
 import eu.kanade.tachiyomi.data.backup.legacy.models.DHistory
-import eu.kanade.tachiyomi.data.backup.models.AbstractBackupRestore
 import eu.kanade.tachiyomi.data.database.models.Chapter
 import eu.kanade.tachiyomi.data.database.models.ChapterImpl
 import eu.kanade.tachiyomi.data.database.models.Manga

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestoreValidator.kt

@@ -6,8 +6,8 @@ import com.google.gson.JsonObject
 import com.google.gson.JsonParser
 import com.google.gson.stream.JsonReader
 import eu.kanade.tachiyomi.R
+import eu.kanade.tachiyomi.data.backup.AbstractBackupRestoreValidator
 import eu.kanade.tachiyomi.data.backup.legacy.models.Backup
-import eu.kanade.tachiyomi.data.backup.models.AbstractBackupRestoreValidator
 
 class LegacyBackupRestoreValidator : AbstractBackupRestoreValidator() {
     /**

+ 68 - 77
app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt

@@ -156,88 +156,73 @@ class SettingsBackupController : SettingsController() {
     }
 
     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
-        when (requestCode) {
-            CODE_BACKUP_DIR -> if (data != null && resultCode == Activity.RESULT_OK) {
-                val activity = activity ?: return
-                // Get uri of backup folder.
-                val uri = data.data
-
-                // Get UriPermission so it's possible to write files
-                val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
-                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION
-
-                if (uri != null) {
-                    activity.contentResolver.takePersistableUriPermission(uri, flags)
-                }
-
-                // Set backup Uri
-                preferences.backupsDirectory().set(uri.toString())
-            }
-            CODE_LEGACY_BACKUP_CREATE -> if (data != null && resultCode == Activity.RESULT_OK) {
-                val activity = activity ?: return
-
-                val uri = data.data
-                val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
-                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION
+        if (data != null && resultCode == Activity.RESULT_OK) {
+            val activity = activity ?: return
+            val uri = data.data
+
+            when (requestCode) {
+                CODE_BACKUP_DIR -> {
+                    // Get UriPermission so it's possible to write files
+                    val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
+                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION
+
+                    if (uri != null) {
+                        activity.contentResolver.takePersistableUriPermission(uri, flags)
+                    }
 
-                if (uri != null) {
-                    activity.contentResolver.takePersistableUriPermission(uri, flags)
+                    // Set backup Uri
+                    preferences.backupsDirectory().set(uri.toString())
                 }
+                CODE_FULL_BACKUP_CREATE, CODE_LEGACY_BACKUP_CREATE -> {
+                    val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
+                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION
 
-                val file = UniFile.fromUri(activity, uri)
-
-                activity.toast(R.string.creating_backup)
-
-                BackupCreateService.start(activity, file.uri, backupFlags, BackupConst.BACKUP_TYPE_LEGACY)
-            }
-            CODE_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) {
-                val uri = data.data
-                if (uri?.path != null) {
-                    if (uri.path!!.endsWith(".proto.gz")) {
-                        val options = arrayOf(
-                            R.string.full_restore_offline,
-                            R.string.full_restore_online
-                        )
-                            .map { activity!!.getString(it) }
-                        MaterialDialog(activity!!)
-                            .title(R.string.full_restore_mode)
-                            .listItemsSingleChoice(
-                                items = options,
-                                initialSelection = 0
-                            ) { _, index, _ ->
-                                RestoreBackupDialog(
-                                    uri,
-                                    BackupConst.BACKUP_TYPE_FULL,
-                                    isOnline = index != 0
-                                ).showDialog(router)
-                            }
-                            .positiveButton(R.string.action_restore)
-                            .show()
-                    } else if (uri.path!!.endsWith(".json")) {
-                        RestoreBackupDialog(
-                            uri,
-                            BackupConst.BACKUP_TYPE_LEGACY,
-                            isOnline = true
-                        ).showDialog(router)
+                    if (uri != null) {
+                        activity.contentResolver.takePersistableUriPermission(uri, flags)
                     }
-                }
-            }
-            CODE_FULL_BACKUP_CREATE -> if (data != null && resultCode == Activity.RESULT_OK) {
-                val activity = activity ?: return
 
-                val uri = data.data
-                val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
-                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION
+                    val file = UniFile.fromUri(activity, uri)
 
-                if (uri != null) {
-                    activity.contentResolver.takePersistableUriPermission(uri, flags)
-                }
-
-                val file = UniFile.fromUri(activity, uri)
-
-                activity.toast(R.string.creating_backup)
+                    activity.toast(R.string.creating_backup)
 
-                BackupCreateService.start(activity, file.uri, backupFlags, BackupConst.BACKUP_TYPE_FULL)
+                    BackupCreateService.start(
+                        activity,
+                        file.uri,
+                        backupFlags,
+                        if (requestCode == CODE_FULL_BACKUP_CREATE) BackupConst.BACKUP_TYPE_FULL else BackupConst.BACKUP_TYPE_LEGACY
+                    )
+                }
+                CODE_BACKUP_RESTORE -> {
+                    if (uri?.path != null) {
+                        if (uri.path!!.endsWith(".proto.gz")) {
+                            val options = arrayOf(
+                                R.string.full_restore_offline,
+                                R.string.full_restore_online
+                            )
+                                .map { activity.getString(it) }
+                            MaterialDialog(activity)
+                                .title(R.string.full_restore_mode)
+                                .listItemsSingleChoice(
+                                    items = options,
+                                    initialSelection = 0
+                                ) { _, index, _ ->
+                                    RestoreBackupDialog(
+                                        uri,
+                                        BackupConst.BACKUP_TYPE_FULL,
+                                        isOnline = index != 0
+                                    ).showDialog(router)
+                                }
+                                .positiveButton(R.string.action_restore)
+                                .show()
+                        } else if (uri.path!!.endsWith(".json")) {
+                            RestoreBackupDialog(
+                                uri,
+                                BackupConst.BACKUP_TYPE_LEGACY,
+                                isOnline = true
+                            ).showDialog(router)
+                        }
+                    }
+                }
             }
         }
     }
@@ -268,10 +253,16 @@ class SettingsBackupController : SettingsController() {
     fun createBackup(flags: Int, type: Int) {
         backupFlags = flags
         val currentDir = preferences.backupsDirectory().get()
-        val code = if (type == BackupConst.BACKUP_TYPE_FULL) CODE_FULL_BACKUP_CREATE else CODE_LEGACY_BACKUP_CREATE
+        val code = when (type) {
+            BackupConst.BACKUP_TYPE_FULL -> CODE_FULL_BACKUP_CREATE
+            else -> CODE_LEGACY_BACKUP_CREATE
+        }
+        val fileName = when (type) {
+            BackupConst.BACKUP_TYPE_FULL -> BackupFull.getDefaultFilename()
+            else -> Backup.getDefaultFilename()
+        }
 
         try {
-            val fileName = if (type == BackupConst.BACKUP_TYPE_FULL) BackupFull.getDefaultFilename() else Backup.getDefaultFilename()
             // Use Android's built-in file creator
             val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
                 .addCategory(Intent.CATEGORY_OPENABLE)