Skip to content

Instantly share code, notes, and snippets.

@Skyyo
Last active June 19, 2022 17:39
Show Gist options
  • Save Skyyo/61ccc09671cd6c81b42e505b63b235c7 to your computer and use it in GitHub Desktop.
Save Skyyo/61ccc09671cd6c81b42e505b63b235c7 to your computer and use it in GitHub Desktop.
class CardsScreenViewModel : ViewModel() {
private val _cards = MutableStateFlow(listOf<CardModel>())
val cards: StateFlow<List<CardModel>> get() = _cards
private val _revealedCardIdsList = MutableStateFlow(listOf<Int>())
val revealedCardIdsList: StateFlow<List<Int>> get() = _revealedCardIdsList
init {
getFakeData()
}
private fun getFakeData() {
viewModelScope.launch {
withContext(Dispatchers.Default) {
val testList = arrayListOf<CardModel>()
repeat(20) { testList += CardModel(id = it, title = "Card $it") }
_cards.emit(testList)
}
}
}
fun onItemExpanded(cardId: Int) {
if (_revealedCardIdsList.value.contains(cardId)) return
_revealedCardIdsList.value = _revealedCardIdsList.value.toMutableList().also { list ->
list.add(cardId)
}
}
fun onItemCollapsed(cardId: Int) {
if (!_revealedCardIdsList.value.contains(cardId)) return
_revealedCardIdsList.value = _revealedCardIdsList.value.toMutableList().also { list ->
list.remove(cardId)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment