优化代码

pull/352/head
gedoor 4 years ago
parent 917cee33c1
commit 13934582a4
  1. 47
      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. 27
      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() {
FileUtils.deleteFile(
FileUtils.getPath(root = downloadDir, subDirs = arrayOf(cacheFolderName))
FileUtils.getPath(downloadDir, cacheFolderName)
)
}
fun clearCache(book: Book) {
val filePath = FileUtils.getPath(root = downloadDir,
subDirs = arrayOf(cacheFolderName, book.getFolderName()))
val filePath = FileUtils.getPath(downloadDir, cacheFolderName, book.getFolderName())
FileUtils.deleteFile(filePath)
}
@ -67,8 +66,9 @@ object BookHelp {
//保存文本
FileUtils.createFileIfNotExist(
downloadDir,
cacheFolderName,
book.getFolderName(),
formatChapterName(bookChapter),
subDirs = arrayOf(cacheFolderName, book.getFolderName())
).writeText(content)
//保存图片
content.split("\n").forEach {
@ -78,19 +78,21 @@ object BookHelp {
src = NetworkUtils.getAbsoluteURL(bookChapter.url, src)
src?.let {
saveImage(book, src)
}
}
}
}
}
postEvent(EventBus.SAVE_CONTENT, bookChapter)
}
fun saveImage(book: Book, src:String) {
fun saveImage(book: Book, src: String) {
val analyzeUrl = AnalyzeUrl(src)
analyzeUrl.getImageBytes(book.origin)?.let {
FileUtils.createFileIfNotExist(
downloadDir,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}",
subDirs = arrayOf(cacheFolderName, book.getFolderName(), cacheImageFolderName)
cacheFolderName,
book.getFolderName(),
cacheImageFolderName,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}"
).writeBytes(it)
}
}
@ -98,8 +100,10 @@ object BookHelp {
fun getImage(book: Book, src: String): File {
return FileUtils.getFile(
downloadDir,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}",
subDirs = arrayOf(cacheFolderName, book.getFolderName(), cacheImageFolderName)
cacheFolderName,
book.getFolderName(),
cacheImageFolderName,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}"
)
}
@ -131,8 +135,9 @@ object BookHelp {
} else {
FileUtils.exists(
downloadDir,
formatChapterName(bookChapter),
subDirs = arrayOf(cacheFolderName, book.getFolderName())
cacheFolderName,
book.getFolderName(),
formatChapterName(bookChapter)
)
}
}
@ -143,8 +148,9 @@ object BookHelp {
} else {
val file = FileUtils.getFile(
downloadDir,
formatChapterName(bookChapter),
subDirs = arrayOf(cacheFolderName, book.getFolderName())
cacheFolderName,
book.getFolderName(),
formatChapterName(bookChapter)
)
if (file.exists()) {
return file.readText()
@ -159,8 +165,9 @@ object BookHelp {
} else {
FileUtils.createFileIfNotExist(
downloadDir,
formatChapterName(bookChapter),
subDirs = arrayOf(cacheFolderName, book.getFolderName())
cacheFolderName,
book.getFolderName(),
formatChapterName(bookChapter)
).delete()
}
}
@ -183,7 +190,7 @@ object BookHelp {
fun getDurChapterIndexByChapterTitle(
title: String?,
index: Int,
chapters: List<BookChapter>
chapters: List<BookChapter>,
): Int {
if (title.isNullOrEmpty()) {
return min(index, chapters.lastIndex)
@ -253,7 +260,7 @@ object BookHelp {
name: String,
origin: String?,
content: String,
enableReplace: Boolean
enableReplace: Boolean,
): List<String> {
var c = content
if (enableReplace) {

@ -19,7 +19,7 @@ import java.io.File
@Keep
object ReadBookConfig {
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()
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("restoreIgnore.json", App.INSTANCE.filesDir)
private val ignoreConfigPath = FileUtils.getPath(App.INSTANCE.filesDir, "restoreIgnore.json")
val ignoreConfig: HashMap<String, Boolean> by lazy {
val file = FileUtils.createFileIfNotExist(ignoreConfigPath)
val json = file.readText()

@ -250,7 +250,7 @@ class AnalyzeTxtFile {
val rootFile = App.INSTANCE.getExternalFilesDir(null)
?: App.INSTANCE.externalCacheDir
?: App.INSTANCE.cacheDir
FileUtils.createFolderIfNotExist(rootFile, subDirs = arrayOf(folderName))
FileUtils.createFolderIfNotExist(rootFile, folderName)
}
fun getContent(book: Book, bookChapter: BookChapter): String {
@ -266,7 +266,7 @@ class AnalyzeTxtFile {
private fun getBookFile(book: Book): File {
if (book.bookUrl.isContentPath()) {
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()) {
bookFile.createNewFile()
DocumentUtils.readBytes(App.INSTANCE, uri)?.let {

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

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

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

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

@ -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(root = logFolder, subDirs = arrayOf("appLog"))
val logPath = FileUtils.getPath(root = logFolder, "appLog")
FileHandler(logPath, 10240, 10).apply {
formatter = object : Formatter() {
override fun format(record: LogRecord): String {

Loading…
Cancel
Save