diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt index e78986640..b71102df7 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt @@ -61,7 +61,6 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea page_view.callback = this viewModel.callBack = this viewModel.bookData.observe(this, Observer { title_bar.title = it.name }) - viewModel.chapterListFinish.observe(this, Observer { bookLoadFinish() }) viewModel.initData(intent) savedInstanceState?.let { changeSourceDialog = @@ -225,17 +224,6 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea return super.onKeyDown(keyCode, event) } - /** - * 书籍加载完成,开始加载章节内容 - */ - private fun bookLoadFinish() { - viewModel.bookData.value?.let { - viewModel.loadContent(it, viewModel.durChapterIndex) - viewModel.loadContent(it, viewModel.durChapterIndex + 1) - viewModel.loadContent(it, viewModel.durChapterIndex - 1) - } - } - /** * 加载章节内容 */ @@ -251,19 +239,19 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea override fun contentLoadFinish(bookChapter: BookChapter, content: String) { when (bookChapter.index) { viewModel.durChapterIndex -> launch { - viewModel.curTextChapter = - ChapterProvider.getTextChapter(content_text_view, bookChapter, content) + viewModel.curTextChapter = ChapterProvider + .getTextChapter(content_text_view, bookChapter, content, viewModel.chapterSize) page_view.upContent() curChapterChanged() } viewModel.durChapterIndex - 1 -> launch { - viewModel.prevTextChapter = - ChapterProvider.getTextChapter(content_text_view, bookChapter, content) + viewModel.prevTextChapter = ChapterProvider + .getTextChapter(content_text_view, bookChapter, content, viewModel.chapterSize) page_view.upContent() } viewModel.durChapterIndex + 1 -> launch { - viewModel.nextTextChapter = - ChapterProvider.getTextChapter(content_text_view, bookChapter, content) + viewModel.nextTextChapter = ChapterProvider + .getTextChapter(content_text_view, bookChapter, content, viewModel.chapterSize) page_view.upContent() } } diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt index cd0687b64..5b62ba8f1 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt @@ -21,7 +21,6 @@ import kotlinx.coroutines.launch class ReadBookViewModel(application: Application) : BaseViewModel(application) { var inBookshelf = false var bookData = MutableLiveData() - val chapterListFinish = MutableLiveData() var chapterSize = 0 var bookSource: BookSource? = null var callBack: CallBack? = null @@ -61,7 +60,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { durChapterIndex = count - 1 } chapterSize = count - chapterListFinish.postValue(true) + loadChapterListFinish() } } @@ -85,7 +84,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { if (!cList.isNullOrEmpty()) { App.db.bookChapterDao().insert(*cList.toTypedArray()) chapterSize = cList.size - chapterListFinish.postValue(true) + loadChapterListFinish() } else { toast(R.string.error_load_toc) } @@ -95,6 +94,14 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } + private fun loadChapterListFinish() { + bookData.value?.let { + loadContent(it, durChapterIndex) + loadContent(it, durChapterIndex + 1) + loadContent(it, durChapterIndex - 1) + } + } + fun loadContent(book: Book, index: Int) { synchronized(loadingLock) { if (loadingChapters.contains(index)) return @@ -193,15 +200,19 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { durPageIndex = 0 } saveRead() - chapterListFinish.postValue(true) + loadChapterListFinish() } fun saveRead() { execute { bookData.value?.let { book -> + book.durChapterTime = System.currentTimeMillis() book.durChapterIndex = durChapterIndex book.durChapterPos = durPageIndex - App.db.bookDao().update(book) + curTextChapter?.let { + book.durChapterTitle = it.title + App.db.bookDao().update(book) + } } } } @@ -233,7 +244,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { loadContent(it, durChapterIndex.plus(1)) launch(IO) { for (i in 2..10) { - delay(5000L) + delay(1000L) bookData.value?.let { book -> download(book, durChapterIndex + i) } diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt index a57ac31c5..7351379a6 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt @@ -13,7 +13,10 @@ object ChapterProvider { val readAloudSpan = ForegroundColorSpan(App.INSTANCE.accentColor) private val titleSpan = RelativeSizeSpan(1.3f) - fun getTextChapter(textView: ContentTextView, bookChapter: BookChapter, content: String): TextChapter { + fun getTextChapter( + textView: ContentTextView, bookChapter: BookChapter, + content: String, chapterSize: Int + ): TextChapter { val textPages = arrayListOf() val pageLengths = arrayListOf() var surplusText = content @@ -32,24 +35,26 @@ object ChapterProvider { pageLengths.add(textView.getCharNum()) textPages.add( TextPage( - pageIndex, - spannableStringBuilder.delete( + index = pageIndex, + text = spannableStringBuilder.delete( pageLengths[pageIndex], spannableStringBuilder.length ), - bookChapter.title + title = bookChapter.title, + chapterSize = chapterSize, + chapterIndex = bookChapter.index ) ) surplusText = surplusText.substring(pageLengths[pageIndex]) pageIndex++ } + for (item in textPages) { + item.pageSize = textPages.size + } return TextChapter( - bookChapter.index, - bookChapter.title, - bookChapter.url, - textPages, - pageLengths + bookChapter.index, bookChapter.title, bookChapter.url, + textPages, pageLengths ) } diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt index f566e672f..3201328f7 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt @@ -1,5 +1,6 @@ package io.legado.app.ui.widget.page +import android.annotation.SuppressLint import android.content.Context import android.graphics.drawable.Drawable import android.util.AttributeSet @@ -79,9 +80,10 @@ class ContentView : FrameLayout { tv_top_right.text = context.getString(R.string.battery_show, battery) } + @SuppressLint("SetTextI18n") fun setContent(page: TextPage?) { content_text_view.text = page?.text tv_bottom_left.text = page?.title - tv_bottom_right.text = page?.index?.plus(1)?.toString() + tv_bottom_right.text = "${page?.index?.plus(1)}/${page?.pageSize}" } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/page/TextPage.kt b/app/src/main/java/io/legado/app/ui/widget/page/TextPage.kt index ea261ff2b..503472390 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/TextPage.kt @@ -3,5 +3,8 @@ package io.legado.app.ui.widget.page data class TextPage( val index: Int, val text: CharSequence, - val title: String + val title: String, + var pageSize: Int = 0, + var chapterSize: Int = 0, + var chapterIndex: Int = 0 )