From 917cee33c170721da86bf2d52c902f4e56a840fe Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 31 Aug 2020 10:06:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=A6=E6=83=85=E9=A1=B5=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B8=85=E7=90=86=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/help/BookHelp.kt | 8 +++++++- app/src/main/java/io/legado/app/help/ReadBookConfig.kt | 2 +- .../main/java/io/legado/app/help/storage/Restore.kt | 2 +- .../java/io/legado/app/model/localBook/EPUBFile.kt | 2 +- .../java/io/legado/app/model/localBook/LocalBook.kt | 2 +- .../io/legado/app/ui/book/info/BookInfoActivity.kt | 1 + .../io/legado/app/ui/book/info/BookInfoViewModel.kt | 10 ++++++++++ .../main/java/io/legado/app/ui/main/MainViewModel.kt | 3 ++- app/src/main/java/io/legado/app/utils/FileUtils.kt | 10 +++++----- app/src/main/java/io/legado/app/utils/LogUtils.kt | 2 +- app/src/main/res/menu/book_info.xml | 5 +++++ app/src/main/res/values-zh-rHK/strings.xml | 5 ++--- app/src/main/res/values-zh-rTW/strings.xml | 3 +-- app/src/main/res/values-zh/strings.xml | 3 +-- app/src/main/res/values/strings.xml | 1 - app/src/main/res/xml/pref_config_other.xml | 2 +- 16 files changed, 40 insertions(+), 21 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 94e7fdde1..68b9f37b9 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -34,10 +34,16 @@ object BookHelp { fun clearCache() { FileUtils.deleteFile( - FileUtils.getPath(downloadDir, subDirs = arrayOf(cacheFolderName)) + FileUtils.getPath(root = downloadDir, subDirs = arrayOf(cacheFolderName)) ) } + fun clearCache(book: Book) { + val filePath = FileUtils.getPath(root = downloadDir, + subDirs = arrayOf(cacheFolderName, book.getFolderName())) + FileUtils.deleteFile(filePath) + } + /** * 清楚已删除书的缓存 */ diff --git a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt index c6c71598c..abbc1c2e8 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -19,7 +19,7 @@ import java.io.File @Keep object ReadBookConfig { const val readConfigFileName = "readConfig.json" - private val configFilePath = FileUtils.getPath(App.INSTANCE.filesDir, readConfigFileName) + private val configFilePath = FileUtils.getPath(readConfigFileName, App.INSTANCE.filesDir) val configList: ArrayList = arrayListOf() private val defaultConfigs by lazy { val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes()) diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 5deeac5dd..0dbfeef01 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -27,7 +27,7 @@ import org.jetbrains.anko.toast import java.io.File object Restore { - private val ignoreConfigPath = FileUtils.getPath(App.INSTANCE.filesDir, "restoreIgnore.json") + private val ignoreConfigPath = FileUtils.getPath("restoreIgnore.json", App.INSTANCE.filesDir) val ignoreConfig: HashMap by lazy { val file = FileUtils.createFileIfNotExist(ignoreConfigPath) val json = file.readText() diff --git a/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt b/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt index 1b0150b4a..9047e3fea 100644 --- a/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt @@ -66,8 +66,8 @@ class EPUBFile(val book: io.legado.app.data.entities.Book) { epubBook = epubReader.readEpub(inputStream) if (book.coverUrl.isNullOrEmpty()) { book.coverUrl = FileUtils.getPath( - App.INSTANCE.externalFilesDir, "${MD5Utils.md5Encode16(book.bookUrl)}.jpg", + App.INSTANCE.externalFilesDir, "covers" ) } diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index d4eeee61c..dc53a3f0c 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -61,8 +61,8 @@ object LocalBook { author = author, originName = fileName, coverUrl = FileUtils.getPath( - App.INSTANCE.externalFilesDir, "${MD5Utils.md5Encode16(path)}.jpg", + App.INSTANCE.externalFilesDir, "covers" ) ) diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index 74c9c1acf..011def56a 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -109,6 +109,7 @@ class BookInfoActivity : toast(R.string.after_add_bookshelf) } } + R.id.menu_clear_cache -> viewModel.clearCache() } return super.onCompatOptionsItemSelected(item) } diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index 71f046826..fb2a16bf8 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -227,6 +227,16 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } + fun clearCache() { + execute { + BookHelp.clearCache(bookData.value!!) + }.onSuccess { + toast(R.string.clear_cache_success) + }.onError { + toast(it.stackTraceToString()) + } + } + fun upEditBook() { bookData.value?.let { App.db.bookDao().getBook(it.bookUrl)?.let { book -> diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index c9fc9066a..ef6155fde 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -131,7 +131,8 @@ class MainViewModel(application: Application) : BaseViewModel(application) { fun postLoad() { execute { - FileUtils.deleteFile(FileUtils.getPath(context.cacheDir, "Fonts")) + FileUtils.deleteFile(FileUtils.getPath(root = context.cacheDir, + subDirs = arrayOf("Fonts"))) if (App.db.httpTTSDao().count == 0) { DefaultValueHelp.initHttpTTS() } diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index 047ab0cec..06616b4bd 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -13,12 +13,12 @@ object FileUtils { } fun createFileIfNotExist(root: File, fileName: String, vararg subDirs: String): File { - val filePath = getPath(root, fileName, *subDirs) + val filePath = getPath(fileName, root, *subDirs) return createFileIfNotExist(filePath) } fun createFolderIfNotExist(root: File, vararg subDirs: String): File { - val filePath = getPath(root, subDirs = subDirs) + val filePath = getPath(root = root, subDirs = subDirs) return createFolderIfNotExist(filePath) } @@ -50,16 +50,16 @@ object FileUtils { } fun getFile(root: File, fileName: String, vararg subDirs: String): File { - val filePath = getPath(root, fileName, *subDirs) + val filePath = getPath(fileName, root, *subDirs) return File(filePath) } fun getDirFile(root: File, vararg subDirs: String): File { - val filePath = getPath(root, subDirs = subDirs) + val filePath = getPath(root = root, subDirs = subDirs) return File(filePath) } - fun getPath(root: File, fileName: String? = null, vararg subDirs: String): String { + fun getPath(fileName: String? = null, root: File, vararg subDirs: String): String { val path = StringBuilder(root.absolutePath) subDirs.forEach { path.append(File.separator).append(it) diff --git a/app/src/main/java/io/legado/app/utils/LogUtils.kt b/app/src/main/java/io/legado/app/utils/LogUtils.kt index e70813dee..8a901ac6f 100644 --- a/app/src/main/java/io/legado/app/utils/LogUtils.kt +++ b/app/src/main/java/io/legado/app/utils/LogUtils.kt @@ -32,7 +32,7 @@ object LogUtils { private val fileHandler by lazy { val root = App.INSTANCE.externalCacheDir ?: return@lazy null val logFolder = FileUtils.createFolderIfNotExist(root, "logs") - val logPath = FileUtils.getPath(logFolder, "appLog") + val logPath = FileUtils.getPath(root = logFolder, subDirs = arrayOf("appLog")) FileHandler(logPath, 10240, 10).apply { formatter = object : Formatter() { override fun format(record: LogRecord): String { diff --git a/app/src/main/res/menu/book_info.xml b/app/src/main/res/menu/book_info.xml index fc88821c1..ff060d6bd 100644 --- a/app/src/main/res/menu/book_info.xml +++ b/app/src/main/res/menu/book_info.xml @@ -19,4 +19,9 @@ android:checkable="true" app:showAsAction="never" /> + + \ No newline at end of file diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index aa5898eee..e3b68fb7a 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -161,8 +161,8 @@ 作者 作者: %s 朗讀停止 - 清除緩存 - 成功清除緩存 + 清理緩存 + 成功清理緩存 保存 編輯源 編輯書源 @@ -322,7 +322,6 @@ 替換範圍,選填書名或者書源url 分組 內容緩存路徑 - 清理緩存 系統文件選擇器 新版本 下載更新 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index b3b22c85f..39dc31f24 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -161,7 +161,7 @@ 作者 作者: %s 朗讀停止 - 清除快取 + 清理快取 成功清理快取 儲存 編輯源 @@ -323,7 +323,6 @@ 取代範圍,選填書名或者書源url 分組 內容快取路徑 - 清理快取 系統檔案選擇器 新版本 下載更新 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 84468bbac..ca76085e3 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -162,7 +162,7 @@ 作者 作者: %s 朗读停止 - 清除缓存 + 清理缓存 成功清理缓存 保存 编辑源 @@ -324,7 +324,6 @@ 替换范围,选填书名或者书源url 分组 内容缓存路径 - 清理缓存 系统文件选择器 新版本 下载更新 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 36daca050..3e4bcc8e3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -324,7 +324,6 @@ Replacement object. Book name or source url is available Groups Cache path - Clear cache System file picker New version Download updates diff --git a/app/src/main/res/xml/pref_config_other.xml b/app/src/main/res/xml/pref_config_other.xml index 877de80cc..0e3930546 100644 --- a/app/src/main/res/xml/pref_config_other.xml +++ b/app/src/main/res/xml/pref_config_other.xml @@ -77,7 +77,7 @@