Skip to content

Instantly share code, notes, and snippets.

@vikasmain
Created January 14, 2022 07:05
Show Gist options
  • Save vikasmain/f3fb0b191e1f1f910da56c3bbcee270f to your computer and use it in GitHub Desktop.
Save vikasmain/f3fb0b191e1f1f910da56c3bbcee270f to your computer and use it in GitHub Desktop.
@MovieActivityScope
class MoviePresenter @Inject constructor(
private val movieView: MovieContract.MovieView,
private val scope: CoroutineScope,
private val movieModel: MovieModel
) {
fun onAttach() {
callMoviesApi()
observeOnClickStateFlow()
}
internal fun callMoviesApi() {
movieModel.callMoviesApi()
.onStart {
movieView.showLoadingView()
}
.catch {
movieView.showErrorView()
}
.onEach {
movieView.showMoviesList(it)
}
.onCompletion {
movieView.hideLoadingView()
}
.launchIn(scope)
}
internal fun observeOnClickStateFlow() {
MovieStateFlow.onClickStateFlow.asStateFlow()
.onEach {
it?.let {
movieView.openSingleItemView(it)
}
}
.catch {
Log.e("MovieActivity", "Error while observing stateflow$it")
}.launchIn(scope)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment