feat: 优化

pull/209/head
kunfei 5 years ago
parent 86f6be1488
commit 581f73054b
  1. 15
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  2. 12
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -36,7 +36,7 @@ object ReadBook {
var msg: String? = null var msg: String? = null
private val loadingChapters = arrayListOf<Int>() private val loadingChapters = arrayListOf<Int>()
fun resetData(book: Book, noSource: (name: String, author: String) -> Unit) { fun resetData(book: Book) {
this.book = book this.book = book
titleDate.postValue(book.name) titleDate.postValue(book.name)
durChapterIndex = book.durChapterIndex durChapterIndex = book.durChapterIndex
@ -46,20 +46,19 @@ object ReadBook {
prevTextChapter = null prevTextChapter = null
curTextChapter = null curTextChapter = null
nextTextChapter = null nextTextChapter = null
upWebBook(book, noSource) upWebBook(book)
} }
fun upWebBook(book: Book?, noSource: (name: String, author: String) -> Unit) { fun upWebBook(book: Book?) {
book ?: return book ?: return
if (book.origin == BookType.local) { webBook = if (book.origin == BookType.local) {
webBook = null null
} else { } else {
val bookSource = App.db.bookSourceDao().getBookSource(book.origin) val bookSource = App.db.bookSourceDao().getBookSource(book.origin)
if (bookSource != null) { if (bookSource != null) {
webBook = WebBook(bookSource) WebBook(bookSource)
} else { } else {
webBook = null null
noSource.invoke(book.name, book.author)
} }
} }
} }

@ -40,8 +40,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun initBook(book: Book) { private fun initBook(book: Book) {
if (ReadBook.book?.bookUrl != book.bookUrl) { if (ReadBook.book?.bookUrl != book.bookUrl) {
ReadBook.resetData(book) { name, author -> ReadBook.resetData(book)
autoChangeSource(name, author) if (ReadBook.webBook == null) {
autoChangeSource(book.name, book.author)
return
} }
isInitFinish = true isInitFinish = true
ReadBook.chapterSize = App.db.bookChapterDao().getChapterCount(book.bookUrl) ReadBook.chapterSize = App.db.bookChapterDao().getChapterCount(book.bookUrl)
@ -63,8 +65,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} else { } else {
isInitFinish = true isInitFinish = true
ReadBook.titleDate.postValue(book.name) ReadBook.titleDate.postValue(book.name)
ReadBook.upWebBook(book) { name, author -> ReadBook.upWebBook(book)
autoChangeSource(name, author) if (ReadBook.webBook == null) {
autoChangeSource(book.name, book.author)
return
} }
ReadBook.chapterSize = App.db.bookChapterDao().getChapterCount(book.bookUrl) ReadBook.chapterSize = App.db.bookChapterDao().getChapterCount(book.bookUrl)
if (ReadBook.chapterSize == 0) { if (ReadBook.chapterSize == 0) {

Loading…
Cancel
Save