优化代码

pull/743/head
gedoor 4 years ago
parent 156864b83d
commit 2801aac80e
  1. 3
      app/src/main/java/io/legado/app/data/entities/Bookmark.kt
  2. 1
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  3. 9
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 5
      app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt
  5. 49
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  6. 7
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  7. 14
      app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt
  8. 8
      app/src/main/java/io/legado/app/ui/book/read/page/api/PageFactory.kt
  9. 6
      app/src/main/java/io/legado/app/ui/book/read/page/entities/PageData.kt
  10. 5
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt
  11. 19
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt
  12. 10
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
  13. 51
      app/src/main/java/io/legado/app/ui/book/read/page/provider/TextPageFactory.kt
  14. 2
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt
  15. 5
      app/src/main/res/menu/book_read.xml
  16. 4
      app/src/main/res/menu/content_select_action.xml

@ -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

@ -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,

@ -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<String>()
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 ->

@ -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)

@ -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
}
}

@ -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) {

@ -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)
}
}
}

@ -10,13 +10,13 @@ abstract class PageFactory<DATA>(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

@ -1,6 +0,0 @@
package io.legado.app.ui.book.read.page.entities
data class PageData(
val textPage: TextPage,
val textChapter: TextChapter? = null
)

@ -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<TextPage>,
val chaptersSize: Int,
var titlePaint: TextPaint? = null,
var contentPaint: TextPaint? = null
val chaptersSize: Int
) {
fun page(index: Int): TextPage? {

@ -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
}
}

@ -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<TextPage>()
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
)
}

@ -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<PageData>(dataSource) {
class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource) {
override fun hasPrev(): Boolean = with(dataSource) {
return hasPrevChapter() || pageIndex > 0
@ -60,81 +59,73 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<PageData>(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()
}
}

@ -76,7 +76,7 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(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()
}

@ -44,11 +44,6 @@
</group>
<item
android:id="@+id/menu_add_bookmark"
android:title="@string/bookmark_add"
app:showAsAction="never" />
<group android:id="@+id/menu_group_login">
<item
android:id="@+id/menu_login"

@ -9,6 +9,10 @@
android:id="@+id/menu_copy"
android:title="@android:string/copy" />
<item
android:id="@+id/menu_bookmark"
android:title="@string/bookmark" />
<item
android:id="@+id/menu_aloud"
android:title="@string/read_aloud" />

Loading…
Cancel
Save