Last active
January 19, 2020 01:07
-
-
Save Rasalexman/1796e785af480cc4eedb7852ebc54dcd to your computer and use it in GitHub Desktop.
PageRepository with Flow
This file contains 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
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