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

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

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

Loading…
Cancel
Save