From 12068318dc09aab10ac5ac11d05be0fd5bb913c4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 29 Nov 2019 20:14:39 +0800 Subject: [PATCH] up --- .../app/service/BaseReadAloudService.kt | 4 +- .../io/legado/app/service/help/ReadBook.kt | 30 ++++++++++++++ .../app/ui/book/read/ReadBookActivity.kt | 40 ++++--------------- .../app/ui/book/read/ReadBookViewModel.kt | 1 + 4 files changed, 40 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index ac3994905..a7fdf2e2d 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -36,6 +36,7 @@ abstract class BaseReadAloudService : BaseService(), companion object { var isRun = false var timeMinute: Int = 0 + var pause = false } private val handler = Handler() @@ -45,7 +46,6 @@ abstract class BaseReadAloudService : BaseService(), private var mediaSessionCompat: MediaSessionCompat? = null private var title: String = "" private var subtitle: String = "" - var pause = false val contentList = arrayListOf() var nowSpeak: Int = 0 var readAloudNumber: Int = 0 @@ -128,7 +128,7 @@ abstract class BaseReadAloudService : BaseService(), @CallSuper open fun pauseReadAloud(pause: Boolean) { postEvent(Bus.ALOUD_STATE, Status.PAUSE) - this.pause = pause + BaseReadAloudService.pause = pause upNotification() upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED) } 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 aa1fbb35a..8de4c7ab9 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 @@ -6,8 +6,10 @@ import io.legado.app.R import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp +import io.legado.app.help.IntentDataHelp import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook +import io.legado.app.service.BaseReadAloudService import io.legado.app.ui.book.read.ReadBookViewModel import io.legado.app.ui.widget.page.TextChapter import kotlinx.coroutines.Dispatchers @@ -56,6 +58,7 @@ object ReadBook { } saveRead() callBack?.curChapterChanged() + curPageChanged() return true } else { return false @@ -85,12 +88,39 @@ object ReadBook { } saveRead() callBack?.curChapterChanged() + curPageChanged() return true } else { return false } } + fun curPageChanged() { + callBack?.upPageProgress() + if (BaseReadAloudService.isRun) { + readAloud(!BaseReadAloudService.pause) + } + } + + /** + * 朗读 + */ + fun readAloud(play: Boolean = true) { + val book = book + val textChapter = curTextChapter + if (book != null && textChapter != null) { + val key = IntentDataHelp.putData(textChapter) + ReadAloud.play( + App.INSTANCE, + book.name, + textChapter.title, + durPageIndex, + key, + play + ) + } + } + 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 a36b4756e..89988917c 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 @@ -17,7 +17,6 @@ import io.legado.app.constant.Bus import io.legado.app.constant.Status import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter -import io.legado.app.help.IntentDataHelp import io.legado.app.help.ReadBookConfig import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.noButton @@ -275,9 +274,10 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo .getTextChapter(bookChapter, content, ReadBook.chapterSize) page_view.upContent() curChapterChanged() + ReadBook.curPageChanged() if (intent.getBooleanExtra("readAloud", false)) { intent.removeExtra("readAloud") - readAloud() + ReadBook.readAloud() } } ReadBook.durChapterIndex - 1 -> launch { @@ -308,18 +308,11 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo seek_read_page.max = it.pageSize().minus(1) tv_pre.isEnabled = ReadBook.durChapterIndex != 0 tv_next.isEnabled = ReadBook.durChapterIndex != ReadBook.chapterSize - 1 - curPageChanged() } } - private fun curPageChanged() { + override fun upPageProgress() { seek_read_page.progress = ReadBook.durPageIndex - when (readAloudStatus) { - Status.PLAY -> readAloud() - Status.PAUSE -> { - readAloud(false) - } - } } override fun showMenu() { @@ -354,7 +347,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo override fun setPageIndex(pageIndex: Int) { ReadBook.durPageIndex = pageIndex ReadBook.saveRead() - curPageChanged() + ReadBook.curPageChanged() } /** @@ -402,7 +395,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo override fun skipToPage(page: Int) { ReadBook.durPageIndex = page page_view.upContent() - curPageChanged() + ReadBook.curPageChanged() ReadBook.saveRead() } @@ -440,31 +433,12 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo SystemUtils.ignoreBatteryOptimization(this) } when (readAloudStatus) { - Status.STOP -> readAloud() + Status.STOP -> ReadBook.readAloud() Status.PLAY -> ReadAloud.pause(this) Status.PAUSE -> ReadAloud.resume(this) } } - /** - * 朗读 - */ - private fun readAloud(play: Boolean = true) { - val book = ReadBook.book - val textChapter = ReadBook.curTextChapter - if (book != null && textChapter != null) { - val key = IntentDataHelp.putData(textChapter) - ReadAloud.play( - this, - book.name, - textChapter.title, - ReadBook.durPageIndex, - key, - play - ) - } - } - override fun onColorSelected(dialogId: Int, color: Int) = with(ReadBookConfig.getConfig()) { when (dialogId) { TEXT_COLOR -> { @@ -535,7 +509,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo if (it) { onClickReadAloud() } else { - readAloud(readAloudStatus == Status.PLAY) + ReadBook.readAloud(readAloudStatus == Status.PLAY) } } observeEvent(Bus.UP_CONFIG) { 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 6adc7b7c5..b386812ae 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 @@ -195,5 +195,6 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { fun contentLoadFinish(bookChapter: BookChapter, content: String) fun upContent() fun curChapterChanged() + fun upPageProgress() } } \ No newline at end of file