todos-contra-el-fuego-mobile/lib/models/fireNotificationsPersist.dart
2026-03-05 01:18:27 +01:00

39 lines
1.3 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:comunes_flutter/comunes_flutter.dart';
import 'package:fires_flutter/models/fireNotification.dart';
import '../globals.dart' as globals;
final String fireNotificationKey = 'fireNotifications';
Future<List<FireNotification>> loadFireNotifications() async {
return await globals.prefs.then((prefs) {
List<String> FireNotifications = prefs.getStringList(fireNotificationKey);
List<FireNotification> persistedList = [];
if (FireNotifications == null) {
FireNotifications = [];
// first run, init with empty list
persistFireNotifications(persistedList);
}
if (FireNotifications is List) {
FireNotifications.forEach((notificationString) {
Map notificationMap = json.decode(notificationString);
persistedList.add(FireNotification.fromJson(notificationMap));
});
}
return persistedList;
});
}
persistFireNotifications(List<FireNotification> notif) {
// print('Persisting $notif');
globals.prefs.then((prefs) {
List<String> notifAsString = [];
notif.where(notNull).toList().forEach((notification) {
notifAsString.add(json.encode(notification.toJson()));
});
prefs.setStringList(fireNotificationKey, notifAsString);
});
}