feat: 优化代码

pull/98/head
kunfei 5 years ago
parent f9664f3fe7
commit 56fa86ee0b
  1. 22
      app/src/main/java/io/legado/app/help/BookHelp.kt

@ -28,7 +28,11 @@ object BookHelp {
} }
fun formatChapterName(bookChapter: BookChapter): String { fun formatChapterName(bookChapter: BookChapter): String {
return String.format("%05d-%s", bookChapter.index, MD5Utils.md5Encode16(bookChapter.title)) return String.format(
"%05d-%s.nb",
bookChapter.index,
MD5Utils.md5Encode16(bookChapter.title)
)
} }
fun clearCache() { fun clearCache() {
@ -53,14 +57,14 @@ object BookHelp {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root -> DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root ->
DocumentUtils.createFileIfNotExist( DocumentUtils.createFileIfNotExist(
root, root,
"${formatChapterName(bookChapter)}.nb", formatChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.uri?.writeText(App.INSTANCE, content) )?.uri?.writeText(App.INSTANCE, content)
} }
} else { } else {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
File(downloadPath), File(downloadPath),
"${formatChapterName(bookChapter)}.nb", formatChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).writeText(content) ).writeText(content)
} }
@ -99,7 +103,7 @@ object BookHelp {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root -> DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root ->
return DocumentUtils.exists( return DocumentUtils.exists(
root, root,
"${formatChapterName(bookChapter)}.nb", formatChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
) )
} }
@ -107,7 +111,7 @@ object BookHelp {
else -> { else -> {
return FileUtils.exists( return FileUtils.exists(
File(downloadPath), File(downloadPath),
"${formatChapterName(bookChapter)}.nb", formatChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
) )
} }
@ -125,14 +129,14 @@ object BookHelp {
return DocumentUtils.getDirDocument( return DocumentUtils.getDirDocument(
root, root,
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.findFile("${formatChapterName(bookChapter)}.nb") )?.findFile(formatChapterName(bookChapter))
?.uri?.readText(App.INSTANCE) ?.uri?.readText(App.INSTANCE)
} }
} }
else -> { else -> {
val file = FileUtils.getFile( val file = FileUtils.getFile(
File(downloadPath), File(downloadPath),
"${formatChapterName(bookChapter)}.nb", formatChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
) )
if (file.exists()) { if (file.exists()) {
@ -151,14 +155,14 @@ object BookHelp {
DocumentUtils.getDirDocument( DocumentUtils.getDirDocument(
root, root,
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.findFile("${formatChapterName(bookChapter)}.nb") )?.findFile(formatChapterName(bookChapter))
?.delete() ?.delete()
} }
} }
else -> { else -> {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
File(downloadPath), File(downloadPath),
"${formatChapterName(bookChapter)}.nb", formatChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).delete() ).delete()
} }

Loading…
Cancel
Save