Skip to content

Instantly share code, notes, and snippets.

@Nimrodda
Last active June 8, 2019 12:17
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 Nimrodda/1a60392402d681ed956e35cfc4e4599c to your computer and use it in GitHub Desktop.
Save Nimrodda/1a60392402d681ed956e35cfc4e4599c to your computer and use it in GitHub Desktop.
class GamesListViewModel : ViewModel() {
val gamesLiveData: LiveData<Container>
get() = _liveData
private val _liveData = MutableLiveData<Container>()
private val onGenreExpanded: OnGenreExpanded = { genre: Genre ->
val oldContainer = _liveData.value
if (oldContainer != null) {
val newGenres = oldContainer.genres.map {
if (it.genre.id == genre.id) {
it.copy(genre = it.genre.copy(isExpanded = !genre.isExpanded))
} else {
it
}
}
_liveData.value = oldContainer.copy(genres = newGenres)
}
}
init {
_liveData.value = Container(
listOf(
GamesPerGenre(
Genre(name = "Action RPG Games"),
listOf(
Game("Dark Souls"),
Game("Diablo"),
Game("Bloodborne"),
Game("Sekiro")
)
),
GamesPerGenre(
Genre(name = "FPS Games"),
listOf(
Game("Doom"),
Game("Borderlands"),
Game("Battlefield"),
Game("Call of Duty")
)
)
),
onGenreExpanded
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment