From 8e426101c2dc80386e1c0b3e45b788ceefe003b8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 21 Sep 2019 21:28:09 +0800 Subject: [PATCH] up --- .../legado/app/ui/widget/page/ContentView.kt | 18 +++++++++++++----- .../io/legado/app/ui/widget/page/PageView.kt | 6 +++++- .../legado/app/ui/widget/page/TextChapter.kt | 13 ++++++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt index 441402562..368622cea 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt @@ -26,8 +26,9 @@ import java.util.* class ContentView : FrameLayout { - private var isScroll: Boolean = false var callBack: CallBack? = null + private var isScroll: Boolean = false + private var pageSize: Int = 0 private val bgImage: AppCompatImageView = AppCompatImageView(context) .apply { scaleType = ImageView.ScaleType.CENTER_CROP @@ -112,11 +113,18 @@ class ContentView : FrameLayout { tv_top_right.text = context.getString(R.string.battery_show, battery) } + fun setContent(textPage: TextPage?) { + content_text_view.text = textPage?.text + tv_bottom_left.text = textPage?.title + pageSize = textPage?.pageSize ?: 0 + setPageIndex(textPage?.index) + } + @SuppressLint("SetTextI18n") - fun setContent(page: TextPage?) { - content_text_view.text = page?.text - tv_bottom_left.text = page?.title - tv_bottom_right.text = "${page?.index?.plus(1)}/${page?.pageSize}" + fun setPageIndex(pageIndex: Int?) { + pageIndex?.let { + tv_bottom_right.text = "${pageIndex.plus(1)}/${pageSize}" + } } fun isTextSelected(): Boolean { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt index dd80a278c..2df6e694e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt @@ -210,7 +210,11 @@ class PageView(context: Context, attrs: AttributeSet) : } override fun scrollToLine(line: Int) { - + callback?.textChapter()?.let { + val pageIndex = it.getPageIndex(line) + curPage?.setPageIndex(pageIndex) + callback?.setPageIndex(pageIndex) + } } interface CallBack { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt b/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt index 72377e3cd..d7fbf375d 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt @@ -31,7 +31,7 @@ data class TextChapter( pages.forEach { spannableStringBuilder.append(it.text) } - return TextPage(0, spannableStringBuilder, title, position, chaptersSize) + return TextPage(0, spannableStringBuilder, title, position, pages.size) } return null } @@ -76,5 +76,16 @@ data class TextChapter( } return 0 } + + fun getPageIndex(line: Int): Int { + var lines = 0 + for (pageIndex in pageLines.indices) { + lines += pageLines[pageIndex] + 1 + if (line < lines) { + return pageIndex + } + } + return 0 + } }