From 964c6d4a5261609c209d768e7238bdb88d0254fc Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 12 Feb 2020 19:28:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 18 +++++------ .../app/ui/chapterlist/ChapterListAdapter.kt | 9 ++++-- .../app/ui/chapterlist/ChapterListFragment.kt | 32 +++++++++++++------ 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 94accaa5e..f28077ab8 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.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() } diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt index 53d1f6d86..f70e6beaf 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt @@ -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(context, R.layout.item_chapter_list) { + val cacheFileNames = arrayListOf() + override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList) { 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)) } } } diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt index 3a9ed036d..5dda2fd94 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt @@ -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(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(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?) {