feat: 优化代码

pull/172/head
kunfei 5 years ago
parent 741835f498
commit e89cae25fe
  1. 48
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  2. 5
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  3. 22
      app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt
  4. 5
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt
  5. 5
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt

@ -21,14 +21,6 @@ object Backup {
FileUtils.getDirFile(App.INSTANCE.filesDir, "backup").absolutePath 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 { val backupFileNames by lazy {
arrayOf( arrayOf(
"bookshelf.json", "bookGroup.json", "bookSource.json", "rssSource.json", "bookshelf.json", "bookGroup.json", "bookSource.json", "rssSource.json",
@ -42,16 +34,13 @@ object Backup {
return return
} }
Coroutine.async { Coroutine.async {
val backupPath = context.getPrefString(PreferKey.backupPath) context.getPrefString(PreferKey.backupPath)?.let {
if (backupPath.isNullOrEmpty()) { backup(context, it, true)
backup(context)
} else {
backup(context, backupPath)
} }
} }
} }
suspend fun backup(context: Context, path: String = legadoPath) { suspend fun backup(context: Context, path: String, isAuto: Boolean = false) {
context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis()) context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis())
withContext(IO) { withContext(IO) {
synchronized(this@Backup) { synchronized(this@Backup) {
@ -81,9 +70,9 @@ object Backup {
} }
WebDavHelp.backUpWebDav(backupPath) WebDavHelp.backUpWebDav(backupPath)
if (path.isContentPath()) { if (path.isContentPath()) {
copyBackup(context, Uri.parse(path)) copyBackup(context, Uri.parse(path), isAuto)
} else { } else {
copyBackup(File(path)) copyBackup(File(path), isAuto)
} }
} }
} }
@ -97,28 +86,39 @@ object Backup {
} }
@Throws(java.lang.Exception::class) @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 -> DocumentFile.fromTreeUri(context, uri)?.let { treeDoc ->
for (fileName in backupFileNames) { for (fileName in backupFileNames) {
val file = File(backupPath + File.separator + fileName) val file = File(backupPath + File.separator + fileName)
if (file.exists()) { if (file.exists()) {
treeDoc.findFile(fileName)?.delete() if (isAuto) {
treeDoc.createFile("", fileName) treeDoc.findFile("auto")?.findFile(fileName)?.delete()
?.writeBytes(context, file.readBytes()) 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) @Throws(java.lang.Exception::class)
private fun copyBackup(rootFile: File) { private fun copyBackup(rootFile: File, isAuto: Boolean) {
for (fileName in backupFileNames) { for (fileName in backupFileNames) {
val file = File(backupPath + File.separator + fileName) val file = File(backupPath + File.separator + fileName)
if (file.exists()) { if (file.exists()) {
file.copyTo( file.copyTo(
FileUtils.createFileIfNotExist(rootFile, fileName), if (isAuto) {
true FileUtils.createFileIfNotExist(rootFile, fileName, "auto")
} else {
FileUtils.createFileIfNotExist(rootFile, fileName)
}, true
) )
} }
} }

@ -8,7 +8,6 @@ import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.http.HttpHelper 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.OldRule
import io.legado.app.help.storage.Restore.jsonPath import io.legado.app.help.storage.Restore.jsonPath
import io.legado.app.utils.* import io.legado.app.utils.*
@ -96,7 +95,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
FileUtils.createFileIfNotExist(file, "exportBookSource.json") FileUtils.createFileIfNotExist(file, "exportBookSource.json")
.writeText(json) .writeText(json)
}.onSuccess { }.onSuccess {
context.toast("成功导出至\n${Backup.exportPath}") context.toast("成功导出至\n${file.absolutePath}")
}.onError { }.onError {
context.toast("导出失败\n${it.localizedMessage}") context.toast("导出失败\n${it.localizedMessage}")
} }
@ -109,7 +108,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
doc.createFile("", "exportBookSource.json") doc.createFile("", "exportBookSource.json")
?.writeText(context, json) ?.writeText(context, json)
}.onSuccess { }.onSuccess {
context.toast("成功导出至\n${Backup.exportPath}") context.toast("成功导出至\n${doc.uri.path}")
}.onError { }.onError {
context.toast("导出失败\n${it.localizedMessage}") context.toast("导出失败\n${it.localizedMessage}")
} }

@ -48,30 +48,24 @@ object BackupRestoreUi {
selectBackupFolder(fragment, backupSelectRequestCode) selectBackupFolder(fragment, backupSelectRequestCode)
} }
} else { } else {
backupUsePermission(fragment, requestCode = backupSelectRequestCode) backupUsePermission(fragment, backupPath)
} }
} }
} }
private fun backupUsePermission( private fun backupUsePermission(
fragment: Fragment, fragment: Fragment,
path: String = Backup.legadoPath, path: String
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 {
when (requestCode) { Coroutine.async {
selectFolderRequestCode -> AppConfig.backupPath = Backup.legadoPath AppConfig.backupPath = path
else -> { Backup.backup(fragment.requireContext(), path)
Coroutine.async { }.onSuccess {
AppConfig.backupPath = path fragment.toast(R.string.backup_success)
Backup.backup(fragment.requireContext(), path)
}.onSuccess {
fragment.toast(R.string.backup_success)
}
}
} }
} }
.request() .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) 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)

