handleOptimisticPlaceSave function

void handleOptimisticPlaceSave({
  1. required Place place,
  2. required List<Place> allPlaces,
  3. required Set<String> savingPlaceIds,
  4. required BuildContext context,
  5. required void setState(
    1. void ()
    ),
  6. required Future<void> performBackgroundSave(
    1. Place
    ),
  7. bool encrypted = false,
})

Handles optimistic save of a place.

Updates UI immediately, then performs background save. If encrypted is true, the place will be marked as encrypted for immediate purple marker display.

Implementation

void handleOptimisticPlaceSave({
  required Place place,
  required List<Place> allPlaces,
  required Set<String> savingPlaceIds,
  required BuildContext context,
  required void Function(void Function()) setState,
  required Future<void> Function(Place) performBackgroundSave,
  bool encrypted = false,
}) {
  // Mark place as encrypted if saving to encrypted storage.
  final placeToSave = encrypted ? place.copyWith(isEncrypted: true) : place;

  // Update state first.

  setState(() {
    allPlaces.insert(0, placeToSave);
    savingPlaceIds.add(placeToSave.id);
  });

  // Show snackbar after frame to avoid jank.

  SchedulerBinding.instance.addPostFrameCallback((_) {
    if (context.mounted) {
      showSavingSnackbar(context, placeToSave);
    }
  });

  // Start background save.

  unawaited(performBackgroundSave(placeToSave));
}