Justin Xu
01/21/2023, 7:05 AMvar selectedGroup: MutableStateFlow<Group?> = MutableStateFlow(null)
...
private val _userInfosFlow = APIServer.getAllUserInfosAsFlow()
val userInfos: StateFlow<List<UserInfo>> =
_userInfosFlow.map { userInfos ->
selectedGroup.value?.let { group ->
userInfos.filter { userInfo ->
userInfo.groupId == group.getId()
}
} ?: emptyList()
}
Where _userInfosFlow
just gets a list of all UserInfo objects from database, and userInfos
is a StateFlow that filters that list to only contain UserInfo's belonging to the current selectedGroup
. Since I use .map
, I believe the userInfos
flow will emit only when _userInfosFlow
emits. However, I also want userInfos
to emit when selectedGroup.value
changes, since that means that a new group is selected, and userInfos
should logically update to now contain UserInfo's from the newly selected group. What is the best way to trigger this emission? Or is there just a better way to do this altogetherAlbert Chang
01/21/2023, 7:35 AMcombine(_userInfosFlow, selectedGroup) { userInfos, group ->
...
}