优化代码

pull/352/head
gedoor 4 years ago
parent 917cee33c1
commit 13934582a4
  1. 39
      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. 4
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  5. 4
      app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
  6. 4
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  7. 3
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  8. 23
      app/src/main/java/io/legado/app/utils/FileUtils.kt
  9. 2
      app/src/main/java/io/legado/app/utils/LogUtils.kt

@ -34,13 +34,12 @@ object BookHelp {
fun clearCache() { fun clearCache() {
FileUtils.deleteFile( FileUtils.deleteFile(
FileUtils.getPath(root = downloadDir, subDirs = arrayOf(cacheFolderName)) FileUtils.getPath(downloadDir, cacheFolderName)
) )
} }
fun clearCache(book: Book) { fun clearCache(book: Book) {
val filePath = FileUtils.getPath(root = downloadDir, val filePath = FileUtils.getPath(downloadDir, cacheFolderName, book.getFolderName())
subDirs = arrayOf(cacheFolderName, book.getFolderName()))
FileUtils.deleteFile(filePath) FileUtils.deleteFile(filePath)
} }
@ -67,8 +66,9 @@ object BookHelp {
//保存文本 //保存文本
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
downloadDir, downloadDir,
cacheFolderName,
book.getFolderName(),
formatChapterName(bookChapter), formatChapterName(bookChapter),
subDirs = arrayOf(cacheFolderName, book.getFolderName())
).writeText(content) ).writeText(content)
//保存图片 //保存图片
content.split("\n").forEach { content.split("\n").forEach {
@ -89,8 +89,10 @@ object BookHelp {
analyzeUrl.getImageBytes(book.origin)?.let { analyzeUrl.getImageBytes(book.origin)?.let {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
downloadDir, downloadDir,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}", cacheFolderName,
subDirs = arrayOf(cacheFolderName, book.getFolderName(), cacheImageFolderName) book.getFolderName(),
cacheImageFolderName,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}"
).writeBytes(it) ).writeBytes(it)
} }
} }
@ -98,8 +100,10 @@ object BookHelp {
fun getImage(book: Book, src: String): File { fun getImage(book: Book, src: String): File {
return FileUtils.getFile( return FileUtils.getFile(
downloadDir, downloadDir,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}", cacheFolderName,
subDirs = arrayOf(cacheFolderName, book.getFolderName(), cacheImageFolderName) book.getFolderName(),
cacheImageFolderName,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}"
) )
} }
@ -131,8 +135,9 @@ object BookHelp {
} else { } else {
FileUtils.exists( FileUtils.exists(
downloadDir, downloadDir,
formatChapterName(bookChapter), cacheFolderName,
subDirs = arrayOf(cacheFolderName, book.getFolderName()) book.getFolderName(),
formatChapterName(bookChapter)
) )
} }
} }
@ -143,8 +148,9 @@ object BookHelp {
} else { } else {
val file = FileUtils.getFile( val file = FileUtils.getFile(
downloadDir, downloadDir,
formatChapterName(bookChapter), cacheFolderName,
subDirs = arrayOf(cacheFolderName, book.getFolderName()) book.getFolderName(),
formatChapterName(bookChapter)
) )
if (file.exists()) { if (file.exists()) {
return file.readText() return file.readText()
@ -159,8 +165,9 @@ object BookHelp {
} else { } else {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
downloadDir, downloadDir,
formatChapterName(bookChapter), cacheFolderName,
subDirs = arrayOf(cacheFolderName, book.getFolderName()) book.getFolderName(),
formatChapterName(bookChapter)
).delete() ).delete()
} }
} }
@ -183,7 +190,7 @@ object BookHelp {
fun getDurChapterIndexByChapterTitle( fun getDurChapterIndexByChapterTitle(
title: String?, title: String?,
index: Int, index: Int,
chapters: List<BookChapter> chapters: List<BookChapter>,
): Int { ): Int {
if (title.isNullOrEmpty()) { if (title.isNullOrEmpty()) {
return min(index, chapters.lastIndex) return min(index, chapters.lastIndex)
@ -253,7 +260,7 @@ object BookHelp {
name: String, name: String,
origin: String?, origin: String?,
content: String, content: String,
enableReplace: Boolean enableReplace: Boolean,
): List<String> { ): List<String> {
var c = content var c = content
if (enableReplace) { if (enableReplace) {

@ -19,7 +19,7 @@ import java.io.File
@Keep @Keep
object ReadBookConfig { object ReadBookConfig {
const val readConfigFileName = "readConfig.json" const val readConfigFileName = "readConfig.json"
private val configFilePath = FileUtils.getPath(readConfigFileName, App.INSTANCE.filesDir) private val configFilePath = FileUtils.getPath(App.INSTANCE.filesDir, readConfigFileName)
val configList: ArrayList<Config> = arrayListOf() val configList: ArrayList<Config> = arrayListOf()
private val defaultConfigs by lazy { private val defaultConfigs by lazy {
val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes()) val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes())

@ -27,7 +27,7 @@ import org.jetbrains.anko.toast
import java.io.File import java.io.File
object Restore { object Restore {
private val ignoreConfigPath = FileUtils.getPath("restoreIgnore.json", App.INSTANCE.filesDir) private val ignoreConfigPath = FileUtils.getPath(App.INSTANCE.filesDir, "restoreIgnore.json")
val ignoreConfig: HashMap<String, Boolean> by lazy { val ignoreConfig: HashMap<String, Boolean> by lazy {
val file = FileUtils.createFileIfNotExist(ignoreConfigPath) val file = FileUtils.createFileIfNotExist(ignoreConfigPath)
val json = file.readText() val json = file.readText()

@ -250,7 +250,7 @@ class AnalyzeTxtFile {
val rootFile = App.INSTANCE.getExternalFilesDir(null) val rootFile = App.INSTANCE.getExternalFilesDir(null)
?: App.INSTANCE.externalCacheDir ?: App.INSTANCE.externalCacheDir
?: App.INSTANCE.cacheDir ?: App.INSTANCE.cacheDir
FileUtils.createFolderIfNotExist(rootFile, subDirs = arrayOf(folderName)) FileUtils.createFolderIfNotExist(rootFile, folderName)
} }
fun getContent(book: Book, bookChapter: BookChapter): String { fun getContent(book: Book, bookChapter: BookChapter): String {
@ -266,7 +266,7 @@ class AnalyzeTxtFile {
private fun getBookFile(book: Book): File { private fun getBookFile(book: Book): File {
if (book.bookUrl.isContentPath()) { if (book.bookUrl.isContentPath()) {
val uri = Uri.parse(book.bookUrl) val uri = Uri.parse(book.bookUrl)
val bookFile = FileUtils.getFile(cacheFolder, book.originName, subDirs = arrayOf()) val bookFile = FileUtils.getFile(cacheFolder, book.originName)
if (!bookFile.exists()) { if (!bookFile.exists()) {
bookFile.createNewFile() bookFile.createNewFile()
DocumentUtils.readBytes(App.INSTANCE, uri)?.let { DocumentUtils.readBytes(App.INSTANCE, uri)?.let {

@ -66,9 +66,9 @@ class EPUBFile(val book: io.legado.app.data.entities.Book) {
epubBook = epubReader.readEpub(inputStream) epubBook = epubReader.readEpub(inputStream)
if (book.coverUrl.isNullOrEmpty()) { if (book.coverUrl.isNullOrEmpty()) {
book.coverUrl = FileUtils.getPath( book.coverUrl = FileUtils.getPath(
"${MD5Utils.md5Encode16(book.bookUrl)}.jpg",
App.INSTANCE.externalFilesDir, App.INSTANCE.externalFilesDir,
"covers" "covers",
"${MD5Utils.md5Encode16(book.bookUrl)}.jpg"
) )
} }
if (!File(book.coverUrl!!).exists()) { if (!File(book.coverUrl!!).exists()) {

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

@ -131,8 +131,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
fun postLoad() { fun postLoad() {
execute { execute {
FileUtils.deleteFile(FileUtils.getPath(root = context.cacheDir, FileUtils.deleteFile(FileUtils.getPath(context.cacheDir, "Fonts"))
subDirs = arrayOf("Fonts")))
if (App.db.httpTTSDao().count == 0) { if (App.db.httpTTSDao().count == 0) {
DefaultValueHelp.initHttpTTS() DefaultValueHelp.initHttpTTS()
} }

@ -8,17 +8,17 @@ import java.io.IOException
@Suppress("unused") @Suppress("unused")
object FileUtils { object FileUtils {
fun exists(root: File, fileName: String, vararg subDirs: String): Boolean { fun exists(root: File, vararg subDirFiles: String): Boolean {
return getFile(root, fileName, subDirs = subDirs).exists() return getFile(root, *subDirFiles).exists()
} }
fun createFileIfNotExist(root: File, fileName: String, vararg subDirs: String): File { fun createFileIfNotExist(root: File, vararg subDirFiles: String): File {
val filePath = getPath(fileName, root, *subDirs) val filePath = getPath(root, *subDirFiles)
return createFileIfNotExist(filePath) return createFileIfNotExist(filePath)
} }
fun createFolderIfNotExist(root: File, vararg subDirs: String): File { fun createFolderIfNotExist(root: File, vararg subDirs: String): File {
val filePath = getPath(root = root, subDirs = subDirs) val filePath = getPath(root, *subDirs)
return createFolderIfNotExist(filePath) return createFolderIfNotExist(filePath)
} }
@ -49,23 +49,22 @@ object FileUtils {
return file return file
} }
fun getFile(root: File, fileName: String, vararg subDirs: String): File { fun getFile(root: File, vararg subDirFiles: String): File {
val filePath = getPath(fileName, root, *subDirs) val filePath = getPath(root, *subDirFiles)
return File(filePath) return File(filePath)
} }
fun getDirFile(root: File, vararg subDirs: String): File { fun getDirFile(root: File, vararg subDirs: String): File {
val filePath = getPath(root = root, subDirs = subDirs) val filePath = getPath(root = root, *subDirs)
return File(filePath) return File(filePath)
} }
fun getPath(fileName: String? = null, root: File, vararg subDirs: String): String { fun getPath(root: File, vararg subDirFiles: String): String {
val path = StringBuilder(root.absolutePath) val path = StringBuilder(root.absolutePath)
subDirs.forEach { subDirFiles.forEach {
if (it.isNotEmpty()) {
path.append(File.separator).append(it) path.append(File.separator).append(it)
} }
if (!fileName.isNullOrEmpty()) {
path.append(File.separator).append(fileName)
} }
return path.toString() return path.toString()
} }

@ -32,7 +32,7 @@ object LogUtils {
private val fileHandler by lazy { private val fileHandler by lazy {
val root = App.INSTANCE.externalCacheDir ?: return@lazy null val root = App.INSTANCE.externalCacheDir ?: return@lazy null
val logFolder = FileUtils.createFolderIfNotExist(root, "logs") val logFolder = FileUtils.createFolderIfNotExist(root, "logs")
val logPath = FileUtils.getPath(root = logFolder, subDirs = arrayOf("appLog")) val logPath = FileUtils.getPath(root = logFolder, "appLog")
FileHandler(logPath, 10240, 10).apply { FileHandler(logPath, 10240, 10).apply {
formatter = object : Formatter() { formatter = object : Formatter() {
override fun format(record: LogRecord): String { override fun format(record: LogRecord): String {

Loading…
Cancel
Save