This commit is contained in:
Horis
2024-07-10 16:59:16 +08:00
parent e63512e8c7
commit 09742cbbae
3 changed files with 55 additions and 51 deletions

View File

@@ -14,6 +14,7 @@ import android.view.WindowManager
import android.widget.FrameLayout
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.AppConst.charsets
@@ -50,6 +51,8 @@ import io.legado.app.utils.setNavigationBarColorAuto
import io.legado.app.utils.showDialogFragment
import io.legado.app.utils.viewbindingdelegate.viewBinding
import io.legado.app.utils.visible
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import java.time.LocalDate
import java.time.format.DateTimeFormatter
@@ -75,7 +78,9 @@ abstract class BaseReadBookActivity :
FileDoc.fromUri(uri, true).find(book.originName)?.let { doc ->
book.bookUrl = doc.uri.toString()
book.save()
viewModel.loadChapterList(book)
lifecycleScope.launch(IO) {
viewModel.loadChapterList(book)
}
} ?: ReadBook.upMsg("找不到文件")
}
} ?: ReadBook.upMsg("没有权限访问")
@@ -355,6 +360,8 @@ abstract class BaseReadBookActivity :
book.setStartChapter(start)
book.setReadSimulating(enabled)
book.save()
ReadBook.clearTextChapter()
viewModel.initData(intent)
}
}
noButton()

View File

@@ -924,7 +924,9 @@ class ReadBookActivity : BaseReadBookActivity(),
override fun loadChapterList(book: Book) {
ReadBook.upMsg(getString(R.string.toc_updateing))
viewModel.loadChapterList(book)
lifecycleScope.launch(IO) {
viewModel.loadChapterList(book)
}
}
/**

View File

@@ -5,7 +5,6 @@ import android.content.Intent
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import io.legado.app.R
import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppLog
@@ -40,7 +39,6 @@ import io.legado.app.utils.mapParallelSafe
import io.legado.app.utils.postEvent
import io.legado.app.utils.toStringArray
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
@@ -97,7 +95,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
}
private fun initBook(book: Book) {
private suspend fun initBook(book: Book) {
val isSameBook = ReadBook.book?.bookUrl == book.bookUrl
if (isSameBook) {
ReadBook.upData(book)
@@ -105,23 +103,23 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.resetData(book)
}
isInitFinish = true
if (ReadBook.chapterSize == 0) {
if (book.tocUrl.isEmpty()) {
loadBookInfo(book)
} else {
loadChapterList(book)
}
} else if (book.isLocalModified()) {
loadChapterList(book)
} else if (isSameBook) {
ReadBook.loadOrUpContent()
checkLocalBookFileExist(book)
if (!book.isLocal && book.tocUrl.isEmpty() && !loadBookInfo(book)) {
return
}
if (book.isLocal && !checkLocalBookFileExist(book)) {
return
}
if ((ReadBook.chapterSize == 0 || book.isLocalModified()) && !loadChapterList(book)) {
return
}
ReadBook.upMsg(null)
if (ReadBook.durChapterIndex > ReadBook.simulatedChapterSize - 1) {
ReadBook.durChapterIndex = ReadBook.simulatedChapterSize - 1
}
if (!isSameBook) {
ReadBook.loadContent(resetPageOffset = true)
} else {
if (ReadBook.durChapterIndex > ReadBook.simulatedChapterSize - 1) {
ReadBook.durChapterIndex = ReadBook.simulatedChapterSize - 1
}
ReadBook.loadContent(resetPageOffset = false)
checkLocalBookFileExist(book)
ReadBook.loadOrUpContent()
}
if (ReadBook.chapterChanged) {
// 有章节跳转不同步阅读进度
@@ -135,42 +133,38 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
}
private fun checkLocalBookFileExist(book: Book) {
if (book.isLocal) {
execute {
LocalBook.getBookInputStream(book)
}.onError {
if (it is FileNotFoundException) {
permissionDenialLiveData.postValue(0)
}
private fun checkLocalBookFileExist(book: Book): Boolean {
try {
LocalBook.getBookInputStream(book)
return true
} catch (e: Throwable) {
if (e is FileNotFoundException) {
permissionDenialLiveData.postValue(0)
}
return false
}
}
/**
* 加载详情页
*/
private fun loadBookInfo(book: Book) {
if (book.isLocal) {
loadChapterList(book)
} else {
ReadBook.bookSource?.let { source ->
WebBook.getBookInfo(viewModelScope, source, book, canReName = false)
.onSuccess {
loadChapterList(book)
}.onError {
ReadBook.upMsg("详情页出错: ${it.localizedMessage}")
}
}
private suspend fun loadBookInfo(book: Book): Boolean {
val source = ReadBook.bookSource ?: return false
try {
WebBook.getBookInfoAwait(source, book, canReName = false)
return true
} catch (e: Throwable) {
ReadBook.upMsg("详情页出错: ${e.localizedMessage}")
return false
}
}
/**
* 加载目录
*/
fun loadChapterList(book: Book) {
suspend fun loadChapterList(book: Book): Boolean {
if (book.isLocal) {
execute {
kotlin.runCatching {
LocalBook.getChapterList(book).let {
book.latestChapterTime = System.currentTimeMillis()
appDb.bookChapterDao.delByBook(book.bookUrl)
@@ -178,10 +172,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
appDb.bookDao.update(book)
ReadBook.chapterSize = it.size
ReadBook.simulatedChapterSize = book.simulatedTotalChapterNum()
ReadBook.upMsg(null)
ReadBook.loadContent(resetPageOffset = true)
}
}.onError {
return true
}.onFailure {
when (it) {
is SecurityException, is FileNotFoundException -> {
permissionDenialLiveData.postValue(1)
@@ -192,12 +185,13 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.upMsg("LoadTocError:${it.localizedMessage}")
}
}
return false
}
} else {
ReadBook.bookSource?.let {
val oldBook = book.copy()
WebBook.getChapterList(viewModelScope, it, book, true)
.onSuccess(IO) { cList ->
WebBook.getChapterListAwait(it, book, true)
.onSuccess { cList ->
if (oldBook.bookUrl == book.bookUrl) {
appDb.bookDao.update(book)
} else {
@@ -208,13 +202,14 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
appDb.bookChapterDao.insert(*cList.toTypedArray())
ReadBook.chapterSize = cList.size
ReadBook.simulatedChapterSize = book.simulatedTotalChapterNum()
ReadBook.upMsg(null)
ReadBook.loadContent(resetPageOffset = true)
}.onError {
return true
}.onFailure {
ReadBook.upMsg(context.getString(R.string.error_load_toc))
return false
}
}
}
return false
}
/**