feat: 优化代码

pull/106/head
kunfei 5 years ago
parent f98d7a3577
commit e9496526c2
  1. 2
      app/src/main/java/io/legado/app/constant/EventBus.kt
  2. 4
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  3. 4
      app/src/main/java/io/legado/app/service/TTSReadAloudService.kt
  4. 24
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt

@ -5,7 +5,7 @@ object EventBus {
const val RECREATE = "RECREATE"
const val UP_BOOK = "sourceDebugLog"
const val ALOUD_STATE = "aloud_state"
const val TTS_START = "ttsStart"
const val TTS_PROGRESS = "ttsStart"
const val TTS_DS = "ttsDs"
const val BATTERY_CHANGED = "batteryChanged"
const val TIME_CHANGED = "timeChanged"

@ -96,7 +96,7 @@ class HttpReadAloudService : BaseReadAloudService(),
mediaPlayer.setDataSource(fd)
mediaPlayer.prepareAsync()
playingIndex = nowSpeak
postEvent(EventBus.TTS_START, readAloudNumber + 1)
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1)
} catch (e: Exception) {
e.printStackTrace()
}
@ -190,7 +190,7 @@ class HttpReadAloudService : BaseReadAloudService(),
ReadBook.moveToNextPage()
}
}
postEvent(EventBus.TTS_START, readAloudNumber + 1)
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1)
}
override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean {

@ -152,7 +152,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
ReadBook.moveToNextPage()
}
}
postEvent(EventBus.TTS_START, readAloudNumber + 1)
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1)
}
override fun onDone(s: String) {
@ -169,7 +169,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
if (readAloudNumber + start > it.getReadLength(pageIndex + 1)) {
pageIndex++
ReadBook.moveToNextPage()
postEvent(EventBus.TTS_START, readAloudNumber + start)
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + start)
}
}
}

@ -501,17 +501,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
override fun observeLiveBus() {
super.observeLiveBus()
observeEvent<Int>(EventBus.ALOUD_STATE) {
if (it == Status.STOP || it == Status.PAUSE) {
ReadBook.curTextChapter?.let { textChapter ->
val page = textChapter.page(ReadBook.durPageIndex)
if (page != null) {
page.removePageAloudSpan()
page_view.upContent()
}
}
}
}
observeEvent<String>(EventBus.TIME_CHANGED) { page_view.upTime() }
observeEvent<Int>(EventBus.BATTERY_CHANGED) { page_view.upBattery(it) }
observeEvent<BookChapter>(EventBus.OPEN_CHAPTER) {
@ -537,7 +526,18 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
page_view.upContent()
}
}
observeEventSticky<Int>(EventBus.TTS_START) { chapterStart ->
observeEvent<Int>(EventBus.ALOUD_STATE) {
if (it == Status.STOP || it == Status.PAUSE) {
ReadBook.curTextChapter?.let { textChapter ->
val page = textChapter.page(ReadBook.durPageIndex)
if (page != null) {
page.removePageAloudSpan()
page_view.upContent()
}
}
}
}
observeEventSticky<Int>(EventBus.TTS_PROGRESS) { chapterStart ->
launch(IO) {
if (BaseReadAloudService.isPlay()) {
ReadBook.curTextChapter?.let { textChapter ->

Loading…
Cancel
Save