From 8025d9c3d5f175be9f3e6f17fa68bea59de027bb Mon Sep 17 00:00:00 2001 From: "Vicente J. Ruiz Jurado" Date: Fri, 3 Aug 2018 08:47:16 +0200 Subject: [PATCH] Connection check (wip) --- lib/firesApp.dart | 52 +++++++++++++++++++++++++++++++++------ lib/redux/appActions.dart | 4 ++- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/lib/firesApp.dart b/lib/firesApp.dart index 74eb27a..420e3b1 100644 --- a/lib/firesApp.dart +++ b/lib/firesApp.dart @@ -1,8 +1,12 @@ +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'; @@ -49,10 +53,37 @@ class _FiresAppState extends State { }; final Store store; + String _connectionStatus = 'Unknown'; + final Connectivity _connectivity = new Connectivity(); + StreamSubscription _connectivitySubscription; // globals.getIt.registerSingleton _FiresAppState(this.store); + // Platform messages are asynchronous, so we initialize in an async method. + Future 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) { StatefulWidget home = new MaterialAppWithIntroHome( @@ -86,13 +117,13 @@ class _FiresAppState extends State { void initState() { super.initState(); _firebaseMessaging.configure(onMessage: (Map message) { - print("onMessage in fireApp: $message"); + debugPrint("onMessage in fireApp: $message"); _showItemDialog(message, store); }, onLaunch: (Map message) { - print("onLaunch: $message"); + debugPrint("onLaunch: $message"); _navigateToItemDetail(message, store); }, onResume: (Map message) { - print("onResume: $message"); + debugPrint("onResume: $message"); _navigateToItemDetail(message, store); }); _firebaseMessaging.requestNotificationPermissions( @@ -106,6 +137,11 @@ class _FiresAppState extends State { store.dispatch(new OnUserTokenAction(token)); setState(() {}); }); + initConnectivity(); + _connectivitySubscription = + _connectivity.onConnectivityChanged.listen((ConnectivityResult result) { + setState(() => _connectionStatus = result.toString()); + }); } void _showItemDialog(Map message, Store store) { @@ -155,23 +191,23 @@ class _FiresAppState extends State { FireNotification _notifForMessage(Map message) { FireNotification notif; - print('start parse 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'], - subsId: new ObjectId.fromHexString(message['subsId'])); - print('end parse notif'); + sealed: message['sealed']); + // print('end parse notif'); debugPrint(notif.toString()); } catch (e) { debugPrint(e.toString()); } - store.dispatch(new AddFireNotificationAction(notif)); + if (notif != null) store.dispatch(new AddFireNotificationAction(notif)); return notif; } } diff --git a/lib/redux/appActions.dart b/lib/redux/appActions.dart index 55f6765..8eb718b 100644 --- a/lib/redux/appActions.dart +++ b/lib/redux/appActions.dart @@ -49,4 +49,6 @@ class FetchFireNotificationsSucceededAction extends AppActions { final int unreadCount; FetchFireNotificationsSucceededAction(this.fetchedFireNotifications, this.unreadCount); -} \ No newline at end of file +} + +// OnConnect \ No newline at end of file