Skip to content

Instantly share code, notes, and snippets.

@uteke
Last active September 2, 2023 08:15
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 uteke/6b265cf7b524ce5e0946e3ee1f16e25c to your computer and use it in GitHub Desktop.
Save uteke/6b265cf7b524ce5e0946e3ee1f16e25c to your computer and use it in GitHub Desktop.
Default reducer implementation
class DefaultReducer(
private val resources: Resources,
) : Reducer<Mutation, ViewState> {
override fun invoke(mutation: Mutation, currentState: ViewState): ViewState =
when (mutation) {
Mutation.DismissLostConnection ->
currentState.mutateToDismissLostConnection()
is Mutation.ShowContent ->
currentState.mutateToShowContent(users = mutation.users)
is Mutation.ShowError ->
currentState.mutateToShowError(exception = mutation.exception)
Mutation.ShowLoader ->
currentState.mutateToShowLoader()
Mutation.ShowLostConnection ->
currentState.mutateToShowLostConnection()
}
private fun ViewState.mutateToShowContent(users: List<UserDataModel>) =
copy(
isLoaderVisible = false,
isUserListVisible = true,
userStates = userStates.toMutableList().apply {
addAll(users.map { it.toUserState() })
},
isErrorVisible = false,
)
private fun ViewState.mutateToShowError(exception: Exception) =
copy(
isLoaderVisible = false,
isUserListVisible = false,
isErrorVisible = true,
errorMessage = resources.getString(R.string.userlist_text_generic_error)
.format(exception.message),
)
private fun ItemModel.toItemState() = ...
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment