|
|
|
@ -33,9 +33,10 @@ import io.legado.app.lib.prefs.fragment.PreferenceFragment |
|
|
|
|
import io.legado.app.lib.theme.primaryColor |
|
|
|
|
import io.legado.app.ui.document.HandleFileContract |
|
|
|
|
import io.legado.app.ui.widget.dialog.TextDialog |
|
|
|
|
import io.legado.app.ui.widget.dialog.WaitDialog |
|
|
|
|
import io.legado.app.utils.* |
|
|
|
|
import kotlinx.coroutines.* |
|
|
|
|
import kotlinx.coroutines.Dispatchers.Main |
|
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
import splitties.init.appCtx |
|
|
|
|
import kotlin.collections.set |
|
|
|
|
|
|
|
|
@ -44,6 +45,9 @@ class BackupConfigFragment : PreferenceFragment(), |
|
|
|
|
MenuProvider { |
|
|
|
|
|
|
|
|
|
private val viewModel by activityViewModels<ConfigViewModel>() |
|
|
|
|
private val waitDialog by lazy { WaitDialog(requireContext()) } |
|
|
|
|
private var backupJob: Job? = null |
|
|
|
|
private var restoreJob: Job? = null |
|
|
|
|
|
|
|
|
|
private val selectBackupPath = registerForActivityResult(HandleFileContract()) { |
|
|
|
|
it.uri?.let { uri -> |
|
|
|
@ -245,7 +249,13 @@ class BackupConfigFragment : PreferenceFragment(), |
|
|
|
|
val uri = Uri.parse(backupPath) |
|
|
|
|
val doc = DocumentFile.fromTreeUri(requireContext(), uri) |
|
|
|
|
if (doc?.canWrite() == true) { |
|
|
|
|
waitDialog.setText("备份中…") |
|
|
|
|
waitDialog.setOnCancelListener { |
|
|
|
|
backupJob?.cancel() |
|
|
|
|
} |
|
|
|
|
waitDialog.show() |
|
|
|
|
Coroutine.async { |
|
|
|
|
backupJob = coroutineContext[Job] |
|
|
|
|
Backup.backup(requireContext(), backupPath) |
|
|
|
|
}.onSuccess { |
|
|
|
|
appCtx.toastOnUi(R.string.backup_success) |
|
|
|
@ -257,6 +267,8 @@ class BackupConfigFragment : PreferenceFragment(), |
|
|
|
|
it.localizedMessage |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
}.onFinally(Main) { |
|
|
|
|
waitDialog.dismiss() |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
backupDir.launch() |
|
|
|
@ -272,7 +284,13 @@ class BackupConfigFragment : PreferenceFragment(), |
|
|
|
|
.addPermissions(*Permissions.Group.STORAGE) |
|
|
|
|
.rationale(R.string.tip_perm_request_storage) |
|
|
|
|
.onGranted { |
|
|
|
|
waitDialog.setText("备份中…") |
|
|
|
|
waitDialog.setOnCancelListener { |
|
|
|
|
backupJob?.cancel() |
|
|
|
|
} |
|
|
|
|
waitDialog.show() |
|
|
|
|
Coroutine.async { |
|
|
|
|
backupJob = coroutineContext[Job] |
|
|
|
|
AppConfig.backupPath = path |
|
|
|
|
Backup.backup(requireContext(), path) |
|
|
|
|
}.onSuccess { |
|
|
|
@ -280,13 +298,21 @@ class BackupConfigFragment : PreferenceFragment(), |
|
|
|
|
}.onError { |
|
|
|
|
AppLog.put("备份出错\n${it.localizedMessage}", it) |
|
|
|
|
appCtx.toastOnUi(appCtx.getString(R.string.backup_fail, it.localizedMessage)) |
|
|
|
|
}.onFinally(Main) { |
|
|
|
|
waitDialog.dismiss() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
.request() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun restore() { |
|
|
|
|
waitDialog.setText(R.string.loading) |
|
|
|
|
waitDialog.setOnCancelListener { |
|
|
|
|
restoreJob?.cancel() |
|
|
|
|
} |
|
|
|
|
waitDialog.show() |
|
|
|
|
Coroutine.async { |
|
|
|
|
restoreJob = coroutineContext[Job] |
|
|
|
|
AppWebDav.showRestoreDialog(requireContext()) |
|
|
|
|
}.onError { |
|
|
|
|
alert { |
|
|
|
@ -297,6 +323,8 @@ class BackupConfigFragment : PreferenceFragment(), |
|
|
|
|
} |
|
|
|
|
cancelButton() |
|
|
|
|
} |
|
|
|
|
}.onFinally(Main) { |
|
|
|
|
waitDialog.dismiss() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|