feat: 优化代码

pull/120/head
kunfei 5 years ago
parent d8c1721a8d
commit 310d89d23a
  1. 21
      app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt

@ -26,7 +26,7 @@ import kotlinx.coroutines.Dispatchers.Main
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
object BackupRestoreUi { object BackupRestoreUi {
private const val selectFolderRequestCode = 21
private const val backupSelectRequestCode = 22 private const val backupSelectRequestCode = 22
private const val restoreSelectRequestCode = 33 private const val restoreSelectRequestCode = 33
private const val oldDataRequestCode = 11 private const val oldDataRequestCode = 11
@ -34,7 +34,7 @@ object BackupRestoreUi {
fun backup(fragment: Fragment) { fun backup(fragment: Fragment) {
val backupPath = AppConfig.backupPath val backupPath = AppConfig.backupPath
if (backupPath.isNullOrEmpty()) { if (backupPath.isNullOrEmpty()) {
selectBackupFolder(fragment) selectBackupFolder(fragment, backupSelectRequestCode)
} else { } else {
if (backupPath.isContentPath()) { if (backupPath.isContentPath()) {
val uri = Uri.parse(backupPath) val uri = Uri.parse(backupPath)
@ -46,7 +46,7 @@ object BackupRestoreUi {
fragment.toast(R.string.backup_success) fragment.toast(R.string.backup_success)
} }
} else { } else {
selectBackupFolder(fragment) selectBackupFolder(fragment, backupSelectRequestCode)
} }
} else { } else {
backupUsePermission(fragment) backupUsePermission(fragment)
@ -69,7 +69,7 @@ object BackupRestoreUi {
.request() .request()
} }
fun selectBackupFolder(fragment: Fragment) { fun selectBackupFolder(fragment: Fragment, requestCode: Int = selectFolderRequestCode) {
fragment.alert { fragment.alert {
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 ->
@ -79,7 +79,7 @@ object BackupRestoreUi {
try { try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
fragment.startActivityForResult(intent, backupSelectRequestCode) fragment.startActivityForResult(intent, requestCode)
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
e.printStackTrace() e.printStackTrace()
fragment.toast(e.localizedMessage ?: "ERROR") fragment.toast(e.localizedMessage ?: "ERROR")
@ -88,7 +88,7 @@ object BackupRestoreUi {
2 -> { 2 -> {
FileChooserDialog.show( FileChooserDialog.show(
fragment.childFragmentManager, fragment.childFragmentManager,
backupSelectRequestCode, requestCode,
mode = FileChooserDialog.DIRECTORY mode = FileChooserDialog.DIRECTORY
) )
} }
@ -264,6 +264,15 @@ object BackupRestoreUi {
} }
} }
} }
selectFolderRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
App.INSTANCE.contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
AppConfig.backupPath = uri.toString()
}
}
oldDataRequestCode -> oldDataRequestCode ->
if (resultCode == RESULT_OK) data?.data?.let { uri -> if (resultCode == RESULT_OK) data?.data?.let { uri ->
ImportOldData.importUri(uri) ImportOldData.importUri(uri)

Loading…
Cancel
Save