Fire notifications

This commit is contained in:
Vicente J. Ruiz Jurado 2018-07-26 19:13:12 +02:00
parent c680482b08
commit 5ecc3c3401
22 changed files with 524 additions and 31 deletions

View file

@ -0,0 +1,36 @@
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);
});
}