Merge pull request #4 from gedoor/master

Pull update from owner
pull/1209/head
SkyNight 4 years ago committed by GitHub
commit 317a29ff01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      app/src/main/java/io/legado/app/data/entities/Book.kt
  2. 33
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  3. 15
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt
  4. 27
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -151,11 +151,26 @@ data class Book(
delete() delete()
App.db.bookDao().insert(newBook) App.db.bookDao().insert(newBook)
} }
fun delete() { fun delete() {
if (ReadBook.book?.bookUrl == bookUrl) { if (ReadBook.book?.bookUrl == bookUrl) {
ReadBook.book = null ReadBook.book = null
} }
App.db.bookDao().delete(this) App.db.bookDao().delete(this)
} }
fun upInfoFromOld(oldBook: Book?) {
oldBook?.let {
group = oldBook.group
durChapterIndex = oldBook.durChapterIndex
durChapterPos = oldBook.durChapterPos
durChapterTitle = oldBook.durChapterTitle
customCoverUrl = oldBook.customCoverUrl
customIntro = oldBook.customIntro
order = oldBook.order
if (coverUrl.isNullOrEmpty()) {
coverUrl = oldBook.getDisplayCover()
}
}
}
} }

@ -85,8 +85,10 @@ object ReadBook {
} }
fun upMsg(msg: String?) { fun upMsg(msg: String?) {
this.msg = msg if (this.msg != msg) {
callBack?.upContent() this.msg = msg
callBack?.upContent()
}
} }
fun moveToNextPage() { fun moveToNextPage() {
@ -263,9 +265,11 @@ object ReadBook {
} }
private fun download(chapter: BookChapter, resetPageOffset: Boolean) { private fun download(chapter: BookChapter, resetPageOffset: Boolean) {
book?.let { book -> val book = book
webBook?.getContent(book, chapter) val webBook = webBook
?.onSuccess(Dispatchers.IO) { content -> if (book != null && webBook != null) {
webBook.getContent(book, chapter)
.onSuccess(Dispatchers.IO) { content ->
if (content.isEmpty()) { if (content.isEmpty()) {
contentLoadFinish( contentLoadFinish(
book, book,
@ -276,10 +280,15 @@ object ReadBook {
removeLoading(chapter.index) removeLoading(chapter.index)
} else { } else {
BookHelp.saveContent(book, chapter, content) BookHelp.saveContent(book, chapter, content)
contentLoadFinish(book, chapter, content, resetPageOffset = resetPageOffset) contentLoadFinish(
book,
chapter,
content,
resetPageOffset = resetPageOffset
)
removeLoading(chapter.index) removeLoading(chapter.index)
} }
}?.onError { }.onError {
contentLoadFinish( contentLoadFinish(
book, book,
chapter, chapter,
@ -288,6 +297,16 @@ object ReadBook {
) )
removeLoading(chapter.index) removeLoading(chapter.index)
} }
} else if (book != null) {
contentLoadFinish(
book,
chapter,
"没有书源",
resetPageOffset = resetPageOffset
)
removeLoading(chapter.index)
} else {
removeLoading(chapter.index)
} }
} }

@ -166,19 +166,8 @@ class ChangeSourceDialog : BaseDialogFragment(),
override fun changeTo(searchBook: SearchBook) { override fun changeTo(searchBook: SearchBook) {
val book = searchBook.toBook() val book = searchBook.toBook()
callBack?.oldBook?.let { oldBook -> book.upInfoFromOld(callBack?.oldBook)
book.group = oldBook.group callBack?.changeTo(book)
book.durChapterIndex = oldBook.durChapterIndex
book.durChapterPos = oldBook.durChapterPos
book.durChapterTitle = oldBook.durChapterTitle
book.customCoverUrl = oldBook.customCoverUrl
book.customIntro = oldBook.customIntro
book.order = oldBook.order
if (book.coverUrl.isNullOrEmpty()) {
book.coverUrl = oldBook.getDisplayCover()
}
callBack?.changeTo(book)
}
dismiss() dismiss()
} }

@ -46,11 +46,11 @@ 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) ReadBook.resetData(book)
isInitFinish = true
if (!book.isLocalBook() && ReadBook.webBook == null) { if (!book.isLocalBook() && ReadBook.webBook == null) {
autoChangeSource(book.name, book.author) autoChangeSource(book.name, book.author)
return return
} }
isInitFinish = true
ReadBook.chapterSize = App.db.bookChapterDao().getChapterCount(book.bookUrl) ReadBook.chapterSize = App.db.bookChapterDao().getChapterCount(book.bookUrl)
if (ReadBook.chapterSize == 0) { if (ReadBook.chapterSize == 0) {
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
@ -65,6 +65,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.loadContent(resetPageOffset = true) ReadBook.loadContent(resetPageOffset = true)
} }
} else { } else {
ReadBook.book = book
if (ReadBook.durChapterIndex != book.durChapterIndex) { if (ReadBook.durChapterIndex != book.durChapterIndex) {
ReadBook.durChapterIndex = book.durChapterIndex ReadBook.durChapterIndex = book.durChapterIndex
ReadBook.durPageIndex = book.durChapterPos ReadBook.durPageIndex = book.durChapterPos
@ -72,7 +73,6 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.curTextChapter = null ReadBook.curTextChapter = null
ReadBook.nextTextChapter = null ReadBook.nextTextChapter = null
} }
ReadBook.book!!.group = book.group
ReadBook.titleDate.postValue(book.name) ReadBook.titleDate.postValue(book.name)
ReadBook.upWebBook(book) ReadBook.upWebBook(book)
isInitFinish = true isInitFinish = true
@ -160,20 +160,24 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
ReadBook.upMsg(null) ReadBook.upMsg(null)
ReadBook.book?.changeTo(newBook) ReadBook.book?.changeTo(newBook)
ReadBook.book = newBook
App.db.bookSourceDao().getBookSource(newBook.origin)?.let {
ReadBook.webBook = WebBook(it)
}
ReadBook.prevTextChapter = null ReadBook.prevTextChapter = null
ReadBook.curTextChapter = null ReadBook.curTextChapter = null
ReadBook.nextTextChapter = null ReadBook.nextTextChapter = null
withContext(Main) { withContext(Main) {
ReadBook.callBack?.upContent() ReadBook.callBack?.upContent()
} }
ReadBook.book = newBook
App.db.bookSourceDao().getBookSource(newBook.origin)?.let {
ReadBook.webBook = WebBook(it)
}
if (newBook.tocUrl.isEmpty()) { if (newBook.tocUrl.isEmpty()) {
loadBookInfo(newBook) { upChangeDurChapterIndex(newBook, it) } loadBookInfo(newBook) {
upChangeDurChapterIndex(newBook, it)
}
} else { } else {
loadChapterList(newBook) { upChangeDurChapterIndex(newBook, it) } loadChapterList(newBook) {
upChangeDurChapterIndex(newBook, it)
}
} }
} }
} }
@ -188,8 +192,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
.searchBookSuspend(this, name, variableBook = variableBook) .searchBookSuspend(this, name, variableBook = variableBook)
.getOrNull(0)?.let { .getOrNull(0)?.let {
if (it.name == name && (it.author == author || author == "")) { if (it.name == name && (it.author == author || author == "")) {
changeTo(it.toBook()) val book = it.toBook()
return@forEach book.upInfoFromOld(ReadBook.book)
changeTo(book)
return@execute
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -215,6 +221,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
App.db.bookDao().update(book) App.db.bookDao().update(book)
App.db.bookChapterDao().insert(*chapters.toTypedArray()) App.db.bookChapterDao().insert(*chapters.toTypedArray())
ReadBook.chapterSize = chapters.size ReadBook.chapterSize = chapters.size
ReadBook.upMsg(null)
ReadBook.loadContent(resetPageOffset = true) ReadBook.loadContent(resetPageOffset = true)
} }
} }

Loading…
Cancel
Save