Merge restore entry points, avoid unnecessary new strings

This commit is contained in:
arkon 2020-11-21 12:28:25 -05:00
parent 47a172df1f
commit eceac4d6e3
4 changed files with 58 additions and 89 deletions

View file

@ -39,9 +39,7 @@ import kotlin.math.max
@OptIn(ExperimentalSerializationApi::class) @OptIn(ExperimentalSerializationApi::class)
class FullBackupManager(context: Context) : AbstractBackupManager(context) { class FullBackupManager(context: Context) : AbstractBackupManager(context) {
/**
* Parser
*/
val parser = ProtoBuf val parser = ProtoBuf
/** /**

View file

@ -58,9 +58,6 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
var version: Int = version var version: Int = version
private set private set
/**
* Json Parser
*/
var parser: Gson = initParser() var parser: Gson = initParser()
/** /**

View file

@ -63,44 +63,25 @@ class SettingsBackupController : SettingsController() {
titleRes = R.string.backup titleRes = R.string.backup
preference { preference {
key = "pref_create_full_backup" key = "pref_create_backup"
titleRes = R.string.pref_create_full_backup
summaryRes = R.string.pref_create_full_backup_summary
onClick {
backupClick(context, BackupConst.BACKUP_TYPE_FULL)
}
}
preference {
key = "pref_restore_full_backup"
titleRes = R.string.pref_restore_full_backup
summaryRes = R.string.pref_restore_full_backup_summary
onClick {
restoreClick(context, CODE_FULL_BACKUP_RESTORE)
}
}
}
preferenceCategory {
titleRes = R.string.legacy_backup
preference {
key = "pref_create_legacy_backup"
titleRes = R.string.pref_create_backup titleRes = R.string.pref_create_backup
summaryRes = R.string.pref_create_backup_summ summaryRes = R.string.pref_create_backup_summ
onClick { onClick { backup(context, BackupConst.BACKUP_TYPE_FULL) }
backupClick(context, BackupConst.BACKUP_TYPE_LEGACY)
}
} }
preference { preference {
key = "pref_restore_legacy_backup" key = "pref_create_legacy_backup"
titleRes = R.string.pref_create_legacy_backup
summaryRes = R.string.pref_create_legacy_backup_summary
onClick { backup(context, BackupConst.BACKUP_TYPE_LEGACY) }
}
preference {
key = "pref_restore_backup"
titleRes = R.string.pref_restore_backup titleRes = R.string.pref_restore_backup
summaryRes = R.string.pref_restore_backup_summ summaryRes = R.string.pref_restore_backup_summ
onClick { onClick { restore(context) }
restoreClick(context, CODE_LEGACY_BACKUP_RESTORE)
}
} }
} }
preferenceCategory { preferenceCategory {
@ -166,7 +147,6 @@ class SettingsBackupController : SettingsController() {
switchPreference { switchPreference {
key = Keys.createLegacyBackup key = Keys.createLegacyBackup
titleRes = R.string.pref_backup_auto_create_legacy titleRes = R.string.pref_backup_auto_create_legacy
summaryRes = R.string.pref_backup_auto_create_legacy_summary
defaultValue = false defaultValue = false
preferences.backupInterval().asImmediateFlow { isVisible = it > 0 } preferences.backupInterval().asImmediateFlow { isVisible = it > 0 }
@ -210,10 +190,36 @@ class SettingsBackupController : SettingsController() {
BackupCreateService.start(activity, file.uri, backupFlags, BackupConst.BACKUP_TYPE_LEGACY) BackupCreateService.start(activity, file.uri, backupFlags, BackupConst.BACKUP_TYPE_LEGACY)
} }
CODE_LEGACY_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) { CODE_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) {
val uri = data.data val uri = data.data
if (uri != null) { if (uri?.path != null) {
RestoreBackupDialog(uri, BackupConst.BACKUP_TYPE_LEGACY, isOnline = true).showDialog(router) 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)
}
} }
} }
CODE_FULL_BACKUP_CREATE -> if (data != null && resultCode == Activity.RESULT_OK) { CODE_FULL_BACKUP_CREATE -> if (data != null && resultCode == Activity.RESULT_OK) {
@ -233,34 +239,10 @@ class SettingsBackupController : SettingsController() {
BackupCreateService.start(activity, file.uri, backupFlags, BackupConst.BACKUP_TYPE_FULL) BackupCreateService.start(activity, file.uri, backupFlags, BackupConst.BACKUP_TYPE_FULL)
} }
CODE_FULL_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) {
val uri = data.data
if (uri != null) {
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()
}
}
} }
} }
private fun backupClick(context: Context, type: Int) { private fun backup(context: Context, type: Int) {
if (!BackupCreateService.isRunning(context)) { if (!BackupCreateService.isRunning(context)) {
val ctrl = CreateBackupDialog(type) val ctrl = CreateBackupDialog(type)
ctrl.targetController = this@SettingsBackupController ctrl.targetController = this@SettingsBackupController
@ -270,14 +252,14 @@ class SettingsBackupController : SettingsController() {
} }
} }
private fun restoreClick(context: Context, type: Int) { private fun restore(context: Context) {
if (!BackupRestoreService.isRunning(context)) { if (!BackupRestoreService.isRunning(context)) {
val intent = Intent(Intent.ACTION_GET_CONTENT) val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE) intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "application/*" intent.type = "application/*"
val title = resources?.getString(R.string.file_select_backup) val title = resources?.getString(R.string.file_select_backup)
val chooser = Intent.createChooser(intent, title) val chooser = Intent.createChooser(intent, title)
startActivityForResult(chooser, type) startActivityForResult(chooser, CODE_BACKUP_RESTORE)
} else { } else {
context.toast(R.string.restore_in_progress) context.toast(R.string.restore_in_progress)
} }
@ -285,9 +267,8 @@ class SettingsBackupController : SettingsController() {
fun createBackup(flags: Int, type: Int) { fun createBackup(flags: Int, type: Int) {
backupFlags = flags backupFlags = flags
// Get dirs
val currentDir = preferences.backupsDirectory().get() val currentDir = preferences.backupsDirectory().get()
val code = if (type == BackupConst.BACKUP_TYPE_FULL) CODE_FULL_BACKUP_CREATE else CODE_LEGACY_BACKUP_CREATE
try { try {
val fileName = if (type == BackupConst.BACKUP_TYPE_FULL) BackupFull.getDefaultFilename() else Backup.getDefaultFilename() val fileName = if (type == BackupConst.BACKUP_TYPE_FULL) BackupFull.getDefaultFilename() else Backup.getDefaultFilename()
@ -297,10 +278,10 @@ class SettingsBackupController : SettingsController() {
.setType("application/*") .setType("application/*")
.putExtra(Intent.EXTRA_TITLE, fileName) .putExtra(Intent.EXTRA_TITLE, fileName)
startActivityForResult(intent, if (type == BackupConst.BACKUP_TYPE_FULL) CODE_FULL_BACKUP_CREATE else CODE_LEGACY_BACKUP_CREATE) startActivityForResult(intent, code)
} catch (e: ActivityNotFoundException) { } catch (e: ActivityNotFoundException) {
// Handle errors where the android ROM doesn't support the built in picker // Handle errors where the Android ROM doesn't support the built in picker
startActivityForResult(preferences.context.getFilePicker(currentDir), if (type == BackupConst.BACKUP_TYPE_FULL) CODE_FULL_BACKUP_CREATE else CODE_LEGACY_BACKUP_CREATE) startActivityForResult(preferences.context.getFilePicker(currentDir), code)
} }
} }
@ -324,7 +305,7 @@ class SettingsBackupController : SettingsController() {
.map { activity.getString(it) } .map { activity.getString(it) }
return MaterialDialog(activity) return MaterialDialog(activity)
.title(R.string.create_backup) .title(R.string.pref_create_backup)
.message(R.string.backup_choice) .message(R.string.backup_choice)
.listItemsMultiChoice( .listItemsMultiChoice(
items = options, items = options,
@ -381,7 +362,7 @@ class SettingsBackupController : SettingsController() {
} }
MaterialDialog(activity) MaterialDialog(activity)
.title(R.string.restore_backup) .title(R.string.pref_restore_backup)
.message(text = message) .message(text = message)
.positiveButton(R.string.action_restore) { .positiveButton(R.string.action_restore) {
BackupRestoreService.start(activity, uri, type, isOnline) BackupRestoreService.start(activity, uri, type, isOnline)
@ -403,9 +384,8 @@ class SettingsBackupController : SettingsController() {
private companion object { private companion object {
const val CODE_LEGACY_BACKUP_CREATE = 501 const val CODE_LEGACY_BACKUP_CREATE = 501
const val CODE_LEGACY_BACKUP_RESTORE = 502
const val CODE_BACKUP_DIR = 503 const val CODE_BACKUP_DIR = 503
const val CODE_FULL_BACKUP_CREATE = 504 const val CODE_FULL_BACKUP_CREATE = 504
const val CODE_FULL_BACKUP_RESTORE = 505 const val CODE_BACKUP_RESTORE = 505
} }
} }

View file

@ -347,17 +347,13 @@
<!-- Backup section --> <!-- Backup section -->
<string name="backup">Backup</string> <string name="backup">Backup</string>
<string name="legacy_backup">Legacy Backup</string> <string name="pref_create_backup">Create backup</string>
<string name="pref_create_full_backup">Create full backup</string> <string name="pref_create_backup_summ">Can be used to restore current library</string>
<string name="pref_create_full_backup_summary">Can be used to restore current library</string> <string name="pref_restore_backup">Restore backup</string>
<string name="pref_restore_full_backup">Restore full backup</string> <string name="pref_restore_backup_summ">Restore library from backup file</string>
<string name="pref_restore_full_backup_summary">Restore library from backup file, only use this if your backup is a full type backup, this can be restored offline as well as online</string> <string name="pref_create_legacy_backup">Create legacy backup</string>
<string name="pref_create_backup">Create legacy backup</string> <string name="pref_create_legacy_backup_summary">Can be used in older versions of Tachiyomi</string>
<string name="pref_create_backup_summ">Can be used to restore current library in older versions of Tachiyomi</string> <string name="pref_backup_auto_create_legacy">Also create legacy backup</string>
<string name="pref_restore_backup">Restore legacy backup</string>
<string name="pref_restore_backup_summ">Restore library from a legacy backup file</string>
<string name="pref_backup_auto_create_legacy">Create legacy backup</string>
<string name="pref_backup_auto_create_legacy_summary">Creates a legacy backup alongside the full backup</string>
<string name="pref_backup_directory">Backup location</string> <string name="pref_backup_directory">Backup location</string>
<string name="pref_backup_service_category">Automatic backups</string> <string name="pref_backup_service_category">Automatic backups</string>
<string name="pref_backup_interval">Backup frequency</string> <string name="pref_backup_interval">Backup frequency</string>
@ -365,8 +361,6 @@
<string name="full_restore_mode">Network Mode</string> <string name="full_restore_mode">Network Mode</string>
<string name="full_restore_online">Restore online, much slower but gives you more updated info and chapters</string> <string name="full_restore_online">Restore online, much slower but gives you more updated info and chapters</string>
<string name="full_restore_offline">Restore offline, finishes quickly but contains only what your backup has</string> <string name="full_restore_offline">Restore offline, finishes quickly but contains only what your backup has</string>
<string name="create_backup">Create backup</string>
<string name="restore_backup">Restore backup</string>
<string name="source_not_found_name">Source not found: %1$s</string> <string name="source_not_found_name">Source not found: %1$s</string>
<string name="tracker_not_logged_in">Not logged in: %1$s</string> <string name="tracker_not_logged_in">Not logged in: %1$s</string>
<string name="backup_created">Backup created</string> <string name="backup_created">Backup created</string>