Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save novodimaporo/eeb6e633de63c5077a3f071d42a5ad28 to your computer and use it in GitHub Desktop.
Save novodimaporo/eeb6e633de63c5077a3f071d42a5ad28 to your computer and use it in GitHub Desktop.
learning rxjava
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