fetchEncryptedPlacesFromPod function

Future<List<Place>> fetchEncryptedPlacesFromPod()

Read encrypted places from Pod. Optimized: tries to read directly without checking existence first.

Implementation

Future<List<Place>> fetchEncryptedPlacesFromPod() async {
  final places = <Place>[];

  try {
    if (!authStateNotifier.value) {
      return places;
    }

    // Read encrypted content directly using relative path
    // If file doesn't exist, readPod will return fail status
    final filePath = getEncryptedPlacesFilePath();
    final content = await readPod(filePath);

    // Handle non-existent file or errors gracefully.

    if (content == SolidFunctionCallStatus.notLoggedIn.toString() ||
        content == SolidFunctionCallStatus.fail.toString() ||
        content.isEmpty) {
      return places;
    }

    // Parse JSON content directly.

    try {
      final jsonList = jsonDecode(content);
      if (jsonList is List) {
        for (final item in jsonList) {
          if (item is Map<String, dynamic>) {
            final place = Place.fromJson(
              item,
              isLocalSource: false,
              isEncryptedSource: true,
            );
            places.add(place);
          }
        }
      }
    } catch (e) {
      debugPrint('Failed to parse encrypted places JSON: $e');
    }

    places.sort((a, b) => b.timestamp.compareTo(a.timestamp));
  } catch (e) {
    debugPrint('Error fetching encrypted places: $e');
  }

  return places;
}