Hamba
11/25/2023, 3:56 AMZach Klippenstein (he/him) [MOD]
11/25/2023, 4:20 AMZach Klippenstein (he/him) [MOD]
11/25/2023, 4:21 AMHamba
11/25/2023, 4:35 AMsegments
and i want to run history.addSnapshot(segments)
every time segments
is changed, so i can keep a list of the history of segments
class TimelineModel {
var segments = mutableStateListOf<TimelineSegment>()
var history = TimelineHistory()
so for the snapshotflow, i tried this but it doesnt trigger when segments has an element added/removed
LaunchedEffect(viewModel.timelineModel.segments) {
snapshotFlow {
viewModel.timelineModel.history.addSnapshot(viewModel.timelineModel.segments)
}
}
Hamba
11/25/2023, 4:37 AMHamba
11/25/2023, 4:43 AMLaunchedEffect(viewModel.timelineModel.segments) {
snapshotFlow { viewModel.timelineModel.segments }
.collect { new ->
viewModel.timelineModel.history.addSnapshot(new)
println(viewModel.timelineModel.history)
}
}
Hamba
11/25/2023, 4:52 AMLaunchedEffect(viewModel.timelineModel.segments.toList()) {
viewModel.timelineModel.history.addSnapshot(viewModel.timelineModel.segments)
}
This ends up working, but it still feels wrong to put something like this in the UI at all. its not UI related, and i feel there should be a way to make this update occur just within the viewmodelHamba
11/25/2023, 4:52 AMandriyo
11/25/2023, 5:41 AMclass TimelineViewModel : ViewModel() {
val timelineModel = TimelineModel()
init {
// Observe changes in segments
viewModelScope.launch {
snapshotFlow { timelineModel.segments }
.collect { currentSegments ->
// Update history whenever segments change
timelineModel.history.addRecord(currentSegments)
}
}
}
}
Hamba
11/25/2023, 6:05 AMHamba
11/25/2023, 6:13 AM