updatePlace static method

Future<bool> updatePlace(
  1. Place updated,
  2. BuildContext context,
  3. Widget returnWidget, {
  4. bool coordinatesChanged = false,
})

Implementation

static Future<bool> updatePlace(
  Place updated,
  BuildContext context,
  Widget returnWidget, {
  bool coordinatesChanged = false,
}) async {
  try {
    if (!authStateNotifier.value) return false;
    final existing = await fetchPodPlaces();
    final list = List<Place>.from(existing);
    final i = list.indexWhere((p) => p.id == updated.id);
    var toSave = updated;

    if (i == -1) {
      list.insert(0, updated);
    } else {
      if (coordinatesChanged) {
        final addr = await GeocodingService.getAddress(
          updated.lat,
          updated.lng,
        );
        toSave = Place(
          id: updated.id,
          lat: updated.lat,
          lng: updated.lng,
          note: updated.note,
          timestamp: updated.timestamp,
          address: addr,
          isLocal: false,
        );
      }
      list[i] = toSave;
    }

    // Update both main file and individual file in parallel.
    final results = await Future.wait([
      writePlacesJsonFile(jsonEncode(list.map((p) => p.toJson()).toList())),
      writeIndividualPlaceFile(toSave),
    ]);
    final success = results[0];

    if (success) {
      await clearCache();
      placesChangeNotifier.value++;

      // Invalidate directory cache and notify file browser.

      PodDirectoryService.invalidateCache('data/places');
      PodDirectoryService.notifyChange();
    }
    return success;
  } catch (_) {
    return false;
  }
}