diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 5393249c3..f174a6f5e 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.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) { 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 a05009dc8..68b1601ac 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 @@ -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() 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 6d636f728..6808baf83 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 @@ -116,7 +116,7 @@ class ReadBookActivity : VMBaseActivity(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(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(R.layout.activity_boo override fun onReplaceRuleSave() { Coroutine.async { BookHelp.upReplaceRules() - ReadBook.loadContent() + ReadBook.loadContent(resetPageOffset = false) } } @@ -709,9 +709,9 @@ class ReadBookActivity : VMBaseActivity(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(EventBus.ALOUD_STATE) { @@ -720,7 +720,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo val page = textChapter.page(ReadBook.durPageIndex) if (page != null) { page.removePageAloudSpan() - page_view.upContent() + page_view.upContent(resetPageOffset = false) } } } 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 75021a323..6c6d52aca 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 @@ -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) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt index b748c2b4a..1cc006512 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt @@ -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() diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt b/app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt index 4f7afa04f..3a128f969 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt @@ -17,5 +17,5 @@ interface DataSource { fun hasPrevChapter(): Boolean - fun upContent(relativePosition: Int = 0) + fun upContent(relativePosition: Int = 0, resetPageOffset: Boolean = true) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt index 1b7b71b07..dd19fdc0e 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt @@ -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) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt b/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt index 27a407cd6..a38ed6e51 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt @@ -38,7 +38,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(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(dataSource } else { ReadBook.setPageIndex(pageIndex.minus(1)) } - if (upContent) upContent() + if (upContent) upContent(resetPageOffset = false) true } else false