Fire notification dialog. Connectivity dialgo

This commit is contained in:
Vicente J. Ruiz Jurado 2018-08-03 23:07:13 +02:00
parent e67256c010
commit 10b8680490
7 changed files with 184 additions and 140 deletions

View file

@ -1,12 +1,6 @@
import 'dart:async';
import 'package:bson_objectid/bson_objectid.dart';
import 'package:comunes_flutter/comunes_flutter.dart';
import 'package:connectivity/connectivity.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:fires_flutter/models/fireNotification.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
@ -53,36 +47,11 @@ class _FiresAppState extends State<FiresApp> {
};
final Store<AppState> store;
String _connectionStatus = 'Unknown';
final Connectivity _connectivity = new Connectivity();
StreamSubscription<ConnectivityResult> _connectivitySubscription;
// globals.getIt.registerSingleton
_FiresAppState(this.store);
// Platform messages are asynchronous, so we initialize in an async method.
Future<Null> initConnectivity() async {
String connectionStatus;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
connectionStatus = (await _connectivity.checkConnectivity()).toString();
} on PlatformException catch (e) {
print(e.toString());
connectionStatus = 'Failed to get connectivity.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) {
return;
}
setState(() {
// store.dispatch(action);
_connectionStatus = connectionStatus;
});
}
@override
Widget build(BuildContext context) {
@ -112,102 +81,4 @@ class _FiresAppState extends State<FiresApp> {
theme: firesTheme,
routes: routes));
}
@override
void initState() {
super.initState();
_firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
debugPrint("onMessage in fireApp: $message");
_showItemDialog(message, store);
}, onLaunch: (Map<String, dynamic> message) {
debugPrint("onLaunch: $message");
_navigateToItemDetail(message, store);
}, onResume: (Map<String, dynamic> message) {
debugPrint("onResume: $message");
_navigateToItemDetail(message, store);
});
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
store.dispatch(new OnUserTokenAction(token));
setState(() {});
});
initConnectivity();
_connectivitySubscription =
_connectivity.onConnectivityChanged.listen((ConnectivityResult result) {
setState(() => _connectionStatus = result.toString());
});
}
void _showItemDialog(Map<String, dynamic> message, Store store) {
final notif = _notifForMessage(message);
showDialog<bool>(
context: navigatorKey.currentContext,
builder: (_) => _buildDialog(navigatorKey.currentContext, notif),
).then((bool shouldNavigate) {
if (shouldNavigate == true) {
_navigateToItemDetail(message, store);
}
}).catchError((e) => print("$e"));
}
Widget _buildDialog(BuildContext context, FireNotification item) {
return new AlertDialog(
content: new Text(item.description),
actions: <Widget>[
new FlatButton(
child: Text(S.of(context).CLOSE),
onPressed: () {
Navigator.pop(context, false);
},
),
new FlatButton(
child: Text(S.of(context).SHOW),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
}
void _navigateToItemDetail(Map<String, dynamic> message, Store store) {
FireNotification notif = _notifForMessage(message);
// Clear away dialogs
Navigator.popUntil(navigatorKey.currentContext,
(Route<dynamic> route) => route is PageRoute);
if (!notif.getRoute(store).isCurrent) {
Navigator.push(navigatorKey.currentContext, notif.getRoute(store));
}
}
// https://pub.dartlang.org/packages/firebase_messaging#-example-tab-
final FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
FireNotification _notifForMessage(Map<String, dynamic> message) {
FireNotification notif;
// print('start parse notif');
try {
notif = new FireNotification(
id: new ObjectId.fromHexString(message['id']),
subsId: new ObjectId.fromHexString(message['subsId']),
lat: double.parse(message['lat']),
lon: double.parse(message['lon']),
description: message['description'],
read: false,
when: DateTime.parse(message['when']),
sealed: message['sealed']);
// print('end parse notif');
debugPrint(notif.toString());
} catch (e) {
debugPrint(e.toString());
}
if (notif != null) store.dispatch(new AddFireNotificationAction(notif));
return notif;
}
}