-
-
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)
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
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