-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from bamlab/feat/add-refresh-when-network-avai…
…lable Feat/add refresh when network available
- Loading branch information
Showing
7 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.distinct(), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
import 'package:riverpod/riverpod.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 | ||
/// status changes and refreshes the provider when the network becomes | ||
/// available. | ||
/// | ||
/// See [refreshWhenNetworkAvailable] | ||
extension RefreshWhenNetworkAvailableExtension<T> on AutoDisposeRef<T> { | ||
/// Refreshes the provider when the network becomes available after being | ||
/// unavailable. | ||
/// | ||
/// This can be useful for scenarios where data needs to be fetched or | ||
/// operations need to be performed only when network connectivity is | ||
/// restored, such as synchronizing local data with a server. | ||
/// | ||
/// Example usages: | ||
/// | ||
/// without codegen: | ||
/// ```dart | ||
/// final myProvider = Provider.autoDispose<int>((ref) { | ||
/// ref.refreshWhenNetworkAvailable(); | ||
/// return fetchData(); // Assume fetchData is a function that fetches data over the network. | ||
/// }); | ||
/// ``` | ||
/// | ||
/// with codegen: | ||
/// ```dart | ||
/// @riverpod | ||
/// int myProvider(AutoDisposeRef ref) { | ||
/// ref.refreshWhenNetworkAvailable(); | ||
/// return fetchData(); | ||
/// } | ||
/// ``` | ||
void refreshWhenNetworkAvailable() { | ||
var isNetworkAvailable = false; | ||
const validResults = [ | ||
ConnectivityResult.mobile, | ||
ConnectivityResult.wifi, | ||
ConnectivityResult.ethernet, | ||
ConnectivityResult.vpn, | ||
ConnectivityResult.other, | ||
]; | ||
|
||
listen(connectivityStreamProvider, (_, connectivityResults) { | ||
connectivityResults.whenData((data) { | ||
final currentlyAvailable = | ||
data.any((result) => validResults.contains(result)); | ||
if (currentlyAvailable && !isNetworkAvailable) { | ||
isNetworkAvailable = true; | ||
invalidateSelf(); | ||
} else { | ||
isNetworkAvailable = false; | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:riverpod/riverpod.dart'; | ||
import 'package:riverpod_community_extensions/src/connectivity_stream_provider.dart'; | ||
|
||
void main() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
group('ConnectivityStreamProvider tests', () { | ||
test('connectivityStreamProvider provides an AsyncValue', () { | ||
final container = ProviderContainer(); | ||
final providerState = container.read(connectivityStreamProvider); | ||
|
||
// Check that the initial state is AsyncLoading. | ||
expect(providerState, isA<AsyncLoading<List<ConnectivityResult>>>()); | ||
}); | ||
}); | ||
} |
77 changes: 77 additions & 0 deletions
77
test/src/refresh_when_network_available_extension_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import 'dart:async'; | ||
|
||
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_stream_provider.dart'; | ||
import 'package:riverpod_community_extensions/src/refresh_when_network_available_extension.dart'; | ||
|
||
class MockConnectivity extends Mock implements Connectivity {} | ||
|
||
void main() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
group('RefreshWhenNetworkAvailableExtension Tests', () { | ||
late StreamController<List<ConnectivityResult>> connectivityController; | ||
late ProviderContainer container; | ||
late AutoDisposeProvider<int> myProvider; | ||
late Stream<List<ConnectivityResult>> connectivityStream; | ||
|
||
var numberOfFetchDataCalls = 0; | ||
|
||
int fetchData() { | ||
numberOfFetchDataCalls++; | ||
return 42; | ||
} | ||
|
||
setUp(() { | ||
numberOfFetchDataCalls = 0; | ||
connectivityController = StreamController<List<ConnectivityResult>>(); | ||
connectivityStream = connectivityController.stream; | ||
|
||
myProvider = Provider.autoDispose<int>((ref) { | ||
ref.refreshWhenNetworkAvailable(); | ||
return fetchData(); | ||
}); | ||
|
||
container = ProviderContainer( | ||
overrides: [ | ||
connectivityStreamProvider.overrideWith( | ||
(ref) => connectivityStream, | ||
), | ||
], | ||
)..listen(myProvider, (_, __) {}); | ||
}); | ||
|
||
tearDown(() { | ||
connectivityController.close(); | ||
container.dispose(); | ||
}); | ||
|
||
test('Should not refresh when connectivity does not change to available', | ||
() async { | ||
expect(numberOfFetchDataCalls, 1); | ||
|
||
// Simulate network being offline | ||
connectivityController.add([ConnectivityResult.none]); | ||
|
||
// Wait for the potential refresh to happen | ||
await Future<void>.delayed(const Duration(seconds: 1)); | ||
|
||
expect(numberOfFetchDataCalls, 1); | ||
}); | ||
|
||
test('Should refresh when connectivity changes to available', () async { | ||
expect(numberOfFetchDataCalls, 1); | ||
|
||
// Simulate network being online | ||
connectivityController.add([ConnectivityResult.wifi]); | ||
|
||
// Wait for the potential refresh to happen | ||
await Future<void>.delayed(const Duration(seconds: 1)); | ||
|
||
expect(numberOfFetchDataCalls, 2); | ||
}); | ||
}); | ||
} |