Connection check (wip)
This commit is contained in:
parent
0c1aa9c791
commit
8025d9c3d5
2 changed files with 47 additions and 9 deletions
|
|
@ -1,8 +1,12 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:bson_objectid/bson_objectid.dart';
|
import 'package:bson_objectid/bson_objectid.dart';
|
||||||
import 'package:comunes_flutter/comunes_flutter.dart';
|
import 'package:comunes_flutter/comunes_flutter.dart';
|
||||||
|
import 'package:connectivity/connectivity.dart';
|
||||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||||
import 'package:fires_flutter/models/fireNotification.dart';
|
import 'package:fires_flutter/models/fireNotification.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||||
import 'package:flutter_redux/flutter_redux.dart';
|
import 'package:flutter_redux/flutter_redux.dart';
|
||||||
import 'package:redux/redux.dart';
|
import 'package:redux/redux.dart';
|
||||||
|
|
@ -49,10 +53,37 @@ class _FiresAppState extends State<FiresApp> {
|
||||||
};
|
};
|
||||||
|
|
||||||
final Store<AppState> store;
|
final Store<AppState> store;
|
||||||
|
String _connectionStatus = 'Unknown';
|
||||||
|
final Connectivity _connectivity = new Connectivity();
|
||||||
|
StreamSubscription<ConnectivityResult> _connectivitySubscription;
|
||||||
|
|
||||||
// globals.getIt.registerSingleton
|
// globals.getIt.registerSingleton
|
||||||
_FiresAppState(this.store);
|
_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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
StatefulWidget home = new MaterialAppWithIntroHome(
|
StatefulWidget home = new MaterialAppWithIntroHome(
|
||||||
|
|
@ -86,13 +117,13 @@ class _FiresAppState extends State<FiresApp> {
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
|
_firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
|
||||||
print("onMessage in fireApp: $message");
|
debugPrint("onMessage in fireApp: $message");
|
||||||
_showItemDialog(message, store);
|
_showItemDialog(message, store);
|
||||||
}, onLaunch: (Map<String, dynamic> message) {
|
}, onLaunch: (Map<String, dynamic> message) {
|
||||||
print("onLaunch: $message");
|
debugPrint("onLaunch: $message");
|
||||||
_navigateToItemDetail(message, store);
|
_navigateToItemDetail(message, store);
|
||||||
}, onResume: (Map<String, dynamic> message) {
|
}, onResume: (Map<String, dynamic> message) {
|
||||||
print("onResume: $message");
|
debugPrint("onResume: $message");
|
||||||
_navigateToItemDetail(message, store);
|
_navigateToItemDetail(message, store);
|
||||||
});
|
});
|
||||||
_firebaseMessaging.requestNotificationPermissions(
|
_firebaseMessaging.requestNotificationPermissions(
|
||||||
|
|
@ -106,6 +137,11 @@ class _FiresAppState extends State<FiresApp> {
|
||||||
store.dispatch(new OnUserTokenAction(token));
|
store.dispatch(new OnUserTokenAction(token));
|
||||||
setState(() {});
|
setState(() {});
|
||||||
});
|
});
|
||||||
|
initConnectivity();
|
||||||
|
_connectivitySubscription =
|
||||||
|
_connectivity.onConnectivityChanged.listen((ConnectivityResult result) {
|
||||||
|
setState(() => _connectionStatus = result.toString());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _showItemDialog(Map<String, dynamic> message, Store store) {
|
void _showItemDialog(Map<String, dynamic> message, Store store) {
|
||||||
|
|
@ -155,23 +191,23 @@ class _FiresAppState extends State<FiresApp> {
|
||||||
|
|
||||||
FireNotification _notifForMessage(Map<String, dynamic> message) {
|
FireNotification _notifForMessage(Map<String, dynamic> message) {
|
||||||
FireNotification notif;
|
FireNotification notif;
|
||||||
print('start parse notif');
|
// print('start parse notif');
|
||||||
try {
|
try {
|
||||||
notif = new FireNotification(
|
notif = new FireNotification(
|
||||||
id: new ObjectId.fromHexString(message['id']),
|
id: new ObjectId.fromHexString(message['id']),
|
||||||
|
subsId: new ObjectId.fromHexString(message['subsId']),
|
||||||
lat: double.parse(message['lat']),
|
lat: double.parse(message['lat']),
|
||||||
lon: double.parse(message['lon']),
|
lon: double.parse(message['lon']),
|
||||||
description: message['description'],
|
description: message['description'],
|
||||||
read: false,
|
read: false,
|
||||||
when: DateTime.parse(message['when']),
|
when: DateTime.parse(message['when']),
|
||||||
sealed: message['sealed'],
|
sealed: message['sealed']);
|
||||||
subsId: new ObjectId.fromHexString(message['subsId']));
|
// print('end parse notif');
|
||||||
print('end parse notif');
|
|
||||||
debugPrint(notif.toString());
|
debugPrint(notif.toString());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint(e.toString());
|
debugPrint(e.toString());
|
||||||
}
|
}
|
||||||
store.dispatch(new AddFireNotificationAction(notif));
|
if (notif != null) store.dispatch(new AddFireNotificationAction(notif));
|
||||||
return notif;
|
return notif;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,4 +49,6 @@ class FetchFireNotificationsSucceededAction extends AppActions {
|
||||||
final int unreadCount;
|
final int unreadCount;
|
||||||
|
|
||||||
FetchFireNotificationsSucceededAction(this.fetchedFireNotifications, this.unreadCount);
|
FetchFireNotificationsSucceededAction(this.fetchedFireNotifications, this.unreadCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnConnect
|
||||||
Loading…
Add table
Add a link
Reference in a new issue