pull/32/head
kunfei 5 years ago
parent da5e2f9c47
commit 0ab8ea7162
  1. 28
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  2. 30
      app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt

@ -4,6 +4,8 @@ import android.app.Activity
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.view.KeyEvent import android.view.KeyEvent
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
@ -34,7 +36,10 @@ import kotlinx.android.synthetic.main.activity_read_book.*
import kotlinx.android.synthetic.main.view_book_page.* import kotlinx.android.synthetic.main.view_book_page.*
import kotlinx.android.synthetic.main.view_read_menu.* import kotlinx.android.synthetic.main.view_read_menu.*
import kotlinx.android.synthetic.main.view_title_bar.* import kotlinx.android.synthetic.main.view_title_bar.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.anko.* import org.jetbrains.anko.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
@ -415,9 +420,28 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
content_view.upStyle() content_view.upStyle()
page_view.upStyle() page_view.upStyle()
} }
observeEvent<Int>(Bus.TTS_START) { observeEvent<Int>(Bus.TTS_START) { chapterStart ->
launch(IO) {
viewModel.curTextChapter?.let { viewModel.curTextChapter?.let {
val pageStart = chapterStart - it.getReadLength(viewModel.durPageIndex)
val page = it.page(viewModel.durPageIndex)
if (page != null && page.text is SpannableStringBuilder) {
page.text.removeSpan(ChapterProvider.readAloudSpan)
var end = page.text.indexOf("\n", pageStart)
if (end == -1) end = page.text.length - 1
var start = page.text.lastIndexOf("\n", pageStart)
if (start == -1) start = 0
page.text.setSpan(
ChapterProvider.readAloudSpan,
start,
end,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE
)
withContext(Main) {
page_view.upContent()
}
}
}
} }
} }
observeEvent<Boolean>(Bus.TTS_NEXT) { observeEvent<Boolean>(Bus.TTS_NEXT) {

@ -1,7 +1,9 @@
package io.legado.app.ui.widget.page package io.legado.app.ui.widget.page
import android.text.SpannableStringBuilder
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.ui.widget.page.ChapterProvider.readAloudSpan
class TextPageFactory private constructor(dataSource: DataSource) : class TextPageFactory private constructor(dataSource: DataSource) :
PageFactory<TextPage>(dataSource) { PageFactory<TextPage>(dataSource) {
@ -70,16 +72,22 @@ class TextPageFactory private constructor(dataSource: DataSource) :
override fun nextPage(): TextPage? = dataSource.pageIndex().let { index -> override fun nextPage(): TextPage? = dataSource.pageIndex().let { index ->
dataSource.getCurrentChapter()?.let { dataSource.getCurrentChapter()?.let {
if (index < it.pageSize() - 1) { if (index < it.pageSize() - 1) {
return dataSource.getCurrentChapter()?.page(index + 1) return dataSource.getCurrentChapter()?.page(index + 1)?.apply {
?: TextPage( if (text is SpannableStringBuilder) {
text.removeSpan(readAloudSpan)
}
} ?: TextPage(
index + 1, index + 1,
App.INSTANCE.getString(R.string.data_loading), App.INSTANCE.getString(R.string.data_loading),
"index:${index + 1}" "index:${index + 1}"
) )
} }
} }
return dataSource.getNextChapter()?.page(0) return dataSource.getNextChapter()?.page(0)?.apply {
?: TextPage( if (text is SpannableStringBuilder) {
text.removeSpan(readAloudSpan)
}
} ?: TextPage(
index + 1, index + 1,
App.INSTANCE.getString(R.string.data_loading), App.INSTANCE.getString(R.string.data_loading),
"index:${index + 1}" "index:${index + 1}"
@ -88,15 +96,21 @@ class TextPageFactory private constructor(dataSource: DataSource) :
override fun previousPage(): TextPage? = dataSource.pageIndex().let { index -> override fun previousPage(): TextPage? = dataSource.pageIndex().let { index ->
if (index > 0) { if (index > 0) {
return dataSource.getCurrentChapter()?.page(index - 1) return dataSource.getCurrentChapter()?.page(index - 1)?.apply {
?: TextPage( if (text is SpannableStringBuilder) {
text.removeSpan(readAloudSpan)
}
} ?: TextPage(
index - 1, index - 1,
App.INSTANCE.getString(R.string.data_loading), App.INSTANCE.getString(R.string.data_loading),
"index:${index - 1}" "index:${index - 1}"
) )
} }
return dataSource.getPreviousChapter()?.lastPage() return dataSource.getPreviousChapter()?.lastPage()?.apply {
?: TextPage( if (text is SpannableStringBuilder) {
text.removeSpan(readAloudSpan)
}
} ?: TextPage(
index - 1, index - 1,
App.INSTANCE.getString(R.string.data_loading), App.INSTANCE.getString(R.string.data_loading),
"index:${index - 1}" "index:${index - 1}"

Loading…
Cancel
Save