feat: 优化代码

pull/117/head
kunfei 5 years ago
parent f753bc33e7
commit 854d11b0b0
  1. 14
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  2. 6
      app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt
  3. 9
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  4. 40
      app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt
  5. 7
      app/src/main/java/io/legado/app/ui/book/read/page/content/ContentTextView.kt

@ -95,14 +95,12 @@ class ContentView(context: Context) : FrameLayout(context) {
tv_top_right.text = context.getString(R.string.battery_show, battery)
}
fun setContent(textPage: TextPage?) {
if (textPage != null) {
tv_bottom_left.text = textPage.title
pageSize = textPage.pageSize
setPageIndex(textPage.index)
content_text_view.resetPageOffset()
content_text_view.setContent(textPage)
}
fun setContent(textPage: TextPage) {
tv_bottom_left.text = textPage.title
pageSize = textPage.pageSize
setPageIndex(textPage.index)
content_text_view.resetPageOffset()
content_text_view.setContent(textPage)
}
fun resetPageOffset() {

@ -7,11 +7,11 @@ interface DataSource {
val pageIndex: Int get() = ReadBook.durChapterPos()
fun getCurrentChapter(): TextChapter?
val currentChapter: TextChapter?
fun getNextChapter(): TextChapter?
val nextChapter: TextChapter?
fun getPreviousChapter(): TextChapter?
val prevChapter: TextChapter?
fun hasNextChapter(): Boolean

@ -164,15 +164,18 @@ class PageView(context: Context, attrs: AttributeSet) :
nextPage.upBattery(battery)
}
override fun getCurrentChapter(): TextChapter? {
override val currentChapter: TextChapter?
get() {
return if (callBack.isInitFinish) ReadBook.textChapter(0) else null
}
override fun getNextChapter(): TextChapter? {
override val nextChapter: TextChapter?
get() {
return if (callBack.isInitFinish) ReadBook.textChapter(1) else null
}
override fun getPreviousChapter(): TextChapter? {
override val prevChapter: TextChapter?
get() {
return if (callBack.isInitFinish) ReadBook.textChapter(-1) else null
}

@ -10,7 +10,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
}
override fun hasNext(): Boolean = with(dataSource) {
return hasNextChapter() || getCurrentChapter()?.isLastIndex(pageIndex) != true
return hasNextChapter() || currentChapter?.isLastIndex(pageIndex) != true
}
override fun moveToFirst() {
@ -18,7 +18,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
}
override fun moveToLast() = with(dataSource) {
getCurrentChapter()?.let {
currentChapter?.let {
if (it.pageSize() == 0) {
ReadBook.setPageIndex(0)
} else {
@ -29,7 +29,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override fun moveToNext(): Boolean = with(dataSource) {
return if (hasNext()) {
if (getCurrentChapter()?.isLastIndex(pageIndex) == true) {
if (currentChapter?.isLastIndex(pageIndex) == true) {
ReadBook.moveToNextChapter(false)
} else {
ReadBook.setPageIndex(pageIndex.plus(1))
@ -51,27 +51,43 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
false
}
override val currentPage: TextPage?
override val currentPage: TextPage
get() = with(dataSource) {
return getCurrentChapter()?.page(pageIndex)
currentChapter?.let {
return@with it.page(pageIndex)
?: TextPage(title = it.title).format()
}
return TextPage().format()
}
override val nextPage: TextPage?
override val nextPage: TextPage
get() = with(dataSource) {
getCurrentChapter()?.let {
currentChapter?.let {
if (pageIndex < it.pageSize() - 1) {
return getCurrentChapter()?.page(pageIndex + 1)?.removePageAloudSpan()
return@with it.page(pageIndex + 1)?.removePageAloudSpan()
?: TextPage(title = it.title).format()
}
}
return getNextChapter()?.page(0)?.removePageAloudSpan()
nextChapter?.let {
return@with it.page(0)?.removePageAloudSpan()
?: TextPage(title = it.title).format()
}
return TextPage().format()
}
override val prevPage: TextPage?
override val prevPage: TextPage
get() = with(dataSource) {
if (pageIndex > 0) {
return getCurrentChapter()?.page(pageIndex - 1)?.removePageAloudSpan()
currentChapter?.let {
return@with it.page(pageIndex - 1)?.removePageAloudSpan()
?: TextPage(title = it.title).format()
}
}
prevChapter?.let {
return@with it.lastPage()?.removePageAloudSpan()
?: TextPage(title = it.title).format()
}
return getPreviousChapter()?.lastPage()?.removePageAloudSpan()
return TextPage().format()
}

@ -45,7 +45,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) :
textLine.isReadAloud
)
}
pageFactory.nextPage?.textLines?.forEach { textLine ->
pageFactory.nextPage.textLines.forEach { textLine ->
val yPy = mPageOffset + textPage.height
val lineTop = textLine.lineTop + yPy
val lineBase = textLine.lineBase + yPy
@ -60,6 +60,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) :
textLine.isReadAloud
)
}
}
fun onScroll(mOffset: Float) {
@ -74,13 +75,13 @@ class ContentTextView(context: Context, attrs: AttributeSet?) :
pageOffset += offset
if (pageOffset > 0) {
pageFactory.moveToPrev()
textPage = pageFactory.currentPage ?: TextPage().format()
textPage = pageFactory.currentPage
pageOffset -= textPage.height
upView?.invoke(textPage)
} else if (pageOffset < -textPage.height) {
pageOffset += textPage.height
pageFactory.moveToNext()
textPage = pageFactory.currentPage ?: TextPage().format()
textPage = pageFactory.currentPage
upView?.invoke(textPage)
}
invalidate()

Loading…
Cancel
Save