-
-
Save uteke/6b265cf7b524ce5e0946e3ee1f16e25c to your computer and use it in GitHub Desktop.
Default reducer implementation
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 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