从文字选择处开始朗读,未完成

pull/2160/head
kunfei 2 years ago
parent ae9b1c92e9
commit 2a198aed8f
  1. 12
      app/src/main/java/io/legado/app/model/ReadBook.kt
  2. 16
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  5. 11
      app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt
  6. 26
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt

@ -131,11 +131,19 @@ object ReadBook : CoroutineScope by MainScope() {
}
}
fun moveToNextPage() {
durChapterPos = curTextChapter?.getNextPageLength(durChapterPos) ?: durChapterPos
fun moveToNextPage(): Boolean {
var hasNextPage = false
curTextChapter?.let {
val nextPagePos = it.getNextPageLength(durChapterPos)
if (nextPagePos >= 0) {
hasNextPage = true
durChapterPos = nextPagePos
callBack?.upContent()
saveRead()
}
}
return hasNextPage
}
fun moveToNextChapter(upContent: Boolean): Boolean {
if (durChapterIndex < chapterSize - 1) {

@ -100,13 +100,11 @@ abstract class BaseReadAloudService : BaseService(),
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
IntentAction.play -> {
textChapter = ReadBook.curTextChapter
pageIndex = intent.getIntExtra("pageIndex", ReadBook.durPageIndex)
newReadAloud(
intent.getBooleanExtra("play", true)
IntentAction.play -> newReadAloud(
intent.getBooleanExtra("play", true),
intent.getIntExtra("pageIndex", ReadBook.durPageIndex),
intent.getIntExtra("startPos", 0)
)
}
IntentAction.pause -> pauseReadAloud(true)
IntentAction.resume -> resumeReadAloud()
IntentAction.upTtsSpeechRate -> upSpeechRate(true)
@ -119,11 +117,11 @@ abstract class BaseReadAloudService : BaseService(),
return super.onStartCommand(intent, flags, startId)
}
@CallSuper
open fun newReadAloud(play: Boolean) {
private fun newReadAloud(play: Boolean, pageIndex: Int, startPos: Int) {
textChapter = ReadBook.curTextChapter
textChapter?.let { textChapter ->
nowSpeak = 0
readAloudNumber = textChapter.getReadLength(pageIndex)
readAloudNumber = textChapter.getReadLength(pageIndex) + startPos
contentList.clear()
if (getPrefBoolean(PreferKey.readAloudByPage)) {
for (index in pageIndex..textChapter.lastIndex) {

@ -616,9 +616,7 @@ class ReadBookActivity : BaseReadBookActivity(),
*/
override fun onMenuItemSelected(itemId: Int): Boolean {
when (itemId) {
R.id.menu_aloud -> {
ReadBook.readAloud()
}
R.id.menu_aloud -> binding.readView.aloudStartSelect()
R.id.menu_bookmark -> binding.readView.curPage.let {
val bookmark = it.createBookmark()
if (bookmark == null) {

@ -14,7 +14,6 @@ import io.legado.app.help.config.ReadTipConfig
import io.legado.app.model.ReadBook
import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.ui.book.read.page.entities.TextPos
import io.legado.app.ui.book.read.page.provider.ChapterProvider
import io.legado.app.ui.widget.BatteryView
import io.legado.app.utils.activity
@ -313,8 +312,7 @@ class PageView(context: Context) : FrameLayout(context) {
val textPage get() = binding.contentTextView.textPage
val selectStart: TextPos get() = binding.contentTextView.selectStart
val selectedText: String get() = binding.contentTextView.getSelectedText()
val selectStartPos get() = binding.contentTextView.selectStart
}

@ -531,6 +531,17 @@ class ReadView(context: Context, attrs: AttributeSet) :
nextPage.upBattery(battery)
}
/**
* 从选择位置开始朗读
*/
fun aloudStartSelect() {
val selectStartPos = curPage.selectStartPos
if (selectStartPos.relativePagePos > 0) {
ReadBook.moveToNextPage()
}
//TODO 未完成
}
fun getSelectText(): String {
return curPage.selectedText
}

@ -28,10 +28,18 @@ data class TextChapter(
val pageSize: Int get() = pages.size
/**
* @param index 页数
* @return 是否是最后一页
*/
fun isLastIndex(index: Int): Boolean {
return index >= pages.size - 1
}
/**
* @param pageIndex 页数
* @return 已读长度
*/
fun getReadLength(pageIndex: Int): Int {
var length = 0
val maxIndex = min(pageIndex, pages.size)
@ -41,10 +49,21 @@ data class TextChapter(
return length
}
/**
* @param length 当前页面文字在章节中的位置
* @return 下一页位置,如果没有下一页返回-1
*/
fun getNextPageLength(length: Int): Int {
return getReadLength(getPageIndexByCharIndex(length) + 1)
val pageIndex = getPageIndexByCharIndex(length)
if (pageIndex + 1 >= pageSize) {
return -1
}
return getReadLength(pageIndex + 1)
}
/**
* @return 获取未读文字
*/
fun getUnRead(pageIndex: Int): String {
val stringBuilder = StringBuilder()
if (pages.isNotEmpty()) {
@ -55,6 +74,9 @@ data class TextChapter(
return stringBuilder.toString()
}
/**
* 获取内容
*/
fun getContent(): String {
val stringBuilder = StringBuilder()
pages.forEach {
@ -64,7 +86,7 @@ data class TextChapter(
}
/**
* 根据索引位置获取所在页
* @return 根据索引位置获取所在页
*/
fun getPageIndexByCharIndex(charIndex: Int): Int {
var length = 0

Loading…
Cancel
Save