详情页菜单添加清理缓存

pull/352/head
gedoor 4 years ago
parent 659aec8460
commit 917cee33c1
  1. 8
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 2
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt
  3. 2
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  4. 2
      app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
  5. 2
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  6. 1
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  7. 10
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  8. 3
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  9. 10
      app/src/main/java/io/legado/app/utils/FileUtils.kt
  10. 2
      app/src/main/java/io/legado/app/utils/LogUtils.kt
  11. 5
      app/src/main/res/menu/book_info.xml
  12. 5
      app/src/main/res/values-zh-rHK/strings.xml
  13. 3
      app/src/main/res/values-zh-rTW/strings.xml
  14. 3
      app/src/main/res/values-zh/strings.xml
  15. 1
      app/src/main/res/values/strings.xml
  16. 2
      app/src/main/res/xml/pref_config_other.xml

@ -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)
}
/**
* 清楚已删除书的缓存
*/

@ -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<Config> = arrayListOf()
private val defaultConfigs by lazy {
val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes())

@ -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<String, Boolean> by lazy {
val file = FileUtils.createFileIfNotExist(ignoreConfigPath)
val json = file.readText()

@ -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"
)
}

@ -61,8 +61,8 @@ object LocalBook {
author = author,
originName = fileName,
coverUrl = FileUtils.getPath(
App.INSTANCE.externalFilesDir,
"${MD5Utils.md5Encode16(path)}.jpg",
App.INSTANCE.externalFilesDir,
"covers"
)
)

@ -109,6 +109,7 @@ class BookInfoActivity :
toast(R.string.after_add_bookshelf)
}
}
R.id.menu_clear_cache -> viewModel.clearCache()
}
return super.onCompatOptionsItemSelected(item)
}

@ -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 ->

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

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

@ -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 {

@ -19,4 +19,9 @@
android:checkable="true"
app:showAsAction="never" />
<item
android:id="@+id/menu_clear_cache"
android:title="@string/clear_cache"
app:showAsAction="never" />
</menu>

@ -161,8 +161,8 @@
<string name="author">作者</string>
<string name="author_show">作者: %s</string>
<string name="aloud_stop">朗讀停止</string>
<string name="clear_cache">緩存</string>
<string name="clear_cache_success">成功清緩存</string>
<string name="clear_cache">緩存</string>
<string name="clear_cache_success">成功清緩存</string>
<string name="action_save">保存</string>
<string name="edit_source">編輯源</string>
<string name="edit_book_source">編輯書源</string>
@ -322,7 +322,6 @@
<string name="use_to">替換範圍,選填書名或者書源url</string>
<string name="menu_action_group">分組</string>
<string name="download_path">內容緩存路徑</string>
<string name="cleanCache">清理緩存</string>
<string name="sys_file_picker">系統文件選擇器</string>
<string name="new_version">新版本</string>
<string name="download_update">下載更新</string>

@ -161,7 +161,7 @@
<string name="author">作者</string>
<string name="author_show">作者: %s</string>
<string name="aloud_stop">朗讀停止</string>
<string name="clear_cache">快取</string>
<string name="clear_cache">快取</string>
<string name="clear_cache_success">成功清理快取</string>
<string name="action_save">儲存</string>
<string name="edit_source">編輯源</string>
@ -323,7 +323,6 @@
<string name="use_to">取代範圍,選填書名或者書源url</string>
<string name="menu_action_group">分組</string>
<string name="download_path">內容快取路徑</string>
<string name="cleanCache">清理快取</string>
<string name="sys_file_picker">系統檔案選擇器</string>
<string name="new_version">新版本</string>
<string name="download_update">下載更新</string>

@ -162,7 +162,7 @@
<string name="author">作者</string>
<string name="author_show">作者: %s</string>
<string name="aloud_stop">朗读停止</string>
<string name="clear_cache">缓存</string>
<string name="clear_cache">缓存</string>
<string name="clear_cache_success">成功清理缓存</string>
<string name="action_save">保存</string>
<string name="edit_source">编辑源</string>
@ -324,7 +324,6 @@
<string name="use_to">替换范围,选填书名或者书源url</string>
<string name="menu_action_group">分组</string>
<string name="download_path">内容缓存路径</string>
<string name="cleanCache">清理缓存</string>
<string name="sys_file_picker">系统文件选择器</string>
<string name="new_version">新版本</string>
<string name="download_update">下载更新</string>

@ -324,7 +324,6 @@
<string name="use_to">Replacement object. Book name or source url is available</string>
<string name="menu_action_group">Groups</string>
<string name="download_path">Cache path</string>
<string name="cleanCache">Clear cache</string>
<string name="sys_file_picker">System file picker</string>
<string name="new_version">New version</string>
<string name="download_update">Download updates</string>

@ -77,7 +77,7 @@
<io.legado.app.ui.widget.prefs.Preference
android:key="cleanCache"
android:title="@string/cleanCache"
android:title="@string/clear_cache"
android:summary="@string/clear_cache_summary"
app:iconSpaceReserved="false" />

Loading…
Cancel
Save