feat: 优化代码

pull/379/head
kunfei 5 years ago
parent 0a247dd28b
commit a889487e5a
  1. 24
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  2. 4
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 2
      app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt
  5. 8
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  6. 10
      app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt

@ -69,11 +69,11 @@ object ReadBook {
nextTextChapter = null
book?.let {
if (curTextChapter == null) {
loadContent(durChapterIndex)
loadContent(durChapterIndex, upContent)
} else if (upContent) {
callBack?.upContent()
}
loadContent(durChapterIndex.plus(1))
loadContent(durChapterIndex.plus(1), upContent)
GlobalScope.launch(Dispatchers.IO) {
for (i in 2..10) {
delay(100)
@ -99,11 +99,11 @@ object ReadBook {
prevTextChapter = null
book?.let {
if (curTextChapter == null) {
loadContent(durChapterIndex)
loadContent(durChapterIndex, upContent)
} else if (upContent) {
callBack?.upContent()
}
loadContent(durChapterIndex.minus(1))
loadContent(durChapterIndex.minus(1), upContent)
GlobalScope.launch(Dispatchers.IO) {
for (i in -5..-2) {
delay(100)
@ -190,13 +190,13 @@ object ReadBook {
loadContent(durChapterIndex - 1)
}
fun loadContent(index: Int) {
fun loadContent(index: Int, upContent: Boolean = true) {
book?.let { book ->
if (addLoading(index)) {
Coroutine.async {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
BookHelp.getContent(book, chapter)?.let {
contentLoadFinish(chapter, it)
contentLoadFinish(chapter, it, upContent)
removeLoading(chapter.index)
} ?: download(chapter)
} ?: removeLoading(index)
@ -262,7 +262,11 @@ object ReadBook {
/**
* 内容加载完成
*/
private fun contentLoadFinish(chapter: BookChapter, content: String) {
private fun contentLoadFinish(
chapter: BookChapter,
content: String,
upContent: Boolean = true
) {
Coroutine.async {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) {
val c = BookHelp.disposeContent(
@ -275,18 +279,18 @@ object ReadBook {
when (chapter.index) {
durChapterIndex -> {
curTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize)
callBack?.upContent()
if (upContent) callBack?.upContent()
callBack?.upView()
curPageChanged()
callBack?.contentLoadFinish()
}
durChapterIndex - 1 -> {
prevTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize)
callBack?.upContent(-1)
if (upContent) callBack?.upContent(-1)
}
durChapterIndex + 1 -> {
nextTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize)
callBack?.upContent(1)
if (upContent) callBack?.upContent(1)
}
}
}

@ -170,13 +170,13 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} else if (!pageFactory.hasNext() && pageOffset < 0) {
pageOffset = 0f
} else if (pageOffset > 0) {
pageFactory.moveToPrev()
pageFactory.moveToPrev(false)
textPage = pageFactory.currentPage
pageOffset -= textPage.height
upView?.invoke(textPage)
} else if (pageOffset < -textPage.height) {
pageOffset += textPage.height
pageFactory.moveToNext()
pageFactory.moveToNext(false)
textPage = pageFactory.currentPage
upView?.invoke(textPage)
}

@ -16,4 +16,6 @@ interface DataSource {
fun hasNextChapter(): Boolean
fun hasPrevChapter(): Boolean
fun upContent(relativePosition: Int = 0)
}

@ -6,9 +6,9 @@ abstract class PageFactory<DATA>(protected val dataSource: DataSource) {
abstract fun moveToLast()
abstract fun moveToNext():Boolean
abstract fun moveToNext(upContent: Boolean): Boolean
abstract fun moveToPrev(): Boolean
abstract fun moveToPrev(upContent: Boolean): Boolean
abstract val nextPage: DATA

@ -76,12 +76,10 @@ class PageView(context: Context, attrs: AttributeSet) :
fun fillPage(direction: PageDelegate.Direction) {
when (direction) {
PageDelegate.Direction.PREV -> {
pageFactory.moveToPrev()
upContent()
pageFactory.moveToPrev(true)
}
PageDelegate.Direction.NEXT -> {
pageFactory.moveToNext()
upContent()
pageFactory.moveToNext(true)
}
else -> Unit
}
@ -100,7 +98,7 @@ class PageView(context: Context, attrs: AttributeSet) :
upContent()
}
fun upContent(relativePosition: Int = 0) {
override fun upContent(relativePosition: Int) {
if (ReadBookConfig.isScroll) {
curPage.setContent(pageFactory.currentPage)
} else {

@ -31,25 +31,27 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
} ?: ReadBook.setPageIndex(0)
}
override fun moveToNext(): Boolean = with(dataSource) {
override fun moveToNext(upContent: Boolean): Boolean = with(dataSource) {
return if (hasNext()) {
if (currentChapter?.isLastIndex(pageIndex) == true) {
ReadBook.moveToNextChapter(false)
ReadBook.moveToNextChapter(upContent)
} else {
ReadBook.setPageIndex(pageIndex.plus(1))
}
if (upContent) upContent()
true
} else
false
}
override fun moveToPrev(): Boolean = with(dataSource) {
override fun moveToPrev(upContent: Boolean): Boolean = with(dataSource) {
return if (hasPrev()) {
if (pageIndex <= 0) {
ReadBook.moveToPrevChapter(false)
ReadBook.moveToPrevChapter(upContent)
} else {
ReadBook.setPageIndex(pageIndex.minus(1))
}
if (upContent) upContent()
true
} else
false

Loading…
Cancel
Save