This commit is contained in:
Horis 2023-08-21 15:22:00 +08:00
parent 3ead458891
commit 4e044df3b2
8 changed files with 84 additions and 44 deletions

View File

@ -11,6 +11,7 @@ object IntentAction {
const val prevParagraph = "prevParagraph" const val prevParagraph = "prevParagraph"
const val nextParagraph = "nextParagraph" const val nextParagraph = "nextParagraph"
const val upTtsSpeechRate = "upTtsSpeechRate" const val upTtsSpeechRate = "upTtsSpeechRate"
const val upTtsProgress = "upTtsProgress"
const val adjustProgress = "adjustProgress" const val adjustProgress = "adjustProgress"
const val adjustSpeed = "adjustSpeed" const val adjustSpeed = "adjustSpeed"
const val prev = "prev" const val prev = "prev"

View File

@ -312,12 +312,14 @@ object CacheBook {
private fun downloadFinish( private fun downloadFinish(
chapter: BookChapter, chapter: BookChapter,
content: String, content: String,
resetPageOffset: Boolean = false resetPageOffset: Boolean = false,
pageChanged: Boolean = false,
) { ) {
if (ReadBook.book?.bookUrl == book.bookUrl) { if (ReadBook.book?.bookUrl == book.bookUrl) {
ReadBook.contentLoadFinish( ReadBook.contentLoadFinish(
book, chapter, content, book, chapter, content,
resetPageOffset = resetPageOffset resetPageOffset = resetPageOffset,
pageChanged = pageChanged
) )
} }
} }

View File

@ -115,6 +115,14 @@ object ReadAloud {
} }
} }
fun upTtsProgress(context: Context) {
if (BaseReadAloudService.isRun) {
val intent = Intent(context, aloudClass)
intent.action = IntentAction.upTtsProgress
ContextCompat.startForegroundService(context, intent)
}
}
fun setTimer(context: Context, minute: Int) { fun setTimer(context: Context, minute: Int) {
if (BaseReadAloudService.isRun) { if (BaseReadAloudService.isRun) {
val intent = Intent(context, aloudClass) val intent = Intent(context, aloudClass)

View File

@ -186,16 +186,17 @@ object ReadBook : CoroutineScope by MainScope() {
nextTextChapter = null nextTextChapter = null
if (curTextChapter == null) { if (curTextChapter == null) {
AppLog.putDebug("moveToNextChapter-章节未加载,开始加载") AppLog.putDebug("moveToNextChapter-章节未加载,开始加载")
loadContent(durChapterIndex, upContent, false) loadContent(durChapterIndex, upContent, resetPageOffset = false, pageChanged = true)
} else if (upContent) { } else if (upContent) {
AppLog.putDebug("moveToNextChapter-章节已加载,刷新视图") AppLog.putDebug("moveToNextChapter-章节已加载,刷新视图")
callBack?.upContent() callBack?.upContent {
callBack?.upMenuView()
AppLog.putDebug("moveToNextChapter-curPageChanged()")
curPageChanged()
}
} }
loadContent(durChapterIndex.plus(1), upContent, false) loadContent(durChapterIndex.plus(1), upContent, false)
saveRead() saveRead()
callBack?.upMenuView()
AppLog.putDebug("moveToNextChapter-curPageChanged()")
curPageChanged()
return true return true
} else { } else {
AppLog.putDebug("跳转下一章失败,没有下一章") AppLog.putDebug("跳转下一章失败,没有下一章")
@ -214,14 +215,15 @@ object ReadBook : CoroutineScope by MainScope() {
curTextChapter = prevTextChapter curTextChapter = prevTextChapter
prevTextChapter = null prevTextChapter = null
if (curTextChapter == null) { if (curTextChapter == null) {
loadContent(durChapterIndex, upContent, false) loadContent(durChapterIndex, upContent, resetPageOffset = false, pageChanged = true)
} else if (upContent) { } else if (upContent) {
callBack?.upContent() callBack?.upContent {
callBack?.upMenuView()
curPageChanged()
}
} }
loadContent(durChapterIndex.minus(1), upContent, false) loadContent(durChapterIndex.minus(1), upContent, false)
saveRead() saveRead()
callBack?.upMenuView()
curPageChanged()
return true return true
} else { } else {
return false return false
@ -302,12 +304,14 @@ object ReadBook : CoroutineScope by MainScope() {
* @param index 章节序号 * @param index 章节序号
* @param upContent 是否更新视图 * @param upContent 是否更新视图
* @param resetPageOffset 滚动阅读是否重置滚动位置 * @param resetPageOffset 滚动阅读是否重置滚动位置
* @param pageChanged 是否发生了翻页
* @param success 加载完成回调 * @param success 加载完成回调
*/ */
fun loadContent( fun loadContent(
index: Int, index: Int,
upContent: Boolean = true, upContent: Boolean = true,
resetPageOffset: Boolean = false, resetPageOffset: Boolean = false,
pageChanged: Boolean = false,
success: (() -> Unit)? = null success: (() -> Unit)? = null
) { ) {
if (addLoading(index)) { if (addLoading(index)) {
@ -315,10 +319,22 @@ object ReadBook : CoroutineScope by MainScope() {
val book = book!! val book = book!!
appDb.bookChapterDao.getChapter(book.bookUrl, index)?.let { chapter -> appDb.bookChapterDao.getChapter(book.bookUrl, index)?.let { chapter ->
BookHelp.getContent(book, chapter)?.let { BookHelp.getContent(book, chapter)?.let {
contentLoadFinish(book, chapter, it, upContent, resetPageOffset) { contentLoadFinish(
book,
chapter,
it,
upContent,
resetPageOffset,
pageChanged
) {
success?.invoke() success?.invoke()
} }
} ?: download(this, chapter, resetPageOffset = resetPageOffset) } ?: download(
this,
chapter,
resetPageOffset = resetPageOffset,
pageChanged = pageChanged
)
} ?: removeLoading(index) } ?: removeLoading(index)
}.onError { }.onError {
removeLoading(index) removeLoading(index)
@ -345,7 +361,7 @@ object ReadBook : CoroutineScope by MainScope() {
downloadedChapters.add(chapter.index) downloadedChapters.add(chapter.index)
} else { } else {
delay(1000) delay(1000)
download(this, chapter, false) download(this, chapter, resetPageOffset = false, pageChanged = false)
} }
} ?: removeLoading(index) } ?: removeLoading(index)
} catch (e: Exception) { } catch (e: Exception) {
@ -361,6 +377,7 @@ object ReadBook : CoroutineScope by MainScope() {
scope: CoroutineScope, scope: CoroutineScope,
chapter: BookChapter, chapter: BookChapter,
resetPageOffset: Boolean, resetPageOffset: Boolean,
pageChanged: Boolean,
success: (() -> Unit)? = null success: (() -> Unit)? = null
) { ) {
val book = book ?: return removeLoading(chapter.index) val book = book ?: return removeLoading(chapter.index)
@ -370,7 +387,11 @@ object ReadBook : CoroutineScope by MainScope() {
} else { } else {
val msg = if (book.isLocal) "无内容" else "没有书源" val msg = if (book.isLocal) "无内容" else "没有书源"
contentLoadFinish( contentLoadFinish(
book, chapter, "加载正文失败\n$msg", resetPageOffset = resetPageOffset book,
chapter,
"加载正文失败\n$msg",
resetPageOffset = resetPageOffset,
pageChanged = pageChanged
) { ) {
success?.invoke() success?.invoke()
} }
@ -400,6 +421,7 @@ object ReadBook : CoroutineScope by MainScope() {
content: String, content: String,
upContent: Boolean = true, upContent: Boolean = true,
resetPageOffset: Boolean, resetPageOffset: Boolean,
pageChanged: Boolean,
success: (() -> Unit)? = null success: (() -> Unit)? = null
) { ) {
Coroutine.async { Coroutine.async {
@ -419,7 +441,7 @@ object ReadBook : CoroutineScope by MainScope() {
curTextChapter = textChapter curTextChapter = textChapter
if (upContent) callBack?.upContent(offset, resetPageOffset) if (upContent) callBack?.upContent(offset, resetPageOffset)
callBack?.upMenuView() callBack?.upMenuView()
curPageChanged() if (pageChanged) curPageChanged()
callBack?.contentLoadFinish() callBack?.contentLoadFinish()
} }

View File

@ -85,6 +85,7 @@ abstract class BaseReadAloudService : BaseService(),
private var cover: Bitmap = private var cover: Bitmap =
BitmapFactory.decodeResource(appCtx.resources, R.drawable.icon_read_book) BitmapFactory.decodeResource(appCtx.resources, R.drawable.icon_read_book)
var pageChanged = false var pageChanged = false
private var ttsProgress = 0
private val broadcastReceiver = object : BroadcastReceiver() { private val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
@ -152,6 +153,7 @@ abstract class BaseReadAloudService : BaseService(),
IntentAction.pause -> pauseReadAloud() IntentAction.pause -> pauseReadAloud()
IntentAction.resume -> resumeReadAloud() IntentAction.resume -> resumeReadAloud()
IntentAction.upTtsSpeechRate -> upSpeechRate(true) IntentAction.upTtsSpeechRate -> upSpeechRate(true)
IntentAction.upTtsProgress -> upTtsProgress(ttsProgress)
IntentAction.prevParagraph -> prevP() IntentAction.prevParagraph -> prevP()
IntentAction.nextParagraph -> nextP() IntentAction.nextParagraph -> nextP()
IntentAction.addTimer -> addTimer() IntentAction.addTimer -> addTimer()
@ -214,6 +216,11 @@ abstract class BaseReadAloudService : BaseService(),
abstract fun upSpeechRate(reset: Boolean = false) abstract fun upSpeechRate(reset: Boolean = false)
fun upTtsProgress(progress: Int) {
ttsProgress = progress
postEvent(EventBus.TTS_PROGRESS, progress)
}
private fun prevP() { private fun prevP() {
if (nowSpeak > 0) { if (nowSpeak > 0) {
playStop() playStop()

View File

@ -300,7 +300,7 @@ class HttpReadAloudService : BaseReadAloudService(),
playIndexJob?.cancel() playIndexJob?.cancel()
val textChapter = textChapter ?: return val textChapter = textChapter ?: return
playIndexJob = lifecycleScope.launch { playIndexJob = lifecycleScope.launch {
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1) upTtsProgress(readAloudNumber + 1)
if (exoPlayer.duration <= 0) { if (exoPlayer.duration <= 0) {
return@launch return@launch
} }
@ -316,7 +316,7 @@ class HttpReadAloudService : BaseReadAloudService(),
if (pageIndex < textChapter.pageSize) { if (pageIndex < textChapter.pageSize) {
ReadBook.moveToNextPage() ReadBook.moveToNextPage()
} }
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + i) upTtsProgress(readAloudNumber + i.toInt())
} }
delay(sleep) delay(sleep)
} }

View File

@ -171,7 +171,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
pageIndex++ pageIndex++
ReadBook.moveToNextPage() ReadBook.moveToNextPage()
} }
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1) upTtsProgress(readAloudNumber + 1)
} }
} }
@ -193,7 +193,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
if (readAloudNumber + start > it.getReadLength(pageIndex + 1)) { if (readAloudNumber + start > it.getReadLength(pageIndex + 1)) {
pageIndex++ pageIndex++
ReadBook.moveToNextPage() ReadBook.moveToNextPage()
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + start) upTtsProgress(readAloudNumber + start)
} }
} }
} }

View File

@ -305,32 +305,30 @@ class ReadBookActivity : BaseReadBookActivity(),
* 更新菜单 * 更新菜单
*/ */
private fun upMenu() { private fun upMenu() {
val menu = menu val menu = menu ?: return
val book = ReadBook.book val book = ReadBook.book ?: return
if (menu != null && book != null) { val onLine = !book.isLocal
val onLine = !book.isLocal for (i in 0 until menu.size) {
for (i in 0 until menu.size) { val item = menu[i]
val item = menu[i] when (item.groupId) {
when (item.groupId) { R.id.menu_group_on_line -> item.isVisible = onLine
R.id.menu_group_on_line -> item.isVisible = onLine R.id.menu_group_local -> item.isVisible = !onLine
R.id.menu_group_local -> item.isVisible = !onLine R.id.menu_group_text -> item.isVisible = book.isLocalTxt
R.id.menu_group_text -> item.isVisible = book.isLocalTxt else -> when (item.itemId) {
else -> when (item.itemId) { R.id.menu_enable_replace -> item.isChecked = book.getUseReplaceRule()
R.id.menu_enable_replace -> item.isChecked = book.getUseReplaceRule() R.id.menu_re_segment -> item.isChecked = book.getReSegment()
R.id.menu_re_segment -> item.isChecked = book.getReSegment() R.id.menu_enable_review -> {
R.id.menu_enable_review -> { item.isVisible = BuildConfig.DEBUG
item.isVisible = BuildConfig.DEBUG item.isChecked = AppConfig.enableReview
item.isChecked = AppConfig.enableReview
}
R.id.menu_reverse_content -> item.isVisible = onLine
} }
R.id.menu_reverse_content -> item.isVisible = onLine
} }
} }
lifecycleScope.launch { }
menu.findItem(R.id.menu_get_progress)?.isVisible = withContext(IO) { lifecycleScope.launch {
AppWebDav.isOk menu.findItem(R.id.menu_get_progress)?.isVisible = withContext(IO) {
} AppWebDav.isOk
} }
} }
} }
@ -774,7 +772,6 @@ class ReadBookActivity : BaseReadBookActivity(),
override fun upMenuView() { override fun upMenuView() {
lifecycleScope.launch { lifecycleScope.launch {
upMenu()
binding.readMenu.upBookView() binding.readMenu.upBookView()
} }
} }
@ -792,6 +789,9 @@ class ReadBookActivity : BaseReadBookActivity(),
intent.removeExtra("readAloud") intent.removeExtra("readAloud")
ReadBook.readAloud() ReadBook.readAloud()
} }
if (BaseReadAloudService.isRun) {
ReadAloud.upTtsProgress(this)
}
loadStates = true loadStates = true
} }