Skip to content

Instantly share code, notes, and snippets.

@MaxMichel2
Last active June 20, 2024 11:56
Show Gist options
  • Save MaxMichel2/1b34c8a58139c63ea52f1cc28242006f to your computer and use it in GitHub Desktop.
Save MaxMichel2/1b34c8a58139c63ea52f1cc28242006f to your computer and use it in GitHub Desktop.
The actual implementation of the reduce function for the ForYouScreen (from the Now In Android application)
override fun reduce(
previousState: ForYouState,
event: ForYouEvent
): Pair<ForYouState, ForYouEffect?> {
return when (event) {
// An Event that has NO associated Effect
is ForYouEvent.UpdateTopicsLoading -> {
previousState.copy(
topicsLoading = event.isLoading
) to null
}
// An Event that has an associated Effect
is ForYouEvent.UpdateNewsIsViewed -> {
val updatedNews = previousState.news.map { news ->
if (news.id == event.newsId) {
news.copy(hasBeenViewed = event.isViewed)
} else {
news
}
}
previousState.copy(
news = updatedNews
) to ForYouEffect.NavigateToNews(updatedNews.first { it.id == event.newsId }.url)
}
// All other Events go here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment