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()