todos-contra-el-fuego-mobile/lib/models/fireNotificationsPersist.dart
Vicente J. Ruiz Jurado 5ecc3c3401 Fire notifications
2018-07-26 19:13:12 +02:00

36 lines
1.1 KiB
Dart

import 'dart:async';
import 'dart:convert';
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 = List<FireNotification>();
if (FireNotifications == null) {
FireNotifications = [];
// first run, init with empty list
persistFireNotifications(persistedList);
}
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.forEach((notification) {
notifAsString.add(json.encode(notification.toJson()));
});
prefs.setStringList(fireNotificationKey, notifAsString);
});
}