From e89cae25fe0aec0e5dba18719273531cb6f59dd2 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 20 Mar 2020 19:50:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/storage/Backup.kt | 48 +++++++++---------- .../book/source/manage/BookSourceViewModel.kt | 5 +- .../legado/app/ui/config/BackupRestoreUi.kt | 22 ++++----- .../ui/replacerule/ReplaceRuleViewModel.kt | 5 +- .../rss/source/manage/RssSourceViewModel.kt | 5 +- 5 files changed, 38 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index 32bc2b765..60d86d6a3 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -21,14 +21,6 @@ object Backup { FileUtils.getDirFile(App.INSTANCE.filesDir, "backup").absolutePath } - val legadoPath by lazy { - FileUtils.getSdCardPath() + File.separator + "YueDu3.0" - } - - val exportPath by lazy { - legadoPath + File.separator + "Export" - } - val backupFileNames by lazy { arrayOf( "bookshelf.json", "bookGroup.json", "bookSource.json", "rssSource.json", @@ -42,16 +34,13 @@ object Backup { return } Coroutine.async { - val backupPath = context.getPrefString(PreferKey.backupPath) - if (backupPath.isNullOrEmpty()) { - backup(context) - } else { - backup(context, backupPath) + context.getPrefString(PreferKey.backupPath)?.let { + backup(context, it, true) } } } - suspend fun backup(context: Context, path: String = legadoPath) { + suspend fun backup(context: Context, path: String, isAuto: Boolean = false) { context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis()) withContext(IO) { synchronized(this@Backup) { @@ -81,9 +70,9 @@ object Backup { } WebDavHelp.backUpWebDav(backupPath) if (path.isContentPath()) { - copyBackup(context, Uri.parse(path)) + copyBackup(context, Uri.parse(path), isAuto) } else { - copyBackup(File(path)) + copyBackup(File(path), isAuto) } } } @@ -97,28 +86,39 @@ object Backup { } @Throws(java.lang.Exception::class) - private fun copyBackup(context: Context, uri: Uri) { - + private fun copyBackup(context: Context, uri: Uri, isAuto: Boolean) { DocumentFile.fromTreeUri(context, uri)?.let { treeDoc -> for (fileName in backupFileNames) { val file = File(backupPath + File.separator + fileName) if (file.exists()) { - treeDoc.findFile(fileName)?.delete() - treeDoc.createFile("", fileName) - ?.writeBytes(context, file.readBytes()) + if (isAuto) { + treeDoc.findFile("auto")?.findFile(fileName)?.delete() + DocumentUtils.createFileIfNotExist( + treeDoc, + fileName, + subDirs = *arrayOf("auto") + )?.writeBytes(context, file.readBytes()) + } else { + treeDoc.findFile(fileName)?.delete() + treeDoc.createFile("", fileName) + ?.writeBytes(context, file.readBytes()) + } } } } } @Throws(java.lang.Exception::class) - private fun copyBackup(rootFile: File) { + private fun copyBackup(rootFile: File, isAuto: Boolean) { for (fileName in backupFileNames) { val file = File(backupPath + File.separator + fileName) if (file.exists()) { file.copyTo( - FileUtils.createFileIfNotExist(rootFile, fileName), - true + if (isAuto) { + FileUtils.createFileIfNotExist(rootFile, fileName, "auto") + } else { + FileUtils.createFileIfNotExist(rootFile, fileName) + }, true ) } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index d44efd3ad..c84497536 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -8,7 +8,6 @@ import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.BookSource import io.legado.app.help.http.HttpHelper -import io.legado.app.help.storage.Backup import io.legado.app.help.storage.OldRule import io.legado.app.help.storage.Restore.jsonPath import io.legado.app.utils.* @@ -96,7 +95,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) FileUtils.createFileIfNotExist(file, "exportBookSource.json") .writeText(json) }.onSuccess { - context.toast("成功导出至\n${Backup.exportPath}") + context.toast("成功导出至\n${file.absolutePath}") }.onError { context.toast("导出失败\n${it.localizedMessage}") } @@ -109,7 +108,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) doc.createFile("", "exportBookSource.json") ?.writeText(context, json) }.onSuccess { - context.toast("成功导出至\n${Backup.exportPath}") + context.toast("成功导出至\n${doc.uri.path}") }.onError { context.toast("导出失败\n${it.localizedMessage}") } diff --git a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt index 409638a4c..c849ae1de 100644 --- a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt +++ b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt @@ -48,30 +48,24 @@ object BackupRestoreUi { selectBackupFolder(fragment, backupSelectRequestCode) } } else { - backupUsePermission(fragment, requestCode = backupSelectRequestCode) + backupUsePermission(fragment, backupPath) } } } private fun backupUsePermission( fragment: Fragment, - path: String = Backup.legadoPath, - requestCode: Int = selectFolderRequestCode + path: String ) { PermissionsCompat.Builder(fragment) .addPermissions(*Permissions.Group.STORAGE) .rationale(R.string.tip_perm_request_storage) .onGranted { - when (requestCode) { - selectFolderRequestCode -> AppConfig.backupPath = Backup.legadoPath - else -> { - Coroutine.async { - AppConfig.backupPath = path - Backup.backup(fragment.requireContext(), path) - }.onSuccess { - fragment.toast(R.string.backup_success) - } - } + Coroutine.async { + AppConfig.backupPath = path + Backup.backup(fragment.requireContext(), path) + }.onSuccess { + fragment.toast(R.string.backup_success) } } .request() @@ -107,7 +101,7 @@ object BackupRestoreUi { } } - private fun restoreUsePermission(fragment: Fragment, path: String = Backup.legadoPath) { + private fun restoreUsePermission(fragment: Fragment, path: String) { PermissionsCompat.Builder(fragment) .addPermissions(*Permissions.Group.STORAGE) .rationale(R.string.tip_perm_request_storage) diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index a63d7ea38..7187c6b88 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -8,7 +8,6 @@ import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.ReplaceRule import io.legado.app.help.http.HttpHelper -import io.legado.app.help.storage.Backup import io.legado.app.help.storage.ImportOldData import io.legado.app.utils.* import org.jetbrains.anko.toast @@ -93,7 +92,7 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application FileUtils.createFileIfNotExist(file, "exportReplaceRule.json") .writeText(json) }.onSuccess { - context.toast("成功导出至\n${Backup.exportPath}") + context.toast("成功导出至\n${file.absolutePath}") }.onError { context.toast("导出失败\n${it.localizedMessage}") } @@ -106,7 +105,7 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application doc.createFile("", "exportReplaceRule.json") ?.writeText(context, json) }.onSuccess { - context.toast("成功导出至\n${Backup.exportPath}") + context.toast("成功导出至\n${doc.uri.path}") }.onError { context.toast("导出失败\n${it.localizedMessage}") } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt index 82bad9584..300d7468c 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt @@ -9,7 +9,6 @@ import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.RssSource import io.legado.app.help.http.HttpHelper -import io.legado.app.help.storage.Backup import io.legado.app.help.storage.Restore.jsonPath import io.legado.app.utils.* import org.jetbrains.anko.toast @@ -74,7 +73,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) FileUtils.createFileIfNotExist(file, "exportRssSource.json") .writeText(json) }.onSuccess { - context.toast("成功导出至\n${Backup.exportPath}") + context.toast("成功导出至\n${file.absolutePath}") }.onError { context.toast("导出失败\n${it.localizedMessage}") } @@ -87,7 +86,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) doc.createFile("", "exportRssSource.json") ?.writeText(context, json) }.onSuccess { - context.toast("成功导出至\n${Backup.exportPath}") + context.toast("成功导出至\n${doc.uri.path}") }.onError { context.toast("导出失败\n${it.localizedMessage}") }