优化 #5596
This commit is contained in:
@@ -2,6 +2,7 @@ package io.legado.app.help.storage
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import androidx.core.net.toUri
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import io.legado.app.constant.AppLog
|
||||
import io.legado.app.constant.PreferKey
|
||||
@@ -14,6 +15,7 @@ import io.legado.app.help.config.LocalConfig
|
||||
import io.legado.app.help.config.ReadBookConfig
|
||||
import io.legado.app.help.config.ThemeConfig
|
||||
import io.legado.app.help.coroutine.Coroutine
|
||||
import io.legado.app.model.BookCover
|
||||
import io.legado.app.utils.FileUtils
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.LogUtils
|
||||
@@ -29,6 +31,7 @@ import io.legado.app.utils.openOutputStream
|
||||
import io.legado.app.utils.outputStream
|
||||
import io.legado.app.utils.writeToOutputStream
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
@@ -41,7 +44,6 @@ import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.coroutines.coroutineContext
|
||||
|
||||
/**
|
||||
* 备份
|
||||
@@ -78,6 +80,7 @@ object Backup {
|
||||
ReadBookConfig.configFileName,
|
||||
ReadBookConfig.shareConfigFileName,
|
||||
ThemeConfig.configFileName,
|
||||
BookCover.configFileName,
|
||||
"config.xml"
|
||||
)
|
||||
}
|
||||
@@ -152,7 +155,7 @@ object Backup {
|
||||
.writeText(it)
|
||||
}
|
||||
}
|
||||
coroutineContext.ensureActive()
|
||||
currentCoroutineContext().ensureActive()
|
||||
GSON.toJson(ReadBookConfig.configList).let {
|
||||
FileUtils.createFileIfNotExist(backupPath + File.separator + ReadBookConfig.configFileName)
|
||||
.writeText(it)
|
||||
@@ -169,7 +172,11 @@ object Backup {
|
||||
FileUtils.createFileIfNotExist(backupPath + File.separator + DirectLinkUpload.ruleFileName)
|
||||
.writeText(GSON.toJson(it))
|
||||
}
|
||||
coroutineContext.ensureActive()
|
||||
BookCover.getConfig()?.let {
|
||||
FileUtils.createFileIfNotExist(backupPath + File.separator + BookCover.configFileName)
|
||||
.writeText(GSON.toJson(it))
|
||||
}
|
||||
currentCoroutineContext().ensureActive()
|
||||
appCtx.getSharedPreferences(backupPath, "config")?.let { sp ->
|
||||
val edit = sp.edit()
|
||||
appCtx.defaultSharedPreferences.all.forEach { (key, value) ->
|
||||
@@ -193,7 +200,7 @@ object Backup {
|
||||
}
|
||||
edit.commit()
|
||||
}
|
||||
coroutineContext.ensureActive()
|
||||
currentCoroutineContext().ensureActive()
|
||||
val zipFileName = getNowZipFileName()
|
||||
val paths = arrayListOf(*backupFileNames)
|
||||
for (i in 0 until paths.size) {
|
||||
@@ -213,7 +220,7 @@ object Backup {
|
||||
}
|
||||
|
||||
path.isContentScheme() -> {
|
||||
copyBackup(context, Uri.parse(path), backupFileName)
|
||||
copyBackup(context, path.toUri(), backupFileName)
|
||||
}
|
||||
|
||||
else -> {
|
||||
@@ -228,7 +235,7 @@ object Backup {
|
||||
}
|
||||
FileUtils.delete(backupPath)
|
||||
FileUtils.delete(zipFilePath)
|
||||
coroutineContext.ensureActive()
|
||||
currentCoroutineContext().ensureActive()
|
||||
ReadBookConfig.getAllPicBgStr().map {
|
||||
if (it.contains(File.separator)) {
|
||||
File(it)
|
||||
@@ -241,7 +248,7 @@ object Backup {
|
||||
}
|
||||
|
||||
private suspend fun writeListToJson(list: List<Any>, fileName: String, path: String) {
|
||||
coroutineContext.ensureActive()
|
||||
currentCoroutineContext().ensureActive()
|
||||
withContext(IO) {
|
||||
if (list.isNotEmpty()) {
|
||||
LogUtils.d(TAG, "阅读备份 $fileName 列表大小 ${list.size}")
|
||||
|
||||
@@ -32,6 +32,7 @@ import io.legado.app.help.book.upType
|
||||
import io.legado.app.help.config.LocalConfig
|
||||
import io.legado.app.help.config.ReadBookConfig
|
||||
import io.legado.app.help.config.ThemeConfig
|
||||
import io.legado.app.model.BookCover
|
||||
import io.legado.app.model.localBook.LocalBook
|
||||
import io.legado.app.utils.ACache
|
||||
import io.legado.app.utils.FileUtils
|
||||
@@ -211,6 +212,14 @@ object Restore {
|
||||
}?.onFailure {
|
||||
AppLog.put("恢复主题出错\n${it.localizedMessage}", it)
|
||||
}
|
||||
File(path, BookCover.configFileName).takeIf {
|
||||
it.exists()
|
||||
}?.runCatching {
|
||||
val json = readText()
|
||||
BookCover.saveCoverRule(json)
|
||||
}?.onFailure {
|
||||
AppLog.put("恢复封面规则出错\n${it.localizedMessage}", it)
|
||||
}
|
||||
if (!BackupConfig.ignoreReadConfig) {
|
||||
//恢复阅读界面配置
|
||||
File(path, ReadBookConfig.configFileName).takeIf {
|
||||
|
||||
@@ -36,14 +36,17 @@ import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.fromJsonObject
|
||||
import io.legado.app.utils.getPrefBoolean
|
||||
import io.legado.app.utils.getPrefString
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import splitties.init.appCtx
|
||||
import java.io.File
|
||||
import kotlin.coroutines.coroutineContext
|
||||
|
||||
@Keep
|
||||
@Suppress("ConstPropertyName")
|
||||
object BookCover {
|
||||
|
||||
private const val coverRuleConfigKey = "legadoCoverRuleConfig"
|
||||
const val configFileName = "coverRule.json"
|
||||
|
||||
var drawBookName = true
|
||||
private set
|
||||
var drawBookAuthor = true
|
||||
@@ -200,9 +203,12 @@ object BookCover {
|
||||
}
|
||||
|
||||
fun getCoverRule(): CoverRule {
|
||||
return getConfig() ?: DefaultData.coverRule
|
||||
}
|
||||
|
||||
fun getConfig(): CoverRule? {
|
||||
return GSON.fromJsonObject<CoverRule>(CacheManager.get(coverRuleConfigKey))
|
||||
.getOrNull()
|
||||
?: DefaultData.coverRule
|
||||
}
|
||||
|
||||
suspend fun searchCover(book: Book): String? {
|
||||
@@ -214,12 +220,12 @@ object BookCover {
|
||||
config.searchUrl,
|
||||
book.name,
|
||||
source = config,
|
||||
coroutineContext = coroutineContext,
|
||||
coroutineContext = currentCoroutineContext(),
|
||||
hasLoginHeader = false
|
||||
)
|
||||
val res = analyzeUrl.getStrResponseAwait()
|
||||
val analyzeRule = AnalyzeRule(book)
|
||||
analyzeRule.setCoroutineContext(coroutineContext)
|
||||
analyzeRule.setCoroutineContext(currentCoroutineContext())
|
||||
analyzeRule.setContent(res.body)
|
||||
analyzeRule.setRedirectUrl(res.url)
|
||||
return analyzeRule.getString(config.coverRule, isUrl = true)
|
||||
@@ -227,6 +233,10 @@ object BookCover {
|
||||
|
||||
fun saveCoverRule(config: CoverRule) {
|
||||
val json = GSON.toJson(config)
|
||||
saveCoverRule(json)
|
||||
}
|
||||
|
||||
fun saveCoverRule(json: String) {
|
||||
CacheManager.put(coverRuleConfigKey, json)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user