Skip to content

Instantly share code, notes, and snippets.

@fededri
Last active January 26, 2022 02:49
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 fededri/9736665fbb6c18b14b807aefbc36dae2 to your computer and use it in GitHub Desktop.
Save fededri/9736665fbb6c18b14b807aefbc36dae2 to your computer and use it in GitHub Desktop.
typealias NextResult = Next<MoviesState, MoviesEffects, MovieEvents>
class MoviesUpdater : Updater<MoviesActions, MoviesState, MoviesEffects, MovieEvents> {
override fun onNewAction(
action: MoviesActions,
currentState: MoviesState
): Next<MoviesState, MoviesEffects, MovieEvents> {
return when (action) {
is MoviesActions.FetchMovies -> fetchMovies(action, currentState)
is MoviesActions.SaveMovies -> saveMovies(action, currentState)
is MoviesActions.SelectMovie -> selectMovie(action, currentState)
}
}
private fun selectMovie(action: MoviesActions.SelectMovie, state: MoviesState): NextResult {
return Next.StateWithEvents(
state.copy(selectedMovie = action.movie),
setOf(MovieEvents.OpenSelectedMovie(action.movie))
)
}
private fun saveMovies(action: MoviesActions.SaveMovies, state: MoviesState): NextResult {
return Next.State(state.copy(movies = action.movies))
}
private fun fetchMovies(action: MoviesActions.FetchMovies, state: MoviesState): NextResult {
return Next.StateWithSideEffects(state, setOf(MoviesEffects.LoadMovies(action.type)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment