diff --git a/app/src/main/java/io/legado/app/data/entities/RssArticle.kt b/app/src/main/java/io/legado/app/data/entities/RssArticle.kt index a5093f3c8c8b..c7eee2fa4f67 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssArticle.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssArticle.kt @@ -48,6 +48,7 @@ data class RssArticle( pubDate = pubDate, description = description, content = content, - image = image + image = image, + variable = variable ) -} \ No newline at end of file +} diff --git a/app/src/main/java/io/legado/app/data/entities/RssStar.kt b/app/src/main/java/io/legado/app/data/entities/RssStar.kt index e8e9a6bbae14..fc5a1199bdf1 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssStar.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssStar.kt @@ -39,6 +39,7 @@ data class RssStar( pubDate = pubDate, description = description, content = content, - image = image + image = image, + variable = variable ) -} \ No newline at end of file +} diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index 1056d41fe8ba..56a28717cc63 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -147,6 +147,7 @@ abstract class BaseReadAloudService : BaseService(), } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + upNotification() when (intent?.action) { IntentAction.play -> newReadAloud( intent.getBooleanExtra("play", true), diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index 97bf4b9e4588..ae46540c787e 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -165,6 +165,7 @@ class BookInfoActivity : } } } + R.id.menu_share_it -> { viewModel.getBook()?.let { val bookJson = GSON.toJson(it) @@ -172,24 +173,29 @@ class BookInfoActivity : shareWithQr(shareStr, it.name) } } + R.id.menu_refresh -> { refreshBook() } + R.id.menu_login -> viewModel.bookSource?.let { startActivity { putExtra("type", "bookSource") putExtra("key", it.bookSourceUrl) } } + R.id.menu_top -> viewModel.topBook() R.id.menu_set_source_variable -> setSourceVariable() R.id.menu_set_book_variable -> setBookVariable() R.id.menu_copy_book_url -> viewModel.getBook()?.bookUrl?.let { sendToClip(it) } + R.id.menu_copy_toc_url -> viewModel.getBook()?.tocUrl?.let { sendToClip(it) } + R.id.menu_can_update -> { viewModel.getBook()?.let { it.canUpdate = !it.canUpdate @@ -198,6 +204,7 @@ class BookInfoActivity : } } } + R.id.menu_clear_cache -> viewModel.clearCache() R.id.menu_log -> showDialogFragment() R.id.menu_split_long_chapter -> { @@ -210,6 +217,7 @@ class BookInfoActivity : item.isChecked = !item.isChecked if (!item.isChecked) longToastOnUi(R.string.need_more_time_load_content) } + R.id.menu_delete_alert -> LocalConfig.bookInfoDeleteAlert = !item.isChecked R.id.menu_upload -> { viewModel.getBook()?.let { book -> @@ -298,12 +306,14 @@ class BookInfoActivity : isLoading -> { binding.tvToc.text = getString(R.string.toc_s, getString(R.string.loading)) } + chapterList.isNullOrEmpty() -> { binding.tvToc.text = getString( R.string.toc_s, getString(R.string.error_load_toc) ) } + else -> { viewModel.bookData.value?.let { if (it.durChapterIndex < chapterList.size) { @@ -619,6 +629,7 @@ class BookInfoActivity : .putExtra("bookUrl", book.bookUrl) .putExtra("inBookshelf", viewModel.inBookshelf) ) + else -> readBookResult.launch( Intent(this, ReadBookActivity::class.java) .putExtra("bookUrl", book.bookUrl) @@ -654,9 +665,9 @@ class BookInfoActivity : if (viewModel.inBookshelf) { viewModel.saveBook(book) } else if (groupId > 0) { - viewModel.saveBook(book) - viewModel.inBookshelf = true - upTvBookshelf() + viewModel.addToBookshelf { + upTvBookshelf() + } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 5f028daa5fb9..215bc643a1f8 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -143,7 +143,6 @@ class ReadBookActivity : BaseReadBookActivity(), } } private var menu: Menu? = null - private var autoPageJob: Job? = null private var backupJob: Job? = null private var keepScreenJon: Job? = null private var tts: TTS? = null @@ -173,6 +172,7 @@ class ReadBookActivity : BaseReadBookActivity(), private var bookChanged = false private var pageChanged = false private var reloadContent = false + private val autoPageRenderer by lazy { SyncedRenderer { doAutoPage(it) } } //恢复跳转前进度对话框的交互结果 private var confirmRestoreProcess: Boolean? = null @@ -961,7 +961,7 @@ class ReadBookActivity : BaseReadBookActivity(), override fun autoPageStop() { if (isAutoPage) { isAutoPage = false - autoPageJob?.cancel() + autoPageRenderer.stop() binding.readView.invalidate() binding.readMenu.setAutoPage(false) upScreenTimeOut() @@ -969,33 +969,27 @@ class ReadBookActivity : BaseReadBookActivity(), } private fun autoPagePlus() { - autoPageJob?.cancel() - autoPageJob = lifecycleScope.launch { - while (isActive) { - var delayMillis = ReadBookConfig.autoReadSpeed * 1000L / binding.readView.height - var scrollOffset = 1 - if (delayMillis < 20) { - var delayInt = delayMillis.toInt() - if (delayInt == 0) delayInt = 1 - scrollOffset = 20 / delayInt - delayMillis = 20 - } - delay(delayMillis) - if (!menuLayoutIsVisible) { - if (binding.readView.isScroll) { - binding.readView.curPage.scroll(-scrollOffset) - } else { - autoPageProgress += scrollOffset - if (autoPageProgress >= binding.readView.height) { - autoPageProgress = 0 - if (!binding.readView.fillPage(PageDirection.NEXT)) { - autoPageStop() - } - } else { - binding.readView.invalidate() - } - } + autoPageRenderer.start() + } + + private fun doAutoPage(frameTime: Double) { + if (menuLayoutIsVisible) { + return + } + val readTime = ReadBookConfig.autoReadSpeed * 1000.0 + val height = binding.readView.height + val scrollOffset = (height / readTime * frameTime).toInt().coerceAtLeast(1) + if (binding.readView.isScroll) { + binding.readView.curPage.scroll(-scrollOffset) + } else { + autoPageProgress += scrollOffset + if (autoPageProgress >= height) { + autoPageProgress = 0 + if (!binding.readView.fillPage(PageDirection.NEXT)) { + autoPageStop() } + } else { + binding.readView.invalidate() } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/AutoReadDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/AutoReadDialog.kt index c14f9faf44d9..b637a9fe6744 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/AutoReadDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/AutoReadDialog.kt @@ -104,7 +104,9 @@ class AutoReadDialog : BaseDialogFragment(R.layout.dialog_auto_read) { binding.llCatalog.setOnClickListener { callBack?.openChapterList() } binding.llAutoPageStop.setOnClickListener { callBack?.autoPageStop() - dismissAllowingStateLoss() + binding.llAutoPageStop.post { + dismissAllowingStateLoss() + } } } diff --git a/app/src/main/java/io/legado/app/utils/SyncedRenderer.kt b/app/src/main/java/io/legado/app/utils/SyncedRenderer.kt new file mode 100644 index 000000000000..8ec825221182 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/SyncedRenderer.kt @@ -0,0 +1,25 @@ +package io.legado.app.utils + +import android.view.Choreographer + +class SyncedRenderer(val doFrame: (frameTime: Double) -> Unit) { + + private var callback: (Long) -> Unit = {} + + fun start() { + var currTime = System.nanoTime() / 1000000.0 + callback = { + val currTimeMs = it / 1000000.0 + val frameTime = currTimeMs - currTime + currTime = currTimeMs + doFrame(frameTime) + Choreographer.getInstance().postFrameCallback(callback) + } + Choreographer.getInstance().postFrameCallback(callback) + } + + fun stop() { + Choreographer.getInstance().removeFrameCallback(callback) + callback = {} + } +}