From 4d19aaedb324c651c4d4d048a30a624ee32ac684 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 21 Aug 2019 19:45:18 +0800 Subject: [PATCH] up --- .../app/ui/readbook/ReadBookActivity.kt | 36 ++++++++++--------- .../app/ui/readbook/ReadBookViewModel.kt | 20 +++++++---- .../legado/app/ui/widget/page/DataSource.kt | 2 +- .../io/legado/app/ui/widget/page/PageView.kt | 22 ++++++++---- 4 files changed, 49 insertions(+), 31 deletions(-) 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 319755c21..c0f1e9411 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 @@ -53,7 +53,6 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea initAnimation() initView() viewModel.callBack = this - viewModel.chapterMaxIndex.observe(this, Observer { bookLoadFinish() }) viewModel.bookData.observe(this, Observer { title_bar.title = it.name }) viewModel.initData(intent) savedInstanceState?.let { @@ -235,7 +234,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea } } - private fun bookLoadFinish() { + override fun bookLoadFinish() { viewModel.bookData.value?.let { viewModel.loadContent(it, viewModel.durChapterIndex) viewModel.loadContent(it, viewModel.durChapterIndex + 1) @@ -243,7 +242,13 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea } } - override fun onLoadFinish(bookChapter: BookChapter, content: String) { + override fun loadChapter(index: Int) { + viewModel.bookData.value?.let { + viewModel.loadContent(it, index) + } + } + + override fun contentLoadFinish(bookChapter: BookChapter, content: String) { launch { when (bookChapter.index) { viewModel.durChapterIndex -> { @@ -268,12 +273,16 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea } } + override fun chapterSize(): Int { + return viewModel.chapterSize + } + override fun oldBook(): Book? { return viewModel.bookData.value } override fun changeTo(book: Book) { - + viewModel.changeTo(book) } override fun durChapterIndex(): Int { @@ -296,15 +305,6 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea } } - fun upStyle() { - content_view.upStyle() - page_view.upStyle() - } - - fun upBg() { - page_view.upBg() - } - private fun onClickReadAloud() { if (!ReadAloudService.isRun) { readAloudStatus = Status.STOP @@ -332,10 +332,14 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea observeEventSticky(Bus.UP_CONFIG) { when (it) { 0 -> { - upBg() - upStyle() + page_view.upBg() + content_view.upStyle() + page_view.upStyle() + } + 1 -> { + content_view.upStyle() + page_view.upStyle() } - 1 -> upStyle() } } } 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 380c70988..d44c15b03 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 @@ -2,7 +2,6 @@ package io.legado.app.ui.readbook import android.app.Application import android.content.Intent -import androidx.lifecycle.MediatorLiveData import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.R @@ -20,7 +19,7 @@ import kotlinx.coroutines.Dispatchers.IO class ReadBookViewModel(application: Application) : BaseViewModel(application) { var bookData = MutableLiveData() - var chapterMaxIndex = MediatorLiveData() + var chapterSize = 0 var bookSource: BookSource? = null var webBook: WebBook? = null var callBack: CallBack? = null @@ -52,7 +51,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ?.onSuccess(IO) { cList -> if (!cList.isNullOrEmpty()) { App.db.bookChapterDao().insert(*cList.toTypedArray()) - chapterMaxIndex.postValue(cList.size) + chapterSize = cList.size + callBack?.bookLoadFinish() } else { toast(R.string.error_load_toc) } @@ -63,7 +63,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { if (durChapterIndex > count - 1) { durChapterIndex = count - 1 } - chapterMaxIndex.postValue(count) + chapterSize = count + callBack?.bookLoadFinish() } } @@ -80,7 +81,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { execute { App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> BookHelp.getContent(book, chapter)?.let { - callBack?.onLoadFinish(chapter, it) + callBack?.contentLoadFinish(chapter, it) synchronized(loadingLock) { loadingChapters.remove(index) } @@ -118,7 +119,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ?.onSuccess(IO) { content -> content?.let { BookHelp.saveContent(book, chapter, it) - callBack?.onLoadFinish(chapter, it) + callBack?.contentLoadFinish(chapter, it) synchronized(loadingLock) { loadingChapters.remove(chapter.index) } @@ -130,6 +131,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } + fun changeTo(book: Book) { + + } + private fun autoChangeSource() { } @@ -140,6 +145,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } interface CallBack { - fun onLoadFinish(bookChapter: BookChapter, content: String) + fun bookLoadFinish() + fun contentLoadFinish(bookChapter: BookChapter, content: String) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt b/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt index 76c11a532..2101ed952 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt @@ -6,7 +6,7 @@ interface DataSource { fun isPrepared(): Boolean - fun getChapterPosition() + fun getChapterPosition(): Int fun getChapter(position: Int): TextChapter? diff --git a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt index 640d1578a..c98785fcd 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt @@ -37,8 +37,8 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - override fun getChapterPosition() { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + override fun getChapterPosition(): Int { + return callback?.durChapterIndex() ?: 0 } override fun getChapter(position: Int): TextChapter? { @@ -46,23 +46,29 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att } override fun getCurrentChapter(): TextChapter? { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return callback?.textChapter(0) } override fun getNextChapter(): TextChapter? { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return callback?.textChapter(1) } override fun getPreviousChapter(): TextChapter? { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return callback?.textChapter(-1) } override fun hasNextChapter(): Boolean { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + callback?.let { + return it.durChapterIndex() < it.chapterSize() - 1 + } + return false } override fun hasPrevChapter(): Boolean { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + callback?.let { + return it.durChapterIndex() > 0 + } + return false } })) } @@ -190,8 +196,10 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att } interface CallBack { + fun chapterSize(): Int fun durChapterIndex(): Int fun durChapterPos(pageSize: Int): Int fun textChapter(chapterOnDur: Int = 0): TextChapter? + fun loadChapter(index: Int) } }