|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'constants.dart'; |
| 4 | +import 'internet_observing_settings/internet_observing_strategy.dart'; |
| 5 | + |
| 6 | +class InternetConnectivity { |
| 7 | + late StreamController<bool> _internetAccessCheckController; |
| 8 | + late final InternetObservingStrategy _internetObservingStrategy; |
| 9 | + bool? _lastInternetAccessCheck; |
| 10 | + Timer? _intervalTimer, disposeTimer; |
| 11 | + bool _streamIsClosed = false; |
| 12 | + |
| 13 | + InternetConnectivity({InternetObservingStrategy? internetObservingStrategy}) { |
| 14 | + _internetObservingStrategy = |
| 15 | + internetObservingStrategy ?? DefaultObServingStrategy(); |
| 16 | + _internetAccessCheckController = StreamController<bool>.broadcast( |
| 17 | + onCancel: _onCancelStream, onListen: _emitInitialInternetAccess); |
| 18 | + } |
| 19 | + |
| 20 | + Future<bool> get hasInternetAccess => |
| 21 | + _internetObservingStrategy.hasInternetAccess; |
| 22 | + |
| 23 | + Stream<bool> get observeInternetAccess => |
| 24 | + _internetAccessCheckController.stream; |
| 25 | + |
| 26 | + Future<void> _emitInternetAccess() async { |
| 27 | + if (_streamIsClosed) return; |
| 28 | + _intervalTimer?.cancel(); |
| 29 | + final isConnected = await hasInternetAccess; |
| 30 | + _addToStream(isConnected); |
| 31 | + _intervalTimer = |
| 32 | + Timer(_internetObservingStrategy.interval, _emitInternetAccess); |
| 33 | + } |
| 34 | + |
| 35 | + Future<void> _emitInitialInternetAccess() async { |
| 36 | + _disposeAfterDuration(); |
| 37 | + final isConnected = await Future.delayed( |
| 38 | + _internetObservingStrategy.initialDuration ?? kDefaultInitialDuration, |
| 39 | + () => hasInternetAccess); |
| 40 | + _addToStream(isConnected); |
| 41 | + _emitInternetAccess(); |
| 42 | + } |
| 43 | + |
| 44 | + void _addToStream(bool value) { |
| 45 | + if (!_hasListener) return; |
| 46 | + if (value == _lastInternetAccessCheck) return; |
| 47 | + _lastInternetAccessCheck = value; |
| 48 | + _internetAccessCheckController.add(value); |
| 49 | + if (value && _internetObservingStrategy.disposeOnFirstConnected) { |
| 50 | + _internetAccessCheckController.close(); |
| 51 | + } |
| 52 | + if (!value && _internetObservingStrategy.disposeOnFirstDisconnected) { |
| 53 | + _internetAccessCheckController.close(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + void _onCancelStream() { |
| 58 | + _intervalTimer?.cancel(); |
| 59 | + disposeTimer?.cancel(); |
| 60 | + _lastInternetAccessCheck = null; |
| 61 | + _streamIsClosed = true; |
| 62 | + } |
| 63 | + |
| 64 | + void _disposeAfterDuration() { |
| 65 | + if (_internetObservingStrategy.duration != null) { |
| 66 | + if (!_hasListener) return; |
| 67 | + disposeTimer = Timer(_internetObservingStrategy.duration!, () { |
| 68 | + _internetAccessCheckController.close(); |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + bool get _hasListener => _internetAccessCheckController.hasListener; |
| 74 | +} |
0 commit comments