From 8fffc25a4438c8d29722bf406fc623855bfddf90 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Apr 2022 11:40:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/audio/AudioPlayActivity.kt | 20 ++++++++++++++++++- .../changesource/ChangeBookSourceDialog.kt | 2 +- .../changesource/ChangeBookSourceViewModel.kt | 11 ++++++---- .../changesource/ChangeChapterSourceDialog.kt | 2 +- 4 files changed, 28 insertions(+), 7 deletions(-) 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 70166c840..4f9b53240 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 @@ -12,6 +12,7 @@ import androidx.activity.viewModels import androidx.compose.runtime.mutableStateOf import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.BookType import io.legado.app.constant.EventBus import io.legado.app.constant.Status import io.legado.app.constant.Theme @@ -25,6 +26,7 @@ import io.legado.app.model.BookCover import io.legado.app.service.AudioPlayService import io.legado.app.ui.about.AppLogDialog import io.legado.app.ui.book.changesource.ChangeBookSourceDialog +import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.toc.TocActivityResult import io.legado.app.ui.login.SourceLoginActivity @@ -32,6 +34,9 @@ import io.legado.app.ui.theme.AppTheme import io.legado.app.ui.widget.seekbar.SeekBarChangeListener import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import splitties.views.onLongClick import java.util.* @@ -200,7 +205,20 @@ class AudioPlayActivity : get() = AudioPlay.book override fun changeTo(source: BookSource, book: Book, toc: List) { - viewModel.changeTo(source, book, toc) + if (book.type == BookType.audio) { + viewModel.changeTo(source, book, toc) + } else { + AudioPlay.stop(this) + launch { + withContext(IO) { + AudioPlay.book?.changeTo(book, toc) + } + startActivity { + putExtra("bookUrl", book.bookUrl) + } + finish() + } + } } override fun finish() { diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt index a4454a4a7..1ebcb9e1b 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt @@ -287,7 +287,7 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_ override fun deleteSource(searchBook: SearchBook) { viewModel.del(searchBook) if (bookUrl == searchBook.bookUrl) { - viewModel.autoChangeSource { book, toc, source -> + viewModel.autoChangeSource(callBack?.oldBook?.type) { book, toc, source -> callBack?.changeTo(source, book, toc) } } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt index d15322ada..f3f1985a7 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt @@ -360,14 +360,17 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a } fun autoChangeSource( + bookType: Int?, onSuccess: (book: Book, toc: List, source: BookSource) -> Unit ) { execute { searchBooks.forEach { - val book = it.toBook() - val result = getToc(book).getOrNull() - if (result != null) { - return@execute Triple(book, result.first, result.second) + if (it.type == bookType) { + val book = it.toBook() + val result = getToc(book).getOrNull() + if (result != null) { + return@execute Triple(book, result.first, result.second) + } } } throw NoStackTraceException("没有有效源") diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt index 481a30437..21b61822e 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt @@ -309,7 +309,7 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c override fun deleteSource(searchBook: SearchBook) { viewModel.del(searchBook) if (bookUrl == searchBook.bookUrl) { - viewModel.autoChangeSource { book, toc, source -> + viewModel.autoChangeSource(callBack?.oldBook?.type) { book, toc, source -> callBack?.changeTo(source, book, toc) } }