Some more code cleanup

This commit is contained in:
arkon 2020-11-21 15:09:56 -05:00
parent eceac4d6e3
commit 858664bfd7
11 changed files with 121 additions and 147 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,7 +63,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
}
try {
// When BackupCreatorJob
val file: UniFile = (
if (isJob) {
// Get dir of file and create
var dir = UniFile.fromUri(context, uri)
@ -80,29 +79,22 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
.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()
dir.createFile(BackupFull.getDefaultFilename())
} else {
val file = UniFile.fromUri(context, uri)
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) }
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)

View File

@ -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

View File

@ -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

View File

@ -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,7 +134,7 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
}
try {
// When BackupCreatorJob
val file: UniFile = (
if (isJob) {
// Get dir of file and create
var dir = UniFile.fromUri(context, uri)
@ -151,23 +150,17 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
.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)
}
return newFile.uri.toString()
dir.createFile(Backup.getDefaultFilename())
} else {
val file = UniFile.fromUri(context, uri)
UniFile.fromUri(context, uri)
}
)
?: throw Exception("Couldn't create backup file")
file.openOutputStream().bufferedWriter().use {
parser.toJson(root, it)
}
return file.uri.toString()
}
} catch (e: Exception) {
Timber.e(e)
throw e

View File

@ -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

View File

@ -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() {
/**

View File

@ -156,12 +156,12 @@ class SettingsBackupController : SettingsController() {
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
CODE_BACKUP_DIR -> if (data != null && resultCode == Activity.RESULT_OK) {
if (data != null && resultCode == Activity.RESULT_OK) {
val activity = activity ?: return
// Get uri of backup folder.
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
@ -173,10 +173,7 @@ class SettingsBackupController : SettingsController() {
// 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
CODE_FULL_BACKUP_CREATE, CODE_LEGACY_BACKUP_CREATE -> {
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
@ -188,18 +185,22 @@ class SettingsBackupController : SettingsController() {
activity.toast(R.string.creating_backup)
BackupCreateService.start(activity, file.uri, backupFlags, BackupConst.BACKUP_TYPE_LEGACY)
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 (data != null && resultCode == Activity.RESULT_OK) {
val uri = data.data
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!!)
.map { activity.getString(it) }
MaterialDialog(activity)
.title(R.string.full_restore_mode)
.listItemsSingleChoice(
items = options,
@ -222,22 +223,6 @@ class SettingsBackupController : SettingsController() {
}
}
}
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
if (uri != null) {
activity.contentResolver.takePersistableUriPermission(uri, flags)
}
val file = UniFile.fromUri(activity, uri)
activity.toast(R.string.creating_backup)
BackupCreateService.start(activity, file.uri, backupFlags, BackupConst.BACKUP_TYPE_FULL)
}
}
}
@ -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)