addEncryptedPlacesBatch static method

Future<bool> addEncryptedPlacesBatch(
  1. List<Place> places,
  2. BuildContext context,
  3. Widget child
)

Add multiple encrypted places in a single batch operation. More efficient than calling addEncryptedPlace multiple times. Uses local cache to avoid fetching from server if available.

Implementation

static Future<bool> addEncryptedPlacesBatch(
  List<Place> places,
  BuildContext context,
  Widget child,
) async {
  if (places.isEmpty) return true;

  try {
    // Load persistent flags first.
    if (!_directoryVerified) {
      await _loadPersistentFlags();
    }

    // If no cached data, we MUST ensure security key is available first
    // before fetching existing places. Otherwise, fetchEncryptedPlaces()
    // might return empty list and we'd overwrite existing data.

    if (_cachedEncryptedPlaces == null) {
      if (!await ensureSecurityKey(context, child)) {
        debugPrint(
          'Security key not available, cannot safely add encrypted places',
        );
        return false;
      }
    }

    // Now safe to get existing places (either from cache or fetch with key)
    final existingPlaces =
        _cachedEncryptedPlaces ?? await fetchEncryptedPlaces();
    final updatedPlaces = [...places, ...existingPlaces];
    return await writeEncryptedPlaces(updatedPlaces, context, child);
  } catch (e) {
    debugPrint('Error adding encrypted places batch: $e');
    return false;
  }
}