From c0158bc925be09c73db22915926dda2b8c8793ea Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 11 Mar 2022 15:10:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/App.kt | 26 ++++++++++++- .../io/legado/app/data/dao/RssSourceDao.kt | 3 ++ .../io/legado/app/help/RuleBigDataHelp.kt | 39 +++++++++++++++++++ .../legado/app/ui/welcome/WelcomeActivity.kt | 34 +++------------- 4 files changed, 73 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index e40fe6932..cb6a62b6a 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -6,18 +6,26 @@ import android.content.Context import android.content.res.Configuration import android.os.Build import androidx.multidex.MultiDexApplication +import com.github.liuyueyi.quick.transfer.ChineseUtils import com.jeremyliao.liveeventbus.LiveEventBus import io.legado.app.base.AppContextWrapper import io.legado.app.constant.AppConst.channelIdDownload import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdWeb +import io.legado.app.constant.PreferKey +import io.legado.app.data.appDb import io.legado.app.help.CrashHandler import io.legado.app.help.LifecycleHelp +import io.legado.app.help.RuleBigDataHelp import io.legado.app.help.config.AppConfig import io.legado.app.help.config.ThemeConfig.applyDayNight +import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.http.cronet.CronetLoader +import io.legado.app.model.BookCover import io.legado.app.utils.defaultSharedPreferences +import io.legado.app.utils.getPrefBoolean import splitties.systemservices.notificationManager +import java.util.concurrent.TimeUnit class App : MultiDexApplication() { @@ -33,7 +41,23 @@ class App : MultiDexApplication() { .autoClear(false) registerActivityLifecycleCallbacks(LifecycleHelp) defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig) - + Coroutine.async { + //初始化封面 + BookCover.toString() + //清除过期数据 + appDb.cacheDao.clearDeadline(System.currentTimeMillis()) + if (getPrefBoolean(PreferKey.autoClearExpired, true)) { + appDb.searchBookDao + .clearExpired(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1)) + } + RuleBigDataHelp.clearInvalid() + //初始化简繁转换引擎 + when (AppConfig.chineseConverterType) { + 1 -> ChineseUtils.t2s("初始化") + 2 -> ChineseUtils.s2t("初始化") + else -> null + } + } } override fun attachBaseContext(base: Context) { diff --git a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt index 85605b3cf..709127642 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt @@ -71,4 +71,7 @@ interface RssSourceDao { @Query("select * from rssSources where sourceGroup like '%' || :group || '%'") fun getByGroup(group: String): List + + @Query("select 1 from rssSources where sourceUrl = :key") + fun has(key: String): Boolean? } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/RuleBigDataHelp.kt b/app/src/main/java/io/legado/app/help/RuleBigDataHelp.kt index 30ca76bff..f38f5d754 100644 --- a/app/src/main/java/io/legado/app/help/RuleBigDataHelp.kt +++ b/app/src/main/java/io/legado/app/help/RuleBigDataHelp.kt @@ -1,8 +1,12 @@ package io.legado.app.help +import io.legado.app.data.appDb import io.legado.app.utils.FileUtils import io.legado.app.utils.MD5Utils import io.legado.app.utils.externalFiles +import io.legado.app.utils.getFile +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.withContext import splitties.init.appCtx import java.io.File @@ -12,6 +16,41 @@ object RuleBigDataHelp { private val bookData = FileUtils.createFolderIfNotExist(ruleDataDir, "book") private val rssData = FileUtils.createFolderIfNotExist(ruleDataDir, "rss") + suspend fun clearInvalid() { + withContext(IO) { + bookData.listFiles()?.forEach { + if (it.isFile) { + FileUtils.delete(it) + } else { + val bookUrlFile = it.getFile("bookUrl.txt") + if (!bookUrlFile.exists()) { + FileUtils.delete(it) + } else { + val bookUrl = bookUrlFile.readText() + if (appDb.bookDao.has(bookUrl) != true) { + FileUtils.delete(it) + } + } + } + } + rssData.listFiles()?.forEach { + if (it.isFile) { + FileUtils.delete(it) + } else { + val originFile = it.getFile("origin.txt") + if (!originFile.exists()) { + FileUtils.delete(it) + } else { + val origin = originFile.readText() + if (appDb.rssSourceDao.has(origin) != true) { + FileUtils.delete(it) + } + } + } + } + } + } + fun putBookVariable(bookUrl: String, key: String, value: String?) { val md5BookUrl = MD5Utils.md5Encode(bookUrl) val md5Key = MD5Utils.md5Encode(key) diff --git a/app/src/main/java/io/legado/app/ui/welcome/WelcomeActivity.kt b/app/src/main/java/io/legado/app/ui/welcome/WelcomeActivity.kt index 692e2b780..94b8d7c98 100644 --- a/app/src/main/java/io/legado/app/ui/welcome/WelcomeActivity.kt +++ b/app/src/main/java/io/legado/app/ui/welcome/WelcomeActivity.kt @@ -3,7 +3,6 @@ package io.legado.app.ui.welcome import android.content.Intent import android.graphics.drawable.BitmapDrawable import android.os.Bundle -import com.github.liuyueyi.quick.transfer.ChineseUtils import io.legado.app.base.BaseActivity import io.legado.app.constant.PreferKey import io.legado.app.constant.Theme @@ -13,12 +12,10 @@ import io.legado.app.help.config.AppConfig import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.storage.AppWebDav import io.legado.app.lib.theme.accentColor -import io.legado.app.model.BookCover import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.main.MainActivity import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding -import java.util.concurrent.TimeUnit open class WelcomeActivity : BaseActivity() { @@ -27,16 +24,8 @@ open class WelcomeActivity : BaseActivity() { override fun onActivityCreated(savedInstanceState: Bundle?) { binding.ivBook.setColorFilter(accentColor) binding.vwTitleLine.setBackgroundColor(accentColor) - // 避免从桌面启动程序后,会重新实例化入口类的activity - if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) { - finish() - } else { - init() - } - } - - private fun init() { Coroutine.async { + //同步阅读记录 if (!AppConfig.syncBookProgress) return@async val books = appDb.bookDao.all books.forEach { book -> @@ -54,23 +43,12 @@ open class WelcomeActivity : BaseActivity() { } } } - Coroutine.async { - //初始化封面 - BookCover.toString() - //清除过期数据 - appDb.cacheDao.clearDeadline(System.currentTimeMillis()) - if (getPrefBoolean(PreferKey.autoClearExpired, true)) { - appDb.searchBookDao - .clearExpired(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1)) - } - //初始化简繁转换引擎 - when (AppConfig.chineseConverterType) { - 1 -> ChineseUtils.t2s("初始化") - 2 -> ChineseUtils.s2t("初始化") - else -> null - } + // 避免从桌面启动程序后,会重新实例化入口类的activity + if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) { + finish() + } else { + binding.root.postDelayed({ startMainActivity() }, 500) } - binding.root.postDelayed({ startMainActivity() }, 500) } override fun upBackgroundImage() {