My app has three flows, namely account, timeline, timelinePosition
val activeAccountFlow = accountDao
.getActiveAccountFlow()
.filterNotNull()
.distinctUntilChanged { old, new -> old.id == new.id }
val timeline = activeAccountFlow
.flatMapLatest { timelineDao.getStatusListWithFlow(it.id) }
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = persistentListOf()
)
val timelinePosition = activeAccountFlow
.mapLatest { TimelinePosition(it.index, it.offset) }
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = TimelinePosition()
)
data class TimelinePosition(val index: Int = 0, val offset: Int = 0)
Where timeline, timeline Position flow depends on account flow
What I want to do now is: when the ID of the account changes, their values can be emitted together synchronously instead of asynchronously. What should I do?
The timeline should be Flow<List<…>> at the end, because I need to collect it in the UI part to get updates to the list, but it also needs to re-emit the entire flow depending on whether the account changes (because different accounts have different Lists , but the List of the same account will also be updated due to UI operations (such as paging requests))
This is a complex data flow and it bothered me for a long time and I don’t know what to do now