confirmAndDeletePlace function

Future<void> confirmAndDeletePlace({
  1. required MarkerData marker,
  2. required BuildContext context,
  3. required List<Place> allPlaces,
  4. required void setState(
    1. void ()
    ),
})

Confirms and deletes a place with optimistic UI updates.

Implementation

Future<void> confirmAndDeletePlace({
  required MarkerData marker,
  required BuildContext context,
  required List<Place> allPlaces,
  required void Function(void Function()) setState,
}) async {
  final confirmed = await showDeleteConfirmationDialog(context, marker);
  if (!confirmed || !context.mounted) return;

  final prep = prepareDeletePlace(marker: marker, allPlaces: allPlaces);
  if (!prep.success) {
    if (context.mounted) showPlaceNotFoundSnackbar(context);
    return;
  }

  setState(() => allPlaces.removeAt(prep.removedIndex!));
  if (!context.mounted) return;

  // Update cache BEFORE server delete to prevent race condition
  // (placesChangeNotifier triggers _loadAllPlaces which would restore old data)

  updateCacheAfterDelete(allPlaces);

  showDeletingSnackbar(context);
  final success = await performDeleteOnServer(
    placeId: marker.id,
    context: context,
    isEncrypted: marker.isEncrypted,
  );

  if (!context.mounted) return;
  if (success) {
    showDeleteSuccessSnackbar(context);
  } else {
    setState(
      () => restorePlace(
        allPlaces: allPlaces,
        originalIndex: prep.removedIndex!,
        removedPlace: prep.removedPlace!,
      ),
    );
    showDeleteErrorSnackbar(context);
  }
}