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

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() { fun moveToNextPage(): Boolean {
durChapterPos = curTextChapter?.getNextPageLength(durChapterPos) ?: durChapterPos var hasNextPage = false
curTextChapter?.let {
val nextPagePos = it.getNextPageLength(durChapterPos)
if (nextPagePos >= 0) {
hasNextPage = true
durChapterPos = nextPagePos
callBack?.upContent() callBack?.upContent()
saveRead() saveRead()
} }
}
return hasNextPage
}
fun moveToNextChapter(upContent: Boolean): Boolean { fun moveToNextChapter(upContent: Boolean): Boolean {
if (durChapterIndex < chapterSize - 1) { if (durChapterIndex < chapterSize - 1) {

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

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

@ -14,7 +14,6 @@ import io.legado.app.help.config.ReadTipConfig
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
import io.legado.app.ui.book.read.ReadBookActivity 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.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.book.read.page.provider.ChapterProvider
import io.legado.app.ui.widget.BatteryView import io.legado.app.ui.widget.BatteryView
import io.legado.app.utils.activity import io.legado.app.utils.activity
@ -313,8 +312,7 @@ class PageView(context: Context) : FrameLayout(context) {
val textPage get() = binding.contentTextView.textPage val textPage get() = binding.contentTextView.textPage
val selectStart: TextPos get() = binding.contentTextView.selectStart
val selectedText: String get() = binding.contentTextView.getSelectedText() 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) nextPage.upBattery(battery)
} }
/**
* 从选择位置开始朗读
*/
fun aloudStartSelect() {
val selectStartPos = curPage.selectStartPos
if (selectStartPos.relativePagePos > 0) {
ReadBook.moveToNextPage()
}
//TODO 未完成
}
fun getSelectText(): String { fun getSelectText(): String {
return curPage.selectedText return curPage.selectedText
} }

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

Loading…
Cancel
Save