feat: 优化代码

pull/159/head
kunfei 5 years ago
parent 12a90a4531
commit bbc5b3637b
  1. 2
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  2. 51
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  3. 14
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 14
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  6. 2
      app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt
  7. 6
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  8. 4
      app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt

@ -128,7 +128,7 @@ object Restore {
bodyIndentCount = App.INSTANCE.getPrefInt(PreferKey.bodyIndent, 2)
}
ChapterProvider.upStyle()
ReadBook.loadContent()
ReadBook.loadContent(resetPageOffset = false)
}
withContext(Main) {
if (AppConfig.isNightTheme && AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_YES) {

@ -79,11 +79,11 @@ object ReadBook {
nextTextChapter = null
book?.let {
if (curTextChapter == null) {
loadContent(durChapterIndex, upContent)
loadContent(durChapterIndex, upContent, false)
} else if (upContent) {
callBack?.upContent()
}
loadContent(durChapterIndex.plus(1), upContent)
loadContent(durChapterIndex.plus(1), upContent, false)
GlobalScope.launch(Dispatchers.IO) {
for (i in 2..10) {
delay(100)
@ -109,11 +109,11 @@ object ReadBook {
prevTextChapter = null
book?.let {
if (curTextChapter == null) {
loadContent(durChapterIndex, upContent)
loadContent(durChapterIndex, upContent, false)
} else if (upContent) {
callBack?.upContent()
}
loadContent(durChapterIndex.minus(1), upContent)
loadContent(durChapterIndex.minus(1), upContent, false)
GlobalScope.launch(Dispatchers.IO) {
for (i in -5..-2) {
delay(100)
@ -194,21 +194,21 @@ object ReadBook {
/**
* 加载章节内容
*/
fun loadContent() {
loadContent(durChapterIndex)
loadContent(durChapterIndex + 1)
loadContent(durChapterIndex - 1)
fun loadContent(resetPageOffset: Boolean) {
loadContent(durChapterIndex, resetPageOffset = resetPageOffset)
loadContent(durChapterIndex + 1, resetPageOffset = resetPageOffset)
loadContent(durChapterIndex - 1, resetPageOffset = resetPageOffset)
}
fun loadContent(index: Int, upContent: Boolean = true) {
fun loadContent(index: Int, upContent: Boolean = true, resetPageOffset: Boolean) {
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, upContent)
contentLoadFinish(chapter, it, upContent, resetPageOffset)
removeLoading(chapter.index)
} ?: download(chapter)
} ?: download(chapter, resetPageOffset = resetPageOffset)
} ?: removeLoading(index)
}.onError {
removeLoading(index)
@ -226,7 +226,7 @@ object ReadBook {
if (BookHelp.hasContent(book, chapter)) {
removeLoading(chapter.index)
} else {
download(chapter)
download(chapter, false)
}
} ?: removeLoading(index)
}.onError {
@ -236,20 +236,28 @@ object ReadBook {
}
}
private fun download(chapter: BookChapter) {
private fun download(chapter: BookChapter, resetPageOffset: Boolean) {
book?.let { book ->
webBook?.getContent(book, chapter)
?.onSuccess(Dispatchers.IO) { content ->
if (content.isNullOrEmpty()) {
contentLoadFinish(chapter, App.INSTANCE.getString(R.string.content_empty))
contentLoadFinish(
chapter,
App.INSTANCE.getString(R.string.content_empty),
resetPageOffset = resetPageOffset
)
removeLoading(chapter.index)
} else {
BookHelp.saveContent(book, chapter, content)
contentLoadFinish(chapter, content)
contentLoadFinish(chapter, content, resetPageOffset = resetPageOffset)
removeLoading(chapter.index)
}
}?.onError {
contentLoadFinish(chapter, it.localizedMessage ?: "未知错误")
contentLoadFinish(
chapter,
it.localizedMessage ?: "未知错误",
resetPageOffset = resetPageOffset
)
removeLoading(chapter.index)
}
}
@ -275,7 +283,8 @@ object ReadBook {
private fun contentLoadFinish(
chapter: BookChapter,
content: String,
upContent: Boolean = true
upContent: Boolean = true,
resetPageOffset: Boolean
) {
Coroutine.async {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) {
@ -289,18 +298,18 @@ object ReadBook {
when (chapter.index) {
durChapterIndex -> {
curTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize)
if (upContent) callBack?.upContent()
if (upContent) callBack?.upContent(resetPageOffset = resetPageOffset)
callBack?.upView()
curPageChanged()
callBack?.contentLoadFinish()
}
durChapterIndex - 1 -> {
prevTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize)
if (upContent) callBack?.upContent(-1)
if (upContent) callBack?.upContent(-1, resetPageOffset)
}
durChapterIndex + 1 -> {
nextTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize)
if (upContent) callBack?.upContent(1)
if (upContent) callBack?.upContent(1, resetPageOffset)
}
}
}
@ -326,7 +335,7 @@ object ReadBook {
}
interface CallBack {
fun upContent(relativePosition: Int = 0)
fun upContent(relativePosition: Int = 0, resetPageOffset: Boolean = true)
fun upView()
fun upPageProgress()
fun contentLoadFinish()

@ -116,7 +116,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
ReadBook.loadContent()
ReadBook.loadContent(resetPageOffset = false)
}
override fun onResume() {
@ -482,9 +482,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
/**
* 更新内容
*/
override fun upContent(relativePosition: Int) {
override fun upContent(relativePosition: Int, resetPageOffset: Boolean) {
launch {
page_view.upContent(relativePosition)
page_view.upContent(relativePosition, resetPageOffset)
}
}
@ -580,7 +580,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
override fun onReplaceRuleSave() {
Coroutine.async {
BookHelp.upReplaceRules()
ReadBook.loadContent()
ReadBook.loadContent(resetPageOffset = false)
}
}
@ -709,9 +709,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
page_view.upBg()
page_view.upStyle()
if (it) {
ReadBook.loadContent()
ReadBook.loadContent(resetPageOffset = false)
} else {
page_view.upContent()
page_view.upContent(resetPageOffset = false)
}
}
observeEvent<Int>(EventBus.ALOUD_STATE) {
@ -720,7 +720,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
val page = textChapter.page(ReadBook.durPageIndex)
if (page != null) {
page.removePageAloudSpan()
page_view.upContent()
page_view.upContent(resetPageOffset = false)
}
}
}

@ -54,7 +54,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (ReadBook.durChapterIndex > ReadBook.chapterSize - 1) {
ReadBook.durChapterIndex = ReadBook.chapterSize - 1
}
ReadBook.loadContent()
ReadBook.loadContent(resetPageOffset = true)
}
if (ReadBook.inBookshelf) {
ReadBook.saveRead()
@ -74,7 +74,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
} else {
if (ReadBook.curTextChapter != null) {
ReadBook.callBack?.upContent()
ReadBook.callBack?.upContent(resetPageOffset = false)
}
}
}
@ -107,7 +107,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
App.db.bookChapterDao().insert(*it.toTypedArray())
App.db.bookDao().update(book)
ReadBook.chapterSize = it.size
ReadBook.loadContent()
ReadBook.loadContent(resetPageOffset = true)
}
} else {
ReadBook.webBook?.getChapterList(book, this)
@ -117,7 +117,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
App.db.bookChapterDao().insert(*cList.toTypedArray())
App.db.bookDao().update(book)
ReadBook.chapterSize = cList.size
ReadBook.loadContent()
ReadBook.loadContent(resetPageOffset = true)
} else {
changeDruChapterIndex(cList)
}
@ -191,7 +191,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
App.db.bookDao().update(book)
App.db.bookChapterDao().insert(*chapters.toTypedArray())
ReadBook.chapterSize = chapters.size
ReadBook.loadContent()
ReadBook.loadContent(resetPageOffset = true)
}
}
@ -205,7 +205,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.durPageIndex = pageIndex
}
ReadBook.saveRead()
ReadBook.loadContent()
ReadBook.loadContent(resetPageOffset = true)
}
fun removeFromBookshelf(success: (() -> Unit)?) {
@ -233,7 +233,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
App.db.bookChapterDao().getChapter(book.bookUrl, ReadBook.durChapterIndex)
?.let { chapter ->
BookHelp.delContent(book, chapter)
ReadBook.loadContent(ReadBook.durChapterIndex)
ReadBook.loadContent(ReadBook.durChapterIndex, resetPageOffset = false)
}
}
}

@ -108,7 +108,7 @@ class ContentView(context: Context) : FrameLayout(context) {
}
}
fun setContent(textPage: TextPage, resetPageOffset: Boolean) {
fun setContent(textPage: TextPage, resetPageOffset: Boolean = true) {
setProgress(textPage)
if (resetPageOffset)
resetPageOffset()

@ -17,5 +17,5 @@ interface DataSource {
fun hasPrevChapter(): Boolean
fun upContent(relativePosition: Int = 0)
fun upContent(relativePosition: Int = 0, resetPageOffset: Boolean = true)
}

@ -38,7 +38,7 @@ class PageView(context: Context, attrs: AttributeSet) :
prevPage.x = -w.toFloat()
pageDelegate?.setViewSize(w, h)
if (oldw != 0 && oldh != 0) {
ReadBook.loadContent()
ReadBook.loadContent(resetPageOffset = false)
}
}
@ -93,9 +93,9 @@ class PageView(context: Context, attrs: AttributeSet) :
upContent()
}
override fun upContent(relativePosition: Int) {
override fun upContent(relativePosition: Int, resetPageOffset: Boolean) {
if (ReadBookConfig.isScroll) {
curPage.setContent(pageFactory.currentPage)
curPage.setContent(pageFactory.currentPage, resetPageOffset)
} else {
when (relativePosition) {
-1 -> prevPage.setContent(pageFactory.prevPage)

@ -38,7 +38,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
} else {
ReadBook.setPageIndex(pageIndex.plus(1))
}
if (upContent) upContent()
if (upContent) upContent(resetPageOffset = false)
true
} else
false
@ -51,7 +51,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
} else {
ReadBook.setPageIndex(pageIndex.minus(1))
}
if (upContent) upContent()
if (upContent) upContent(resetPageOffset = false)
true
} else
false

Loading…
Cancel
Save