refactor(settings/data): Simplify backup code

* Remove picker argument, it's permanently false now
* Simplify backup file path retrieval
* Remove unused code
This commit is contained in:
Ahmad Ansori Palembani 2024-09-20 08:22:09 +07:00
parent 5d457a7ae5
commit 58c5a17c50
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -251,10 +251,6 @@ class SettingsDataLegacyController : SettingsLegacyController() {
storagePreferences.baseStorageDirectory().set(file.uri.toString()) storagePreferences.baseStorageDirectory().set(file.uri.toString())
} }
CODE_BACKUP_CREATE -> {
doBackup(backupFlags, uri, true)
}
CODE_BACKUP_RESTORE -> { CODE_BACKUP_RESTORE -> {
(activity as? MainActivity)?.showNotificationPermissionPrompt(true) (activity as? MainActivity)?.showNotificationPermissionPrompt(true)
showBackupRestoreDialog(uri) showBackupRestoreDialog(uri)
@ -263,48 +259,28 @@ class SettingsDataLegacyController : SettingsLegacyController() {
} }
} }
private fun doBackup(options: BackupOptions, uri: Uri, requirePersist: Boolean = false) { private fun createBackup(options: BackupOptions) {
val activity = activity ?: return val activity = activity ?: return
val actualUri =
if (requirePersist) {
val intentFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
activity.tryTakePersistableUriPermission(uri, intentFlags)
uri
} else {
UniFile.fromUri(activity, uri)?.createFile(Backup.getBackupFilename())?.uri
} ?: return
activity.toast(MR.strings.creating_backup)
BackupCreatorJob.startNow(activity, actualUri, options)
}
private fun createBackup(options: BackupOptions, picker: Boolean = false) {
backupFlags = options backupFlags = options
val dir = storageManager.getBackupsDirectory() val uri = storageManager.getBackupsDirectory()?.createFile(Backup.getBackupFilename())?.uri
if (dir == null) { if (uri == null) {
activity?.toast(MR.strings.invalid_location_generic) activity?.toast(MR.strings.invalid_location_generic)
return return
} }
if (!picker) { /* Might not need it
doBackup(backupFlags, dir.uri) if (requirePersist) {
return val intentFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
} Intent.FLAG_GRANT_WRITE_URI_PERMISSION
try { activity.tryTakePersistableUriPermission(uri, intentFlags)
// Use Android's built-in file creator
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("application/*")
.putExtra(Intent.EXTRA_TITLE, Backup.getBackupFilename())
startActivityForResult(intent, CODE_BACKUP_CREATE)
} catch (e: ActivityNotFoundException) {
activity?.toast(MR.strings.file_picker_error)
} }
*/
activity.toast(MR.strings.creating_backup)
BackupCreatorJob.startNow(activity, uri, options)
} }
private fun showBackupCreateDialog() { private fun showBackupCreateDialog() {
@ -406,7 +382,6 @@ class SettingsDataLegacyController : SettingsLegacyController() {
private const val CLEAR_CACHE_KEY = "pref_clear_cache_key" private const val CLEAR_CACHE_KEY = "pref_clear_cache_key"
private const val CODE_DATA_DIR = 104 private const val CODE_DATA_DIR = 104
private const val CODE_BACKUP_CREATE = 504
private const val CODE_BACKUP_RESTORE = 505 private const val CODE_BACKUP_RESTORE = 505
private const val HELP_URL = "https://tachiyomi.org/docs/guides/backups" private const val HELP_URL = "https://tachiyomi.org/docs/guides/backups"