feat: 优化代码

pull/123/head
kunfei 5 years ago
parent aa110cc9c5
commit 8de2bd73d0
  1. 11
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  2. 22
      app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt

@ -17,15 +17,12 @@ import org.jetbrains.anko.toast
import java.io.File import java.io.File
object ImportOldData { object ImportOldData {
val yueDuPath by lazy {
FileUtils.getSdCardPath() + File.separator + "YueDu"
}
fun import(context: Context) { fun import(context: Context, file: File) {
GlobalScope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.IO) {
try {// 导入书架 try {// 导入书架
val shelfFile = val shelfFile =
FileUtils.createFileIfNotExist(yueDuPath + File.separator + "myBookShelf.json") FileUtils.createFileIfNotExist(file, "myBookShelf.json")
val json = shelfFile.readText() val json = shelfFile.readText()
val importCount = importOldBookshelf(json) val importCount = importOldBookshelf(json)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
@ -39,7 +36,7 @@ object ImportOldData {
try {// Book source try {// Book source
val sourceFile = val sourceFile =
FileUtils.createFileIfNotExist(yueDuPath + File.separator + "myBookSource.json") FileUtils.createFileIfNotExist(file, "myBookSource.json")
val json = sourceFile.readText() val json = sourceFile.readText()
val importCount = importOldSource(json) val importCount = importOldSource(json)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
@ -53,7 +50,7 @@ object ImportOldData {
try {// Replace rules try {// Replace rules
val ruleFile = val ruleFile =
FileUtils.createFileIfNotExist(yueDuPath + File.separator + "myBookReplaceRule.json") FileUtils.createFileIfNotExist(file, "myBookReplaceRule.json")
val json = ruleFile.readText() val json = ruleFile.readText()
val importCount = importOldReplaceRule(json) val importCount = importOldReplaceRule(json)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {

@ -3,7 +3,6 @@ package io.legado.app.ui.config
import android.app.Activity.RESULT_OK import android.app.Activity.RESULT_OK
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import io.legado.app.App import io.legado.app.App
@ -23,6 +22,7 @@ import io.legado.app.utils.isContentPath
import io.legado.app.utils.toast import io.legado.app.utils.toast
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import java.io.File
object BackupRestoreUi { object BackupRestoreUi {
private const val selectFolderRequestCode = 21 private const val selectFolderRequestCode = 21
@ -123,22 +123,7 @@ object BackupRestoreUi {
} }
fun importOldData(fragment: Fragment) { fun importOldData(fragment: Fragment) {
FilePicker.selectFolder(fragment, oldDataRequestCode) { FilePicker.selectFolder(fragment, oldDataRequestCode)
importOldUsePermission(fragment)
}
}
private fun importOldUsePermission(fragment: Fragment) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
fragment.toast(R.string.a10_permission_toast)
}
PermissionsCompat.Builder(fragment)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
ImportOldData.import(fragment.requireContext())
}
.request()
} }
fun onFilePicked(requestCode: Int, currentPath: String) { fun onFilePicked(requestCode: Int, currentPath: String) {
@ -162,6 +147,9 @@ object BackupRestoreUi {
selectFolderRequestCode -> { selectFolderRequestCode -> {
AppConfig.backupPath = currentPath AppConfig.backupPath = currentPath
} }
oldDataRequestCode -> {
ImportOldData.import(App.INSTANCE, File(currentPath))
}
} }
} }

Loading…
Cancel
Save