feat: 优化代码

pull/98/head
kunfei 5 years ago
parent 7f128b8365
commit 964c6d4a52
  1. 18
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 9
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt
  3. 32
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt

@ -27,7 +27,7 @@ object BookHelp {
return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl) return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl)
} }
private fun bookChapterName(bookChapter: BookChapter): String { fun formatChapterName(bookChapter: BookChapter): String {
return String.format("%05d-%s", bookChapter.index, MD5Utils.md5Encode16(bookChapter.title)) return String.format("%05d-%s", bookChapter.index, MD5Utils.md5Encode16(bookChapter.title))
} }
@ -53,14 +53,14 @@ object BookHelp {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root -> DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root ->
DocumentUtils.createFileIfNotExist( DocumentUtils.createFileIfNotExist(
root, root,
"${bookChapterName(bookChapter)}.nb", "${formatChapterName(bookChapter)}.nb",
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),
"${bookChapterName(bookChapter)}.nb", "${formatChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).writeText(content) ).writeText(content)
} }
@ -99,7 +99,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,
"${bookChapterName(bookChapter)}.nb", "${formatChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
) )
} }
@ -107,7 +107,7 @@ object BookHelp {
else -> { else -> {
return FileUtils.exists( return FileUtils.exists(
File(downloadPath), File(downloadPath),
"${bookChapterName(bookChapter)}.nb", "${formatChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
) )
} }
@ -125,14 +125,14 @@ object BookHelp {
return DocumentUtils.getDirDocument( return DocumentUtils.getDirDocument(
root, root,
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.findFile("${bookChapterName(bookChapter)}.nb") )?.findFile("${formatChapterName(bookChapter)}.nb")
?.uri?.readText(App.INSTANCE) ?.uri?.readText(App.INSTANCE)
} }
} }
else -> { else -> {
val file = FileUtils.getFile( val file = FileUtils.getFile(
File(downloadPath), File(downloadPath),
"${bookChapterName(bookChapter)}.nb", "${formatChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
) )
if (file.exists()) { if (file.exists()) {
@ -151,14 +151,14 @@ object BookHelp {
DocumentUtils.getDirDocument( DocumentUtils.getDirDocument(
root, root,
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.findFile("${bookChapterName(bookChapter)}.nb") )?.findFile("${formatChapterName(bookChapter)}.nb")
?.delete() ?.delete()
} }
} }
else -> { else -> {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
File(downloadPath), File(downloadPath),
"${bookChapterName(bookChapter)}.nb", "${formatChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).delete() ).delete()
} }

@ -6,6 +6,7 @@ import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.utils.getCompatColor import io.legado.app.utils.getCompatColor
import io.legado.app.utils.visible import io.legado.app.utils.visible
@ -16,6 +17,8 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class ChapterListAdapter(context: Context, val callback: Callback) : class ChapterListAdapter(context: Context, val callback: Callback) :
SimpleRecyclerAdapter<BookChapter>(context, R.layout.item_chapter_list) { SimpleRecyclerAdapter<BookChapter>(context, R.layout.item_chapter_list) {
val cacheFileNames = arrayListOf<String>()
override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
if (payloads.isEmpty()) { if (payloads.isEmpty()) {
@ -32,9 +35,11 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
this.onClick { this.onClick {
callback.openChapter(item) callback.openChapter(item)
} }
tv_chapter_name.paint.isFakeBoldText =
cacheFileNames.contains(BookHelp.formatChapterName(item))
} else { } else {
val hasContent = payloads[0] as Boolean tv_chapter_name.paint.isFakeBoldText =
tv_chapter_name.paint.isFakeBoldText = hasContent cacheFileNames.contains(BookHelp.formatChapterName(item))
} }
} }
} }

@ -11,12 +11,14 @@ import io.legado.app.R
import io.legado.app.base.VMBaseFragment import io.legado.app.base.VMBaseFragment
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import io.legado.app.ui.widget.recycler.UpLinearLayoutManager import io.legado.app.ui.widget.recycler.UpLinearLayoutManager
import io.legado.app.utils.getVerticalDivider import io.legado.app.utils.getVerticalDivider
import io.legado.app.utils.getViewModelOfActivity import io.legado.app.utils.getViewModelOfActivity
import kotlinx.android.synthetic.main.fragment_chapter_list.* import kotlinx.android.synthetic.main.fragment_chapter_list.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
@ -49,17 +51,31 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
recycler_view.adapter = adapter recycler_view.adapter = adapter
} }
private fun initView() {
ll_chapter_base_info.setBackgroundColor(backgroundColor)
iv_chapter_top.onClick { mLayoutManager.scrollToPositionWithOffset(0, 0) }
iv_chapter_bottom.onClick {
if (adapter.itemCount > 0) {
mLayoutManager.scrollToPositionWithOffset(adapter.itemCount - 1, 0)
}
}
tv_current_chapter_info.onClick {
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
}
}
private fun initBook() { private fun initBook() {
launch { launch {
withContext(IO) { withContext(IO) {
book = App.db.bookDao().getBook(viewModel.bookUrl) book = App.db.bookDao().getBook(viewModel.bookUrl)
} }
initDoc()
book?.let { book?.let {
durChapterIndex = it.durChapterIndex durChapterIndex = it.durChapterIndex
tv_current_chapter_info.text = it.durChapterTitle tv_current_chapter_info.text = it.durChapterTitle
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
initCatchFileNames(it)
} }
initDoc()
} }
} }
@ -72,17 +88,13 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
}) })
} }
private fun initView() { private fun initCatchFileNames(book: Book) {
ll_chapter_base_info.setBackgroundColor(backgroundColor) launch(IO) {
iv_chapter_top.onClick { mLayoutManager.scrollToPositionWithOffset(0, 0) } adapter.cacheFileNames.addAll(BookHelp.getChapterFiles(book))
iv_chapter_bottom.onClick { withContext(Main) {
if (adapter.itemCount > 0) { adapter.notifyItemRangeChanged(0, adapter.getActualItemCount(), true)
mLayoutManager.scrollToPositionWithOffset(adapter.itemCount - 1, 0)
} }
} }
tv_current_chapter_info.onClick {
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
}
} }
override fun startChapterListSearch(newText: String?) { override fun startChapterListSearch(newText: String?) {

Loading…
Cancel
Save