Skip to content

Commit

Permalink
feat(refreshWhenNetworkAvailable): add refreshWhenNetworkAvailable ex…
Browse files Browse the repository at this point in the history
…tension
  • Loading branch information
tanguymossion committed Jul 4, 2024
1 parent 578f1ef commit 76e111b
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/riverpod_community_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export 'src/cache_data_for_extension.dart';
export 'src/cache_for_extension.dart';
export 'src/debounce_extension.dart';
export 'src/refresh_extension.dart';
export 'src/refresh_when_network_available_extension.dart';
62 changes: 62 additions & 0 deletions lib/src/refresh_when_network_available_extension.dart
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);
}
}
69 changes: 69 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.18.0"
connectivity_plus:
dependency: "direct main"
description:
name: connectivity_plus
sha256: db7a4e143dc72cc3cb2044ef9b052a7ebfe729513e6a82943bc3526f784365b8
url: "https://pub.dev"
source: hosted
version: "6.0.3"
connectivity_plus_platform_interface:
dependency: transitive
description:
name: connectivity_plus_platform_interface
sha256: b6a56efe1e6675be240de39107281d4034b64ac23438026355b4234042a35adb
url: "https://pub.dev"
source: hosted
version: "2.0.0"
conventional_commit:
dependency: transitive
description:
Expand All @@ -89,6 +105,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.0+1"
dbus:
dependency: transitive
description:
name: dbus
sha256: "365c771ac3b0e58845f39ec6deebc76e3276aa9922b0cc60840712094d9047ac"
url: "https://pub.dev"
source: hosted
version: "0.7.10"
fake_async:
dependency: transitive
description:
Expand All @@ -97,6 +121,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
ffi:
dependency: transitive
description:
name: ffi
sha256: "493f37e7df1804778ff3a53bd691d8692ddf69702cf4c1c1096a2e41b4779e21"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
file:
dependency: transitive
description:
Expand All @@ -115,6 +147,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
glob:
dependency: transitive
description:
Expand Down Expand Up @@ -243,6 +280,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.0"
nm:
dependency: transitive
description:
name: nm
sha256: "2c9aae4127bdc8993206464fcc063611e0e36e72018696cd9631023a31b24254"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
path:
dependency: transitive
description:
Expand All @@ -251,6 +296,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.9.0"
petitparser:
dependency: transitive
description:
name: petitparser
sha256: c15605cd28af66339f8eb6fbe0e541bfe2d1b72d5825efc6598f3e0a31b9ad27
url: "https://pub.dev"
source: hosted
version: "6.0.2"
platform:
dependency: transitive
description:
Expand All @@ -259,6 +312,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.1.5"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.8"
pool:
dependency: transitive
description:
Expand Down Expand Up @@ -432,6 +493,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.5.1"
xml:
dependency: transitive
description:
name: xml
sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226
url: "https://pub.dev"
source: hosted
version: "6.5.0"
yaml:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ dev_dependencies:
dependencies:
flutter:
sdk: flutter
connectivity_plus: ^6.0.3
riverpod: ^2.0.0

0 comments on commit 76e111b

Please sign in to comment.