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

@ -1,5 +1,6 @@
import 'package:comunes_flutter/comunes_flutter.dart';
import 'package:fires_flutter/models/yourLocation.dart';
import 'package:fires_flutter/models/fireNotification.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart';
@ -28,6 +29,7 @@ class AppState extends Object with _$AppStateSerializerMixin {
@JsonKey(ignore: true)
final String firesApiUrl;
final List<YourLocation> yourLocations;
final List<FireNotification> fireNotifications;
@JsonKey(ignore: true)
final FireMapState fireMapState;
@ -37,6 +39,7 @@ class AppState extends Object with _$AppStateSerializerMixin {
AppState(
{this.yourLocations: const <YourLocation>[],
this.fireNotifications: const <FireNotification>[],
this.user: const User.initial(),
this.isLoading: false,
this.isLoaded: false,
@ -55,6 +58,7 @@ class AppState extends Object with _$AppStateSerializerMixin {
String firesApiKey,
String firesApiUrl,
List<YourLocation> yourLocations,
List<FireNotification> fireNotifications,
FireMapState fireMapState}) {
return new AppState(
isLoading: isLoading ?? this.isLoading,
@ -65,6 +69,7 @@ class AppState extends Object with _$AppStateSerializerMixin {
firesApiKey: firesApiKey ?? this.firesApiKey,
firesApiUrl: firesApiUrl ?? this.firesApiUrl,
yourLocations: yourLocations ?? this.yourLocations,
fireNotifications: fireNotifications ?? this.fireNotifications,
fireMapState: fireMapState ?? this.fireMapState);
}
@ -73,24 +78,26 @@ class AppState extends Object with _$AppStateSerializerMixin {
return 'AppState{\nuser: ${user}\nisLoading: $isLoading\nisLoaded: $isLoaded\napiKey: ${ellipse(
firesApiKey, 8)}\napiUrl: ${ellipse(
firesApiUrl, 8)}\nyourLocations count: ${yourLocations
.length}\nyourLocations: ${yourLocations}\nfireMapState: $fireMapState}';
.length}\nyourLocations: ${yourLocations}\nfireNotifications: ${fireNotifications}\nfireMapState: $fireMapState}';
}
}
typedef void AddYourLocationFunction(YourLocation loc);
typedef void DeleteYourLocationFunction(YourLocation loc);
typedef void ToggleSubscriptionFunction(YourLocation loc);
typedef void OnLocationTapFunction(YourLocation loc);
typedef void OnSubscribeFunction(YourLocation loc);
typedef void OnSubscribeDistanceChangeFunction(YourLocation loc);
typedef void OnUnSubscribeFunction(YourLocation loc);
typedef void OnSubscribeConfirmedFunction(YourLocation loc);
typedef void OnLocationEdit(YourLocation loc);
typedef void OnLocationEditing(YourLocation loc);
typedef void OnLocationEditConfirm(YourLocation loc);
typedef void OnLocationEditCancel(YourLocation loc);
typedef void TapFireNotificationFunction(FireNotification notif);
// typedef void OnReceivedFireNotificationFunction(FireNotification notif);
typedef void DeleteFireNotificationFunction(FireNotification notif);
typedef void DeleteAllFireNotificationFunction();
// unused
// typedef void UpdateYourLocationFunction(ObjectId id, YourLocation loc);

View file

@ -11,10 +11,14 @@ part of 'appState.dart';
AppState _$AppStateFromJson(Map<String, dynamic> json) => new AppState(
yourLocations: (json['yourLocations'] as List)
.map((e) => new YourLocation.fromJson(e as Map<String, dynamic>))
.toList(),
fireNotifications: (json['fireNotifications'] as List)
.map((e) => new FireNotification.fromJson(e as Map<String, dynamic>))
.toList());
abstract class _$AppStateSerializerMixin {
List<YourLocation> get yourLocations;
List<FireNotification> get fireNotifications;
Map<String, dynamic> toJson() => new _$AppStateJsonMapWrapper(this);
}
@ -23,7 +27,7 @@ class _$AppStateJsonMapWrapper extends $JsonMapWrapper {
_$AppStateJsonMapWrapper(this._v);
@override
Iterable<String> get keys => const ['yourLocations'];
Iterable<String> get keys => const ['yourLocations', 'fireNotifications'];
@override
dynamic operator [](Object key) {
@ -31,6 +35,8 @@ class _$AppStateJsonMapWrapper extends $JsonMapWrapper {
switch (key) {
case 'yourLocations':
return _v.yourLocations;
case 'fireNotifications':
return _v.fireNotifications;
}
}
return null;

View file

@ -0,0 +1,59 @@
import 'package:bson_objectid/bson_objectid.dart';
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
part 'fireNotification.g.dart';
_objectIdFromJson(String json) {
return new ObjectId.fromHexString(json);
}
_objectIdToJson(ObjectId o) {
return o.toString();
}
@JsonSerializable(nullable: false)
class FireNotification extends Object with _$FireNotificationSerializerMixin {
@JsonKey(toJson: _objectIdToJson, fromJson: _objectIdFromJson)
ObjectId id;
final double lat;
final double lon;
final String description;
final DateTime when;
factory FireNotification.fromJson(Map<String, dynamic> json) => _$FireNotificationFromJson(json);
// static FireNotification noLocation = new FireNotification(lat: 0.0, lon: 0.0, description: '', when: new DateTime(0));
FireNotification({this.id, @required this.lat, @required this.lon, @required this.description, @required this.when}) {
if (this.id == null) this.id = new ObjectId();
}
FireNotification copyWith({id, lat, lon, description, when}) {
return new FireNotification(
id: id ?? this.id,
lat: lat ?? this.lat,
lon: lon ?? this.lon,
description: description ?? this.description,
when: when ?? this.when);
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is FireNotification &&
runtimeType == other.runtimeType &&
id == other.id &&
lat == other.lat &&
lon == other.lon &&
description == other.description &&
when == other.when;
@override
int get hashCode => id.hashCode ^ lat.hashCode ^ lon.hashCode ^ description.hashCode ^ when.hashCode;
@override
String toString() {
return 'FireNotification {id: $id, lat: $lat, lon: $lon, description: $description, when: $when}';
}
}

View file

@ -0,0 +1,54 @@
// Copyright (c) 2018, Comunes Association.
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'fireNotification.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
FireNotification _$FireNotificationFromJson(Map<String, dynamic> json) =>
new FireNotification(
id: _objectIdFromJson(json['id'] as String),
lat: (json['lat'] as num).toDouble(),
lon: (json['lon'] as num).toDouble(),
description: json['description'] as String,
when: DateTime.parse(json['when'] as String));
abstract class _$FireNotificationSerializerMixin {
ObjectId get id;
double get lat;
double get lon;
String get description;
DateTime get when;
Map<String, dynamic> toJson() => new _$FireNotificationJsonMapWrapper(this);
}
class _$FireNotificationJsonMapWrapper extends $JsonMapWrapper {
final _$FireNotificationSerializerMixin _v;
_$FireNotificationJsonMapWrapper(this._v);
@override
Iterable<String> get keys =>
const ['id', 'lat', 'lon', 'description', 'when'];
@override
dynamic operator [](Object key) {
if (key is String) {
switch (key) {
case 'id':
return _objectIdToJson(_v.id);
case 'lat':
return _v.lat;
case 'lon':
return _v.lon;
case 'description':
return _v.description;
case 'when':
return _v.when.toIso8601String();
}
}
return null;
}
}

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);
});
}