From cd802bad500b7fdc76a3d3536ac9b05eaa2999f5 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 10 Aug 2020 11:43:51 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E5=87=BA=E8=BD=AF=E4=BB=B6=E6=97=B6?= =?UTF-8?q?=E6=B8=85=E9=99=A4=E5=B7=B2=E5=88=A0=E9=99=A4=E4=B9=A6=E7=B1=8D?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 24 +++++++++++++++---- .../io/legado/app/ui/main/MainActivity.kt | 6 +++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 43d61c0f4..7bbbbd001 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -6,6 +6,7 @@ import io.legado.app.constant.EventBus import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.ReplaceRule +import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.localBook.LocalBook import io.legado.app.utils.FileUtils import io.legado.app.utils.MD5Utils @@ -37,13 +38,28 @@ object BookHelp { fun clearCache() { FileUtils.deleteFile( - FileUtils.getPath( - downloadDir, - subDirs = *arrayOf(cacheFolderName) - ) + FileUtils.getPath(downloadDir, subDirs = *arrayOf(cacheFolderName)) ) } + /** + * 清楚已删除书的缓存 + */ + fun clearRemovedCache() { + Coroutine.async { + val bookFolderNames = arrayListOf() + App.db.bookDao().all.forEach { + bookFolderNames.add(bookFolderName(it)) + } + val file = FileUtils.getDirFile(downloadDir, cacheFolderName) + file.listFiles()?.forEach { bookFile -> + if (!bookFolderNames.contains(bookFile.name)) { + FileUtils.deleteFile(bookFile.absolutePath) + } + } + } + } + @Synchronized fun saveContent(book: Book, bookChapter: BookChapter, content: String) { if (content.isEmpty()) return diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index fc9505a77..0c02d5abd 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -15,6 +15,7 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.help.AppConfig +import io.legado.app.help.BookHelp import io.legado.app.help.storage.Backup import io.legado.app.lib.theme.ATH import io.legado.app.service.BaseReadAloudService @@ -146,6 +147,11 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } } + override fun onDestroy() { + super.onDestroy() + BookHelp.clearRemovedCache() + } + override fun observeLiveBus() { observeEvent(EventBus.RECREATE) { recreate()