-
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.
feat(refreshWhenNetworkAvailable): add refreshWhenNetworkAvailable ex…
…tension
- Loading branch information
1 parent
578f1ef
commit 76e111b
Showing
4 changed files
with
133 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,62 @@ | ||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
import 'package:riverpod/riverpod.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() { | ||
final connectivityStream = Connectivity().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; | ||
} | ||
}); | ||
|
||
onDispose(connectivitySubscription.cancel); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,4 +15,5 @@ dev_dependencies: | |
dependencies: | ||
flutter: | ||
sdk: flutter | ||
connectivity_plus: ^6.0.3 | ||
riverpod: ^2.0.0 |