@@ -2,6 +2,8 @@ package xyz.jienan.xkcd.model
22
33import android.text.TextUtils
44import io.reactivex.Completable
5+ import io.reactivex.Flowable
6+ import io.reactivex.Maybe
57import io.reactivex.Observable
68import io.reactivex.Single
79import io.reactivex.android.schedulers.AndroidSchedulers
@@ -26,7 +28,7 @@ object WhatIfModel {
2628 val favWhatIf: List <WhatIfArticle >
2729 get() = BoxManager .favWhatIf
2830
29- val thumbUpList: Observable <List <WhatIfArticle >>
31+ val thumbUpList: Single <List <WhatIfArticle >>
3032 get() = whatIfAPI.getTopWhatIfs(WHAT_IF_TOP , XKCD_TOP_SORT_BY_THUMB_UP )
3133 .subscribeOn(Schedulers .io())
3234
@@ -52,7 +54,6 @@ object WhatIfModel {
5254 fun loadAllWhatIf (): Single <List <WhatIfArticle >> {
5355 return whatIfAPI.archive
5456 .subscribeOn(Schedulers .io())
55- .singleOrError()
5657 .map { WhatIfArticleUtil .getArticlesFromArchive(it) }
5758 .map { BoxManager .updateAndSaveWhatIf(it.toMutableList()) }
5859 }
@@ -65,7 +66,6 @@ object WhatIfModel {
6566 fun loadArticle (id : Long ): Single <WhatIfArticle > {
6667 return loadArticleContentFromDB(id)
6768 .switchIfEmpty(loadArticleFromAPI(id))
68- .singleOrError()
6969 .observeOn(AndroidSchedulers .mainThread())
7070 }
7171
@@ -90,42 +90,46 @@ object WhatIfModel {
9090 * @param index
9191 * @return thumb up count
9292 */
93- fun thumbsUp (index : Long ): Observable <Long > {
93+ fun thumbsUp (index : Long ): Single <Long > {
9494 return whatIfAPI.thumbsUpWhatIf(WHAT_IF_THUMBS_UP , index.toInt())
9595 .subscribeOn(Schedulers .io())
9696 .doOnSubscribe { BoxManager .likeWhatIf(index) }
9797 .map { it.thumbCount }
9898 }
9999
100100 fun fastLoadWhatIfs (index : Long ): Completable =
101- Observable .just(index)
102- .flatMap< Long > {
101+ Flowable .just(index)
102+ .flatMap {
103103 if (index == 0L ) {
104104 loadLatest().map { it.num }
105- .flatMapObservable { Observable .rangeLong(1 , it) }
105+ .toFlowable()
106+ .flatMap { Flowable .rangeLong(1 , it) }
106107 } else {
107- Observable .rangeLong(1 , index)
108+ Flowable .rangeLong(1 , index)
108109 }
109- }.flatMap {
110+ }.flatMapSingle {
110111 if (loadArticleFromDB(it) == null || loadArticleFromDB(it)!! .content.isNullOrBlank()) {
111112 loadArticleFromAPI(it)
113+ .map { index }
114+ .onErrorReturnItem(index)
112115 } else {
113- Observable .just(it)
116+ Single .just(it)
114117 }
115118 }
116119 .toList()
117120 .ignoreElement()
121+ .onErrorComplete()
118122
119- private fun loadArticleFromAPI (id : Long ): Observable <WhatIfArticle > {
123+ private fun loadArticleFromAPI (id : Long ): Single <WhatIfArticle > {
120124 return whatIfAPI.getArticle(id)
121125 .subscribeOn(Schedulers .io())
122126 .map { WhatIfArticleUtil .getArticleFromHtml(it) }
123127 .map { it.html() }
124128 .map { BoxManager .updateAndSaveWhatIf(id, it) }
125129 }
126130
127- private fun loadArticleContentFromDB (id : Long ): Observable <WhatIfArticle > {
131+ private fun loadArticleContentFromDB (id : Long ): Maybe <WhatIfArticle > {
128132 val article = BoxManager .getWhatIf(id)
129- return if (article == null || TextUtils .isEmpty(article.content)) Observable .empty() else Observable .just(article)
133+ return if (article == null || TextUtils .isEmpty(article.content)) Maybe .empty() else Maybe .just(article)
130134 }
131135}
0 commit comments