Skip to content

Commit

Permalink
refactor(settings/data): Simplify backup code
Browse files Browse the repository at this point in the history
* Remove picker argument, it's permanently false now
* Simplify backup file path retrieval
* Remove unused code
  • Loading branch information
null2264 committed Sep 20, 2024
1 parent 5d457a7 commit 58c5a17
Showing 1 changed file with 12 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,6 @@ class SettingsDataLegacyController : SettingsLegacyController() {
storagePreferences.baseStorageDirectory().set(file.uri.toString())
}

CODE_BACKUP_CREATE -> {
doBackup(backupFlags, uri, true)
}

CODE_BACKUP_RESTORE -> {
(activity as? MainActivity)?.showNotificationPermissionPrompt(true)
showBackupRestoreDialog(uri)
Expand All @@ -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 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

val dir = storageManager.getBackupsDirectory()
if (dir == null) {
val uri = storageManager.getBackupsDirectory()?.createFile(Backup.getBackupFilename())?.uri
if (uri == null) {
activity?.toast(MR.strings.invalid_location_generic)
return
}

if (!picker) {
doBackup(backupFlags, dir.uri)
return
}
/* Might not need it
if (requirePersist) {
val intentFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
try {
// 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.tryTakePersistableUriPermission(uri, intentFlags)
}
*/

activity.toast(MR.strings.creating_backup)
BackupCreatorJob.startNow(activity, uri, options)
}

private fun showBackupCreateDialog() {
Expand Down Expand Up @@ -406,7 +382,6 @@ class SettingsDataLegacyController : SettingsLegacyController() {
private const val CLEAR_CACHE_KEY = "pref_clear_cache_key"

private const val CODE_DATA_DIR = 104
private const val CODE_BACKUP_CREATE = 504
private const val CODE_BACKUP_RESTORE = 505

private const val HELP_URL = "https://tachiyomi.org/docs/guides/backups"

0 comments on commit 58c5a17

Please sign in to comment.