Skip to content

Instantly share code, notes, and snippets.

@rygelouv
Last active April 24, 2022 16:35
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 rygelouv/f748fdb043246f08f68f9264f88e34d7 to your computer and use it in GitHub Desktop.
Save rygelouv/f748fdb043246f08f68f9264f88e34d7 to your computer and use it in GitHub Desktop.
class MoviesViewModel @Inject constructor(private val mapper: MoviesUIMapper) : ViewModel() {
private val _actionFlow =
MutableSharedFlow<MoviesAction>(extraBufferCapacity = 16)
init {
_actionFlow.process().launchIn(viewModelScope)
}
private fun Flow<MoviesAction>.process() = onEach {
when(it) {
is MoviesAction.SearchMovieAction -> onSearchMovie(it.query)
is MoviesAction.AddToHistoryAction -> onAddToHistory(it.movieId)
MoviesAction.RefreshMoviesAction -> onRefreshMovies()
}
}
/**
* This is the only function exposed to the view.
*/
fun processAction(action: MoviesAction) = _actionFlow.tryEmit(action)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment