Skip to content

Instantly share code, notes, and snippets.

@gumil
Created February 17, 2019 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gumil/2529836dbb93e88a7d90f7fef1c5fe5d to your computer and use it in GitHub Desktop.
Save gumil/2529836dbb93e88a7d90f7fef1c5fe5d to your computer and use it in GitHub Desktop.
fun <A : ListAction> Observable<ActionState<A, ListState>>.loadTrending(
mode: ListState.Mode,
offset: Int = 0,
limit: Int = 10,
listStateFunction: (ListState.Screen, List<GiphyItem>) -> ListState.Screen
): Observable<ListState> = this
.map { it.state as ListState.Screen }
.flatMap { state ->
repository.getTrending(offset, limit)
.map { giphies ->
val items = giphies.map { it.mapToItem() }
listStateFunction(state, items)
}
.startWith(state.copy(loadingMode = mode))
}
.ofType(ListState::class.java)
.onErrorReturn {
Timber.e(it, "Error loading gifs")
ListState.Error(R.string.error_loading)
}
.applySchedulers()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment