From 2801aac80e285fa9004567ff3880864f00058263 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 18 Dec 2020 22:40:28 +0800 Subject: [PATCH] =?UTF-8?q?=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 --- .../io/legado/app/data/entities/Bookmark.kt | 3 +- .../io/legado/app/data/entities/RssSource.kt | 1 + .../app/ui/book/read/ReadBookActivity.kt | 9 ++-- .../app/ui/book/read/ReadBookBaseActivity.kt | 5 +- .../app/ui/book/read/page/ContentTextView.kt | 49 +++++++++++------- .../legado/app/ui/book/read/page/PageView.kt | 7 ++- .../legado/app/ui/book/read/page/ReadView.kt | 14 ++--- .../app/ui/book/read/page/api/PageFactory.kt | 8 +-- .../ui/book/read/page/entities/PageData.kt | 6 --- .../ui/book/read/page/entities/TextChapter.kt | 5 +- .../ui/book/read/page/entities/TextPage.kt | 19 +++++++ .../read/page/provider/ChapterProvider.kt | 10 +--- .../read/page/provider/TextPageFactory.kt | 51 ++++++++----------- .../app/ui/book/toc/BookmarkFragment.kt | 2 +- app/src/main/res/menu/book_read.xml | 5 -- .../main/res/menu/content_select_action.xml | 4 ++ 16 files changed, 106 insertions(+), 92 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/ui/book/read/page/entities/PageData.kt diff --git a/app/src/main/java/io/legado/app/data/entities/Bookmark.kt b/app/src/main/java/io/legado/app/data/entities/Bookmark.kt index 7b3d0a280..799bf7146 100644 --- a/app/src/main/java/io/legado/app/data/entities/Bookmark.kt +++ b/app/src/main/java/io/legado/app/data/entities/Bookmark.kt @@ -15,8 +15,9 @@ data class Bookmark( var bookName: String = "", val bookAuthor: String = "", var chapterIndex: Int = 0, - var pageIndex: Int = 0, + var chapterPos: Int = 0, var chapterName: String = "", + var bookText: String = "", var content: String = "" ) : Parcelable \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index ee6d3c5c6..9fe33e039 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -25,6 +25,7 @@ data class RssSource( var sourceGroup: String? = null, var enabled: Boolean = true, var sortUrl: String? = null, + var singleUrl: Boolean = false, var articleStyle: Int = 0, //列表规则 var ruleArticles: String? = null, diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index dfdee0a86..3de4e9447 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -199,7 +199,6 @@ class ReadBookActivity : ReadBookBaseActivity(), } } R.id.menu_download -> showDownloadDialog() - R.id.menu_add_bookmark -> showBookMark() R.id.menu_copy_text -> TextDialog.show(supportFragmentManager, ReadBook.curTextChapter?.getContent()) R.id.menu_update_toc -> ReadBook.book?.let { @@ -430,6 +429,10 @@ class ReadBookActivity : ReadBookBaseActivity(), */ override fun onMenuItemSelected(itemId: Int): Boolean { when (itemId) { + R.id.menu_bookmark -> binding.readView.curPage.let { + showBookMark(0, it.selectedText) + return true + } R.id.menu_replace -> { val scopes = arrayListOf() ReadBook.book?.name?.let { @@ -748,8 +751,8 @@ class ReadBookActivity : ReadBookBaseActivity(), requestCodeChapterList -> data?.getIntExtra("index", ReadBook.durChapterIndex)?.let { index -> if (index != ReadBook.durChapterIndex) { - val pageIndex = data.getIntExtra("pageIndex", 0) - viewModel.openChapter(index, pageIndex) + val chapterPos = data.getIntExtra("chapterPos", 0) + viewModel.openChapter(index, chapterPos) } } requestCodeSearchResult -> diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt index 1df2554ec..34f687ce6 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt @@ -199,7 +199,7 @@ abstract class ReadBookBaseActivity : } @SuppressLint("InflateParams") - fun showBookMark() { + fun showBookMark(chapterPos: Int, bookText: String) { val book = ReadBook.book ?: return val textChapter = ReadBook.curTextChapter ?: return alert(title = getString(R.string.bookmark_add)) { @@ -215,8 +215,9 @@ abstract class ReadBookBaseActivity : bookUrl = book.bookUrl, bookName = book.name, chapterIndex = ReadBook.durChapterIndex, - pageIndex = ReadBook.durChapterPos, + chapterPos = chapterPos, chapterName = textChapter.title, + bookText = bookText, content = editContent ) App.db.bookmarkDao.insert(bookmark) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt index 551da3c8f..0a6244bb7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt @@ -12,7 +12,9 @@ import io.legado.app.constant.PreferKey import io.legado.app.help.ReadBookConfig import io.legado.app.lib.theme.accentColor import io.legado.app.service.help.ReadBook -import io.legado.app.ui.book.read.page.entities.* +import io.legado.app.ui.book.read.page.entities.TextChar +import io.legado.app.ui.book.read.page.entities.TextLine +import io.legado.app.ui.book.read.page.entities.TextPage import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.ui.book.read.page.provider.ImageProvider import io.legado.app.ui.book.read.page.provider.TextPageFactory @@ -39,7 +41,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at private val visibleRect = RectF() private val selectStart = arrayOf(0, 0, 0) private val selectEnd = arrayOf(0, 0, 0) - private var textChapter: TextChapter? = null var textPage: TextPage = TextPage() private set @@ -51,9 +52,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at callBack = activity as CallBack } - fun setContent(pageData: PageData) { - this.textChapter = pageData.textChapter - this.textPage = pageData.textPage + fun setContent(textPage: TextPage) { + this.textPage = textPage invalidate() } @@ -140,9 +140,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at isReadAloud: Boolean, ) { val textPaint = if (isTitle) { - textChapter?.titlePaint ?: ChapterProvider.titlePaint + ChapterProvider.titlePaint } else { - textChapter?.contentPaint ?: ChapterProvider.contentPaint + ChapterProvider.contentPaint } textPaint.color = if (isReadAloud) context.accentColor else ReadBookConfig.textColor @@ -190,14 +190,14 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at pageOffset = min(0, offset) } else if (pageOffset > 0) { pageFactory.moveToPrev(false) - textPage = pageFactory.curData.textPage + textPage = pageFactory.curPage pageOffset -= textPage.height.toInt() upView?.invoke(textPage) contentDescription = textPage.text } else if (pageOffset < -textPage.height) { pageOffset += textPage.height.toInt() pageFactory.moveToNext(false) - textPage = pageFactory.curData.textPage + textPage = pageFactory.curPage upView?.invoke(textPage) contentDescription = textPage.text } @@ -308,15 +308,19 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at } Log.e("y", "$y") for ((lineIndex, textLine) in relativePage(relativePos).textLines.withIndex()) { - if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) { + if (y > textLine.lineTop + relativeOffset + && y < textLine.lineBottom + relativeOffset + ) { Log.e("line", "$relativePos $lineIndex") for ((charIndex, textChar) in textLine.textChars.withIndex()) { if (x > textChar.start && x < textChar.end) { Log.e("char", "$relativePos $lineIndex $charIndex") - if (selectEnd[0] != relativePos || selectEnd[1] != lineIndex || selectEnd[2] != charIndex) { - if (selectToInt(relativePos, lineIndex, charIndex) < selectToInt( - selectStart - ) + if (selectEnd[0] != relativePos + || selectEnd[1] != lineIndex + || selectEnd[2] != charIndex + ) { + if (selectToInt(relativePos, lineIndex, charIndex) + < selectToInt(selectStart) ) { return } @@ -476,6 +480,17 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at return stringBuilder.toString() } + val selectStartPos: Int + get() { + for (i in 0 until selectStart[0]) { + + } + + + + return 0 + } + private fun selectToInt(page: Int, line: Int, char: Int): Int { return page * 10000000 + line * 100000 + char } @@ -488,15 +503,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at return when (relativePos) { 0 -> pageOffset.toFloat() 1 -> pageOffset + textPage.height - else -> pageOffset + textPage.height + pageFactory.nextData.textPage.height + else -> pageOffset + textPage.height + pageFactory.nextPage.height } } private fun relativePage(relativePos: Int): TextPage { return when (relativePos) { 0 -> textPage - 1 -> pageFactory.nextData.textPage - else -> pageFactory.nextPlusData.textPage + 1 -> pageFactory.nextPage + else -> pageFactory.nextPlusPage } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt index dc2919500..e457d5c80 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt @@ -15,7 +15,6 @@ import io.legado.app.databinding.ViewBookPageBinding import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadTipConfig import io.legado.app.service.help.ReadBook -import io.legado.app.ui.book.read.page.entities.PageData import io.legado.app.ui.book.read.page.entities.TextPage import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.ui.widget.BatteryView @@ -211,12 +210,12 @@ class PageView(context: Context) : FrameLayout(context) { } } - fun setContent(pageData: PageData, resetPageOffset: Boolean = true) { - setProgress(pageData.textPage) + fun setContent(textPage: TextPage, resetPageOffset: Boolean = true) { + setProgress(textPage) if (resetPageOffset) { resetPageOffset() } - binding.contentTextView.setContent(pageData) + binding.contentTextView.setContent(textPage) } fun setContentDescription(content: String) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt index 188f67c15..25803ac2a 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt @@ -471,18 +471,18 @@ class ReadView(context: Context, attrs: AttributeSet) : } override fun upContent(relativePosition: Int, resetPageOffset: Boolean) { - curPage.setContentDescription(pageFactory.curData.textPage.text) + curPage.setContentDescription(pageFactory.curPage.text) if (isScroll && !callBack.isAutoPage) { - curPage.setContent(pageFactory.curData, resetPageOffset) + curPage.setContent(pageFactory.curPage, resetPageOffset) } else { curPage.resetPageOffset() when (relativePosition) { - -1 -> prevPage.setContent(pageFactory.prevData) - 1 -> nextPage.setContent(pageFactory.nextData) + -1 -> prevPage.setContent(pageFactory.prevPage) + 1 -> nextPage.setContent(pageFactory.nextPage) else -> { - curPage.setContent(pageFactory.curData) - nextPage.setContent(pageFactory.nextData) - prevPage.setContent(pageFactory.prevData) + curPage.setContent(pageFactory.curPage) + nextPage.setContent(pageFactory.nextPage) + prevPage.setContent(pageFactory.prevPage) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/api/PageFactory.kt b/app/src/main/java/io/legado/app/ui/book/read/page/api/PageFactory.kt index 55cc69471..ecf0e56a7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/api/PageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/api/PageFactory.kt @@ -10,13 +10,13 @@ abstract class PageFactory(protected val dataSource: DataSource) { abstract fun moveToPrev(upContent: Boolean): Boolean - abstract val nextData: DATA + abstract val nextPage: DATA - abstract val prevData: DATA + abstract val prevPage: DATA - abstract val curData: DATA + abstract val curPage: DATA - abstract val nextPlusData: DATA + abstract val nextPlusPage: DATA abstract fun hasNext(): Boolean diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/PageData.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/PageData.kt deleted file mode 100644 index 3d9bec50c..000000000 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/PageData.kt +++ /dev/null @@ -1,6 +0,0 @@ -package io.legado.app.ui.book.read.page.entities - -data class PageData( - val textPage: TextPage, - val textChapter: TextChapter? = null -) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt index bb79a2e67..abb704c94 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt @@ -1,6 +1,5 @@ package io.legado.app.ui.book.read.page.entities -import android.text.TextPaint import kotlin.math.min data class TextChapter( @@ -8,9 +7,7 @@ data class TextChapter( val title: String, val url: String, val pages: List, - val chaptersSize: Int, - var titlePaint: TextPaint? = null, - var contentPaint: TextPaint? = null + val chaptersSize: Int ) { fun page(index: Int): TextPage? { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index 77ee4cc44..afc49711e 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -5,6 +5,7 @@ import android.text.StaticLayout import io.legado.app.App import io.legado.app.R import io.legado.app.help.ReadBookConfig +import io.legado.app.service.help.ReadBook import io.legado.app.ui.book.read.page.provider.ChapterProvider import java.text.DecimalFormat @@ -123,4 +124,22 @@ data class TextPage( return percent } + fun getTextChapter(): TextChapter? { + ReadBook.curTextChapter?.let { + if (it.position == chapterIndex) { + return it + } + } + ReadBook.nextTextChapter?.let { + if (it.position == chapterIndex) { + return it + } + } + ReadBook.prevTextChapter?.let { + if (it.position == chapterIndex) { + return it + } + } + return null + } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 399666b14..ef4e8a0db 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -12,7 +12,6 @@ import io.legado.app.constant.EventBus import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.AppConfig -import io.legado.app.help.BookHelp import io.legado.app.help.ReadBookConfig import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.entities.TextChar @@ -86,11 +85,6 @@ object ChapterProvider { val textPages = arrayListOf() val stringBuilder = StringBuilder() var durY = 0f - var paint = Pair(titlePaint, contentPaint) - BookHelp.getFontPath(book, bookChapter)?.let { - val typeface = getTypeface(it) - paint = getPaint(typeface) - } textPages.add(TextPage()) contents.forEachIndexed { index, text -> val matcher = AppPattern.imgPattern.matcher(text) @@ -105,7 +99,7 @@ object ChapterProvider { } } else { val isTitle = index == 0 - val textPaint = if (isTitle) paint.first else paint.second + val textPaint = if (isTitle) titlePaint else contentPaint if (!(isTitle && ReadBookConfig.titleMode == 2)) { durY = setTypeText(text, durY, textPages, stringBuilder, isTitle, textPaint) } @@ -125,7 +119,7 @@ object ChapterProvider { return TextChapter( bookChapter.index, bookChapter.title, bookChapter.getAbsoluteURL().substringBefore(","), - textPages, chapterSize, paint.first, paint.second + textPages, chapterSize ) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextPageFactory.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextPageFactory.kt index 6a2437871..f824f05bb 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextPageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextPageFactory.kt @@ -3,10 +3,9 @@ package io.legado.app.ui.book.read.page.provider import io.legado.app.service.help.ReadBook import io.legado.app.ui.book.read.page.api.DataSource import io.legado.app.ui.book.read.page.api.PageFactory -import io.legado.app.ui.book.read.page.entities.PageData import io.legado.app.ui.book.read.page.entities.TextPage -class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource) { +class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource) { override fun hasPrev(): Boolean = with(dataSource) { return hasPrevChapter() || pageIndex > 0 @@ -60,81 +59,73 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource false } - override val curData: PageData + override val curPage: TextPage get() = with(dataSource) { ReadBook.msg?.let { - return@with PageData(TextPage(text = it).format()) + return@with TextPage(text = it).format() } currentChapter?.let { - val page = it.page(pageIndex) ?: TextPage(title = it.title).format() - return@with PageData(page, it) + return@with it.page(pageIndex) ?: TextPage(title = it.title).format() } - return PageData(TextPage().format()) + return TextPage().format() } - override val nextData: PageData + override val nextPage: TextPage get() = with(dataSource) { ReadBook.msg?.let { - return@with PageData(TextPage(text = it).format()) + return@with TextPage(text = it).format() } currentChapter?.let { if (pageIndex < it.pageSize - 1) { - val page = it.page(pageIndex + 1)?.removePageAloudSpan() + return@with it.page(pageIndex + 1)?.removePageAloudSpan() ?: TextPage(title = it.title).format() - return@with PageData(page, it) } } if (!hasNextChapter()) { - return@with PageData(TextPage(text = "")) + return@with TextPage(text = "") } nextChapter?.let { - val page = it.page(0)?.removePageAloudSpan() + return@with it.page(0)?.removePageAloudSpan() ?: TextPage(title = it.title).format() - return@with PageData(page, it) } - return PageData(TextPage().format()) + return TextPage().format() } - override val prevData: PageData + override val prevPage: TextPage get() = with(dataSource) { ReadBook.msg?.let { - return@with PageData(TextPage(text = it).format()) + return@with TextPage(text = it).format() } if (pageIndex > 0) { currentChapter?.let { - val page = it.page(pageIndex - 1)?.removePageAloudSpan() + return@with it.page(pageIndex - 1)?.removePageAloudSpan() ?: TextPage(title = it.title).format() - return@with PageData(page, it) } } prevChapter?.let { - val page = it.lastPage?.removePageAloudSpan() + return@with it.lastPage?.removePageAloudSpan() ?: TextPage(title = it.title).format() - return@with PageData(page, it) } - return PageData(TextPage().format()) + return TextPage().format() } - override val nextPlusData: PageData + override val nextPlusPage: TextPage get() = with(dataSource) { currentChapter?.let { if (pageIndex < it.pageSize - 2) { - val page = it.page(pageIndex + 2)?.removePageAloudSpan() + return@with it.page(pageIndex + 2)?.removePageAloudSpan() ?: TextPage(title = it.title).format() - return@with PageData(page, it) } nextChapter?.let { nc -> if (pageIndex < it.pageSize - 1) { - val page = nc.page(0)?.removePageAloudSpan() + return@with nc.page(0)?.removePageAloudSpan() ?: TextPage(title = nc.title).format() - return@with PageData(page, nc) } - val page = nc.page(1)?.removePageAloudSpan() + return@with nc.page(1)?.removePageAloudSpan() ?: TextPage(title = nc.title).format() - return@with PageData(page, nc) } } - return PageData(TextPage().format()) + return TextPage().format() } } diff --git a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt index a54001c66..c4b634ac9 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt @@ -76,7 +76,7 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_ override fun onClick(bookmark: Bookmark) { val bookmarkData = Intent() bookmarkData.putExtra("index", bookmark.chapterIndex) - bookmarkData.putExtra("pageIndex", bookmark.pageIndex) + bookmarkData.putExtra("chapterPos", bookmark.chapterPos) activity?.setResult(Activity.RESULT_OK, bookmarkData) activity?.finish() } diff --git a/app/src/main/res/menu/book_read.xml b/app/src/main/res/menu/book_read.xml index f35b5b901..d7bb23caf 100644 --- a/app/src/main/res/menu/book_read.xml +++ b/app/src/main/res/menu/book_read.xml @@ -44,11 +44,6 @@ - - + +