Chris Fillmore
10/04/2021, 9:00 PMdata class MyDomainObject(
val id: String,
val enabled: StateFlow<Boolean>
)
val myDomainObjects: StateFlow<List<MyDomainObject> = /* This data comes from somewhere */
// I would like a list of objects which have `enabled.value == true`, and
// I would like this to be updated any time any `enabled` state changes
val enabledObjects: Flow<List<MyDomainObject> = myDomainObjects.transform { objects ->
combine(*objects.map { it.enabled }.toTypedArray()) { enabledStates ->
emit(objects.filterIndexed { index, _ -> enabledStates[index] })
}
}
The implementation seems slightly wrong, the way I’m using index
to emit objects which are enabled. But I don’t see another way to do this. Any advice? Thanks in advance.ephemient
10/04/2021, 9:03 PMmyDomainObjects.map { list -> list.filter { it.enabled } }
?Chris Fillmore
10/04/2021, 9:04 PMmyDomainObjects
updates, not when the individual enabled
states updateephemient
10/04/2021, 9:07 PMtransformLatest
or similar so that you switch to a new set of objects when emittedChris Fillmore
10/04/2021, 9:19 PMephemient
10/05/2021, 1:09 AMmyDomainObjects.flatMapLatest { objects ->
combine(
objects.map { object ->
object.enabled.map { if (it) object else null }
}
) { it.filterNotNull() }
}