From 0ab8ea716243380cc1206a622c9c5e815e9bc4c4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 26 Aug 2019 07:07:22 +0800 Subject: [PATCH] up --- .../app/ui/readbook/ReadBookActivity.kt | 30 ++++++++++-- .../app/ui/widget/page/TextPageFactory.kt | 48 ++++++++++++------- 2 files changed, 58 insertions(+), 20 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 762a02ac0..248400518 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 @@ -4,6 +4,8 @@ import android.app.Activity import android.content.Intent import android.net.Uri import android.os.Bundle +import android.text.Spannable +import android.text.SpannableStringBuilder import android.view.KeyEvent import android.view.Menu 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_read_menu.* 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.withContext import org.jetbrains.anko.* import org.jetbrains.anko.sdk27.listeners.onClick @@ -415,9 +420,28 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea content_view.upStyle() page_view.upStyle() } - observeEvent(Bus.TTS_START) { - viewModel.curTextChapter?.let { - + observeEvent(Bus.TTS_START) { chapterStart -> + launch(IO) { + 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(Bus.TTS_NEXT) { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt b/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt index ba5ce5153..d5d3414e1 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt @@ -1,7 +1,9 @@ package io.legado.app.ui.widget.page +import android.text.SpannableStringBuilder import io.legado.app.App import io.legado.app.R +import io.legado.app.ui.widget.page.ChapterProvider.readAloudSpan class TextPageFactory private constructor(dataSource: DataSource) : PageFactory(dataSource) { @@ -70,16 +72,22 @@ class TextPageFactory private constructor(dataSource: DataSource) : override fun nextPage(): TextPage? = dataSource.pageIndex().let { index -> dataSource.getCurrentChapter()?.let { if (index < it.pageSize() - 1) { - return dataSource.getCurrentChapter()?.page(index + 1) - ?: TextPage( - index + 1, - App.INSTANCE.getString(R.string.data_loading), - "index:${index + 1}" - ) + return dataSource.getCurrentChapter()?.page(index + 1)?.apply { + if (text is SpannableStringBuilder) { + text.removeSpan(readAloudSpan) + } + } ?: TextPage( + index + 1, + App.INSTANCE.getString(R.string.data_loading), + "index:${index + 1}" + ) } } - return dataSource.getNextChapter()?.page(0) - ?: TextPage( + return dataSource.getNextChapter()?.page(0)?.apply { + if (text is SpannableStringBuilder) { + text.removeSpan(readAloudSpan) + } + } ?: TextPage( index + 1, App.INSTANCE.getString(R.string.data_loading), "index:${index + 1}" @@ -88,19 +96,25 @@ class TextPageFactory private constructor(dataSource: DataSource) : override fun previousPage(): TextPage? = dataSource.pageIndex().let { index -> if (index > 0) { - return dataSource.getCurrentChapter()?.page(index - 1) - ?: TextPage( - index - 1, - App.INSTANCE.getString(R.string.data_loading), - "index:${index - 1}" - ) - } - return dataSource.getPreviousChapter()?.lastPage() - ?: TextPage( + return dataSource.getCurrentChapter()?.page(index - 1)?.apply { + if (text is SpannableStringBuilder) { + text.removeSpan(readAloudSpan) + } + } ?: TextPage( index - 1, App.INSTANCE.getString(R.string.data_loading), "index:${index - 1}" ) + } + return dataSource.getPreviousChapter()?.lastPage()?.apply { + if (text is SpannableStringBuilder) { + text.removeSpan(readAloudSpan) + } + } ?: TextPage( + index - 1, + App.INSTANCE.getString(R.string.data_loading), + "index:${index - 1}" + ) }