writeEncryptedPlaces static method

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

Write encrypted places to Pod. Optimized: Uses persistent directoryVerified flag to skip repeated directory status checks across app sessions.

Implementation

static Future<bool> writeEncryptedPlaces(
  List<Place> places,
  BuildContext context,
  Widget child,
) async {
  // Ensure security key is available.
  if (!await ensureSecurityKey(context, child)) {
    return false;
  }

  // Write to Pod using IO helper
  // The directoryVerified flag (loaded from persistent storage) prevents
  // repeated checkResourceStatus calls for the directory.
  final (success, dirCreated) = await writeEncryptedPlacesToPod(
    places,
    _directoryVerified,
  );
  if (success) {
    // Update and persist directory verification flag if write succeeded.
    if (!_directoryVerified) {
      await _saveDirVerifiedFlag(true);
    }
    _cachedEncryptedPlaces = places;

    // Notify places change to trigger UI refresh.

    placesChangeNotifier.value++;

    // If directory was newly created, update security key cache and notify UI
    // (directory creation confirms encryption keys are available)

    if (dirCreated) {
      _securityKeyAvailableCache = true; // Keys are now known to be available
      if (context.mounted) {
        const SecurityKeyStatusChangedNotification(
          isKeySaved: true,
        ).dispatch(context);
        debugPrint(
          'Directory created, security key status notification dispatched',
        );
      }
    }
  } else {
    // Error recovery: If write failed while assuming directory was verified,
    // clear the persisted flag to force re-verification on next attempt.
    // This handles cases where the directory was deleted on the server side
    // (e.g., manual cleanup or by another client) without the app knowing.
    if (_directoryVerified) {
      await _saveDirVerifiedFlag(false);
      debugPrint('Write failed, cleared directory verified flag for retry');
    }
  }
  return success;
}