From f9a5c9ee63112562a7c895da8a1e0f06c9814339 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 22 Jun 2021 10:39:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=AC=E4=B9=A6=E4=BB=8E?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=89=93=E5=BC=80=E6=97=B6=E4=B8=8D=E5=AF=B9?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 1 + .../io/legado/app/service/help/AudioPlay.kt | 46 ++++++++----------- .../app/ui/book/audio/AudioPlayActivity.kt | 11 +++-- .../app/ui/book/audio/AudioPlayViewModel.kt | 10 ++-- 4 files changed, 29 insertions(+), 39 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 67382e0e3..2d34955c6 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -11,6 +11,7 @@ **2021/06/22** * 修复隐藏未读设置不生效的bug * 修复系统字体大小选择大时导入界面按钮显示不全的bug +* 修复听书从后台打开时不对的bug **2021/06/20** * viewPager2 改回 viewPager diff --git a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt index 7a599a190..daf2f2156 100644 --- a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt +++ b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt @@ -98,9 +98,7 @@ object AudioPlay { durChapterIndex = index durChapterPos = 0 durChapter = null - book.durChapterIndex = durChapterIndex - book.durChapterPos = 0 - saveRead() + saveRead(book) play(context) } } @@ -115,28 +113,22 @@ object AudioPlay { durChapterIndex-- durChapterPos = 0 durChapter = null - book.durChapterIndex = durChapterIndex - book.durChapterPos = 0 - saveRead() + saveRead(book) play(context) } } } fun next(context: Context) { - Coroutine.async { - book?.let { book -> - if (book.durChapterIndex >= book.totalChapterNum) { - return@let - } - durChapterIndex++ - durChapterPos = 0 - durChapter = null - book.durChapterIndex = durChapterIndex - book.durChapterPos = 0 - saveRead() - play(context) + book?.let { book -> + if (book.durChapterIndex >= book.totalChapterNum) { + return@let } + durChapterIndex++ + durChapterPos = 0 + durChapter = null + saveRead(book) + play(context) } } @@ -146,18 +138,16 @@ object AudioPlay { context.startService(intent) } - fun saveRead() { + fun saveRead(book: Book) { + book.lastCheckCount = 0 + book.durChapterTime = System.currentTimeMillis() + book.durChapterIndex = durChapterIndex + book.durChapterPos = durChapterPos Coroutine.async { - book?.let { book -> - book.lastCheckCount = 0 - book.durChapterTime = System.currentTimeMillis() - book.durChapterIndex = durChapterIndex - book.durChapterPos = durChapterPos - appDb.bookChapterDao.getChapter(book.bookUrl, book.durChapterIndex)?.let { - book.durChapterTitle = it.title - } - appDb.bookDao.update(book) + appDb.bookChapterDao.getChapter(book.bookUrl, book.durChapterIndex)?.let { + book.durChapterTitle = it.title } + book.save() } } diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt index 1209ca0c0..d13b395ca 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt @@ -40,8 +40,7 @@ class AudioPlayActivity : ChangeSourceDialog.CallBack { override val binding by viewBinding(ActivityAudioPlayBinding::inflate) - override val viewModel: AudioPlayViewModel - by viewModels() + override val viewModel by viewModels() private var adjustProgress = false private val progressTimeFormat by lazy { @@ -61,8 +60,12 @@ class AudioPlayActivity : override fun onActivityCreated(savedInstanceState: Bundle?) { binding.titleBar.transparent() - AudioPlay.titleData.observe(this, { binding.titleBar.title = it }) - AudioPlay.coverData.observe(this, { upCover(it) }) + AudioPlay.titleData.observe(this) { + binding.titleBar.title = it + } + AudioPlay.coverData.observe(this) { + upCover(it) + } viewModel.initData(intent) initView() } diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt index 7be490591..5ac20f454 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt @@ -17,14 +17,10 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) fun initData(intent: Intent) = AudioPlay.apply { execute { val bookUrl = intent.getStringExtra("bookUrl") - if (book?.bookUrl != bookUrl) { + if (bookUrl != null && bookUrl != book?.bookUrl) { stop(context) inBookshelf = intent.getBooleanExtra("inBookshelf", true) - book = if (!bookUrl.isNullOrEmpty()) { - appDb.bookDao.getBook(bookUrl) - } else { - appDb.bookDao.lastReadBook - } + book = appDb.bookDao.getBook(bookUrl) book?.let { book -> titleData.postValue(book.name) coverData.postValue(book.getDisplayCover()) @@ -42,8 +38,8 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) loadChapterList(book) } } + saveRead(book) } - saveRead() } } }