Last active
June 19, 2022 17:44
-
-
Save Skyyo/731c9fdf3be4c242c649705f50aa077b to your computer and use it in GitHub Desktop.
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
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