feat: 优化代码

pull/120/head
kunfei 5 years ago
parent 6b73533992
commit c1bcea6964
  1. 37
      app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt

@ -49,21 +49,30 @@ object BackupRestoreUi {
selectBackupFolder(fragment, backupSelectRequestCode) selectBackupFolder(fragment, backupSelectRequestCode)
} }
} else { } else {
backupUsePermission(fragment) backupUsePermission(fragment, requestCode = backupSelectRequestCode)
} }
} }
} }
private fun backupUsePermission(fragment: Fragment, path: String = Backup.legadoPath) { private fun backupUsePermission(
fragment: Fragment,
path: String = Backup.legadoPath,
requestCode: Int = selectFolderRequestCode
) {
PermissionsCompat.Builder(fragment) PermissionsCompat.Builder(fragment)
.addPermissions(*Permissions.Group.STORAGE) .addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage) .rationale(R.string.tip_perm_request_storage)
.onGranted { .onGranted {
Coroutine.async { when (requestCode) {
AppConfig.backupPath = Backup.legadoPath selectFolderRequestCode -> AppConfig.backupPath = Backup.legadoPath
Backup.backup(fragment.requireContext(), path) else -> {
}.onSuccess { Coroutine.async {
fragment.toast(R.string.backup_success) AppConfig.backupPath = Backup.legadoPath
Backup.backup(fragment.requireContext(), path)
}.onSuccess {
fragment.toast(R.string.backup_success)
}
}
} }
} }
.request() .request()
@ -74,7 +83,7 @@ object BackupRestoreUi {
titleResource = R.string.select_folder titleResource = R.string.select_folder
items(fragment.resources.getStringArray(R.array.select_folder).toList()) { _, index -> items(fragment.resources.getStringArray(R.array.select_folder).toList()) { _, index ->
when (index) { when (index) {
0 -> backupUsePermission(fragment) 0 -> backupUsePermission(fragment, requestCode = requestCode)
1 -> { 1 -> {
try { try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
@ -231,6 +240,9 @@ object BackupRestoreUi {
App.INSTANCE.toast(R.string.restore_success) App.INSTANCE.toast(R.string.restore_success)
} }
} }
selectFolderRequestCode -> {
AppConfig.backupPath = currentPath
}
} }
} }
@ -240,7 +252,8 @@ object BackupRestoreUi {
data?.data?.let { uri -> data?.data?.let { uri ->
App.INSTANCE.contentResolver.takePersistableUriPermission( App.INSTANCE.contentResolver.takePersistableUriPermission(
uri, uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
AppConfig.backupPath = uri.toString() AppConfig.backupPath = uri.toString()
Coroutine.async { Coroutine.async {
@ -254,7 +267,8 @@ object BackupRestoreUi {
data?.data?.let { uri -> data?.data?.let { uri ->
App.INSTANCE.contentResolver.takePersistableUriPermission( App.INSTANCE.contentResolver.takePersistableUriPermission(
uri, uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
AppConfig.backupPath = uri.toString() AppConfig.backupPath = uri.toString()
Coroutine.async { Coroutine.async {
@ -268,7 +282,8 @@ object BackupRestoreUi {
data?.data?.let { uri -> data?.data?.let { uri ->
App.INSTANCE.contentResolver.takePersistableUriPermission( App.INSTANCE.contentResolver.takePersistableUriPermission(
uri, uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
AppConfig.backupPath = uri.toString() AppConfig.backupPath = uri.toString()
} }

Loading…
Cancel
Save