From 9e3698f3c56bb03fcd873ea188d48ac545ab9653 Mon Sep 17 00:00:00 2001 From: Tanguy Mossion Date: Wed, 7 Aug 2024 10:56:22 +0200 Subject: [PATCH] stream instead of connectivity --- lib/src/connectivity_provider.dart | 5 --- lib/src/connectivity_stream_provider.dart | 7 ++++ ...resh_when_network_available_extension.dart | 33 +++++-------------- ...when_network_available_extension_test.dart | 14 ++++---- 4 files changed, 23 insertions(+), 36 deletions(-) delete mode 100644 lib/src/connectivity_provider.dart create mode 100644 lib/src/connectivity_stream_provider.dart diff --git a/lib/src/connectivity_provider.dart b/lib/src/connectivity_provider.dart deleted file mode 100644 index 39917e8..0000000 --- a/lib/src/connectivity_provider.dart +++ /dev/null @@ -1,5 +0,0 @@ -import 'package:connectivity_plus/connectivity_plus.dart'; -import 'package:riverpod/riverpod.dart'; - -/// A provider that exposes a [Connectivity] instance. -final connectivityProvider = Provider((ref) => Connectivity()); diff --git a/lib/src/connectivity_stream_provider.dart b/lib/src/connectivity_stream_provider.dart new file mode 100644 index 0000000..5540054 --- /dev/null +++ b/lib/src/connectivity_stream_provider.dart @@ -0,0 +1,7 @@ +import 'package:connectivity_plus/connectivity_plus.dart'; +import 'package:riverpod/riverpod.dart'; + +/// A stream provider that listens to network connectivity changes. +final connectivityStreamProvider = StreamProvider( + (ref) => Connectivity().onConnectivityChanged, +); diff --git a/lib/src/refresh_when_network_available_extension.dart b/lib/src/refresh_when_network_available_extension.dart index d401008..b08b9e3 100644 --- a/lib/src/refresh_when_network_available_extension.dart +++ b/lib/src/refresh_when_network_available_extension.dart @@ -1,6 +1,6 @@ import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:riverpod/riverpod.dart'; -import 'package:riverpod_community_extensions/src/connectivity_provider.dart'; +import 'package:riverpod_community_extensions/src/connectivity_stream_provider.dart'; /// Adds network-aware refresh functionality to AutoDisposeRef objects. /// This extension uses the connectivity_plus package to listen for network @@ -35,30 +35,13 @@ extension RefreshWhenNetworkAvailableExtension on AutoDisposeRef { /// } /// ``` void refreshWhenNetworkAvailable() { - final connectivityStream = - watch(connectivityProvider).onConnectivityChanged; - - var isNetworkAvailable = false; - const validResults = [ - ConnectivityResult.mobile, - ConnectivityResult.wifi, - ConnectivityResult.ethernet, - ConnectivityResult.vpn, - ConnectivityResult.other, - ]; - - final connectivitySubscription = - connectivityStream.listen((connectivityResults) { - final currentlyAvailable = - connectivityResults.any((result) => validResults.contains(result)); - if (currentlyAvailable && !isNetworkAvailable) { - isNetworkAvailable = true; - invalidateSelf(); - } else { - isNetworkAvailable = false; - } + listen(connectivityStreamProvider, (_, connectivityResults) { + connectivityResults.whenData((data) { + if (data.last != ConnectivityResult.none && + data.last != ConnectivityResult.bluetooth) { + invalidateSelf(); + } + }); }); - - onDispose(connectivitySubscription.cancel); } } diff --git a/test/src/refresh_when_network_available_extension_test.dart b/test/src/refresh_when_network_available_extension_test.dart index c11fdb3..a7a37bf 100644 --- a/test/src/refresh_when_network_available_extension_test.dart +++ b/test/src/refresh_when_network_available_extension_test.dart @@ -4,7 +4,7 @@ import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:riverpod/riverpod.dart'; -import 'package:riverpod_community_extensions/src/connectivity_provider.dart'; +import 'package:riverpod_community_extensions/src/connectivity_stream_provider.dart'; import 'package:riverpod_community_extensions/src/refresh_when_network_available_extension.dart'; class MockConnectivity extends Mock implements Connectivity {} @@ -13,10 +13,10 @@ void main() { TestWidgetsFlutterBinding.ensureInitialized(); group('RefreshWhenNetworkAvailableExtension Tests', () { - late MockConnectivity mockConnectivity; late StreamController> connectivityController; late ProviderContainer container; late AutoDisposeProvider myProvider; + late Stream> connectivityStream; var numberOfFetchDataCalls = 0; @@ -27,10 +27,8 @@ void main() { setUp(() { numberOfFetchDataCalls = 0; - mockConnectivity = MockConnectivity(); connectivityController = StreamController>(); - when(() => mockConnectivity.onConnectivityChanged) - .thenAnswer((_) => connectivityController.stream); + connectivityStream = connectivityController.stream; myProvider = Provider.autoDispose((ref) { ref.refreshWhenNetworkAvailable(); @@ -38,7 +36,11 @@ void main() { }); container = ProviderContainer( - overrides: [connectivityProvider.overrideWithValue(mockConnectivity)], + overrides: [ + connectivityStreamProvider.overrideWith( + (ref) => connectivityStream, + ), + ], )..listen(myProvider, (_, __) {}); });