feat: 优化代码

pull/103/head
kunfei 5 years ago
parent f2a7c6b644
commit 859a2d893f
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 24
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  3. 14
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@ -34,4 +34,5 @@ object PreferKey {
const val chineseConverterType = "chineseConverterType" const val chineseConverterType = "chineseConverterType"
const val launcherIcon = "launcherIcon" const val launcherIcon = "launcherIcon"
const val selectText = "selectText" const val selectText = "selectText"
const val lastBackup = "lastBackup"
} }

@ -4,15 +4,15 @@ import android.content.Context
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.App import io.legado.app.App
import io.legado.app.constant.PreferKey
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.utils.DocumentUtils import io.legado.app.help.coroutine.Coroutine
import io.legado.app.utils.FileUtils import io.legado.app.utils.*
import io.legado.app.utils.GSON
import io.legado.app.utils.isContentPath
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.defaultSharedPreferences import org.jetbrains.anko.defaultSharedPreferences
import java.io.File import java.io.File
import java.util.concurrent.TimeUnit
object Backup { object Backup {
@ -34,7 +34,23 @@ object Backup {
) )
} }
fun autoBack(context: Context) {
val lastBackup = context.getPrefLong(PreferKey.lastBackup)
if (lastBackup + TimeUnit.DAYS.toMillis(1) < System.currentTimeMillis()) {
return
}
Coroutine.async {
val backupPath = context.getPrefString(PreferKey.backupPath)
if (backupPath.isNullOrEmpty()) {
backup(context)
} else {
backup(context, backupPath)
}
}
}
suspend fun backup(context: Context, path: String = legadoPath) { suspend fun backup(context: Context, path: String = legadoPath) {
context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis())
withContext(IO) { withContext(IO) {
writeListToJson(App.db.bookDao().all, "bookshelf.json", backupPath) writeListToJson(App.db.bookDao().all, "bookshelf.json", backupPath)
writeListToJson(App.db.bookGroupDao().all, "bookGroup.json", backupPath) writeListToJson(App.db.bookGroupDao().all, "bookGroup.json", backupPath)

@ -15,7 +15,6 @@ import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.storage.Backup import io.legado.app.help.storage.Backup
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
@ -131,22 +130,11 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
override fun finish() { override fun finish() {
if (!BuildConfig.DEBUG) { if (!BuildConfig.DEBUG) {
backup() Backup.autoBack(this)
} }
super.finish() super.finish()
} }
private fun backup() {
Coroutine.async {
val backupPath = getPrefString(PreferKey.backupPath)
if (backupPath.isNullOrEmpty()) {
Backup.backup(this@MainActivity)
} else {
Backup.backup(this@MainActivity, backupPath)
}
}
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
ReadAloud.stop(this) ReadAloud.stop(this)

Loading…
Cancel
Save