getCachedPodPlaces static method

Future<List<Place>?> getCachedPodPlaces()

Get cached Pod places from SharedPreferences.

Returns null if no cache exists or cache is too old.

Implementation

static Future<List<Place>?> getCachedPodPlaces() async {
  try {
    final prefs = await SharedPreferences.getInstance();

    // Check cache age.
    final timestamp = prefs.getInt(_podPlacesCacheTimestampKey);
    if (timestamp != null) {
      final cacheTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
      final age = DateTime.now().difference(cacheTime);
      if (age.inHours > _maxCacheAgeHours) {
        // Cache is too old, return null to trigger refresh
        return null;
      }
    }

    final cachedJson = prefs.getString(_podPlacesCacheKey);
    if (cachedJson == null || cachedJson.isEmpty) return null;

    final decoded = jsonDecode(cachedJson);
    final places = <Place>[];

    if (decoded is List) {
      for (final item in decoded) {
        if (item is Map<String, dynamic>) {
          try {
            places.add(Place.fromJson(item, isLocalSource: false));
          } catch (_) {
            // Skip malformed entries.
          }
        }
      }
    }

    return places.isEmpty ? null : places;
  } catch (_) {
    return null;
  }
}