addPlace static method

Future<bool> addPlace(
  1. Place place,
  2. BuildContext context,
  3. Widget returnWidget
)

Implementation

static Future<bool> addPlace(
  Place place,
  BuildContext context,
  Widget returnWidget,
) async {
  try {
    if (!authStateNotifier.value) return false;
    final cm = PlacesCacheManager();
    var existing = cm.podPlaces ?? await fetchPodPlaces();
    final updated = List<Place>.from(existing)..insert(0, place);

    // Write main file first to ensure it succeeds.
    final mainSuccess = await writePlacesJsonFile(
      jsonEncode(updated.map((p) => p.toJson()).toList()),
    );

    if (mainSuccess) {
      // Write individual file (don't block on failure)
      final individualSuccess = await writeIndividualPlaceFile(place);
      debugPrint(
        'addPlace: main=$mainSuccess, individual=$individualSuccess',
      );

      await clearCache();
      placesChangeNotifier.value++;

      // Clear directory cache completely to force refresh.

      PodDirectoryService.clearCache();
      PodDirectoryService.notifyChange();
    }
    return mainSuccess;
  } catch (e) {
    debugPrint('Error in addPlace: $e');
    return false;
  }
}