@ -8,7 +8,6 @@ import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.http.HttpHelper 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.help.storage.ImportOldData
import io.legado.app.utils.* import io.legado.app.utils.*
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
@ -93,7 +92,7 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
FileUtils.createFileIfNotExist(file, "exportReplaceRule.json") FileUtils.createFileIfNotExist(file, "exportReplaceRule.json")
.writeText(json) .writeText(json)
}.onSuccess { }.onSuccess {
context.toast("成功导出至\n${Backup.exportPath}") context.toast("成功导出至\n${file.absolutePath}")
}.onError { }.onError {
context.toast("导出失败\n${it.localizedMessage}") context.toast("导出失败\n${it.localizedMessage}")
} }
@ -106,7 +105,7 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
doc.createFile("", "exportReplaceRule.json") doc.createFile("", "exportReplaceRule.json")
?.writeText(context, json) ?.writeText(context, json)
}.onSuccess { }.onSuccess {
context.toast("成功导出至\n${Backup.exportPath}") context.toast("成功导出至\n${doc.uri.path}")
}.onError { }.onError {
context.toast("导出失败\n${it.localizedMessage}") context.toast("导出失败\n${it.localizedMessage}")
} }

@ -9,7 +9,6 @@ import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.http.HttpHelper 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.help.storage.Restore.jsonPath
import io.legado.app.utils.* import io.legado.app.utils.*
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
@ -74,7 +73,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
FileUtils.createFileIfNotExist(file, "exportRssSource.json") FileUtils.createFileIfNotExist(file, "exportRssSource.json")
.writeText(json) .writeText(json)
}.onSuccess { }.onSuccess {
context.toast("成功导出至\n${Backup.exportPath}") context.toast("成功导出至\n${file.absolutePath}")
}.onError { }.onError {
context.toast("导出失败\n${it.localizedMessage}") context.toast("导出失败\n${it.localizedMessage}")
} }
@ -87,7 +86,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
doc.createFile("", "exportRssSource.json") doc.createFile("", "exportRssSource.json")
?.writeText(context, json) ?.writeText(context, json)
}.onSuccess { }.onSuccess {
context.toast("成功导出至\n${Backup.exportPath}") context.toast("成功导出至\n${doc.uri.path}")
}.onError { }.onError {
context.toast("导出失败\n${it.localizedMessage}") context.toast("导出失败\n${it.localizedMessage}")
} }

Loading…
Cancel
Save