100 lines
3.1 KiB
Dart
100 lines
3.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class ClaimsOverviewCacheEntry {
|
|
final String policyId;
|
|
final Map<String, dynamic> claimsKpiBySlug;
|
|
final Map<String, dynamic> enrollmentKpiBySlug;
|
|
/// Raw `generated_at` from the claims API (e.g. `01-07-2026`), if present.
|
|
final String? generatedAt;
|
|
final String? claimsLoadError;
|
|
final String? enrollmentLoadError;
|
|
|
|
const ClaimsOverviewCacheEntry({
|
|
required this.policyId,
|
|
required this.claimsKpiBySlug,
|
|
required this.enrollmentKpiBySlug,
|
|
this.generatedAt,
|
|
this.claimsLoadError,
|
|
this.enrollmentLoadError,
|
|
});
|
|
|
|
factory ClaimsOverviewCacheEntry.fromJson(Map<String, dynamic> json) {
|
|
final raw = json['generatedAt']?.toString().trim();
|
|
return ClaimsOverviewCacheEntry(
|
|
policyId: json['policyId']?.toString() ?? '',
|
|
claimsKpiBySlug: Map<String, dynamic>.from(json['claimsKpiBySlug'] ?? {}),
|
|
enrollmentKpiBySlug:
|
|
Map<String, dynamic>.from(json['enrollmentKpiBySlug'] ?? {}),
|
|
generatedAt: (raw != null && raw.isNotEmpty) ? raw : null,
|
|
claimsLoadError: json['claimsLoadError']?.toString(),
|
|
enrollmentLoadError: json['enrollmentLoadError']?.toString(),
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
'policyId': policyId,
|
|
'claimsKpiBySlug': claimsKpiBySlug,
|
|
'enrollmentKpiBySlug': enrollmentKpiBySlug,
|
|
'generatedAt': generatedAt,
|
|
'claimsLoadError': claimsLoadError,
|
|
'enrollmentLoadError': enrollmentLoadError,
|
|
};
|
|
}
|
|
|
|
abstract final class ClaimsOverviewCache {
|
|
static const _keyPrefix = 'claims_overview_cache_v2_';
|
|
|
|
static String _key(String branchId, String policyId) =>
|
|
'$_keyPrefix${branchId}_$policyId';
|
|
|
|
static Future<ClaimsOverviewCacheEntry?> read(
|
|
String branchId,
|
|
String policyId,
|
|
) async {
|
|
if (branchId.isEmpty || policyId.isEmpty) return null;
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final raw = prefs.getString(_key(branchId, policyId));
|
|
if (raw == null || raw.isEmpty) return null;
|
|
try {
|
|
final decoded = jsonDecode(raw);
|
|
if (decoded is! Map) return null;
|
|
return ClaimsOverviewCacheEntry.fromJson(
|
|
Map<String, dynamic>.from(decoded),
|
|
);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static Future<void> write(
|
|
String branchId,
|
|
ClaimsOverviewCacheEntry entry,
|
|
) async {
|
|
if (branchId.isEmpty || entry.policyId.isEmpty) return;
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(
|
|
_key(branchId, entry.policyId),
|
|
jsonEncode(entry.toJson()),
|
|
);
|
|
}
|
|
|
|
static Future<void> clear(String branchId, String policyId) async {
|
|
if (branchId.isEmpty || policyId.isEmpty) return;
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove(_key(branchId, policyId));
|
|
}
|
|
|
|
static Future<void> clearAll() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final keys = prefs
|
|
.getKeys()
|
|
.where((key) => key.startsWith(_keyPrefix))
|
|
.toList();
|
|
for (final key in keys) {
|
|
await prefs.remove(key);
|
|
}
|
|
}
|
|
}
|