Last active
April 24, 2022 16:35
-
-
Save rygelouv/f748fdb043246f08f68f9264f88e34d7 to your computer and use it in GitHub Desktop.
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 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