added connectivity service

This commit is contained in:
Mehul Ahal 2022-12-20 09:47:07 +01:00
parent 8e54cfffc5
commit 193ae3b0ea
12 changed files with 384 additions and 8 deletions

View file

@ -0,0 +1,133 @@
import 'dart:async';
import 'dart:io';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';
import '/locator.dart';
/// Used to observe the current connection type.
class ConnectionService {
ConnectionService({
required Connectivity connectivity,
required InternetConnectionChecker internetConnectionChecker,
bool shouldInitialize = true,
}) : _connectivity = connectivity,
_internetConnectionChecker = internetConnectionChecker {
if (shouldInitialize) initialize();
}
ConnectivityResult? _connectivityResult;
ConnectivityResult? get connectivityResult => _connectivityResult;
final Connectivity _connectivity;
final Map<String, StreamSubscription> _connectivitySubscriptions = {};
final InternetConnectionChecker _internetConnectionChecker;
final Completer _isInitialized = Completer();
Completer<bool>? _hasInternetConnection;
final ValueNotifier<bool> _hasInternetConnectionListenable = ValueNotifier(false);
ValueListenable<bool> get hasInternetConnectionListenable => _hasInternetConnectionListenable;
Future<bool> get hasInternetConnection async {
try {
if (_hasInternetConnection == null) {
_hasInternetConnection = Completer<bool>();
try {
final hasInternetConnection = await _internetConnectionChecker.hasConnection;
_hasInternetConnection!.complete(hasInternetConnection);
return hasInternetConnection;
} catch (error) {
_hasInternetConnection!.complete(false);
return false;
} finally {
_hasInternetConnection = null;
}
} else {
final awaitedHasInternet = await _hasInternetConnection!.future;
return awaitedHasInternet;
}
} on SocketException catch (_, __) {
return false;
}
}
Future<void> initialize() async {
try {
final tag = runtimeType.toString();
if (_connectivitySubscriptions[tag] != null) {
await dispose();
}
_connectivitySubscriptions[tag] = _connectivity.onConnectivityChanged.listen(
_onConnectivityChanged,
cancelOnError: false,
onError: (error, stack) {},
);
await _onConnectivityChanged(await _connectivity.checkConnectivity());
_internetConnectionChecker.onStatusChange.listen((final event) {
switch (event) {
case InternetConnectionStatus.connected:
_hasInternetConnectionListenable.value = true;
break;
case InternetConnectionStatus.disconnected:
_hasInternetConnectionListenable.value = false;
break;
}
});
_isInitialized.complete();
} catch (error, stackTrace) {}
}
Future<void> dispose() async {
for (final subscription in _connectivitySubscriptions.values) {
await subscription.cancel();
}
_connectivitySubscriptions.clear();
_connectivityResult = null;
}
Future<void> addListener({
required String tag,
required Future<void> Function({
required ConnectivityResult connectivityResult,
required bool hasInternet,
})
listener,
bool tryCallListenerOnAdd = true,
}) async {
try {
if (_connectivityResult != null && tryCallListenerOnAdd)
await listener(
connectivityResult: _connectivityResult!, hasInternet: await hasInternetConnection);
_connectivitySubscriptions[tag] =
_connectivity.onConnectivityChanged.listen((connectivityResult) async {
await listener(
connectivityResult: connectivityResult, hasInternet: await hasInternetConnection);
});
} catch (error, stackTrace) {}
}
Future<void> removeListener({required String tag}) async {
try {
final subscription = _connectivitySubscriptions[tag];
if (subscription != null) {
await subscription.cancel();
} else {}
} catch (error, stackTrace) {}
}
Future<void> _onConnectivityChanged(ConnectivityResult connectivityResult) async {
try {
_connectivityResult = connectivityResult;
} catch (error, stackTrace) {}
}
static ConnectionService get locate => Locator.locate();
}
class NoInternetException implements Exception {}

View file

@ -0,0 +1,58 @@
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:mc_gallery/features/core/services/logging_service.dart';
import 'package:mc_gallery/locator.dart';
class OverlayService {
const OverlayService({
required LoggingService loggingService,
}) : _loggingService = loggingService;
final LoggingService _loggingService;
final Map<int, AnimationController> _animationControllerMap = const {};
final Map<int, OverlayEntry> _overlayEntryMap = const {};
Future<void> playOverlayEntry({
required BuildContext context,
required AnimationController animationController,
required OverlayEntry overlayEntry,
}) async {
try {
_animationControllerMap[animationController.hashCode] = animationController;
_overlayEntryMap[overlayEntry.hashCode] = overlayEntry;
Overlay.of(
context,
rootOverlay: true,
)!
.insert(overlayEntry);
await animationController.forward();
if (overlayEntry.mounted) overlayEntry.remove();
_overlayEntryMap.remove(overlayEntry.hashCode);
animationController.dispose();
_animationControllerMap.remove(animationController.hashCode);
} catch (error, stackTrace) {
_loggingService.handle(error, stackTrace);
}
}
void dispose() {
for (final overlayEntry in _overlayEntryMap.values) {
if (overlayEntry.mounted) {
overlayEntry.remove();
}
}
_overlayEntryMap.clear();
for (final animationController in _animationControllerMap.values) {
if (animationController.isAnimating) {
animationController.dispose();
}
}
_animationControllerMap.clear();
}
static OverlayService get locate => Locator.locate();
}