Skip to content

Commit

Permalink
stream instead of connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
tanguymossion committed Aug 7, 2024
1 parent b7c8cfc commit 9e3698f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
5 changes: 0 additions & 5 deletions lib/src/connectivity_provider.dart

This file was deleted.

7 changes: 7 additions & 0 deletions lib/src/connectivity_stream_provider.dart
Original file line number Diff line number Diff line change
@@ -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,
);
33 changes: 8 additions & 25 deletions lib/src/refresh_when_network_available_extension.dart
Original file line number Diff line number Diff line change
@@ -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<T> objects.
/// This extension uses the connectivity_plus package to listen for network
Expand Down Expand Up @@ -35,30 +35,13 @@ extension RefreshWhenNetworkAvailableExtension<T> on AutoDisposeRef<T> {
/// }
/// ```
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);
}
}
14 changes: 8 additions & 6 deletions test/src/refresh_when_network_available_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -13,10 +13,10 @@ void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('RefreshWhenNetworkAvailableExtension Tests', () {
late MockConnectivity mockConnectivity;
late StreamController<List<ConnectivityResult>> connectivityController;
late ProviderContainer container;
late AutoDisposeProvider<int> myProvider;
late Stream<List<ConnectivityResult>> connectivityStream;

var numberOfFetchDataCalls = 0;

Expand All @@ -27,18 +27,20 @@ void main() {

setUp(() {
numberOfFetchDataCalls = 0;
mockConnectivity = MockConnectivity();
connectivityController = StreamController<List<ConnectivityResult>>();
when(() => mockConnectivity.onConnectivityChanged)
.thenAnswer((_) => connectivityController.stream);
connectivityStream = connectivityController.stream;

myProvider = Provider.autoDispose<int>((ref) {
ref.refreshWhenNetworkAvailable();
return fetchData();
});

container = ProviderContainer(
overrides: [connectivityProvider.overrideWithValue(mockConnectivity)],
overrides: [
connectivityStreamProvider.overrideWith(
(ref) => connectivityStream,
),
],
)..listen(myProvider, (_, __) {});
});

Expand Down

0 comments on commit 9e3698f

Please sign in to comment.