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 nextParagraph = "nextParagraph"
const val upTtsSpeechRate = "upTtsSpeechRate"
const val upTtsProgress = "upTtsProgress"
const val adjustProgress = "adjustProgress"
const val adjustSpeed = "adjustSpeed"
const val prev = "prev"

View File

@ -312,12 +312,14 @@ object CacheBook {
private fun downloadFinish(
chapter: BookChapter,
content: String,
resetPageOffset: Boolean = false
resetPageOffset: Boolean = false,
pageChanged: Boolean = false,
) {
if (ReadBook.book?.bookUrl == book.bookUrl) {
ReadBook.contentLoadFinish(
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) {
if (BaseReadAloudService.isRun) {
val intent = Intent(context, aloudClass)

View File

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

View File

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

View File

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

View File

@ -171,7 +171,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
pageIndex++
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)) {
pageIndex++
ReadBook.moveToNextPage()
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + start)
upTtsProgress(readAloudNumber + start)
}
}
}

View File

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