优化代码

pull/754/head
gedoor 4 years ago
parent 1a02f43343
commit 7a0467edc4
  1. 7
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 14
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -108,9 +108,6 @@ class ReadBookActivity : ReadBookBaseActivity(),
upMenu() upMenu()
upView() upView()
} }
viewModel.processLiveData.observe(this) {
sureSyncProgress(it)
}
viewModel.initData(intent) viewModel.initData(intent)
} }
@ -232,7 +229,9 @@ class ReadBookActivity : ReadBookBaseActivity(),
} }
R.id.menu_set_charset -> showCharsetConfig() R.id.menu_set_charset -> showCharsetConfig()
R.id.menu_get_progress -> ReadBook.book?.let { R.id.menu_get_progress -> ReadBook.book?.let {
viewModel.syncBookProgress(it) viewModel.syncBookProgress(it) { progress ->
sureSyncProgress(progress)
}
} }
R.id.menu_help -> showReadMenuHelp() R.id.menu_help -> showReadMenuHelp()
} }

@ -2,7 +2,6 @@ package io.legado.app.ui.book.read
import android.app.Application import android.app.Application
import android.content.Intent import android.content.Intent
import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
@ -26,7 +25,6 @@ import kotlinx.coroutines.withContext
class ReadBookViewModel(application: Application) : BaseViewModel(application) { class ReadBookViewModel(application: Application) : BaseViewModel(application) {
var isInitFinish = false var isInitFinish = false
var searchContentQuery = "" var searchContentQuery = ""
val processLiveData = MutableLiveData<BookProgress>()
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { execute {
@ -162,14 +160,20 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
} }
fun syncBookProgress(book: Book, syncBookProgress: Boolean = AppConfig.syncBookProgress) { fun syncBookProgress(
book: Book,
syncBookProgress: Boolean = AppConfig.syncBookProgress,
alertSync: ((progress: BookProgress) -> Unit)? = null
) {
if (syncBookProgress) if (syncBookProgress)
execute { execute {
BookWebDav.getBookProgress(book)?.let { progress -> BookWebDav.getBookProgress(book)
}.onSuccess {
it?.let { progress ->
if (progress.durChapterIndex < book.durChapterIndex || if (progress.durChapterIndex < book.durChapterIndex ||
(progress.durChapterIndex == book.durChapterIndex && progress.durChapterPos < book.durChapterPos) (progress.durChapterIndex == book.durChapterIndex && progress.durChapterPos < book.durChapterPos)
) { ) {
processLiveData.postValue(progress) alertSync?.invoke(progress)
} else { } else {
ReadBook.setProgress(progress) ReadBook.setProgress(progress)
} }

Loading…
Cancel
Save