diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt index d4a8f128a..7c6c2d780 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt @@ -7,6 +7,7 @@ import android.view.MenuItem import android.view.animation.Animation import android.view.animation.AnimationUtils import androidx.core.view.isVisible +import androidx.lifecycle.Observer import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -39,6 +40,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea setSupportActionBar(toolbar) initAnimation() initView() + viewModel.chapterMaxIndex.observe(this, Observer { }) viewModel.initData(intent) savedInstanceState?.let { changeSourceDialog = supportFragmentManager.findFragmentByTag(ChangeSourceDialog.tag) as? ChangeSourceDialog diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt index 0daa33437..da91edc8a 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.MediatorLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.help.BookHelp import io.legado.app.model.WebBook @@ -47,14 +48,20 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } - fun download(book: Book, index: Int) { + fun getContent(book: Book, index: Int) { App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> - webBook?.getContent(book, chapter) - ?.onSuccess(IO) { content -> - content?.let { - BookHelp.saveContent(book, chapter, it) - } - } + BookHelp.getContent(book, chapter)?.let { + + } ?: download(book, chapter) } } + + private fun download(book: Book, chapter: BookChapter) { + webBook?.getContent(book, chapter) + ?.onSuccess(IO) { content -> + content?.let { + BookHelp.saveContent(book, chapter, it) + } + } + } } \ No newline at end of file