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