mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
[skip ci] fix: bookInfo viewModel hold activity
This commit is contained in:
parent
1834afe309
commit
1b6ec86984
@ -151,7 +151,7 @@ object BookInfo {
|
||||
coroutineContext.ensureActive()
|
||||
Debug.log(bookSource.bookSourceUrl, "┌获取文件下载链接")
|
||||
book.downloadUrls = analyzeRule.getStringList(infoRule.downloadUrls, isUrl = true)
|
||||
if (book.downloadUrls == null) {
|
||||
if (book.downloadUrls.isNullOrEmpty()) {
|
||||
Debug.log(bookSource.bookSourceUrl, "└")
|
||||
throw NoStackTraceException("下载链接为空")
|
||||
} else {
|
||||
|
@ -52,8 +52,7 @@ class BookInfoActivity :
|
||||
VMBaseActivity<ActivityBookInfoBinding, BookInfoViewModel>(toolBarTheme = Theme.Dark),
|
||||
GroupSelectDialog.CallBack,
|
||||
ChangeBookSourceDialog.CallBack,
|
||||
ChangeCoverDialog.CallBack,
|
||||
BookInfoViewModel.CallBack {
|
||||
ChangeCoverDialog.CallBack {
|
||||
|
||||
private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
|
||||
it?.let {
|
||||
@ -111,7 +110,7 @@ class BookInfoActivity :
|
||||
viewModel.callBack = this
|
||||
viewModel.bookData.observe(this) { showBook(it) }
|
||||
viewModel.chapterListData.observe(this) { upLoading(false, it) }
|
||||
//viewModel.webFileData.observe(this) { showWebFileDownloadAlert() }
|
||||
viewModel.waitDialogData.observe(this) { upWaitDialogStatus(it) }
|
||||
viewModel.initData(intent)
|
||||
initViewEvent()
|
||||
}
|
||||
@ -499,31 +498,34 @@ class BookInfoActivity :
|
||||
private fun showWebFileDownloadAlert(
|
||||
onClick: ((Book) -> Unit)? = null
|
||||
) {
|
||||
viewModel.webFileData.value?.let {
|
||||
alert(titleResource = R.string.download_and_import_file) {
|
||||
items<BookInfoViewModel.WebFile>(it) { _, webFile, _ ->
|
||||
if (webFile.isSupported) {
|
||||
/* import */
|
||||
viewModel.importOrDownloadWebFile<Book>(webFile) {
|
||||
onClick?.invoke(it)
|
||||
}
|
||||
} else {
|
||||
alert(
|
||||
title = getString(R.string.draw),
|
||||
message = getString(R.string.file_not_supported, webFile.name)
|
||||
) {
|
||||
neutralButton(R.string.open_fun) {
|
||||
/* download only */
|
||||
viewModel.importOrDownloadWebFile<Uri>(webFile) { uri ->
|
||||
openFileUri(uri, "*/*")
|
||||
}
|
||||
val webFiles = viewModel.webFiles
|
||||
if (webFiles.isEmpty()) {
|
||||
toastOnUi("Unexpected webFileData")
|
||||
return
|
||||
}
|
||||
alert(titleResource = R.string.download_and_import_file) {
|
||||
items<BookInfoViewModel.WebFile>(webFiles) { _, webFile, _ ->
|
||||
if (webFile.isSupported) {
|
||||
/* import */
|
||||
viewModel.importOrDownloadWebFile<Book>(webFile) {
|
||||
onClick?.invoke(it)
|
||||
}
|
||||
} else {
|
||||
alert(
|
||||
title = getString(R.string.draw),
|
||||
message = getString(R.string.file_not_supported, webFile.name)
|
||||
) {
|
||||
neutralButton(R.string.open_fun) {
|
||||
/* download only */
|
||||
viewModel.importOrDownloadWebFile<Uri>(webFile) { uri ->
|
||||
openFileUri(uri, "*/*")
|
||||
}
|
||||
noButton()
|
||||
}
|
||||
noButton()
|
||||
}
|
||||
}
|
||||
}
|
||||
} ?: toastOnUi("Unexpected webFileData")
|
||||
}
|
||||
}
|
||||
|
||||
private fun readBook(book: Book) {
|
||||
@ -588,14 +590,15 @@ class BookInfoActivity :
|
||||
}
|
||||
}
|
||||
|
||||
override fun onWebFileProcessFinally() {
|
||||
waitDialog.dismiss()
|
||||
}
|
||||
|
||||
override fun onWebFileProcessStart() {
|
||||
waitDialog.run {
|
||||
setText("Loading.....")
|
||||
show()
|
||||
private fun upWaitDialogStatus(isShow: Boolean) {
|
||||
val showText = "Loading....."
|
||||
if (isShow) {
|
||||
waitDialog.run {
|
||||
setText(showText)
|
||||
show()
|
||||
}
|
||||
} else {
|
||||
waitDialog.dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,8 +21,6 @@ import io.legado.app.help.AppWebDav
|
||||
import io.legado.app.help.book.*
|
||||
import io.legado.app.help.coroutine.Coroutine
|
||||
import io.legado.app.lib.webdav.ObjectNotFoundException
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.model.BookCover
|
||||
import io.legado.app.model.ReadBook
|
||||
import io.legado.app.model.localBook.LocalBook
|
||||
@ -30,18 +28,17 @@ import io.legado.app.model.webBook.WebBook
|
||||
import io.legado.app.utils.isContentScheme
|
||||
import io.legado.app.utils.postEvent
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import io.legado.app.utils.runOnUI
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
|
||||
class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
val bookData = MutableLiveData<Book>()
|
||||
val chapterListData = MutableLiveData<List<BookChapter>>()
|
||||
val webFileData = MutableLiveData<List<WebFile>>()
|
||||
val webFiles = mutableListOf<WebFile>()
|
||||
var inBookshelf = false
|
||||
var bookSource: BookSource? = null
|
||||
private var changeSourceCoroutine: Coroutine<*>? = null
|
||||
var callBack: CallBack? = null
|
||||
val waitDialogData = MutableLiveData<Boolean>()
|
||||
|
||||
fun initData(intent: Intent) {
|
||||
execute {
|
||||
@ -241,45 +238,24 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
scope: CoroutineScope = viewModelScope
|
||||
) {
|
||||
execute(scope) {
|
||||
webFiles.clear()
|
||||
val fileName = "${book.name} 作者:${book.author}"
|
||||
if (book.downloadUrls.isNullOrEmpty()) {
|
||||
val ruleDownloadUrls = bookSource.getBookInfoRule().downloadUrls
|
||||
val content = AnalyzeUrl(book.bookUrl, source = bookSource).getStrResponse().body
|
||||
val analyzeRule = AnalyzeRule(book, bookSource)
|
||||
analyzeRule.setContent(content).setBaseUrl(book.bookUrl)
|
||||
analyzeRule.getStringList(ruleDownloadUrls, isUrl = true)?.let {
|
||||
parseDownloadUrls(it, fileName)
|
||||
} ?: throw NoStackTraceException("Unexpected ruleDownloadUrls")
|
||||
} else {
|
||||
parseDownloadUrls(book.downloadUrls, fileName)
|
||||
book.downloadUrls!!.map {
|
||||
val mFileName = "${fileName}.${LocalBook.parseFileSuffix(it)}"
|
||||
val isSupportedFile = AppPattern.bookFileRegex.matches(mFileName)
|
||||
WebFile(it, mFileName, isSupportedFile)
|
||||
}
|
||||
}.onError {
|
||||
context.toastOnUi("LoadWebFileError\n${it.localizedMessage}")
|
||||
}.onSuccess {
|
||||
webFileData.postValue(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseDownloadUrls(
|
||||
downloadUrls: List<String>?,
|
||||
fileName: String
|
||||
): List<WebFile>? {
|
||||
val urls = downloadUrls
|
||||
return urls?.map {
|
||||
val mFileName = "${fileName}.${LocalBook.parseFileSuffix(it)}"
|
||||
val isSupportedFile = AppPattern.bookFileRegex.matches(mFileName)
|
||||
WebFile(it, mFileName, isSupportedFile)
|
||||
webFiles.addAll(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> importOrDownloadWebFile(webFile: WebFile, success: ((T) -> Unit)?) {
|
||||
bookSource ?: return
|
||||
execute {
|
||||
callBack?.run {
|
||||
runOnUI {
|
||||
onWebFileProcessStart()
|
||||
}
|
||||
}
|
||||
waitDialogData.postValue(true)
|
||||
if (webFile.isSupported) {
|
||||
val book = LocalBook.importFileOnLine(webFile.url, webFile.name, bookSource)
|
||||
changeToLocalBook(book)
|
||||
@ -291,12 +267,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
}.onError {
|
||||
context.toastOnUi("ImportWebFileError\n${it.localizedMessage}")
|
||||
}.onFinally {
|
||||
callBack?.run {
|
||||
runOnUI {
|
||||
onWebFileProcessFinally()
|
||||
}
|
||||
}
|
||||
|
||||
waitDialogData.postValue(false)
|
||||
}
|
||||
}
|
||||
|
||||
@ -433,9 +404,4 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
interface CallBack {
|
||||
fun onWebFileProcessStart()
|
||||
fun onWebFileProcessFinally()
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user