Files
fcs/lib/helpers/network_connectivity.dart

100 lines
2.6 KiB
Dart
Raw Normal View History

2020-09-04 01:42:58 +06:30
import 'dart:async';
import 'dart:io';
import 'package:fcs/config.dart';
2024-12-27 22:36:48 +06:30
import 'package:flutter/foundation.dart';
2020-09-04 01:42:58 +06:30
import 'package:logging/logging.dart';
2024-12-27 22:36:48 +06:30
import 'package:connectivity_plus/connectivity_plus.dart';
2020-09-04 01:42:58 +06:30
2020-10-07 02:33:06 +06:30
import 'api_helper.dart';
2024-12-27 22:36:48 +06:30
enum ConnectivityStatus { none, online }
2020-09-04 01:42:58 +06:30
class NetworkConnectivity {
final log = Logger('NetworkConnectivity');
static final NetworkConnectivity instance = NetworkConnectivity._internal();
2024-12-27 22:36:48 +06:30
static String _hostName = "";
static ConnectivityStatus? _currentResult;
2020-09-04 01:42:58 +06:30
NetworkConnectivity._internal() {
_initialise();
var uri = Uri.parse(Config.instance.apiURL);
2024-12-27 22:36:48 +06:30
_hostName = uri.host;
log.info("host name:$_hostName");
2020-09-04 01:42:58 +06:30
}
Connectivity connectivity = Connectivity();
2024-12-27 22:36:48 +06:30
final StreamController<ConnectivityStatus> _controller =
StreamController.broadcast();
2020-09-04 01:42:58 +06:30
2024-12-27 22:36:48 +06:30
Stream<ConnectivityStatus> get statusStream => _controller.stream;
2020-09-04 01:42:58 +06:30
2024-12-27 22:36:48 +06:30
Timer? _checkTimer;
2020-09-04 01:42:58 +06:30
void _initialise() async {
2024-12-27 22:36:48 +06:30
await _checkStatus(await connectivity.checkConnectivity());
2020-09-04 01:42:58 +06:30
connectivity.onConnectivityChanged.listen((result) {
2024-12-27 22:36:48 +06:30
if (_currentResult == null) {
_checkStatus(result);
} else {
_checkTimer?.cancel();
_checkTimer = Timer(const Duration(seconds: 7), () {
_checkTimer?.cancel();
_checkStatus(result);
});
}
2020-09-04 01:42:58 +06:30
});
}
2024-12-27 22:36:48 +06:30
Future<void> _checkStatus(List<ConnectivityResult> result) async {
2020-09-04 01:42:58 +06:30
bool isOnline = false;
2024-12-27 22:36:48 +06:30
if (!result.contains(ConnectivityResult.none) && !kIsWeb) {
2020-09-04 01:42:58 +06:30
try {
2024-12-27 22:36:48 +06:30
final hostNameLookup = await InternetAddress.lookup(_hostName);
2020-09-04 01:42:58 +06:30
if (hostNameLookup.isNotEmpty &&
hostNameLookup[0].rawAddress.isNotEmpty) {
if (await checkHeartbeat()) {
isOnline = true;
}
2024-12-27 22:36:48 +06:30
} else {
2020-09-04 01:42:58 +06:30
isOnline = false;
2024-12-27 22:36:48 +06:30
}
isOnline = true;
} on Exception catch (_) {
2020-09-04 01:42:58 +06:30
isOnline = false;
}
}
2024-12-27 22:36:48 +06:30
_addStream(isOnline || kIsWeb
? ConnectivityStatus.online
: ConnectivityStatus.none);
2020-09-04 01:42:58 +06:30
}
Future<bool> checkHeartbeat() async {
2024-12-27 22:36:48 +06:30
try {
var result = await requestAPI("/hb", "GET");
var status = result["status"];
if (status != null && status != "") {
return true;
}
} catch (e) {
return false;
2020-09-04 01:42:58 +06:30
}
return false;
}
void disposeStream() => _controller.close();
2024-12-27 22:36:48 +06:30
_addStream(ConnectivityStatus? updateResult) async {
if (updateResult == null) return;
if (updateResult == _currentResult) return;
_currentResult = updateResult;
if (!_controller.isClosed) {
log.info("sink: $_currentResult");
_controller.sink.add(_currentResult!);
}
}
2020-09-04 01:42:58 +06:30
}