退出软件时清除已删除书籍缓存

pull/306/head
gedoor 4 years ago
parent 4899df03e9
commit cd802bad50
  1. 24
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 6
      app/src/main/java/io/legado/app/ui/main/MainActivity.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<String>()
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

@ -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<MainViewModel>(R.layout.activity_main),
}
}
override fun onDestroy() {
super.onDestroy()
BookHelp.clearRemovedCache()
}
override fun observeLiveBus() {
observeEvent<String>(EventBus.RECREATE) {
recreate()

Loading…
Cancel
Save