optimitic update in flutter

This commit is contained in:
2026-03-02 11:17:58 +01:00
parent dbfb29703c
commit 9d5ea49d85

View File

@@ -122,21 +122,41 @@ class _MainAppState extends State<MainApp> {
String newGroupId, String newGroupId,
int newLane, int newLane,
) { ) {
final payload = <String, Object?>{ final duration = entry.end.difference(entry.start);
final newEnd = entry.hasEnd ? newStart.add(duration) : null;
// Optimistic update apply locally before the host round-trips.
if (_state case final state?) {
final oldItem = state.items[entry.id];
if (oldItem != null) {
final updatedItems = Map<String, TimelineItemData>.of(state.items);
updatedItems[entry.id] = TimelineItemData(
id: oldItem.id,
groupId: newGroupId,
title: oldItem.title,
description: oldItem.description,
start: newStart.toIso8601String(),
end: newEnd?.toIso8601String(),
lane: newLane,
);
final updatedState = TimelineState(
timeline: state.timeline,
groups: state.groups,
items: updatedItems,
groupOrder: state.groupOrder,
selectedItemId: state.selectedItemId,
);
_applyState(updatedState);
}
}
emitEvent('entry_moved', <String, Object?>{
'entryId': entry.id, 'entryId': entry.id,
'newStart': newStart.toIso8601String(), 'newStart': newStart.toIso8601String(),
'newGroupId': newGroupId, 'newGroupId': newGroupId,
'newLane': newLane, 'newLane': newLane,
}; 'newEnd': newEnd?.toIso8601String(),
});
if (entry.hasEnd) {
final duration = entry.end.difference(entry.start);
payload['newEnd'] = newStart.add(duration).toIso8601String();
} else {
payload['newEnd'] = null;
}
emitEvent('entry_moved', payload);
} }
void _emitContentHeight() { void _emitContentHeight() {