Compare commits
2 Commits
c950d33a71
...
7f07e4188a
Author | SHA1 | Date |
---|---|---|
shihern | 7f07e4188a | |
shihern | c82168996c |
|
@ -1,9 +1,11 @@
|
|||
import "package:flutter/services.dart";
|
||||
import 'routes.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
// import 'package:flutter/rendering.dart';
|
||||
|
||||
void main() {
|
||||
// debugPaintSizeEnabled = true;
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
|
||||
Routes();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
import 'dart:io';
|
||||
import 'dart:async';
|
||||
import 'package:connectivity/connectivity.dart';
|
||||
|
||||
/// Singleton class to get a stream of the current network connection status.
|
||||
class ConnectionStatus {
|
||||
static final ConnectionStatus _singleton = ConnectionStatus._internal();
|
||||
static final Connectivity _connectivity = Connectivity();
|
||||
|
||||
StreamController<bool> _connectionChangeController;
|
||||
|
||||
bool _hasConnection = false;
|
||||
|
||||
/// Stream of current connection status. Only updates when there is a change in connection.
|
||||
Stream<bool> get connectionChange => _connectionChangeController.stream;
|
||||
|
||||
/// Private internal constructor for the Singleton
|
||||
ConnectionStatus._internal() {
|
||||
// Only call the init function when there is a subscriber to the stream
|
||||
_connectionChangeController =
|
||||
StreamController<bool>.broadcast(onListen: _init);
|
||||
}
|
||||
|
||||
_init() async {
|
||||
_checkConnection();
|
||||
|
||||
// Recheck the connection when device connectivity changes (e.g. WiFi enabled/disabled)
|
||||
_connectivity.onConnectivityChanged.listen((result) => _checkConnection());
|
||||
|
||||
// Set a timer to check the connection periodically
|
||||
Timer.periodic(Duration(seconds: 20), (Timer t) {
|
||||
_checkConnection();
|
||||
});
|
||||
}
|
||||
|
||||
/// Checks the current connection status and updates _connectionChangeController if required.
|
||||
Future<bool> _checkConnection() async {
|
||||
bool prevConnection = _hasConnection;
|
||||
|
||||
try {
|
||||
// Verify connection by resolving example.com
|
||||
final result = await InternetAddress.lookup('example.com');
|
||||
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
|
||||
_hasConnection = true;
|
||||
} else {
|
||||
_hasConnection = false;
|
||||
}
|
||||
} on SocketException {
|
||||
_hasConnection = false;
|
||||
}
|
||||
|
||||
// Update the stream controller if there is a change in connection
|
||||
if (prevConnection != _hasConnection) {
|
||||
_connectionChangeController.add(_hasConnection);
|
||||
}
|
||||
|
||||
return _hasConnection;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_connectionChangeController.close();
|
||||
}
|
||||
|
||||
factory ConnectionStatus() {
|
||||
return _singleton;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
import "package:flutter/material.dart";
|
||||
import 'package:frontend_flutter/src/services/connection_status.dart';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import "../services/heartbeat_manager.dart";
|
||||
|
@ -35,6 +36,10 @@ class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
|
|||
// Current conversaton
|
||||
String currentConversationId = "";
|
||||
|
||||
// Online status
|
||||
ConnectionStatus connectionStatus = ConnectionStatus();
|
||||
bool isOnline = true;
|
||||
|
||||
@override
|
||||
initState() {
|
||||
super.initState();
|
||||
|
@ -48,6 +53,13 @@ class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
|
|||
|
||||
messageChannel.bus.listen(
|
||||
(Map<String, String> data) async => await _processMessage(data));
|
||||
|
||||
connectionStatus.connectionChange.listen((bool online) {
|
||||
// TODO: implement some timer to remove the message after 3 seconds
|
||||
setState(() {
|
||||
isOnline = online;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -124,7 +136,12 @@ class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
|
|||
: Icon(data, color: Colors.grey, size: 20),
|
||||
title: Container(),
|
||||
);
|
||||
}).toList())
|
||||
}).toList()),
|
||||
Container(
|
||||
width: MediaQuery.of(context).size.width,
|
||||
alignment: Alignment(0.0, 0.0),
|
||||
color: isOnline ? Colors.green : Colors.red,
|
||||
child: Text(isOnline ? 'Online' : 'Offline')),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ dependencies:
|
|||
flutter_slidable: ^0.5.3
|
||||
image_jpeg: ^1.1.1
|
||||
permission_handler: ^4.0.0
|
||||
connectivity: ^0.4.6+1
|
||||
|
||||
|
||||
dev_dependencies:
|
||||
|
|
Loading…
Reference in New Issue