From 6f875ac04de59b4de60e7bf80cafb06e8cd36366 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Sat, 15 Mar 2025 22:36:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/model/ReadManga.kt | 52 +++----- .../app/ui/book/manga/ReadMangaActivity.kt | 115 +++++++++--------- .../ui/book/manga/entities/BaseMangaPage.kt | 6 + .../{MangaContent.kt => MangaPage.kt} | 8 +- .../ui/book/manga/entities/ReaderLoading.kt | 7 +- .../book/manga/recyclerview/MangaAdapter.kt | 26 ++-- .../io/legado/app/ui/book/read/MangaMenu.kt | 9 +- 7 files changed, 105 insertions(+), 118 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/manga/entities/BaseMangaPage.kt rename app/src/main/java/io/legado/app/ui/book/manga/entities/{MangaContent.kt => MangaPage.kt} (61%) diff --git a/app/src/main/java/io/legado/app/model/ReadManga.kt b/app/src/main/java/io/legado/app/model/ReadManga.kt index ca56f4b67..89352909b 100644 --- a/app/src/main/java/io/legado/app/model/ReadManga.kt +++ b/app/src/main/java/io/legado/app/model/ReadManga.kt @@ -21,8 +21,8 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.globalExecutor import io.legado.app.model.webBook.WebBook import io.legado.app.ui.book.manga.entities.MangaChapter -import io.legado.app.ui.book.manga.entities.MangaContent import io.legado.app.ui.book.manga.entities.MangaContentData +import io.legado.app.ui.book.manga.entities.MangaPage import io.legado.app.ui.book.manga.entities.ReaderLoading import io.legado.app.utils.mapIndexed import kotlinx.coroutines.CoroutineScope @@ -67,7 +67,6 @@ object ReadManga : CoroutineScope by MainScope() { var rateLimiter = ConcurrentRateLimiter(null) val mangaContents get() = buildContentList() val hasNextChapter get() = durChapterIndex < simulatedChapterSize - 1 - val mSeekParPos = mutableMapOf>() fun resetData(book: Book) { ReadManga.book = book @@ -260,14 +259,18 @@ object ReadManga : CoroutineScope by MainScope() { /** * 加载下一章 */ - fun moveToNextChapter(startLoad: () -> Unit): Boolean { + fun moveToNextChapter(toFirst: Boolean = false): Boolean { if (durChapterIndex < simulatedChapterSize - 1) { + if (toFirst) { + mCallback?.showLoading() + durChapterPos = 0 + } durChapterIndex++ prevMangaChapter = curMangaChapter curMangaChapter = nextMangaChapter nextMangaChapter = null if (curMangaChapter == null) { - startLoad.invoke() + mCallback?.startLoad() loadContent(durChapterIndex) } else { mCallback?.upContent() @@ -283,8 +286,12 @@ object ReadManga : CoroutineScope by MainScope() { } } - fun moveToPrevChapter(): Boolean { + fun moveToPrevChapter(toFirst: Boolean = false): Boolean { if (durChapterIndex > 0) { + if (toFirst) { + mCallback?.showLoading() + durChapterPos = 0 + } durChapterIndex-- nextMangaChapter = curMangaChapter curMangaChapter = prevMangaChapter @@ -561,8 +568,8 @@ object ReadManga : CoroutineScope by MainScope() { private suspend fun getManageChapter(chapter: BookChapter, content: String): MangaChapter { val list = BookHelp.flowImages(chapter, content) .distinctUntilChanged().mapIndexed { index, src -> - MangaContent( - mChapterIndex = chapter.index, + MangaPage( + chapterIndex = chapter.index, chapterSize = chapterSize, mImageUrl = src, index = index, @@ -577,43 +584,18 @@ object ReadManga : CoroutineScope by MainScope() { } val contentList = mutableListOf() - contentList.add(ReaderLoading(chapter.index, "阅读 ${chapter.title}")) + contentList.add(ReaderLoading(chapter.index, -1, "阅读 ${chapter.title}")) contentList.addAll(list) - contentList.add(ReaderLoading(chapter.index, "已读完 ${chapter.title}")) + contentList.add(ReaderLoading(chapter.index, imageCount, "已读完 ${chapter.title}")) return MangaChapter(chapter, contentList, imageCount) } - fun recordMangaPosition(dataList: MutableList) { - Coroutine.async { - var globalPosition = 0 - val mangaList = mutableListOf() - dataList.forEach { - if (it is MangaContent) { - mangaList.add(it) - } - } - dataList.groupBy { if (it is MangaContent) it.mChapterIndex else (it as ReaderLoading).mChapterIndex } - .forEach { (chapterIndex, items) -> - val itemMap = mutableMapOf() - for (i in items.indices) { - val item = items[i] - if (item is MangaContent) { - itemMap[item.index] = globalPosition++ - } else { - globalPosition++ - } - } - mSeekParPos[chapterIndex] = itemMap - } - } - } - - interface Callback { fun upContent() fun loadFail(msg: String) fun sureNewProgress(progress: BookProgress) fun showLoading() + fun startLoad() } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/manga/ReadMangaActivity.kt b/app/src/main/java/io/legado/app/ui/book/manga/ReadMangaActivity.kt index 404bd551c..c7935b5b2 100644 --- a/app/src/main/java/io/legado/app/ui/book/manga/ReadMangaActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/manga/ReadMangaActivity.kt @@ -46,7 +46,8 @@ import io.legado.app.ui.book.manga.config.MangaColorFilterConfig import io.legado.app.ui.book.manga.config.MangaColorFilterDialog import io.legado.app.ui.book.manga.config.MangaFooterConfig import io.legado.app.ui.book.manga.config.MangaFooterSettingDialog -import io.legado.app.ui.book.manga.entities.MangaContent +import io.legado.app.ui.book.manga.entities.BaseMangaPage +import io.legado.app.ui.book.manga.entities.MangaPage import io.legado.app.ui.book.manga.recyclerview.MangaAdapter import io.legado.app.ui.book.read.MangaMenu import io.legado.app.ui.book.read.ReadBookActivity.Companion.RESULT_DELETED @@ -56,6 +57,8 @@ import io.legado.app.ui.widget.recycler.LoadMoreView import io.legado.app.utils.GSON import io.legado.app.utils.NetworkUtils import io.legado.app.utils.StartActivityContract +import io.legado.app.utils.fastBinarySearch +import io.legado.app.utils.findCenterViewPosition import io.legado.app.utils.fromJsonObject import io.legado.app.utils.getCompatColor import io.legado.app.utils.gone @@ -187,13 +190,8 @@ class ReadMangaActivity : VMBaseActivity(EventBus.UP_MANGA_CONFIG) { mMangaFooterConfig = it - upInfoBar( - ReadManga.durChapterIndex, - ReadManga.chapterSize, - ReadManga.durChapterPos, - ReadManga.durChapterImageCount, - ReadManga.curMangaChapter?.chapter?.title ?: "" - ) + val item = mAdapter.getItem(binding.mRecyclerManga.findCenterViewPosition()) + upInfoBar(item) } } @@ -215,26 +213,18 @@ class ReadMangaActivity : VMBaseActivity if (mAdapter.isNotEmpty()) { - val content = mAdapter.getItem(position) - if (content is MangaContent) { - if (ReadManga.durChapterIndex < content.mChapterIndex) { - ReadManga.moveToNextChapter { - loadMoreView.startLoad() - } - } else if (ReadManga.durChapterIndex > content.mChapterIndex) { + val item = mAdapter.getItem(position) + if (item is MangaPage) { + if (ReadManga.durChapterIndex < item.chapterIndex) { + ReadManga.moveToNextChapter() + } else if (ReadManga.durChapterIndex > item.chapterIndex) { ReadManga.moveToPrevChapter() } else { - ReadManga.durChapterPos = content.index + ReadManga.durChapterPos = item.index ReadManga.curPageChanged() } - binding.mangaMenu.upSeekBar(content.index, content.imageCount) - upInfoBar( - content.mChapterIndex, - content.chapterSize, - content.index, - content.imageCount, - content.mChapterName - ) + binding.mangaMenu.upSeekBar(item.index, item.imageCount) + upInfoBar(item) } } } @@ -279,16 +269,9 @@ class ReadMangaActivity : VMBaseActivity { @@ -567,12 +555,6 @@ class ReadMangaActivity : VMBaseActivity -1) { + binding.mRecyclerManga.scrollToPosition(pos) + upInfoBar(mAdapter.getItem(pos)) } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/manga/entities/BaseMangaPage.kt b/app/src/main/java/io/legado/app/ui/book/manga/entities/BaseMangaPage.kt new file mode 100644 index 000000000..60bac9109 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/manga/entities/BaseMangaPage.kt @@ -0,0 +1,6 @@ +package io.legado.app.ui.book.manga.entities + +interface BaseMangaPage { + val chapterIndex: Int + val index: Int +} diff --git a/app/src/main/java/io/legado/app/ui/book/manga/entities/MangaContent.kt b/app/src/main/java/io/legado/app/ui/book/manga/entities/MangaPage.kt similarity index 61% rename from app/src/main/java/io/legado/app/ui/book/manga/entities/MangaContent.kt rename to app/src/main/java/io/legado/app/ui/book/manga/entities/MangaPage.kt index 9d842e672..99ecb4768 100644 --- a/app/src/main/java/io/legado/app/ui/book/manga/entities/MangaContent.kt +++ b/app/src/main/java/io/legado/app/ui/book/manga/entities/MangaPage.kt @@ -1,10 +1,10 @@ package io.legado.app.ui.book.manga.entities -data class MangaContent( - val mChapterIndex: Int = 0,//总章节位置 +data class MangaPage( + override val chapterIndex: Int = 0,//总章节位置 val chapterSize: Int,//总章节数量 val mImageUrl: String = "",//当前URL - val index: Int = 0,//当前章节位置 + override val index: Int = 0,//当前章节位置 var imageCount: Int = 0,//当前章节内容总数 val mChapterName: String = "",//章节名称 -) \ No newline at end of file +) : BaseMangaPage diff --git a/app/src/main/java/io/legado/app/ui/book/manga/entities/ReaderLoading.kt b/app/src/main/java/io/legado/app/ui/book/manga/entities/ReaderLoading.kt index d49dad065..a3fa90b57 100644 --- a/app/src/main/java/io/legado/app/ui/book/manga/entities/ReaderLoading.kt +++ b/app/src/main/java/io/legado/app/ui/book/manga/entities/ReaderLoading.kt @@ -1,7 +1,8 @@ package io.legado.app.ui.book.manga.entities data class ReaderLoading( - val mChapterIndex: Int = 0, + override val chapterIndex: Int = 0, + override val index: Int = 0, val mMessage: String? = null, - var mLoading: Boolean = false, -) +) : BaseMangaPage + diff --git a/app/src/main/java/io/legado/app/ui/book/manga/recyclerview/MangaAdapter.kt b/app/src/main/java/io/legado/app/ui/book/manga/recyclerview/MangaAdapter.kt index d3de4d586..e087f9fd6 100644 --- a/app/src/main/java/io/legado/app/ui/book/manga/recyclerview/MangaAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/manga/recyclerview/MangaAdapter.kt @@ -26,7 +26,7 @@ import io.legado.app.help.glide.progress.ProgressManager import io.legado.app.model.BookCover import io.legado.app.model.ReadManga import io.legado.app.ui.book.manga.config.MangaColorFilterConfig -import io.legado.app.ui.book.manga.entities.MangaContent +import io.legado.app.ui.book.manga.entities.MangaPage import io.legado.app.ui.book.manga.entities.ReaderLoading import io.legado.app.utils.getCompatDrawable import java.util.Collections @@ -49,7 +49,7 @@ class MangaAdapter(private val context: Context) : override fun areItemsTheSame(oldItem: Any, newItem: Any): Boolean { return if (oldItem is ReaderLoading && newItem is ReaderLoading) { newItem.mMessage == oldItem.mMessage - } else if (oldItem is MangaContent && newItem is MangaContent) { + } else if (oldItem is MangaPage && newItem is MangaPage) { oldItem.mImageUrl == newItem.mImageUrl } else false } @@ -57,7 +57,7 @@ class MangaAdapter(private val context: Context) : override fun areContentsTheSame(oldItem: Any, newItem: Any): Boolean { return if (oldItem is ReaderLoading && newItem is ReaderLoading) { oldItem == newItem - } else if (oldItem is MangaContent && newItem is MangaContent) { + } else if (oldItem is MangaPage && newItem is MangaPage) { oldItem == newItem } else false } @@ -67,7 +67,7 @@ class MangaAdapter(private val context: Context) : fun getItem(@IntRange(from = 0) position: Int) = mDiffer.currentList.getOrNull(position) - fun getCurrentList() = mDiffer.currentList + fun getItems() = mDiffer.currentList fun isEmpty() = mDiffer.currentList.isEmpty() @@ -91,7 +91,7 @@ class MangaAdapter(private val context: Context) : ) binding.retry.setOnClickListener { val item = mDiffer.currentList[layoutPosition] - if (item is MangaContent) { + if (item is MangaPage) { loadImageWithRetry( item.mImageUrl, isHorizontal, item.imageCount == 1 ) @@ -99,7 +99,7 @@ class MangaAdapter(private val context: Context) : } } - fun onBind(item: MangaContent) { + fun onBind(item: MangaPage) { setImageColorFilter() loadImageWithRetry(item.mImageUrl, isHorizontal, item.imageCount == 1) } @@ -155,7 +155,7 @@ class MangaAdapter(private val context: Context) : override fun getItemViewType(position: Int): Int { return when { isFooter(position) -> TYPE_FOOTER_VIEW + position - getActualItemCount() - getItem(position) is MangaContent -> CONTENT_VIEW + getItem(position) is MangaPage -> CONTENT_VIEW getItem(position) is ReaderLoading -> LOADING_VIEW else -> error("Unknown view type!") } @@ -182,7 +182,7 @@ class MangaAdapter(private val context: Context) : override fun onBindViewHolder(vh: RecyclerView.ViewHolder, position: Int) { when (vh) { - is PageViewHolder -> vh.onBind(getItem(position) as MangaContent) + is PageViewHolder -> vh.onBind(getItem(position) as MangaPage) is PageMoreViewHolder -> vh.onBind(getItem(position) as ReaderLoading) } } @@ -202,7 +202,7 @@ class MangaAdapter(private val context: Context) : /** * 除去header和footer */ - fun getActualItemCount() = getCurrentList().size + fun getActualItemCount() = getItems().size @Synchronized fun removeFooterView(footer: ((parent: ViewGroup) -> ViewBinding)) { @@ -216,13 +216,13 @@ class MangaAdapter(private val context: Context) : } override fun getPreloadItems(position: Int): MutableList { - if (getCurrentList().isEmpty()) return Collections.emptyList() - if (position >= getCurrentList().size) return Collections.emptyList() - return getCurrentList().subList(position, position + 1) + if (getItems().isEmpty()) return Collections.emptyList() + if (position >= getItems().size) return Collections.emptyList() + return getItems().subList(position, position + 1) } override fun getPreloadRequestBuilder(item: Any): RequestBuilder<*>? { - if (item is MangaContent) { + if (item is MangaPage) { return BookCover.loadManga( context, item.mImageUrl, diff --git a/app/src/main/java/io/legado/app/ui/book/read/MangaMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/MangaMenu.kt index 6bd6cc37c..87592e094 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/MangaMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/MangaMenu.kt @@ -236,15 +236,15 @@ class MangaMenu @JvmOverloads constructor( tvChapterUrl.setOnLongClickListener(chapterViewLongClickListener) tvNext.setOnClickListener { - callBack.moveToTargetIndex(true) + ReadManga.moveToNextChapter(true) } tvPre.setOnClickListener { - callBack.moveToTargetIndex(false) + ReadManga.moveToPrevChapter(true) } seekReadPage.setOnSeekBarChangeListener(object : SeekBarChangeListener { override fun onStopTrackingTouch(seekBar: SeekBar) { - callBack.seekValue(seekBar.progress) + callBack.skipToPage(seekBar.progress) } }) } @@ -259,8 +259,7 @@ class MangaMenu @JvmOverloads constructor( interface CallBack { fun openBookInfoActivity() fun upSystemUiVisibility(menuIsVisible: Boolean) - fun moveToTargetIndex(isNext: Boolean) - fun seekValue(pos: Int) + fun skipToPage(pos: Int) } } \ No newline at end of file