优化
This commit is contained in:
@@ -138,7 +138,7 @@ class Coroutine<T>(
|
||||
job.cancel(cause)
|
||||
}
|
||||
cancel?.let {
|
||||
MainScope().launch {
|
||||
MainScope().launch(executeContext) {
|
||||
if (null == it.context) {
|
||||
it.block.invoke(scope)
|
||||
} else {
|
||||
|
||||
@@ -20,6 +20,7 @@ import io.legado.app.utils.toastOnUi
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import splitties.init.appCtx
|
||||
@@ -55,6 +56,7 @@ object ReadBook : CoroutineScope by MainScope() {
|
||||
var preDownloadTask: Coroutine<*>? = null
|
||||
val downloadedChapters = hashSetOf<Int>()
|
||||
var contentProcessor: ContentProcessor? = null
|
||||
val downloadScope = CoroutineScope(SupervisorJob() + IO)
|
||||
|
||||
//暂时保存跳转前进度
|
||||
fun saveCurrentBookProcess() {
|
||||
@@ -347,7 +349,7 @@ object ReadBook : CoroutineScope by MainScope() {
|
||||
success?.invoke()
|
||||
}
|
||||
} ?: download(
|
||||
this,
|
||||
downloadScope,
|
||||
chapter,
|
||||
resetPageOffset = resetPageOffset,
|
||||
pageChanged = pageChanged
|
||||
@@ -378,7 +380,12 @@ object ReadBook : CoroutineScope by MainScope() {
|
||||
downloadedChapters.add(chapter.index)
|
||||
} else {
|
||||
delay(1000)
|
||||
download(this, chapter, resetPageOffset = false, pageChanged = false)
|
||||
download(
|
||||
downloadScope,
|
||||
chapter,
|
||||
resetPageOffset = false,
|
||||
pageChanged = false
|
||||
)
|
||||
}
|
||||
} ?: removeLoading(index)
|
||||
} catch (e: Exception) {
|
||||
@@ -513,16 +520,19 @@ object ReadBook : CoroutineScope by MainScope() {
|
||||
}
|
||||
|
||||
fun saveRead() {
|
||||
Coroutine.async {
|
||||
Coroutine.async(executeContext = IO) {
|
||||
val book = book ?: return@async
|
||||
book.lastCheckCount = 0
|
||||
book.durChapterTime = System.currentTimeMillis()
|
||||
val chapterChanged = book.durChapterIndex != durChapterIndex
|
||||
book.durChapterIndex = durChapterIndex
|
||||
book.durChapterPos = durChapterPos
|
||||
appDb.bookChapterDao.getChapter(book.bookUrl, durChapterIndex)?.let {
|
||||
book.durChapterTitle = it.getDisplayTitle(
|
||||
ContentProcessor.get(book.name, book.origin).getTitleReplaceRules()
|
||||
)
|
||||
if (chapterChanged) {
|
||||
appDb.bookChapterDao.getChapter(book.bookUrl, durChapterIndex)?.let {
|
||||
book.durChapterTitle = it.getDisplayTitle(
|
||||
ContentProcessor.get(book.name, book.origin).getTitleReplaceRules()
|
||||
)
|
||||
}
|
||||
}
|
||||
appDb.bookDao.update(book)
|
||||
}
|
||||
|
||||
@@ -215,12 +215,16 @@ class ReadBookActivity : BaseReadBookActivity(),
|
||||
|
||||
override fun onPostCreate(savedInstanceState: Bundle?) {
|
||||
super.onPostCreate(savedInstanceState)
|
||||
viewModel.initData(intent)
|
||||
viewModel.initData(intent) {
|
||||
upMenu()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent?) {
|
||||
super.onNewIntent(intent)
|
||||
viewModel.initData(intent ?: return)
|
||||
viewModel.initData(intent ?: return) {
|
||||
upMenu()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
||||
@@ -244,7 +248,9 @@ class ReadBookActivity : BaseReadBookActivity(),
|
||||
if (bookChanged) {
|
||||
bookChanged = false
|
||||
ReadBook.callBack = this
|
||||
viewModel.initData(intent)
|
||||
viewModel.initData(intent) {
|
||||
upMenu()
|
||||
}
|
||||
} else {
|
||||
//web端阅读时,app处于阅读界面,本地记录会覆盖web保存的进度,在此处恢复
|
||||
ReadBook.webBookProgress?.let {
|
||||
@@ -1368,6 +1374,7 @@ class ReadBookActivity : BaseReadBookActivity(),
|
||||
ReadBook.callBack = null
|
||||
}
|
||||
ReadBook.preDownloadTask?.cancel()
|
||||
ReadBook.downloadScope.coroutineContext.cancelChildren()
|
||||
ReadBook.downloadedChapters.clear()
|
||||
if (!BuildConfig.DEBUG) {
|
||||
Backup.autoBack(this)
|
||||
|
||||
@@ -56,7 +56,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
fun initData(intent: Intent) {
|
||||
fun initData(intent: Intent, success: (() -> Unit)? = null) {
|
||||
execute {
|
||||
ReadBook.inBookshelf = intent.getBooleanExtra("inBookshelf", true)
|
||||
ReadBook.tocChanged = intent.getBooleanExtra("tocChanged", false)
|
||||
@@ -70,6 +70,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
|
||||
book != null -> initBook(book)
|
||||
else -> ReadBook.upMsg(context.getString(R.string.no_book))
|
||||
}
|
||||
}.onSuccess {
|
||||
success?.invoke()
|
||||
}.onError {
|
||||
val msg = "初始化数据失败\n${it.localizedMessage}"
|
||||
ReadBook.upMsg(msg)
|
||||
|
||||
@@ -285,14 +285,14 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
|
||||
val offset = (ChapterProvider.visibleHeight - textPage.height).toInt()
|
||||
pageOffset = min(0, offset)
|
||||
} else if (pageOffset > 0) {
|
||||
pageFactory.moveToPrev(false)
|
||||
pageFactory.moveToPrev(true)
|
||||
textPage = pageFactory.curPage
|
||||
pageOffset -= textPage.height.toInt()
|
||||
upView?.invoke(textPage)
|
||||
contentDescription = textPage.text
|
||||
} else if (pageOffset < -textPage.height) {
|
||||
pageOffset += textPage.height.toInt()
|
||||
pageFactory.moveToNext(false)
|
||||
pageFactory.moveToNext(true)
|
||||
textPage = pageFactory.curPage
|
||||
upView?.invoke(textPage)
|
||||
contentDescription = textPage.text
|
||||
|
||||
@@ -39,8 +39,8 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
|
||||
ReadBook.moveToNextChapter(upContent)
|
||||
} else {
|
||||
ReadBook.setPageIndex(pageIndex.plus(1))
|
||||
if (upContent) upContent(resetPageOffset = false)
|
||||
}
|
||||
if (upContent) upContent(resetPageOffset = false)
|
||||
true
|
||||
} else
|
||||
false
|
||||
@@ -52,8 +52,8 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
|
||||
ReadBook.moveToPrevChapter(upContent)
|
||||
} else {
|
||||
ReadBook.setPageIndex(pageIndex.minus(1))
|
||||
if (upContent) upContent(resetPageOffset = false)
|
||||
}
|
||||
if (upContent) upContent(resetPageOffset = false)
|
||||
true
|
||||
} else
|
||||
false
|
||||
|
||||
Reference in New Issue
Block a user