|
1 | 1 | import 'dart:async'; |
2 | 2 |
|
3 | | -import 'package:connectivity_plus/connectivity_plus.dart'; |
4 | | -import 'package:flutter/material.dart'; |
5 | | -import 'package:flutter_it/flutter_it.dart'; |
| 3 | +import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart'; |
6 | 4 | import 'package:safe_change_notifier/safe_change_notifier.dart'; |
7 | 5 |
|
8 | | -import '../common/data/audio_type.dart'; |
9 | | -import '../common/view/snackbars.dart'; |
10 | | -import '../extensions/connectivity_x.dart'; |
11 | | -import '../l10n/l10n.dart'; |
12 | 6 | import '../player/player_service.dart'; |
13 | | -import '../radio/radio_model.dart'; |
14 | | -import '../settings/settings_model.dart'; |
15 | 7 |
|
16 | 8 | class ConnectivityModel extends SafeChangeNotifier { |
17 | 9 | ConnectivityModel({ |
18 | 10 | required PlayerService playerService, |
19 | | - required Connectivity connectivity, |
20 | | - }) : _connectivity = connectivity, |
21 | | - _playerService = playerService; |
| 11 | + required InternetConnection internetConnection, |
| 12 | + }) : _playerService = playerService, |
| 13 | + _internetConnection = internetConnection; |
22 | 14 |
|
23 | 15 | final PlayerService _playerService; |
24 | | - final Connectivity _connectivity; |
25 | | - Stream<List<ConnectivityResult>> get onConnectivityChanged => |
26 | | - _connectivity.onConnectivityChanged; |
27 | | - StreamSubscription<List<ConnectivityResult>>? _connectivitySubscription; |
| 16 | + final InternetConnection _internetConnection; |
| 17 | + StreamSubscription<InternetStatus>? _internetConnectionSubscription; |
28 | 18 |
|
29 | | - Future<void> init() async { |
30 | | - // TODO: fix https://github.com/fluttercommunity/plus_plugins/issues/1451 |
31 | | - final fallback = [ConnectivityResult.wifi]; |
32 | | - |
33 | | - _connectivitySubscription ??= _connectivity.onConnectivityChanged.listen( |
34 | | - _updateConnectivity, |
35 | | - onError: (e) => _result = fallback, |
36 | | - ); |
37 | | - |
38 | | - return _connectivity |
39 | | - .checkConnectivity() |
40 | | - .then(_updateConnectivity) |
41 | | - .catchError((_) => _updateConnectivity(fallback)); |
42 | | - } |
43 | | - |
44 | | - bool get isOnline => _connectivity.isOnline(_result); |
45 | | - |
46 | | - bool get isMaybeLowBandWidth => _connectivity.isNotWifiNorEthernet(_result); |
| 19 | + InternetStatus? _internetStatus; |
| 20 | + bool get isOnline => _internetStatus == InternetStatus.connected; |
47 | 21 |
|
48 | | - List<ConnectivityResult>? get result => _result; |
49 | | - List<ConnectivityResult>? _result; |
50 | | - void _updateConnectivity(List<ConnectivityResult> newResult) { |
51 | | - if (!_connectivity.isOnline(newResult) && |
52 | | - _playerService.audio?.audioType == AudioType.radio) { |
53 | | - _playerService.pause(); |
54 | | - } |
55 | | - _result = newResult; |
56 | | - notifyListeners(); |
| 22 | + Future<void> init() async { |
| 23 | + _internetConnectionSubscription ??= _internetConnection.onStatusChange |
| 24 | + .listen((status) { |
| 25 | + if (status == InternetStatus.disconnected && |
| 26 | + _playerService.audio!.path == null) { |
| 27 | + _playerService.pause(); |
| 28 | + } |
| 29 | + _internetStatus = status; |
| 30 | + notifyListeners(); |
| 31 | + }); |
| 32 | + |
| 33 | + return _internetConnection.internetStatus.then((status) { |
| 34 | + _internetStatus = status; |
| 35 | + notifyListeners(); |
| 36 | + }); |
57 | 37 | } |
58 | 38 |
|
59 | 39 | @override |
60 | 40 | Future<void> dispose() async { |
61 | | - await _connectivitySubscription?.cancel(); |
| 41 | + await _internetConnectionSubscription?.cancel(); |
62 | 42 | super.dispose(); |
63 | 43 | } |
64 | 44 | } |
65 | | - |
66 | | -void onConnectivityChangedHandler( |
67 | | - BuildContext context, |
68 | | - AsyncSnapshot<List<ConnectivityResult>?> res, |
69 | | - void Function() cancel, |
70 | | -) { |
71 | | - final l10n = context.l10n; |
72 | | - final dataSafeMode = di<RadioModel>().dataSafeMode; |
73 | | - final notifyDataSafeMode = di<SettingsModel>().notifyDataSafeMode; |
74 | | - if (!res.hasData || !context.mounted || !notifyDataSafeMode) { |
75 | | - return; |
76 | | - } |
77 | | - |
78 | | - if (!dataSafeMode && di<Connectivity>().isNotWifiNorEthernet(res.data)) { |
79 | | - di<RadioModel>().setDataSafeMode(true); |
80 | | - showSnackBar(context: context, content: Text(l10n.dataSafeModeEnabled)); |
81 | | - } else if (dataSafeMode && |
82 | | - !di<Connectivity>().isNotWifiNorEthernet(res.data)) { |
83 | | - di<RadioModel>().setDataSafeMode(false); |
84 | | - showSnackBar(context: context, content: Text(l10n.dataSafeModeDisabled)); |
85 | | - } |
86 | | -} |
0 commit comments