Skip to content

Instantly share code, notes, and snippets.

@Skyyo
Last active June 19, 2022 17:44
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 Skyyo/731c9fdf3be4c242c649705f50aa077b to your computer and use it in GitHub Desktop.
Save Skyyo/731c9fdf3be4c242c649705f50aa077b to your computer and use it in GitHub Desktop.
class CardsViewModel : ViewModel() {
private val _cards = MutableStateFlow(listOf<ExpandableCardModel>())
val cards: StateFlow<List<ExpandableCardModel>> get() = _cards
private val _expandedCardIdsList = MutableStateFlow(listOf<Int>())
val expandedCardIdsList: StateFlow<List<Int>> get() = _expandedCardIdsList
init {
getFakeData()
}
private fun getFakeData() {
viewModelScope.launch {
withContext(Dispatchers.Default) {
val testList = arrayListOf<ExpandableCardModel>()
repeat(20) { testList += ExpandableCardModel(id = it, title = "Card $it") }
_cards.emit(testList)
}
}
}
fun onCardArrowClicked(cardId: Int) {
_expandedCardIdsList.value = _expandedCardIdsList.value.toMutableList().also { list ->
if (list.contains(cardId)) list.remove(cardId) else list.add(cardId)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment