@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.data.source.SourceManager
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.ui.manga.MangaEvent
import eu.kanade.tachiyomi.ui.manga.MangaEvent
import eu.kanade.tachiyomi.ui.manga.info.ChapterCountEvent
import eu.kanade.tachiyomi.ui.manga.info.ChapterCountEvent
import eu.kanade.tachiyomi.ui.manga.info.MangaFavoriteEvent
import eu.kanade.tachiyomi.util.SharedData
import eu.kanade.tachiyomi.util.SharedData
import eu.kanade.tachiyomi.util.syncChaptersWithSource
import eu.kanade.tachiyomi.util.syncChaptersWithSource
import rx.Observable
import rx.Observable
@ -68,7 +69,8 @@ class ChaptersPresenter : BasePresenter<ChaptersFragment>() {
/ * *
/ * *
* Subject of list of chapters to allow updating the view without going to DB .
* Subject of list of chapters to allow updating the view without going to DB .
* /
* /
val chaptersSubject by lazy { PublishSubject . create < List < ChapterModel > > ( ) }
val chaptersSubject : PublishSubject < List < ChapterModel > >
by lazy { PublishSubject . create < List < ChapterModel > > ( ) }
/ * *
/ * *
* Whether the chapter list has been requested to the source .
* Whether the chapter list has been requested to the source .
@ -100,23 +102,23 @@ class ChaptersPresenter : BasePresenter<ChaptersFragment>() {
// On each subject emission, apply filters and sort then update the view.
// On each subject emission, apply filters and sort then update the view.
{ chaptersSubject
{ chaptersSubject
. flatMap { applyChapterFilters ( it ) }
. flatMap { applyChapterFilters ( it ) }
. observeOn ( AndroidSchedulers . mainThread ( ) ) } ,
. observeOn ( AndroidSchedulers . mainThread ( ) )
{ view , chapters -> view . onNextChapters ( chapters ) } )
}, ChaptersFragment :: onNextChapters )
startableFirst ( FETCH _CHAPTERS ,
startableFirst ( FETCH _CHAPTERS ,
{ getRemoteChaptersObservable ( ) } ,
{ getRemoteChaptersObservable ( ) } ,
{ view , result -> view . onFetchChaptersDone ( ) } ,
{ view , result -> view . onFetchChaptersDone ( ) } ,
{ view , error -> view . onFetchChaptersError ( error ) } )
ChaptersFragment :: onFetchChaptersError )
startableLatestCache ( CHAPTER _STATUS _CHANGES ,
startableLatestCache ( CHAPTER _STATUS _CHANGES ,
{ getChapterStatusObservable ( ) } ,
{ getChapterStatusObservable ( ) } ,
{ view , download -> view . onChapterStatusChange ( download ) } ,
ChaptersFragment :: onChapterStatusChange ,
{ view , error -> Timber . e ( error ) } )
{ view , error -> Timber . e ( error ) } )
// Find the active manga from the shared data or return.
// Find the active manga from the shared data or return.
manga = SharedData . get ( MangaEvent :: class . java ) ?. manga ?: return
manga = SharedData . get ( MangaEvent :: class . java ) ?. manga ?: return
Observable . just ( manga )
Observable . just ( manga )
. subscribeLatestCache ( { view , manga -> view . onNextManga ( manga ) } )
. subscribeLatestCache ( ChaptersFragment :: onNextManga )
// Find the source for this manga.
// Find the source for this manga.
source = sourceManager . get ( manga . source ) !!
source = sourceManager . get ( manga . source ) !!
@ -197,18 +199,20 @@ class ChaptersPresenter : BasePresenter<ChaptersFragment>() {
/ * *
/ * *
* Returns an observable that updates the chapter list with the latest from the source .
* Returns an observable that updates the chapter list with the latest from the source .
* /
* /
fun getRemoteChaptersObservable ( ) = Observable . defer { source . fetchChapterList ( manga ) }
fun getRemoteChaptersObservable ( ) : Observable < Pair < List < Chapter > , List < Chapter > > > =
. subscribeOn ( Schedulers . io ( ) )
Observable . defer { source . fetchChapterList ( manga ) }
. map { syncChaptersWithSource ( db , it , manga , source ) }
. subscribeOn ( Schedulers . io ( ) )
. observeOn ( AndroidSchedulers . mainThread ( ) )
. map { syncChaptersWithSource ( db , it , manga , source ) }
. observeOn ( AndroidSchedulers . mainThread ( ) )
/ * *
/ * *
* Returns an observable that listens to download queue status changes .
* Returns an observable that listens to download queue status changes .
* /
* /
fun getChapterStatusObservable ( ) = downloadManager . queue . getStatusObservable ( )
fun getChapterStatusObservable ( ) : Observable < Download > =
. observeOn ( AndroidSchedulers . mainThread ( ) )
downloadManager . queue . getStatusObservable ( )
. filter { download -> download . manga . id == manga . id }
. observeOn ( AndroidSchedulers . mainThread ( ) )
. doOnNext { onDownloadStatusChange ( it ) }
. filter { download -> download . manga . id == manga . id }
. doOnNext { onDownloadStatusChange ( it ) }
/ * *
/ * *
* Applies the view filters to the list of chapters obtained from the database .
* Applies the view filters to the list of chapters obtained from the database .
@ -231,11 +235,11 @@ class ChaptersPresenter : BasePresenter<ChaptersFragment>() {
}
}
val sortFunction : ( Chapter , Chapter ) -> Int = when ( manga . sorting ) {
val sortFunction : ( Chapter , Chapter ) -> Int = when ( manga . sorting ) {
Manga . SORTING _SOURCE -> when ( sortDescending ( ) ) {
Manga . SORTING _SOURCE -> when ( sortDescending ( ) ) {
true -> { c1 , c2 -> c1 . source _order . compareTo ( c2 . source _order ) }
true -> { c1 , c2 -> c1 . source _order . compareTo ( c2 . source _order ) }
false -> { c1 , c2 -> c2 . source _order . compareTo ( c1 . source _order ) }
false -> { c1 , c2 -> c2 . source _order . compareTo ( c1 . source _order ) }
}
}
Manga . SORTING _NUMBER -> when ( sortDescending ( ) ) {
Manga . SORTING _NUMBER -> when ( sortDescending ( ) ) {
true -> { c1 , c2 -> c2 . chapter _number . compareTo ( c1 . chapter _number ) }
true -> { c1 , c2 -> c2 . chapter _number . compareTo ( c1 . chapter _number ) }
false -> { c1 , c2 -> c1 . chapter _number . compareTo ( c2 . chapter _number ) }
false -> { c1 , c2 -> c1 . chapter _number . compareTo ( c2 . chapter _number ) }
}
}
else -> throw NotImplementedError ( " Unimplemented sorting method " )
else -> throw NotImplementedError ( " Unimplemented sorting method " )
@ -325,9 +329,7 @@ class ChaptersPresenter : BasePresenter<ChaptersFragment>() {
. observeOn ( AndroidSchedulers . mainThread ( ) )
. observeOn ( AndroidSchedulers . mainThread ( ) )
. subscribeFirst ( { view , result ->
. subscribeFirst ( { view , result ->
view . onChaptersDeleted ( )
view . onChaptersDeleted ( )
} , { view , error ->
} , ChaptersFragment :: onChaptersDeletedError )
view . onChaptersDeletedError ( error )
} )
}
}
/ * *
/ * *
@ -401,6 +403,13 @@ class ChaptersPresenter : BasePresenter<ChaptersFragment>() {
refreshChapters ( )
refreshChapters ( )
}
}
/ * *
* Adds manga to library
* /
fun addToLibrary ( ) {
SharedData . get ( MangaFavoriteEvent :: class . java ) ?. call ( true )
}
/ * *
/ * *
* Sets the active display mode .
* Sets the active display mode .
* @param mode the mode to set .
* @param mode the mode to set .