pull/37/head
kunfei 5 years ago
parent 07845fab8c
commit 8ee85510c5
  1. 14
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt
  2. 11
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  3. 12
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  4. 14
      app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt

@ -19,8 +19,8 @@ import java.io.IOException
* 阅读界面配置 * 阅读界面配置
*/ */
object ReadBookConfig { object ReadBookConfig {
private const val fileName = "readConfig.json" const val readConfigFileName = "readConfig.json"
private val configList: ArrayList<Config> = arrayListOf<Config>() val configList: ArrayList<Config> = arrayListOf<Config>()
.apply { .apply {
upConfig(this) upConfig(this)
} }
@ -35,11 +35,12 @@ object ReadBookConfig {
} }
fun upConfig(list: ArrayList<Config> = configList) { fun upConfig(list: ArrayList<Config> = configList) {
val configFile = File(App.INSTANCE.filesDir.absolutePath + File.separator + fileName) val configFile =
File(App.INSTANCE.filesDir.absolutePath + File.separator + readConfigFileName)
val json = if (configFile.exists()) { val json = if (configFile.exists()) {
String(configFile.readBytes()) String(configFile.readBytes())
} else { } else {
String(App.INSTANCE.assets.open(fileName).readBytes()) String(App.INSTANCE.assets.open(readConfigFileName).readBytes())
} }
try { try {
GSON.fromJsonArray<Config>(json)?.let { GSON.fromJsonArray<Config>(json)?.let {
@ -63,7 +64,8 @@ object ReadBookConfig {
fun save() { fun save() {
GlobalScope.launch(IO) { GlobalScope.launch(IO) {
val json = GSON.toJson(configList) val json = GSON.toJson(configList)
val configFile = File(App.INSTANCE.filesDir.absolutePath + File.separator + fileName) val configFile =
File(App.INSTANCE.filesDir.absolutePath + File.separator + readConfigFileName)
//获取流并存储 //获取流并存储
try { try {
BufferedWriter(FileWriter(configFile)).use { writer -> BufferedWriter(FileWriter(configFile)).use { writer ->
@ -78,7 +80,7 @@ object ReadBookConfig {
fun reset() { fun reset() {
try { try {
val json = String(App.INSTANCE.assets.open(fileName).readBytes()) val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes())
GSON.fromJsonArray<Config>(json)?.let { GSON.fromJsonArray<Config>(json)?.let {
configList.clear() configList.clear()
configList.addAll(it) configList.addAll(it)

@ -3,6 +3,7 @@ package io.legado.app.help.storage
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.help.FileHelp import io.legado.app.help.FileHelp
import io.legado.app.help.ReadBookConfig
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import org.jetbrains.anko.defaultSharedPreferences import org.jetbrains.anko.defaultSharedPreferences
@ -25,6 +26,7 @@ object Backup {
backupBookSource(path) backupBookSource(path)
backupRssSource(path) backupRssSource(path)
backupReplaceRule(path) backupReplaceRule(path)
backupReadConfig(path)
backupPreference(path) backupPreference(path)
WebDavHelp.backUpWebDav(path) WebDavHelp.backUpWebDav(path)
uiThread { uiThread {
@ -40,6 +42,8 @@ object Backup {
backupBookSource(path) backupBookSource(path)
backupRssSource(path) backupRssSource(path)
backupReplaceRule(path) backupReplaceRule(path)
backupReadConfig(path)
backupPreference(path)
WebDavHelp.backUpWebDav(path) WebDavHelp.backUpWebDav(path)
} }
} }
@ -84,6 +88,13 @@ object Backup {
} }
} }
private fun backupReadConfig(path: String) {
GSON.toJson(ReadBookConfig.configList)?.let {
FileHelp.getFile(path + File.separator + ReadBookConfig.readConfigFileName)
.writeText(it)
}
}
private fun backupPreference(path: String) { private fun backupPreference(path: String) {
Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.let { sp -> Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.let { sp ->
val edit = sp.edit() val edit = sp.edit()

@ -13,6 +13,7 @@ import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.FileHelp import io.legado.app.help.FileHelp
import io.legado.app.help.ReadBookConfig
import io.legado.app.help.storage.Backup.defaultPath import io.legado.app.help.storage.Backup.defaultPath
import io.legado.app.utils.* import io.legado.app.utils.*
import org.jetbrains.anko.defaultSharedPreferences import org.jetbrains.anko.defaultSharedPreferences
@ -68,6 +69,17 @@ object Restore {
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
try {
val file =
FileHelp.getFile(path + File.separator + ReadBookConfig.readConfigFileName)
val configFile =
File(App.INSTANCE.filesDir.absolutePath + File.separator + ReadBookConfig.readConfigFileName)
if (file.exists()) {
file.copyTo(configFile, true)
}
} catch (e: Exception) {
e.printStackTrace()
}
Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.all?.map { Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.all?.map {
val edit = App.INSTANCE.defaultSharedPreferences.edit() val edit = App.INSTANCE.defaultSharedPreferences.edit()
when (val value = it.value) { when (val value = it.value) {

@ -3,6 +3,7 @@ package io.legado.app.help.storage
import android.content.Context import android.content.Context
import io.legado.app.App import io.legado.app.App
import io.legado.app.help.FileHelp import io.legado.app.help.FileHelp
import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.webdav.WebDav import io.legado.app.lib.webdav.WebDav
import io.legado.app.lib.webdav.http.HttpAuth import io.legado.app.lib.webdav.http.HttpAuth
import io.legado.app.utils.ZipUtils import io.legado.app.utils.ZipUtils
@ -80,11 +81,14 @@ object WebDavHelp {
fun backUpWebDav(path: String) { fun backUpWebDav(path: String) {
if (initWebDav()) { if (initWebDav()) {
val paths = arrayListOf<String>() val paths = arrayListOf(
paths.add(path + File.separator + "bookshelf.json") path + File.separator + "bookshelf.json",
paths.add(path + File.separator + "bookSource.json") path + File.separator + "bookSource.json",
paths.add(path + File.separator + "rssSource.json") path + File.separator + "rssSource.json",
paths.add(path + File.separator + "replaceRule.json") path + File.separator + "replaceRule.json",
path + File.separator + "config.xml",
path + File.separator + ReadBookConfig.readConfigFileName
)
FileHelp.deleteFile(zipFilePath) FileHelp.deleteFile(zipFilePath)
if (ZipUtils.zipFiles(paths, zipFilePath)) { if (ZipUtils.zipFiles(paths, zipFilePath)) {
WebDav(getWebDavUrl() + "legado").makeAsDir() WebDav(getWebDavUrl() + "legado").makeAsDir()

Loading…
Cancel
Save