@ -3,9 +3,9 @@ package io.legado.app.ui.book.info
import android.app.Application
import android.app.Application
import android.content.Intent
import android.content.Intent
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.MutableLiveData
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
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.help.BookHelp
@ -24,10 +24,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
execute {
execute {
val name = intent . getStringExtra ( " name " ) ?: " "
val name = intent . getStringExtra ( " name " ) ?: " "
val author = intent . getStringExtra ( " author " ) ?: " "
val author = intent . getStringExtra ( " author " ) ?: " "
App . d b. bookDao . getBook ( name , author ) ?. let { book ->
appD b. bookDao . getBook ( name , author ) ?. let { book ->
inBookshelf = true
inBookshelf = true
setBook ( book )
setBook ( book )
} ?: App . d b. searchBookDao . getFirstByNameAuthor ( name , author ) ?. toBook ( ) ?. let { book ->
} ?: appD b. searchBookDao . getFirstByNameAuthor ( name , author ) ?. toBook ( ) ?. let { book ->
setBook ( book )
setBook ( book )
}
}
}
}
@ -39,7 +39,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
if ( book . tocUrl . isEmpty ( ) ) {
if ( book . tocUrl . isEmpty ( ) ) {
loadBookInfo ( book )
loadBookInfo ( book )
} else {
} else {
val chapterList = App . d b. bookChapterDao . getChapterList ( book . bookUrl )
val chapterList = appD b. bookChapterDao . getChapterList ( book . bookUrl )
if ( chapterList . isNotEmpty ( ) ) {
if ( chapterList . isNotEmpty ( ) ) {
chapterListData . postValue ( chapterList )
chapterListData . postValue ( chapterList )
} else {
} else {
@ -56,12 +56,12 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
if ( book . isLocalBook ( ) ) {
if ( book . isLocalBook ( ) ) {
loadChapter ( book , changeDruChapterIndex )
loadChapter ( book , changeDruChapterIndex )
} else {
} else {
App . d b. bookSourceDao . getBookSource ( book . origin ) ?. let { bookSource ->
appD b. bookSourceDao . getBookSource ( book . origin ) ?. let { bookSource ->
WebBook ( bookSource ) . getBookInfo ( this , book , canReName = canReName )
WebBook ( bookSource ) . getBookInfo ( this , book , canReName = canReName )
. onSuccess ( IO ) {
. onSuccess ( IO ) {
bookData . postValue ( book )
bookData . postValue ( book )
if ( inBookshelf ) {
if ( inBookshelf ) {
App . d b. bookDao . update ( book )
appD b. bookDao . update ( book )
}
}
loadChapter ( it , changeDruChapterIndex )
loadChapter ( it , changeDruChapterIndex )
} . onError {
} . onError {
@ -82,18 +82,18 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
execute {
execute {
if ( book . isLocalBook ( ) ) {
if ( book . isLocalBook ( ) ) {
LocalBook . getChapterList ( book ) . let {
LocalBook . getChapterList ( book ) . let {
App . d b. bookDao . update ( book )
appD b. bookDao . update ( book )
App . d b. bookChapterDao . insert ( * it . toTypedArray ( ) )
appD b. bookChapterDao . insert ( * it . toTypedArray ( ) )
chapterListData . postValue ( it )
chapterListData . postValue ( it )
}
}
} else {
} else {
App . d b. bookSourceDao . getBookSource ( book . origin ) ?. let { bookSource ->
appD b. bookSourceDao . getBookSource ( book . origin ) ?. let { bookSource ->
WebBook ( bookSource ) . getChapterList ( this , book )
WebBook ( bookSource ) . getChapterList ( this , book )
. onSuccess ( IO ) {
. onSuccess ( IO ) {
if ( it . isNotEmpty ( ) ) {
if ( it . isNotEmpty ( ) ) {
if ( inBookshelf ) {
if ( inBookshelf ) {
App . d b. bookDao . update ( book )
appD b. bookDao . update ( book )
App . d b. bookChapterDao . insert ( * it . toTypedArray ( ) )
appD b. bookChapterDao . insert ( * it . toTypedArray ( ) )
}
}
if ( changeDruChapterIndex == null ) {
if ( changeDruChapterIndex == null ) {
chapterListData . postValue ( it )
chapterListData . postValue ( it )
@ -119,7 +119,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
fun loadGroup ( groupId : Long , success : ( ( groupNames : String ? ) -> Unit ) ) {
fun loadGroup ( groupId : Long , success : ( ( groupNames : String ? ) -> Unit ) ) {
execute {
execute {
App . d b. bookGroupDao . getGroupNames ( groupId ) . joinToString ( " , " )
appD b. bookGroupDao . getGroupNames ( groupId ) . joinToString ( " , " )
} . onSuccess {
} . onSuccess {
success . invoke ( it )
success . invoke ( it )
}
}
@ -161,8 +161,8 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
)
)
book . durChapterTitle = chapters [ book . durChapterIndex ] . title
book . durChapterTitle = chapters [ book . durChapterIndex ] . title
if ( inBookshelf ) {
if ( inBookshelf ) {
App . d b. bookDao . update ( book )
appD b. bookDao . update ( book )
App . d b. bookChapterDao . insert ( * chapters . toTypedArray ( ) )
appD b. bookChapterDao . insert ( * chapters . toTypedArray ( ) )
}
}
bookData . postValue ( book )
bookData . postValue ( book )
chapterListData . postValue ( chapters )
chapterListData . postValue ( chapters )
@ -173,13 +173,13 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
execute {
execute {
bookData . value ?. let { book ->
bookData . value ?. let { book ->
if ( book . order == 0 ) {
if ( book . order == 0 ) {
book . order = App . d b. bookDao . maxOrder + 1
book . order = appD b. bookDao . maxOrder + 1
}
}
App . d b. bookDao . getBook ( book . name , book . author ) ?. let {
appD b. bookDao . getBook ( book . name , book . author ) ?. let {
book . durChapterPos = it . durChapterPos
book . durChapterPos = it . durChapterPos
book . durChapterTitle = it . durChapterTitle
book . durChapterTitle = it . durChapterTitle
}
}
App . d b. bookDao . insert ( book )
appD b. bookDao . insert ( book )
if ( ReadBook . book ?. name == book . name && ReadBook . book ?. author == book . author ) {
if ( ReadBook . book ?. name == book . name && ReadBook . book ?. author == book . author ) {
ReadBook . book = book
ReadBook . book = book
}
}
@ -192,7 +192,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
fun saveChapterList ( success : ( ( ) -> Unit ) ? ) {
fun saveChapterList ( success : ( ( ) -> Unit ) ? ) {
execute {
execute {
chapterListData . value ?. let {
chapterListData . value ?. let {
App . d b. bookChapterDao . insert ( * it . toTypedArray ( ) )
appD b. bookChapterDao . insert ( * it . toTypedArray ( ) )
}
}
} . onSuccess {
} . onSuccess {
success ?. invoke ( )
success ?. invoke ( )
@ -203,16 +203,16 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
execute {
execute {
bookData . value ?. let { book ->
bookData . value ?. let { book ->
if ( book . order == 0 ) {
if ( book . order == 0 ) {
book . order = App . d b. bookDao . maxOrder + 1
book . order = appD b. bookDao . maxOrder + 1
}
}
App . d b. bookDao . getBook ( book . name , book . author ) ?. let {
appD b. bookDao . getBook ( book . name , book . author ) ?. let {
book . durChapterPos = it . durChapterPos
book . durChapterPos = it . durChapterPos
book . durChapterTitle = it . durChapterTitle
book . durChapterTitle = it . durChapterTitle
}
}
App . d b. bookDao . insert ( book )
appD b. bookDao . insert ( book )
}
}
chapterListData . value ?. let {
chapterListData . value ?. let {
App . d b. bookChapterDao . insert ( * it . toTypedArray ( ) )
appD b. bookChapterDao . insert ( * it . toTypedArray ( ) )
}
}
inBookshelf = true
inBookshelf = true
} . onSuccess {
} . onSuccess {
@ -246,7 +246,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
fun upEditBook ( ) {
fun upEditBook ( ) {
bookData . value ?. let {
bookData . value ?. let {
App . d b. bookDao . getBook ( it . bookUrl ) ?. let { book ->
appD b. bookDao . getBook ( it . bookUrl ) ?. let { book ->
bookData . postValue ( book )
bookData . postValue ( book )
}
}
}
}