Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add refresh when returning to foreground extension #22

Merged
merged 7 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Riverpods Community Extensions
# Riverpod Community Extensions

[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
[![Powered by Mason](https://img.shields.io/endpoint?url=https%3A%2F%2Ftinyurl.com%2Fmason-badge)](https://github.com/felangel/mason)
Expand All @@ -22,12 +22,12 @@ This package adds the following methods to ref types:

## Installation 💻

**❗ In order to start using Riverpods Community Extensions you must have the [Dart SDK][dart_install_link] installed on your machine.**
**❗ In order to start using Riverpod Community Extensions you must have the [Dart SDK][dart_install_link] installed on your machine.**

Install via `dart pub add`:

```sh
dart pub add riverpods_community_extensions
dart pub add riverpod_community_extensions
```

## Usage 🎨
Expand All @@ -37,7 +37,7 @@ Simply import the package and use the provided extensions on your ref types.
Example without codegen:

```dart
import 'package:riverpods_community_extensions/riverpods_community_extensions.dart';
import 'package:riverpod_community_extensions/riverpod_community_extensions.dart';
import 'package:riverpod/riverpod.dart';

final dataProvider = FutureProvider.autoDispose((ref) async {
Expand All @@ -50,7 +50,7 @@ final dataProvider = FutureProvider.autoDispose((ref) async {
Example with codegen:

```dart
import 'package:riverpods_community_extensions/riverpods_community_extensions.dart';
import 'package:riverpod_community_extensions/riverpod_community_extensions.dart';
import 'package:riverpod/riverpod.dart';

part 'data_provider.g.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Community extensions for Riverpods
library riverpods_community_extensions;
/// Community extensions for riverpod
library riverpod_community_extensions;

export 'src/auto_refresh_extension.dart';
export 'src/cache_data_for_extension.dart';
export 'src/cache_for_extension.dart';
export 'src/debounce_extension.dart';
export 'src/refresh_extension.dart';
37 changes: 0 additions & 37 deletions lib/src/auto_refresh_extension.dart

This file was deleted.

61 changes: 61 additions & 0 deletions lib/src/refresh_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:riverpod/riverpod.dart';

/// Adds auto-refresh functionalities to AutoDisposeRef<T> objects.
///
/// See [autoRefresh] and [refreshWhenReturningToForeground] for more details.
extension RefreshExtension<T> on AutoDisposeRef<T> {
/// Refreshes the value at the specified interval.
///
/// This can be useful for scenarios where you want to refresh a value at a
/// specified interval.
///
/// Example usages:
///
/// without codegen:
/// ```dart
/// final dataProvider = Provider<int>((ref) {
/// ref.autoRefresh(const Duration(seconds: 1));
/// return fetchData();
/// });
/// ```
///
/// with codegen:
/// ```dart
/// @riverpod
/// int data(DataRef ref) {
/// ref.autoRefresh(const Duration(seconds: 1));
/// return fetchData();
/// }
/// ```
void autoRefresh(Duration duration) {
final timer = Timer(duration, invalidateSelf);
onDispose(timer.cancel);
}

/// Refreshes the value each time the app returns to foreground.
///
/// Example usages:
/// without codegen:
/// ```dart
/// final dataProvider = Provider<int>((ref) {
/// ref.refreshWhenReturningToForeground();
/// return fetchData();
/// });
/// ```
///
/// with codegen:
/// ```dart
/// @riverpod
/// int data(DataRef ref) {
/// ref.refreshWhenReturningToForeground();
/// return fetchData();
/// }
/// ```
///
void refreshWhenReturningToForeground() {
final listener = AppLifecycleListener(onResume: invalidateSelf);
onDispose(listener.dispose);
}
}
Loading
Loading