Skip to content

Commit

Permalink
Update providers for all the repositories and service classes to the …
Browse files Browse the repository at this point in the history
…new Riverpod Generator syntax
  • Loading branch information
bizz84 committed Nov 16, 2023
1 parent 4262192 commit 981240f
Show file tree
Hide file tree
Showing 28 changed files with 1,616 additions and 81 deletions.
9 changes: 6 additions & 3 deletions ecommerce_app/lib/src/exceptions/error_logger.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:ecommerce_app/src/exceptions/app_exception.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'error_logger.g.dart';

class ErrorLogger {
void logError(Object error, StackTrace? stackTrace) {
Expand All @@ -14,6 +16,7 @@ class ErrorLogger {
}
}

final errorLoggerProvider = Provider<ErrorLogger>((ref) {
@riverpod
ErrorLogger errorLogger(ErrorLoggerRef ref) {
return ErrorLogger();
});
}
24 changes: 24 additions & 0 deletions ecommerce_app/lib/src/exceptions/error_logger.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'package:ecommerce_app/src/features/authentication/domain/app_user.dart';
import 'package:ecommerce_app/src/features/authentication/domain/fake_app_user.dart';
import 'package:ecommerce_app/src/utils/delay.dart';
import 'package:ecommerce_app/src/utils/in_memory_store.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'fake_auth_repository.g.dart';

class FakeAuthRepository {
FakeAuthRepository({this.addDelay = true});
Expand Down Expand Up @@ -70,13 +72,15 @@ class FakeAuthRepository {
}
}

final authRepositoryProvider = Provider<FakeAuthRepository>((ref) {
@Riverpod(keepAlive: true)
FakeAuthRepository authRepository(AuthRepositoryRef ref) {
final auth = FakeAuthRepository();
ref.onDispose(() => auth.dispose());
return auth;
});
}

final authStateChangesProvider = StreamProvider<AppUser?>((ref) {
@Riverpod(keepAlive: true)
Stream<AppUser?> authStateChanges(AuthStateChangesRef ref) {
final authRepository = ref.watch(authRepositoryProvider);
return authRepository.authStateChanges();
});
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 18 additions & 11 deletions ecommerce_app/lib/src/features/cart/application/cart_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import 'package:ecommerce_app/src/features/cart/domain/mutable_cart.dart';
import 'package:ecommerce_app/src/features/products/data/fake_products_repository.dart';
import 'package:ecommerce_app/src/features/products/domain/product.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'cart_service.g.dart';

class CartService {
CartService(this.ref);
Expand Down Expand Up @@ -59,27 +62,31 @@ class CartService {
}
}

final cartServiceProvider = Provider<CartService>((ref) {
@Riverpod(keepAlive: true)
CartService cartService(CartServiceRef ref) {
return CartService(ref);
});
}

final cartProvider = StreamProvider<Cart>((ref) {
@Riverpod(keepAlive: true)
Stream<Cart> cart(CartRef ref) {
final user = ref.watch(authStateChangesProvider).value;
if (user != null) {
return ref.watch(remoteCartRepositoryProvider).watchCart(user.uid);
} else {
return ref.watch(localCartRepositoryProvider).watchCart();
}
});
}

final cartItemsCountProvider = Provider<int>((ref) {
@Riverpod(keepAlive: true)
int cartItemsCount(CartItemsCountRef ref) {
return ref.watch(cartProvider).maybeMap(
data: (cart) => cart.value.items.length,
orElse: () => 0,
);
});
}

final cartTotalProvider = Provider.autoDispose<double>((ref) {
@riverpod
double cartTotal(CartTotalRef ref) {
final cart = ref.watch(cartProvider).value ?? const Cart();
final productsList = ref.watch(productsListStreamProvider).value ?? [];
if (cart.items.isNotEmpty && productsList.isNotEmpty) {
Expand All @@ -93,10 +100,10 @@ final cartTotalProvider = Provider.autoDispose<double>((ref) {
} else {
return 0.0;
}
});
}

final itemAvailableQuantityProvider =
Provider.autoDispose.family<int, Product>((ref, product) {
@riverpod
int itemAvailableQuantity(ItemAvailableQuantityRef ref, Product product) {
final cart = ref.watch(cartProvider).value;
if (cart != null) {
// get the current quantity for the given product in the cart
Expand All @@ -106,4 +113,4 @@ final itemAvailableQuantityProvider =
} else {
return product.availableQuantity;
}
});
}
Loading

0 comments on commit 981240f

Please sign in to comment.