performPlaceBackgroundSave function
Performs background save and updates UI on completion.
Implementation
Future<void> performPlaceBackgroundSave({
required Place originalPlace,
required BuildContext context,
required List<Place> allPlaces,
required Set<String> savingPlaceIds,
required void Function(void Function()) setState,
bool encrypted = false,
}) async {
try {
final updatedPlace = await performBackgroundSave(
originalPlace,
context,
encrypted: encrypted,
);
if (!context.mounted) return;
if (updatedPlace != null) {
// Schedule state update after current frame to avoid animation jank.
SchedulerBinding.instance.addPostFrameCallback((_) {
if (!context.mounted) return;
setState(() {
final index = allPlaces.indexWhere((x) => x.id == originalPlace.id);
if (index != -1) allPlaces[index] = updatedPlace;
savingPlaceIds.remove(originalPlace.id);
});
PlacesCacheManager().cacheAllPlaces(allPlaces);
showSaveSuccessSnackbar(context);
});
}
} catch (e) {
if (!context.mounted) return;
// Schedule error handling after current frame.
SchedulerBinding.instance.addPostFrameCallback((_) {
if (!context.mounted) return;
setState(() {
allPlaces.removeWhere((x) => x.id == originalPlace.id);
savingPlaceIds.remove(originalPlace.id);
});
showSaveErrorSnackbar(context, e);
});
}
}