diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index f1ad589ad..aa1fbb35a 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -35,6 +35,7 @@ object ReadBook { fun moveToNextChapter(upContent: Boolean): Boolean { if (durChapterIndex < chapterSize - 1) { + durPageIndex = 0 durChapterIndex++ prevTextChapter = curTextChapter curTextChapter = nextTextChapter @@ -61,6 +62,35 @@ object ReadBook { } } + fun moveToPrevChapter(upContent: Boolean, toLast: Boolean): Boolean { + if (durChapterIndex > 0) { + durPageIndex = if (toLast) prevTextChapter?.lastIndex() ?: 0 else 0 + durChapterIndex-- + nextTextChapter = curTextChapter + curTextChapter = prevTextChapter + prevTextChapter = null + book?.let { + if (curTextChapter == null) { + loadContent(durChapterIndex) + } else if (upContent) { + callBack?.upContent() + } + loadContent(durChapterIndex.minus(1)) + GlobalScope.launch(Dispatchers.IO) { + for (i in -5..-2) { + delay(100) + download(durChapterIndex + i) + } + } + } + saveRead() + callBack?.curChapterChanged() + return true + } else { + return false + } + } + fun loadContent(index: Int) { book?.let { book -> if (addLoading(index)) { 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 44715df78..a36b4756e 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 @@ -380,15 +380,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo * 上一页 */ override fun moveToPrevChapter(upContent: Boolean, last: Boolean): Boolean { - return if (ReadBook.durChapterIndex > 0) { - ReadBook.durPageIndex = if (last) ReadBook.prevTextChapter?.lastIndex() ?: 0 else 0 - viewModel.moveToPrevChapter(upContent) - ReadBook.saveRead() - curChapterChanged() - true - } else { - false - } + return ReadBook.moveToPrevChapter(upContent, last) } override fun clickCenter() { diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index a78949c26..6adc7b7c5 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -15,8 +15,6 @@ import io.legado.app.service.help.ReadAloud import io.legado.app.service.help.ReadBook import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class ReadBookViewModel(application: Application) : BaseViewModel(application) { @@ -100,27 +98,6 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } - fun moveToPrevChapter(upContent: Boolean) { - ReadBook.durChapterIndex-- - ReadBook.nextTextChapter = ReadBook.curTextChapter - ReadBook.curTextChapter = ReadBook.prevTextChapter - ReadBook.prevTextChapter = null - ReadBook.book?.let { - if (ReadBook.curTextChapter == null) { - ReadBook.loadContent(ReadBook.durChapterIndex) - } else if (upContent) { - ReadBook.callBack?.upContent() - } - ReadBook.loadContent(ReadBook.durChapterIndex.minus(1)) - launch(IO) { - for (i in -5..-2) { - delay(100) - ReadBook.download(ReadBook.durChapterIndex + i) - } - } - } - } - fun changeTo(book1: Book) { execute { ReadBook.book?.let {