Horis 3 лет назад
Родитель
Сommit
db22e72919

+ 1 - 0
app/src/main/java/io/legado/app/constant/IntentAction.kt

@@ -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"

+ 4 - 2
app/src/main/java/io/legado/app/model/CacheBook.kt

@@ -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
                 )
             }
         }

+ 8 - 0
app/src/main/java/io/legado/app/model/ReadAloud.kt

@@ -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)

+ 36 - 14
app/src/main/java/io/legado/app/model/ReadBook.kt

@@ -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()
                     }
 

+ 7 - 0
app/src/main/java/io/legado/app/service/BaseReadAloudService.kt

@@ -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()

+ 2 - 2
app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@@ -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)
             }

+ 2 - 2
app/src/main/java/io/legado/app/service/TTSReadAloudService.kt

@@ -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)
                 }
             }
         }

+ 24 - 24
app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt

@@ -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
     }