Skip to content

Instantly share code, notes, and snippets.

@Rasalexman
Last active January 19, 2020 01:07
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 Rasalexman/1796e785af480cc4eedb7852ebc54dcd to your computer and use it in GitHub Desktop.
Save Rasalexman/1796e785af480cc4eedb7852ebc54dcd to your computer and use it in GitHub Desktop.
PageRepository with Flow
class PageRepository(
private val simpleApi: ISimpleApi
) : IPageRepository {
override suspend fun getNextPageData(eventPageLiveData: LiveData<ViewEvent<Int>>): Flow<Result<List<MyUIModel>>> {
return flow {
// convert liveData to Flow
eventPageLiveData.asFlow().collect { viewEvent ->
val viewPage = viewEvent.data
// emit success state
emit(Result.success(simpleApi.getListData(viewPage)))
}
}.catch {
// log some error or retur error state with emit(error)
emit(Result.failure<List<MyUIModel>>(it))
}.flowOn(Dispatchers.IO)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment