Last active
June 19, 2022 17:39
-
-
Save Skyyo/61ccc09671cd6c81b42e505b63b235c7 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 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