diff --git a/ecommerce_app/lib/src/exceptions/error_logger.dart b/ecommerce_app/lib/src/exceptions/error_logger.dart index d45ddae1..94ee82e5 100644 --- a/ecommerce_app/lib/src/exceptions/error_logger.dart +++ b/ecommerce_app/lib/src/exceptions/error_logger.dart @@ -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) { @@ -14,6 +16,7 @@ class ErrorLogger { } } -final errorLoggerProvider = Provider((ref) { +@riverpod +ErrorLogger errorLogger(ErrorLoggerRef ref) { return ErrorLogger(); -}); +} diff --git a/ecommerce_app/lib/src/exceptions/error_logger.g.dart b/ecommerce_app/lib/src/exceptions/error_logger.g.dart new file mode 100644 index 00000000..2541ebd4 --- /dev/null +++ b/ecommerce_app/lib/src/exceptions/error_logger.g.dart @@ -0,0 +1,24 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'error_logger.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$errorLoggerHash() => r'9020cd58e56e6c6490fd2ff6cd3b8748bd0165dd'; + +/// See also [errorLogger]. +@ProviderFor(errorLogger) +final errorLoggerProvider = AutoDisposeProvider.internal( + errorLogger, + name: r'errorLoggerProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') ? null : _$errorLoggerHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef ErrorLoggerRef = AutoDisposeProviderRef; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/authentication/data/fake_auth_repository.dart b/ecommerce_app/lib/src/features/authentication/data/fake_auth_repository.dart index 63543e16..a1f0c312 100644 --- a/ecommerce_app/lib/src/features/authentication/data/fake_auth_repository.dart +++ b/ecommerce_app/lib/src/features/authentication/data/fake_auth_repository.dart @@ -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}); @@ -70,13 +72,15 @@ class FakeAuthRepository { } } -final authRepositoryProvider = Provider((ref) { +@Riverpod(keepAlive: true) +FakeAuthRepository authRepository(AuthRepositoryRef ref) { final auth = FakeAuthRepository(); ref.onDispose(() => auth.dispose()); return auth; -}); +} -final authStateChangesProvider = StreamProvider((ref) { +@Riverpod(keepAlive: true) +Stream authStateChanges(AuthStateChangesRef ref) { final authRepository = ref.watch(authRepositoryProvider); return authRepository.authStateChanges(); -}); +} diff --git a/ecommerce_app/lib/src/features/authentication/data/fake_auth_repository.g.dart b/ecommerce_app/lib/src/features/authentication/data/fake_auth_repository.g.dart new file mode 100644 index 00000000..e432efc0 --- /dev/null +++ b/ecommerce_app/lib/src/features/authentication/data/fake_auth_repository.g.dart @@ -0,0 +1,40 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'fake_auth_repository.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$authRepositoryHash() => r'f94389a2b4b189c4aeaf4b993818ca745215d122'; + +/// See also [authRepository]. +@ProviderFor(authRepository) +final authRepositoryProvider = Provider.internal( + authRepository, + name: r'authRepositoryProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$authRepositoryHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef AuthRepositoryRef = ProviderRef; +String _$authStateChangesHash() => r'70d96078e41cd44270b2118f16bcbed842f33064'; + +/// See also [authStateChanges]. +@ProviderFor(authStateChanges) +final authStateChangesProvider = StreamProvider.internal( + authStateChanges, + name: r'authStateChangesProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$authStateChangesHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef AuthStateChangesRef = StreamProviderRef; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/cart/application/cart_service.dart b/ecommerce_app/lib/src/features/cart/application/cart_service.dart index 45b89764..da7f053d 100644 --- a/ecommerce_app/lib/src/features/cart/application/cart_service.dart +++ b/ecommerce_app/lib/src/features/cart/application/cart_service.dart @@ -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); @@ -59,27 +62,31 @@ class CartService { } } -final cartServiceProvider = Provider((ref) { +@Riverpod(keepAlive: true) +CartService cartService(CartServiceRef ref) { return CartService(ref); -}); +} -final cartProvider = StreamProvider((ref) { +@Riverpod(keepAlive: true) +Stream 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((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((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) { @@ -93,10 +100,10 @@ final cartTotalProvider = Provider.autoDispose((ref) { } else { return 0.0; } -}); +} -final itemAvailableQuantityProvider = - Provider.autoDispose.family((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 @@ -106,4 +113,4 @@ final itemAvailableQuantityProvider = } else { return product.availableQuantity; } -}); +} diff --git a/ecommerce_app/lib/src/features/cart/application/cart_service.g.dart b/ecommerce_app/lib/src/features/cart/application/cart_service.g.dart new file mode 100644 index 00000000..8ca3a5e9 --- /dev/null +++ b/ecommerce_app/lib/src/features/cart/application/cart_service.g.dart @@ -0,0 +1,216 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'cart_service.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$cartServiceHash() => r'db56fab553dde3f2dafbe456c14e199a66bab2d5'; + +/// See also [cartService]. +@ProviderFor(cartService) +final cartServiceProvider = Provider.internal( + cartService, + name: r'cartServiceProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') ? null : _$cartServiceHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef CartServiceRef = ProviderRef; +String _$cartHash() => r'8c55e19ee78a30cecb0d2b076ce688f11e9d5c1a'; + +/// See also [cart]. +@ProviderFor(cart) +final cartProvider = StreamProvider.internal( + cart, + name: r'cartProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') ? null : _$cartHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef CartRef = StreamProviderRef; +String _$cartItemsCountHash() => r'c8d67d2c5fa13d83da6dec350bca0418820ad8d8'; + +/// See also [cartItemsCount]. +@ProviderFor(cartItemsCount) +final cartItemsCountProvider = Provider.internal( + cartItemsCount, + name: r'cartItemsCountProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$cartItemsCountHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef CartItemsCountRef = ProviderRef; +String _$cartTotalHash() => r'620fa13b324b41b44713aede70cb19b607f395cb'; + +/// See also [cartTotal]. +@ProviderFor(cartTotal) +final cartTotalProvider = AutoDisposeProvider.internal( + cartTotal, + name: r'cartTotalProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') ? null : _$cartTotalHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef CartTotalRef = AutoDisposeProviderRef; +String _$itemAvailableQuantityHash() => + r'bf8ea212feaa0322b97753a9a241cbd1da278c2f'; + +/// Copied from Dart SDK +class _SystemHash { + _SystemHash._(); + + static int combine(int hash, int value) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + value); + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); + return hash ^ (hash >> 6); + } + + static int finish(int hash) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); + // ignore: parameter_assignments + hash = hash ^ (hash >> 11); + return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); + } +} + +/// See also [itemAvailableQuantity]. +@ProviderFor(itemAvailableQuantity) +const itemAvailableQuantityProvider = ItemAvailableQuantityFamily(); + +/// See also [itemAvailableQuantity]. +class ItemAvailableQuantityFamily extends Family { + /// See also [itemAvailableQuantity]. + const ItemAvailableQuantityFamily(); + + /// See also [itemAvailableQuantity]. + ItemAvailableQuantityProvider call( + Product product, + ) { + return ItemAvailableQuantityProvider( + product, + ); + } + + @override + ItemAvailableQuantityProvider getProviderOverride( + covariant ItemAvailableQuantityProvider provider, + ) { + return call( + provider.product, + ); + } + + static const Iterable? _dependencies = null; + + @override + Iterable? get dependencies => _dependencies; + + static const Iterable? _allTransitiveDependencies = null; + + @override + Iterable? get allTransitiveDependencies => + _allTransitiveDependencies; + + @override + String? get name => r'itemAvailableQuantityProvider'; +} + +/// See also [itemAvailableQuantity]. +class ItemAvailableQuantityProvider extends AutoDisposeProvider { + /// See also [itemAvailableQuantity]. + ItemAvailableQuantityProvider( + Product product, + ) : this._internal( + (ref) => itemAvailableQuantity( + ref as ItemAvailableQuantityRef, + product, + ), + from: itemAvailableQuantityProvider, + name: r'itemAvailableQuantityProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') + ? null + : _$itemAvailableQuantityHash, + dependencies: ItemAvailableQuantityFamily._dependencies, + allTransitiveDependencies: + ItemAvailableQuantityFamily._allTransitiveDependencies, + product: product, + ); + + ItemAvailableQuantityProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.product, + }) : super.internal(); + + final Product product; + + @override + Override overrideWith( + int Function(ItemAvailableQuantityRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: ItemAvailableQuantityProvider._internal( + (ref) => create(ref as ItemAvailableQuantityRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + product: product, + ), + ); + } + + @override + AutoDisposeProviderElement createElement() { + return _ItemAvailableQuantityProviderElement(this); + } + + @override + bool operator ==(Object other) { + return other is ItemAvailableQuantityProvider && other.product == product; + } + + @override + int get hashCode { + var hash = _SystemHash.combine(0, runtimeType.hashCode); + hash = _SystemHash.combine(hash, product.hashCode); + + return _SystemHash.finish(hash); + } +} + +mixin ItemAvailableQuantityRef on AutoDisposeProviderRef { + /// The parameter `product` of this provider. + Product get product; +} + +class _ItemAvailableQuantityProviderElement + extends AutoDisposeProviderElement with ItemAvailableQuantityRef { + _ItemAvailableQuantityProviderElement(super.provider); + + @override + Product get product => (origin as ItemAvailableQuantityProvider).product; +} +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/cart/application/cart_sync_service.dart b/ecommerce_app/lib/src/features/cart/application/cart_sync_service.dart index e09bb345..dd38a2b5 100644 --- a/ecommerce_app/lib/src/features/cart/application/cart_sync_service.dart +++ b/ecommerce_app/lib/src/features/cart/application/cart_sync_service.dart @@ -10,6 +10,9 @@ import 'package:ecommerce_app/src/features/cart/domain/item.dart'; 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:flutter_riverpod/flutter_riverpod.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'cart_sync_service.g.dart'; class CartSyncService { CartSyncService(this.ref) { @@ -81,6 +84,7 @@ class CartSyncService { } } -final cartSyncServiceProvider = Provider((ref) { +@Riverpod(keepAlive: true) +CartSyncService cartSyncService(CartSyncServiceRef ref) { return CartSyncService(ref); -}); +} diff --git a/ecommerce_app/lib/src/features/cart/application/cart_sync_service.g.dart b/ecommerce_app/lib/src/features/cart/application/cart_sync_service.g.dart new file mode 100644 index 00000000..01c15313 --- /dev/null +++ b/ecommerce_app/lib/src/features/cart/application/cart_sync_service.g.dart @@ -0,0 +1,25 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'cart_sync_service.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$cartSyncServiceHash() => r'3ad28e876fb58d96ae4b4aa09f8f494e6940de94'; + +/// See also [cartSyncService]. +@ProviderFor(cartSyncService) +final cartSyncServiceProvider = Provider.internal( + cartSyncService, + name: r'cartSyncServiceProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$cartSyncServiceHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef CartSyncServiceRef = ProviderRef; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/cart/data/local/local_cart_repository.dart b/ecommerce_app/lib/src/features/cart/data/local/local_cart_repository.dart index bcc4e867..71463812 100644 --- a/ecommerce_app/lib/src/features/cart/data/local/local_cart_repository.dart +++ b/ecommerce_app/lib/src/features/cart/data/local/local_cart_repository.dart @@ -1,5 +1,7 @@ -import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:ecommerce_app/src/features/cart/domain/cart.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'local_cart_repository.g.dart'; /// API for reading, watching and writing local cart data (guest user) abstract class LocalCartRepository { @@ -10,7 +12,8 @@ abstract class LocalCartRepository { Future setCart(Cart cart); } -final localCartRepositoryProvider = Provider((ref) { +@Riverpod(keepAlive: true) +LocalCartRepository localCartRepository(LocalCartRepositoryRef ref) { // * Override this in the main method throw UnimplementedError(); -}); +} diff --git a/ecommerce_app/lib/src/features/cart/data/local/local_cart_repository.g.dart b/ecommerce_app/lib/src/features/cart/data/local/local_cart_repository.g.dart new file mode 100644 index 00000000..ed1fc8d5 --- /dev/null +++ b/ecommerce_app/lib/src/features/cart/data/local/local_cart_repository.g.dart @@ -0,0 +1,26 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'local_cart_repository.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$localCartRepositoryHash() => + r'e13ac4190cf19fa3f638b583d346d5145c6f6a1e'; + +/// See also [localCartRepository]. +@ProviderFor(localCartRepository) +final localCartRepositoryProvider = Provider.internal( + localCartRepository, + name: r'localCartRepositoryProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$localCartRepositoryHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef LocalCartRepositoryRef = ProviderRef; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/cart/data/remote/remote_cart_repository.dart b/ecommerce_app/lib/src/features/cart/data/remote/remote_cart_repository.dart index 00db6b83..323e718f 100644 --- a/ecommerce_app/lib/src/features/cart/data/remote/remote_cart_repository.dart +++ b/ecommerce_app/lib/src/features/cart/data/remote/remote_cart_repository.dart @@ -1,6 +1,8 @@ import 'package:ecommerce_app/src/features/cart/data/remote/fake_remote_cart_repository.dart'; import 'package:ecommerce_app/src/features/cart/domain/cart.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'remote_cart_repository.g.dart'; /// API for reading, watching and writing cart data for a specific user ID abstract class RemoteCartRepository { @@ -11,7 +13,8 @@ abstract class RemoteCartRepository { Future setCart(String uid, Cart cart); } -final remoteCartRepositoryProvider = Provider((ref) { +@Riverpod(keepAlive: true) +RemoteCartRepository remoteCartRepository(RemoteCartRepositoryRef ref) { // TODO: replace with "real" remote cart repository return FakeRemoteCartRepository(addDelay: false); -}); +} diff --git a/ecommerce_app/lib/src/features/cart/data/remote/remote_cart_repository.g.dart b/ecommerce_app/lib/src/features/cart/data/remote/remote_cart_repository.g.dart new file mode 100644 index 00000000..b890e8b9 --- /dev/null +++ b/ecommerce_app/lib/src/features/cart/data/remote/remote_cart_repository.g.dart @@ -0,0 +1,26 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'remote_cart_repository.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$remoteCartRepositoryHash() => + r'2284e4e23b2a40cd5ae1af5928686e983ecd4512'; + +/// See also [remoteCartRepository]. +@ProviderFor(remoteCartRepository) +final remoteCartRepositoryProvider = Provider.internal( + remoteCartRepository, + name: r'remoteCartRepositoryProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$remoteCartRepositoryHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef RemoteCartRepositoryRef = ProviderRef; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/checkout/application/fake_checkout_service.dart b/ecommerce_app/lib/src/features/checkout/application/fake_checkout_service.dart index db4720db..99db816e 100644 --- a/ecommerce_app/lib/src/features/checkout/application/fake_checkout_service.dart +++ b/ecommerce_app/lib/src/features/checkout/application/fake_checkout_service.dart @@ -7,6 +7,9 @@ import 'package:ecommerce_app/src/features/products/data/fake_products_repositor import 'package:ecommerce_app/src/localization/string_hardcoded.dart'; import 'package:ecommerce_app/src/utils/current_date_provider.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'fake_checkout_service.g.dart'; /// A fake checkout service that doesn't process real payments. class FakeCheckoutService { @@ -69,6 +72,7 @@ class FakeCheckoutService { } } -final checkoutServiceProvider = Provider((ref) { +@riverpod +FakeCheckoutService checkoutService(CheckoutServiceRef ref) { return FakeCheckoutService(ref); -}); +} diff --git a/ecommerce_app/lib/src/features/checkout/application/fake_checkout_service.g.dart b/ecommerce_app/lib/src/features/checkout/application/fake_checkout_service.g.dart new file mode 100644 index 00000000..dc656051 --- /dev/null +++ b/ecommerce_app/lib/src/features/checkout/application/fake_checkout_service.g.dart @@ -0,0 +1,26 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'fake_checkout_service.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$checkoutServiceHash() => r'3ede6113d124c0a83c8517175d0f90cb8ae34fd4'; + +/// See also [checkoutService]. +@ProviderFor(checkoutService) +final checkoutServiceProvider = + AutoDisposeProvider.internal( + checkoutService, + name: r'checkoutServiceProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$checkoutServiceHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef CheckoutServiceRef = AutoDisposeProviderRef; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/orders/application/user_orders_provider.dart b/ecommerce_app/lib/src/features/orders/application/user_orders_provider.dart index fa2d057d..5f592df8 100644 --- a/ecommerce_app/lib/src/features/orders/application/user_orders_provider.dart +++ b/ecommerce_app/lib/src/features/orders/application/user_orders_provider.dart @@ -2,11 +2,14 @@ import 'package:ecommerce_app/src/features/authentication/data/fake_auth_reposit import 'package:ecommerce_app/src/features/orders/data/fake_orders_repository.dart'; import 'package:ecommerce_app/src/features/orders/domain/order.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 'user_orders_provider.g.dart'; /// Watch the list of user orders /// NOTE: Only watch this provider if the user is signed in. -final userOrdersProvider = StreamProvider.autoDispose>((ref) { +@riverpod +Stream> userOrders(UserOrdersRef ref) { final user = ref.watch(authStateChangesProvider).value; if (user != null) { return ref.watch(ordersRepositoryProvider).watchUserOrders(user.uid); @@ -14,18 +17,19 @@ final userOrdersProvider = StreamProvider.autoDispose>((ref) { // If the user is null, return an empty list (no orders) return Stream.value([]); } -}); +} /// Check if a product was previously purchased by the user -final matchingUserOrdersProvider = - StreamProvider.autoDispose.family, ProductID>((ref, productId) { +@riverpod +Stream> matchingUserOrders( + MatchingUserOrdersRef ref, ProductID id) { final user = ref.watch(authStateChangesProvider).value; if (user != null) { return ref .watch(ordersRepositoryProvider) - .watchUserOrders(user.uid, productId: productId); + .watchUserOrders(user.uid, productId: id); } else { // If the user is null, return an empty list (no orders) return Stream.value([]); } -}); +} diff --git a/ecommerce_app/lib/src/features/orders/application/user_orders_provider.g.dart b/ecommerce_app/lib/src/features/orders/application/user_orders_provider.g.dart new file mode 100644 index 00000000..2de80a6d --- /dev/null +++ b/ecommerce_app/lib/src/features/orders/application/user_orders_provider.g.dart @@ -0,0 +1,190 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'user_orders_provider.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$userOrdersHash() => r'ba100312a11b7f0eff43d8835cdfe16b71938684'; + +/// Watch the list of user orders +/// NOTE: Only watch this provider if the user is signed in. +/// +/// Copied from [userOrders]. +@ProviderFor(userOrders) +final userOrdersProvider = AutoDisposeStreamProvider>.internal( + userOrders, + name: r'userOrdersProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') ? null : _$userOrdersHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef UserOrdersRef = AutoDisposeStreamProviderRef>; +String _$matchingUserOrdersHash() => + r'3a1cbda4eb9a0756b63e803bc8b7615a5d296339'; + +/// Copied from Dart SDK +class _SystemHash { + _SystemHash._(); + + static int combine(int hash, int value) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + value); + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); + return hash ^ (hash >> 6); + } + + static int finish(int hash) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); + // ignore: parameter_assignments + hash = hash ^ (hash >> 11); + return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); + } +} + +/// Check if a product was previously purchased by the user +/// +/// Copied from [matchingUserOrders]. +@ProviderFor(matchingUserOrders) +const matchingUserOrdersProvider = MatchingUserOrdersFamily(); + +/// Check if a product was previously purchased by the user +/// +/// Copied from [matchingUserOrders]. +class MatchingUserOrdersFamily extends Family>> { + /// Check if a product was previously purchased by the user + /// + /// Copied from [matchingUserOrders]. + const MatchingUserOrdersFamily(); + + /// Check if a product was previously purchased by the user + /// + /// Copied from [matchingUserOrders]. + MatchingUserOrdersProvider call( + String id, + ) { + return MatchingUserOrdersProvider( + id, + ); + } + + @override + MatchingUserOrdersProvider getProviderOverride( + covariant MatchingUserOrdersProvider provider, + ) { + return call( + provider.id, + ); + } + + static const Iterable? _dependencies = null; + + @override + Iterable? get dependencies => _dependencies; + + static const Iterable? _allTransitiveDependencies = null; + + @override + Iterable? get allTransitiveDependencies => + _allTransitiveDependencies; + + @override + String? get name => r'matchingUserOrdersProvider'; +} + +/// Check if a product was previously purchased by the user +/// +/// Copied from [matchingUserOrders]. +class MatchingUserOrdersProvider + extends AutoDisposeStreamProvider> { + /// Check if a product was previously purchased by the user + /// + /// Copied from [matchingUserOrders]. + MatchingUserOrdersProvider( + String id, + ) : this._internal( + (ref) => matchingUserOrders( + ref as MatchingUserOrdersRef, + id, + ), + from: matchingUserOrdersProvider, + name: r'matchingUserOrdersProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') + ? null + : _$matchingUserOrdersHash, + dependencies: MatchingUserOrdersFamily._dependencies, + allTransitiveDependencies: + MatchingUserOrdersFamily._allTransitiveDependencies, + id: id, + ); + + MatchingUserOrdersProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + + final String id; + + @override + Override overrideWith( + Stream> Function(MatchingUserOrdersRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: MatchingUserOrdersProvider._internal( + (ref) => create(ref as MatchingUserOrdersRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeStreamProviderElement> createElement() { + return _MatchingUserOrdersProviderElement(this); + } + + @override + bool operator ==(Object other) { + return other is MatchingUserOrdersProvider && other.id == id; + } + + @override + int get hashCode { + var hash = _SystemHash.combine(0, runtimeType.hashCode); + hash = _SystemHash.combine(hash, id.hashCode); + + return _SystemHash.finish(hash); + } +} + +mixin MatchingUserOrdersRef on AutoDisposeStreamProviderRef> { + /// The parameter `id` of this provider. + String get id; +} + +class _MatchingUserOrdersProviderElement + extends AutoDisposeStreamProviderElement> + with MatchingUserOrdersRef { + _MatchingUserOrdersProviderElement(super.provider); + + @override + String get id => (origin as MatchingUserOrdersProvider).id; +} +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/orders/data/fake_orders_repository.dart b/ecommerce_app/lib/src/features/orders/data/fake_orders_repository.dart index c1fe12d1..78a353a8 100644 --- a/ecommerce_app/lib/src/features/orders/data/fake_orders_repository.dart +++ b/ecommerce_app/lib/src/features/orders/data/fake_orders_repository.dart @@ -2,7 +2,9 @@ import 'package:ecommerce_app/src/features/orders/domain/order.dart'; import 'package:ecommerce_app/src/features/products/domain/product.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_orders_repository.g.dart'; class FakeOrdersRepository { FakeOrdersRepository({this.addDelay = true}); @@ -43,6 +45,7 @@ class FakeOrdersRepository { } } -final ordersRepositoryProvider = Provider((ref) { +@Riverpod(keepAlive: true) +FakeOrdersRepository ordersRepository(OrdersRepositoryRef ref) { return FakeOrdersRepository(); -}); +} diff --git a/ecommerce_app/lib/src/features/orders/data/fake_orders_repository.g.dart b/ecommerce_app/lib/src/features/orders/data/fake_orders_repository.g.dart new file mode 100644 index 00000000..7445dd7b --- /dev/null +++ b/ecommerce_app/lib/src/features/orders/data/fake_orders_repository.g.dart @@ -0,0 +1,25 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'fake_orders_repository.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$ordersRepositoryHash() => r'c7c5986491943ae9308614b91187411a58f26781'; + +/// See also [ordersRepository]. +@ProviderFor(ordersRepository) +final ordersRepositoryProvider = Provider.internal( + ordersRepository, + name: r'ordersRepositoryProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$ordersRepositoryHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef OrdersRepositoryRef = ProviderRef; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/products/data/fake_products_repository.dart b/ecommerce_app/lib/src/features/products/data/fake_products_repository.dart index 418f1813..527e2b38 100644 --- a/ecommerce_app/lib/src/features/products/data/fake_products_repository.dart +++ b/ecommerce_app/lib/src/features/products/data/fake_products_repository.dart @@ -4,7 +4,9 @@ import 'package:ecommerce_app/src/constants/test_products.dart'; import 'package:ecommerce_app/src/features/products/domain/product.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_products_repository.g.dart'; class FakeProductsRepository { FakeProductsRepository({this.addDelay = true}); @@ -73,31 +75,33 @@ class FakeProductsRepository { } } -final productsRepositoryProvider = Provider((ref) { +@riverpod +FakeProductsRepository productsRepository(ProductsRepositoryRef ref) { // * Set addDelay to false for faster loading return FakeProductsRepository(addDelay: false); -}); +} -final productsListStreamProvider = - StreamProvider.autoDispose>((ref) { +@riverpod +Stream> productsListStream(ProductsListStreamRef ref) { final productsRepository = ref.watch(productsRepositoryProvider); return productsRepository.watchProductsList(); -}); +} -final productsListFutureProvider = - FutureProvider.autoDispose>((ref) { +@riverpod +Future> productsListFuture(ProductsListFutureRef ref) { final productsRepository = ref.watch(productsRepositoryProvider); return productsRepository.fetchProductsList(); -}); +} -final productProvider = - StreamProvider.autoDispose.family((ref, id) { +@riverpod +Stream product(ProductRef ref, ProductID id) { final productsRepository = ref.watch(productsRepositoryProvider); return productsRepository.watchProduct(id); -}); +} -final productsListSearchProvider = FutureProvider.autoDispose - .family, String>((ref, query) async { +@riverpod +Future> productsListSearch( + ProductsListSearchRef ref, String query) async { final link = ref.keepAlive(); // * keep previous search results in memory for 60 seconds final timer = Timer(const Duration(seconds: 60), () { @@ -106,4 +110,4 @@ final productsListSearchProvider = FutureProvider.autoDispose ref.onDispose(() => timer.cancel()); final productsRepository = ref.watch(productsRepositoryProvider); return productsRepository.searchProducts(query); -}); +} diff --git a/ecommerce_app/lib/src/features/products/data/fake_products_repository.g.dart b/ecommerce_app/lib/src/features/products/data/fake_products_repository.g.dart new file mode 100644 index 00000000..e247cd70 --- /dev/null +++ b/ecommerce_app/lib/src/features/products/data/fake_products_repository.g.dart @@ -0,0 +1,339 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'fake_products_repository.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$productsRepositoryHash() => + r'201c3eee525a7f0e114e2c35f87c15487b91626e'; + +/// See also [productsRepository]. +@ProviderFor(productsRepository) +final productsRepositoryProvider = + AutoDisposeProvider.internal( + productsRepository, + name: r'productsRepositoryProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$productsRepositoryHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef ProductsRepositoryRef = AutoDisposeProviderRef; +String _$productsListStreamHash() => + r'6ce4881c188ad3ff4e1f795150e00decd5aa2fa0'; + +/// See also [productsListStream]. +@ProviderFor(productsListStream) +final productsListStreamProvider = + AutoDisposeStreamProvider>.internal( + productsListStream, + name: r'productsListStreamProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$productsListStreamHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef ProductsListStreamRef = AutoDisposeStreamProviderRef>; +String _$productsListFutureHash() => + r'8f018bf574c889710ce2ba492782d7bb421cfbed'; + +/// See also [productsListFuture]. +@ProviderFor(productsListFuture) +final productsListFutureProvider = + AutoDisposeFutureProvider>.internal( + productsListFuture, + name: r'productsListFutureProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$productsListFutureHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef ProductsListFutureRef = AutoDisposeFutureProviderRef>; +String _$productHash() => r'5a13397cd9ecf77474aef18035961240f3400e67'; + +/// Copied from Dart SDK +class _SystemHash { + _SystemHash._(); + + static int combine(int hash, int value) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + value); + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); + return hash ^ (hash >> 6); + } + + static int finish(int hash) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); + // ignore: parameter_assignments + hash = hash ^ (hash >> 11); + return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); + } +} + +/// See also [product]. +@ProviderFor(product) +const productProvider = ProductFamily(); + +/// See also [product]. +class ProductFamily extends Family> { + /// See also [product]. + const ProductFamily(); + + /// See also [product]. + ProductProvider call( + String id, + ) { + return ProductProvider( + id, + ); + } + + @override + ProductProvider getProviderOverride( + covariant ProductProvider provider, + ) { + return call( + provider.id, + ); + } + + static const Iterable? _dependencies = null; + + @override + Iterable? get dependencies => _dependencies; + + static const Iterable? _allTransitiveDependencies = null; + + @override + Iterable? get allTransitiveDependencies => + _allTransitiveDependencies; + + @override + String? get name => r'productProvider'; +} + +/// See also [product]. +class ProductProvider extends AutoDisposeStreamProvider { + /// See also [product]. + ProductProvider( + String id, + ) : this._internal( + (ref) => product( + ref as ProductRef, + id, + ), + from: productProvider, + name: r'productProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') + ? null + : _$productHash, + dependencies: ProductFamily._dependencies, + allTransitiveDependencies: ProductFamily._allTransitiveDependencies, + id: id, + ); + + ProductProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + + final String id; + + @override + Override overrideWith( + Stream Function(ProductRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: ProductProvider._internal( + (ref) => create(ref as ProductRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeStreamProviderElement createElement() { + return _ProductProviderElement(this); + } + + @override + bool operator ==(Object other) { + return other is ProductProvider && other.id == id; + } + + @override + int get hashCode { + var hash = _SystemHash.combine(0, runtimeType.hashCode); + hash = _SystemHash.combine(hash, id.hashCode); + + return _SystemHash.finish(hash); + } +} + +mixin ProductRef on AutoDisposeStreamProviderRef { + /// The parameter `id` of this provider. + String get id; +} + +class _ProductProviderElement extends AutoDisposeStreamProviderElement + with ProductRef { + _ProductProviderElement(super.provider); + + @override + String get id => (origin as ProductProvider).id; +} + +String _$productsListSearchHash() => + r'fe33258d5369d856552d1eb45b72467729a06adc'; + +/// See also [productsListSearch]. +@ProviderFor(productsListSearch) +const productsListSearchProvider = ProductsListSearchFamily(); + +/// See also [productsListSearch]. +class ProductsListSearchFamily extends Family>> { + /// See also [productsListSearch]. + const ProductsListSearchFamily(); + + /// See also [productsListSearch]. + ProductsListSearchProvider call( + String query, + ) { + return ProductsListSearchProvider( + query, + ); + } + + @override + ProductsListSearchProvider getProviderOverride( + covariant ProductsListSearchProvider provider, + ) { + return call( + provider.query, + ); + } + + static const Iterable? _dependencies = null; + + @override + Iterable? get dependencies => _dependencies; + + static const Iterable? _allTransitiveDependencies = null; + + @override + Iterable? get allTransitiveDependencies => + _allTransitiveDependencies; + + @override + String? get name => r'productsListSearchProvider'; +} + +/// See also [productsListSearch]. +class ProductsListSearchProvider + extends AutoDisposeFutureProvider> { + /// See also [productsListSearch]. + ProductsListSearchProvider( + String query, + ) : this._internal( + (ref) => productsListSearch( + ref as ProductsListSearchRef, + query, + ), + from: productsListSearchProvider, + name: r'productsListSearchProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') + ? null + : _$productsListSearchHash, + dependencies: ProductsListSearchFamily._dependencies, + allTransitiveDependencies: + ProductsListSearchFamily._allTransitiveDependencies, + query: query, + ); + + ProductsListSearchProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.query, + }) : super.internal(); + + final String query; + + @override + Override overrideWith( + FutureOr> Function(ProductsListSearchRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: ProductsListSearchProvider._internal( + (ref) => create(ref as ProductsListSearchRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + query: query, + ), + ); + } + + @override + AutoDisposeFutureProviderElement> createElement() { + return _ProductsListSearchProviderElement(this); + } + + @override + bool operator ==(Object other) { + return other is ProductsListSearchProvider && other.query == query; + } + + @override + int get hashCode { + var hash = _SystemHash.combine(0, runtimeType.hashCode); + hash = _SystemHash.combine(hash, query.hashCode); + + return _SystemHash.finish(hash); + } +} + +mixin ProductsListSearchRef on AutoDisposeFutureProviderRef> { + /// The parameter `query` of this provider. + String get query; +} + +class _ProductsListSearchProviderElement + extends AutoDisposeFutureProviderElement> + with ProductsListSearchRef { + _ProductsListSearchProviderElement(super.provider); + + @override + String get query => (origin as ProductsListSearchProvider).query; +} +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/products/presentation/products_list/products_search_state_provider.dart b/ecommerce_app/lib/src/features/products/presentation/products_list/products_search_state_provider.dart index e5f8ea45..1377f418 100644 --- a/ecommerce_app/lib/src/features/products/presentation/products_list/products_search_state_provider.dart +++ b/ecommerce_app/lib/src/features/products/presentation/products_list/products_search_state_provider.dart @@ -1,13 +1,16 @@ 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 'products_search_state_provider.g.dart'; final productsSearchQueryStateProvider = StateProvider((ref) { return ''; }); -final productsSearchResultsProvider = - FutureProvider.autoDispose>((ref) async { +@riverpod +Future> productsSearchResults(ProductsSearchResultsRef ref) { final searchQuery = ref.watch(productsSearchQueryStateProvider); return ref.watch(productsListSearchProvider(searchQuery).future); -}); +} diff --git a/ecommerce_app/lib/src/features/products/presentation/products_list/products_search_state_provider.g.dart b/ecommerce_app/lib/src/features/products/presentation/products_list/products_search_state_provider.g.dart new file mode 100644 index 00000000..a1c478f1 --- /dev/null +++ b/ecommerce_app/lib/src/features/products/presentation/products_list/products_search_state_provider.g.dart @@ -0,0 +1,27 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'products_search_state_provider.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$productsSearchResultsHash() => + r'18e0559fde761f1931c731c58aa0b32fe5a1bde6'; + +/// See also [productsSearchResults]. +@ProviderFor(productsSearchResults) +final productsSearchResultsProvider = + AutoDisposeFutureProvider>.internal( + productsSearchResults, + name: r'productsSearchResultsProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$productsSearchResultsHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef ProductsSearchResultsRef = AutoDisposeFutureProviderRef>; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/reviews/application/reviews_service.dart b/ecommerce_app/lib/src/features/reviews/application/reviews_service.dart index e5539cc6..5012cb4d 100644 --- a/ecommerce_app/lib/src/features/reviews/application/reviews_service.dart +++ b/ecommerce_app/lib/src/features/reviews/application/reviews_service.dart @@ -5,6 +5,9 @@ import 'package:ecommerce_app/src/features/reviews/data/fake_reviews_repository. import 'package:ecommerce_app/src/features/reviews/domain/review.dart'; import 'package:ecommerce_app/src/localization/string_hardcoded.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'reviews_service.g.dart'; class ReviewsService { ReviewsService(this.ref); @@ -60,32 +63,29 @@ class ReviewsService { } } -final reviewsServiceProvider = Provider((ref) { +@Riverpod(keepAlive: true) +ReviewsService reviewsService(ReviewsServiceRef ref) { return ReviewsService(ref); -}); +} /// Check if a product was previously reviewed by the user -final userReviewFutureProvider = - FutureProvider.autoDispose.family((ref, productId) { +@riverpod +Future userReviewFuture(UserReviewFutureRef ref, ProductID id) { final user = ref.watch(authStateChangesProvider).value; if (user != null) { - return ref - .watch(reviewsRepositoryProvider) - .fetchUserReview(productId, user.uid); + return ref.watch(reviewsRepositoryProvider).fetchUserReview(id, user.uid); } else { return Future.value(null); } -}); +} /// Check if a product was previously reviewed by the user -final userReviewStreamProvider = - StreamProvider.autoDispose.family((ref, productId) { +@riverpod +Stream userReviewStream(UserReviewStreamRef ref, ProductID id) { final user = ref.watch(authStateChangesProvider).value; if (user != null) { - return ref - .watch(reviewsRepositoryProvider) - .watchUserReview(productId, user.uid); + return ref.watch(reviewsRepositoryProvider).watchUserReview(id, user.uid); } else { return Stream.value(null); } -}); +} diff --git a/ecommerce_app/lib/src/features/reviews/application/reviews_service.g.dart b/ecommerce_app/lib/src/features/reviews/application/reviews_service.g.dart new file mode 100644 index 00000000..0ef1a3fe --- /dev/null +++ b/ecommerce_app/lib/src/features/reviews/application/reviews_service.g.dart @@ -0,0 +1,325 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'reviews_service.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$reviewsServiceHash() => r'd91877727c3a90c4267086fa9cd81c8ab6938afd'; + +/// See also [reviewsService]. +@ProviderFor(reviewsService) +final reviewsServiceProvider = Provider.internal( + reviewsService, + name: r'reviewsServiceProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$reviewsServiceHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef ReviewsServiceRef = ProviderRef; +String _$userReviewFutureHash() => r'a0946dc6e8e861944f7a069a345f1036879b5b1f'; + +/// Copied from Dart SDK +class _SystemHash { + _SystemHash._(); + + static int combine(int hash, int value) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + value); + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); + return hash ^ (hash >> 6); + } + + static int finish(int hash) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); + // ignore: parameter_assignments + hash = hash ^ (hash >> 11); + return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); + } +} + +/// Check if a product was previously reviewed by the user +/// +/// Copied from [userReviewFuture]. +@ProviderFor(userReviewFuture) +const userReviewFutureProvider = UserReviewFutureFamily(); + +/// Check if a product was previously reviewed by the user +/// +/// Copied from [userReviewFuture]. +class UserReviewFutureFamily extends Family> { + /// Check if a product was previously reviewed by the user + /// + /// Copied from [userReviewFuture]. + const UserReviewFutureFamily(); + + /// Check if a product was previously reviewed by the user + /// + /// Copied from [userReviewFuture]. + UserReviewFutureProvider call( + String id, + ) { + return UserReviewFutureProvider( + id, + ); + } + + @override + UserReviewFutureProvider getProviderOverride( + covariant UserReviewFutureProvider provider, + ) { + return call( + provider.id, + ); + } + + static const Iterable? _dependencies = null; + + @override + Iterable? get dependencies => _dependencies; + + static const Iterable? _allTransitiveDependencies = null; + + @override + Iterable? get allTransitiveDependencies => + _allTransitiveDependencies; + + @override + String? get name => r'userReviewFutureProvider'; +} + +/// Check if a product was previously reviewed by the user +/// +/// Copied from [userReviewFuture]. +class UserReviewFutureProvider extends AutoDisposeFutureProvider { + /// Check if a product was previously reviewed by the user + /// + /// Copied from [userReviewFuture]. + UserReviewFutureProvider( + String id, + ) : this._internal( + (ref) => userReviewFuture( + ref as UserReviewFutureRef, + id, + ), + from: userReviewFutureProvider, + name: r'userReviewFutureProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') + ? null + : _$userReviewFutureHash, + dependencies: UserReviewFutureFamily._dependencies, + allTransitiveDependencies: + UserReviewFutureFamily._allTransitiveDependencies, + id: id, + ); + + UserReviewFutureProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + + final String id; + + @override + Override overrideWith( + FutureOr Function(UserReviewFutureRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: UserReviewFutureProvider._internal( + (ref) => create(ref as UserReviewFutureRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeFutureProviderElement createElement() { + return _UserReviewFutureProviderElement(this); + } + + @override + bool operator ==(Object other) { + return other is UserReviewFutureProvider && other.id == id; + } + + @override + int get hashCode { + var hash = _SystemHash.combine(0, runtimeType.hashCode); + hash = _SystemHash.combine(hash, id.hashCode); + + return _SystemHash.finish(hash); + } +} + +mixin UserReviewFutureRef on AutoDisposeFutureProviderRef { + /// The parameter `id` of this provider. + String get id; +} + +class _UserReviewFutureProviderElement + extends AutoDisposeFutureProviderElement with UserReviewFutureRef { + _UserReviewFutureProviderElement(super.provider); + + @override + String get id => (origin as UserReviewFutureProvider).id; +} + +String _$userReviewStreamHash() => r'63d6101443637e0a266dbb2c496abfe44113218f'; + +/// Check if a product was previously reviewed by the user +/// +/// Copied from [userReviewStream]. +@ProviderFor(userReviewStream) +const userReviewStreamProvider = UserReviewStreamFamily(); + +/// Check if a product was previously reviewed by the user +/// +/// Copied from [userReviewStream]. +class UserReviewStreamFamily extends Family> { + /// Check if a product was previously reviewed by the user + /// + /// Copied from [userReviewStream]. + const UserReviewStreamFamily(); + + /// Check if a product was previously reviewed by the user + /// + /// Copied from [userReviewStream]. + UserReviewStreamProvider call( + String id, + ) { + return UserReviewStreamProvider( + id, + ); + } + + @override + UserReviewStreamProvider getProviderOverride( + covariant UserReviewStreamProvider provider, + ) { + return call( + provider.id, + ); + } + + static const Iterable? _dependencies = null; + + @override + Iterable? get dependencies => _dependencies; + + static const Iterable? _allTransitiveDependencies = null; + + @override + Iterable? get allTransitiveDependencies => + _allTransitiveDependencies; + + @override + String? get name => r'userReviewStreamProvider'; +} + +/// Check if a product was previously reviewed by the user +/// +/// Copied from [userReviewStream]. +class UserReviewStreamProvider extends AutoDisposeStreamProvider { + /// Check if a product was previously reviewed by the user + /// + /// Copied from [userReviewStream]. + UserReviewStreamProvider( + String id, + ) : this._internal( + (ref) => userReviewStream( + ref as UserReviewStreamRef, + id, + ), + from: userReviewStreamProvider, + name: r'userReviewStreamProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') + ? null + : _$userReviewStreamHash, + dependencies: UserReviewStreamFamily._dependencies, + allTransitiveDependencies: + UserReviewStreamFamily._allTransitiveDependencies, + id: id, + ); + + UserReviewStreamProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + + final String id; + + @override + Override overrideWith( + Stream Function(UserReviewStreamRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: UserReviewStreamProvider._internal( + (ref) => create(ref as UserReviewStreamRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeStreamProviderElement createElement() { + return _UserReviewStreamProviderElement(this); + } + + @override + bool operator ==(Object other) { + return other is UserReviewStreamProvider && other.id == id; + } + + @override + int get hashCode { + var hash = _SystemHash.combine(0, runtimeType.hashCode); + hash = _SystemHash.combine(hash, id.hashCode); + + return _SystemHash.finish(hash); + } +} + +mixin UserReviewStreamRef on AutoDisposeStreamProviderRef { + /// The parameter `id` of this provider. + String get id; +} + +class _UserReviewStreamProviderElement + extends AutoDisposeStreamProviderElement with UserReviewStreamRef { + _UserReviewStreamProviderElement(super.provider); + + @override + String get id => (origin as UserReviewStreamProvider).id; +} +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/features/reviews/data/fake_reviews_repository.dart b/ecommerce_app/lib/src/features/reviews/data/fake_reviews_repository.dart index d4fa0acf..2767dd4c 100644 --- a/ecommerce_app/lib/src/features/reviews/data/fake_reviews_repository.dart +++ b/ecommerce_app/lib/src/features/reviews/data/fake_reviews_repository.dart @@ -2,7 +2,9 @@ import 'package:ecommerce_app/src/features/products/domain/product.dart'; import 'package:ecommerce_app/src/features/reviews/domain/review.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_reviews_repository.g.dart'; /// A repository used to store all user reviews for all products class FakeReviewsRepository { @@ -78,11 +80,12 @@ class FakeReviewsRepository { } } -final reviewsRepositoryProvider = Provider((ref) { +@Riverpod(keepAlive: true) +FakeReviewsRepository reviewsRepository(ReviewsRepositoryRef ref) { return FakeReviewsRepository(); -}); +} -final productReviewsProvider = StreamProvider.autoDispose - .family, ProductID>((ref, productId) { - return ref.watch(reviewsRepositoryProvider).watchReviews(productId); -}); +@riverpod +Stream> productReviews(ProductReviewsRef ref, ProductID id) { + return ref.watch(reviewsRepositoryProvider).watchReviews(id); +} diff --git a/ecommerce_app/lib/src/features/reviews/data/fake_reviews_repository.g.dart b/ecommerce_app/lib/src/features/reviews/data/fake_reviews_repository.g.dart new file mode 100644 index 00000000..08837cdf --- /dev/null +++ b/ecommerce_app/lib/src/features/reviews/data/fake_reviews_repository.g.dart @@ -0,0 +1,174 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'fake_reviews_repository.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$reviewsRepositoryHash() => r'8a69281fa37026cd50057faf754621a4e46629e5'; + +/// See also [reviewsRepository]. +@ProviderFor(reviewsRepository) +final reviewsRepositoryProvider = Provider.internal( + reviewsRepository, + name: r'reviewsRepositoryProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$reviewsRepositoryHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef ReviewsRepositoryRef = ProviderRef; +String _$productReviewsHash() => r'a406f54f708a91b59663ad194a30c08533f20ca8'; + +/// Copied from Dart SDK +class _SystemHash { + _SystemHash._(); + + static int combine(int hash, int value) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + value); + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); + return hash ^ (hash >> 6); + } + + static int finish(int hash) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); + // ignore: parameter_assignments + hash = hash ^ (hash >> 11); + return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); + } +} + +/// See also [productReviews]. +@ProviderFor(productReviews) +const productReviewsProvider = ProductReviewsFamily(); + +/// See also [productReviews]. +class ProductReviewsFamily extends Family>> { + /// See also [productReviews]. + const ProductReviewsFamily(); + + /// See also [productReviews]. + ProductReviewsProvider call( + String id, + ) { + return ProductReviewsProvider( + id, + ); + } + + @override + ProductReviewsProvider getProviderOverride( + covariant ProductReviewsProvider provider, + ) { + return call( + provider.id, + ); + } + + static const Iterable? _dependencies = null; + + @override + Iterable? get dependencies => _dependencies; + + static const Iterable? _allTransitiveDependencies = null; + + @override + Iterable? get allTransitiveDependencies => + _allTransitiveDependencies; + + @override + String? get name => r'productReviewsProvider'; +} + +/// See also [productReviews]. +class ProductReviewsProvider extends AutoDisposeStreamProvider> { + /// See also [productReviews]. + ProductReviewsProvider( + String id, + ) : this._internal( + (ref) => productReviews( + ref as ProductReviewsRef, + id, + ), + from: productReviewsProvider, + name: r'productReviewsProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') + ? null + : _$productReviewsHash, + dependencies: ProductReviewsFamily._dependencies, + allTransitiveDependencies: + ProductReviewsFamily._allTransitiveDependencies, + id: id, + ); + + ProductReviewsProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + + final String id; + + @override + Override overrideWith( + Stream> Function(ProductReviewsRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: ProductReviewsProvider._internal( + (ref) => create(ref as ProductReviewsRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeStreamProviderElement> createElement() { + return _ProductReviewsProviderElement(this); + } + + @override + bool operator ==(Object other) { + return other is ProductReviewsProvider && other.id == id; + } + + @override + int get hashCode { + var hash = _SystemHash.combine(0, runtimeType.hashCode); + hash = _SystemHash.combine(hash, id.hashCode); + + return _SystemHash.finish(hash); + } +} + +mixin ProductReviewsRef on AutoDisposeStreamProviderRef> { + /// The parameter `id` of this provider. + String get id; +} + +class _ProductReviewsProviderElement + extends AutoDisposeStreamProviderElement> + with ProductReviewsRef { + _ProductReviewsProviderElement(super.provider); + + @override + String get id => (origin as ProductReviewsProvider).id; +} +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/ecommerce_app/lib/src/routing/app_router.dart b/ecommerce_app/lib/src/routing/app_router.dart index bab5d6f9..dad384fd 100644 --- a/ecommerce_app/lib/src/routing/app_router.dart +++ b/ecommerce_app/lib/src/routing/app_router.dart @@ -11,8 +11,10 @@ import 'package:ecommerce_app/src/features/reviews/presentation/leave_review_scr import 'package:ecommerce_app/src/routing/go_router_refresh_stream.dart'; import 'package:ecommerce_app/src/routing/not_found_screen.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'app_router.g.dart'; enum AppRoute { home, @@ -25,7 +27,8 @@ enum AppRoute { signIn, } -final goRouterProvider = Provider((ref) { +@Riverpod(keepAlive: true) +GoRouter goRouter(GoRouterRef ref) { final authRepository = ref.watch(authRepositoryProvider); return GoRouter( initialLocation: '/', @@ -121,4 +124,4 @@ final goRouterProvider = Provider((ref) { ], errorBuilder: (context, state) => const NotFoundScreen(), ); -}); +} diff --git a/ecommerce_app/lib/src/routing/app_router.g.dart b/ecommerce_app/lib/src/routing/app_router.g.dart new file mode 100644 index 00000000..6b690997 --- /dev/null +++ b/ecommerce_app/lib/src/routing/app_router.g.dart @@ -0,0 +1,24 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'app_router.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$goRouterHash() => r'b797c58e80ae15d49cd7d8cbda49537ea8a91614'; + +/// See also [goRouter]. +@ProviderFor(goRouter) +final goRouterProvider = Provider.internal( + goRouter, + name: r'goRouterProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') ? null : _$goRouterHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef GoRouterRef = ProviderRef; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member