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