Last active
June 8, 2019 12:17
-
-
Save Nimrodda/1a60392402d681ed956e35cfc4e4599c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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