Created
June 6, 2017 08:18
-
-
Save novodimaporo/eeb6e633de63c5077a3f071d42a5ad28 to your computer and use it in GitHub Desktop.
learning rxjava
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun loadFirstPage(): ObservableTransformer<LoadFirstPageAction, MainResult> { | |
return ObservableTransformer { | |
actionStream -> actionStream.flatMap { _ -> mainModel.getNews("", "10") // <-- whats wrong with you! took 1 day of my life | |
.map { data -> MainResult.success(data) } | |
.onErrorReturn { error -> MainResult.failure(error.message ?: "Unknown error") } | |
.observeOn(AndroidSchedulers.mainThread()) | |
.startWith { MainResult.loading()} } | |
} | |
} | |
fun loadNextPage(): ObservableTransformer<LoadNextPageAction, MainResult> { | |
return ObservableTransformer { | |
actionStream -> actionStream.flatMap {(after) -> mainModel.getNews(after, "10") | |
.map { data -> MainResult.success(data) } | |
.onErrorReturn { error -> MainResult.failure(error.message ?: "Unknown error") } | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.startWith { MainResult.loading()} } | |
} | |
} | |
fun actionToResultTransformer(): ObservableTransformer<MainAction, MainResult> { | |
return ObservableTransformer { | |
actionStream -> actionStream.publish { shared -> Observable.merge( | |
shared.ofType(LoadFirstPageAction::class.java).compose(loadFirstPage()), | |
shared.ofType(LoadNextPageAction::class.java).compose(loadNextPage())) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment