diff --git a/ecommerce_app/lib/src/features/cart/presentation/add_to_cart/add_to_cart_controller.dart b/ecommerce_app/lib/src/features/cart/presentation/add_to_cart/add_to_cart_controller.dart index dc35fbfc..996780cd 100644 --- a/ecommerce_app/lib/src/features/cart/presentation/add_to_cart/add_to_cart_controller.dart +++ b/ecommerce_app/lib/src/features/cart/presentation/add_to_cart/add_to_cart_controller.dart @@ -1,17 +1,23 @@ import 'package:ecommerce_app/src/features/cart/application/cart_service.dart'; import 'package:ecommerce_app/src/features/cart/domain/item.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'; -class AddToCartController extends StateNotifier> { - AddToCartController({required this.cartService}) : super(const AsyncData(1)); - final CartService cartService; +part 'add_to_cart_controller.g.dart'; + +@riverpod +class AddToCartController extends _$AddToCartController { + @override + FutureOr build() { + return 1; + } void updateQuantity(int quantity) { state = AsyncData(quantity); } Future addItem(ProductID productId) async { + final cartService = ref.read(cartServiceProvider); final item = Item(productId: productId, quantity: state.value!); state = const AsyncLoading().copyWithPrevious(state); final value = await AsyncValue.guard(() => cartService.addItem(item)); @@ -22,11 +28,3 @@ class AddToCartController extends StateNotifier> { } } } - -final addToCartControllerProvider = - StateNotifierProvider.autoDispose>( - (ref) { - return AddToCartController( - cartService: ref.watch(cartServiceProvider), - ); -}); diff --git a/ecommerce_app/lib/src/features/cart/presentation/add_to_cart/add_to_cart_controller.g.dart b/ecommerce_app/lib/src/features/cart/presentation/add_to_cart/add_to_cart_controller.g.dart new file mode 100644 index 00000000..b38998e8 --- /dev/null +++ b/ecommerce_app/lib/src/features/cart/presentation/add_to_cart/add_to_cart_controller.g.dart @@ -0,0 +1,27 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'add_to_cart_controller.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$addToCartControllerHash() => + r'0238f81034cce3512a17fc50b91aba81ff5ac664'; + +/// See also [AddToCartController]. +@ProviderFor(AddToCartController) +final addToCartControllerProvider = + AutoDisposeAsyncNotifierProvider.internal( + AddToCartController.new, + name: r'addToCartControllerProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$addToCartControllerHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef _$AddToCartController = AutoDisposeAsyncNotifier; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/ecommerce_app/lib/src/features/cart/presentation/shopping_cart/shopping_cart_screen_controller.dart b/ecommerce_app/lib/src/features/cart/presentation/shopping_cart/shopping_cart_screen_controller.dart index 1b801255..56e67db1 100644 --- a/ecommerce_app/lib/src/features/cart/presentation/shopping_cart/shopping_cart_screen_controller.dart +++ b/ecommerce_app/lib/src/features/cart/presentation/shopping_cart/shopping_cart_screen_controller.dart @@ -1,12 +1,18 @@ import 'package:ecommerce_app/src/features/cart/application/cart_service.dart'; import 'package:ecommerce_app/src/features/cart/domain/item.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'; -class ShoppingCartScreenController extends StateNotifier> { - ShoppingCartScreenController({required this.cartService}) - : super(const AsyncData(null)); - final CartService cartService; +part 'shopping_cart_screen_controller.g.dart'; + +@riverpod +class ShoppingCartScreenController extends _$ShoppingCartScreenController { + @override + FutureOr build() { + // nothing to do + } + + CartService get cartService => ref.read(cartServiceProvider); Future updateItemQuantity(ProductID productId, int quantity) async { state = const AsyncLoading(); @@ -19,11 +25,3 @@ class ShoppingCartScreenController extends StateNotifier> { state = await AsyncValue.guard(() => cartService.removeItemById(productId)); } } - -final shoppingCartScreenControllerProvider = - StateNotifierProvider>( - (ref) { - return ShoppingCartScreenController( - cartService: ref.watch(cartServiceProvider), - ); -}); diff --git a/ecommerce_app/lib/src/features/cart/presentation/shopping_cart/shopping_cart_screen_controller.g.dart b/ecommerce_app/lib/src/features/cart/presentation/shopping_cart/shopping_cart_screen_controller.g.dart new file mode 100644 index 00000000..32f903cc --- /dev/null +++ b/ecommerce_app/lib/src/features/cart/presentation/shopping_cart/shopping_cart_screen_controller.g.dart @@ -0,0 +1,27 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'shopping_cart_screen_controller.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$shoppingCartScreenControllerHash() => + r'8123dd3aaf0bf02858cb11556ecba6c16b72aedd'; + +/// See also [ShoppingCartScreenController]. +@ProviderFor(ShoppingCartScreenController) +final shoppingCartScreenControllerProvider = AutoDisposeAsyncNotifierProvider< + ShoppingCartScreenController, void>.internal( + ShoppingCartScreenController.new, + name: r'shoppingCartScreenControllerProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$shoppingCartScreenControllerHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef _$ShoppingCartScreenController = AutoDisposeAsyncNotifier; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/ecommerce_app/lib/src/features/checkout/presentation/payment/payment_button_controller.dart b/ecommerce_app/lib/src/features/checkout/presentation/payment/payment_button_controller.dart index 4bee63a7..5aa7192e 100644 --- a/ecommerce_app/lib/src/features/checkout/presentation/payment/payment_button_controller.dart +++ b/ecommerce_app/lib/src/features/checkout/presentation/payment/payment_button_controller.dart @@ -1,12 +1,22 @@ +import 'dart:async'; + import 'package:ecommerce_app/src/features/checkout/application/fake_checkout_service.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'payment_button_controller.g.dart'; -class PaymentButtonController extends StateNotifier> { - PaymentButtonController({required this.checkoutService}) - : super(const AsyncData(null)); - final FakeCheckoutService checkoutService; +@riverpod +class PaymentButtonController extends _$PaymentButtonController { + bool mounted = true; + + @override + FutureOr build() { + ref.onDispose(() => mounted = false); + // nothing to do + } Future pay() async { + final checkoutService = ref.read(checkoutServiceProvider); state = const AsyncLoading(); final newState = await AsyncValue.guard(checkoutService.placeOrder); // * Check if the controller is mounted before setting the state to prevent: @@ -16,10 +26,3 @@ class PaymentButtonController extends StateNotifier> { } } } - -final paymentButtonControllerProvider = StateNotifierProvider.autoDispose< - PaymentButtonController, AsyncValue>((ref) { - return PaymentButtonController( - checkoutService: ref.watch(checkoutServiceProvider), - ); -}); diff --git a/ecommerce_app/lib/src/features/checkout/presentation/payment/payment_button_controller.g.dart b/ecommerce_app/lib/src/features/checkout/presentation/payment/payment_button_controller.g.dart new file mode 100644 index 00000000..0c47f44e --- /dev/null +++ b/ecommerce_app/lib/src/features/checkout/presentation/payment/payment_button_controller.g.dart @@ -0,0 +1,27 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'payment_button_controller.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$paymentButtonControllerHash() => + r'32af9b54f0a19136e29fc2e868b1bf9e903976e8'; + +/// See also [PaymentButtonController]. +@ProviderFor(PaymentButtonController) +final paymentButtonControllerProvider = + AutoDisposeAsyncNotifierProvider.internal( + PaymentButtonController.new, + name: r'paymentButtonControllerProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$paymentButtonControllerHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef _$PaymentButtonController = AutoDisposeAsyncNotifier; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_controller.dart b/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_controller.dart index c62dd09b..02db6f03 100644 --- a/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_controller.dart +++ b/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_controller.dart @@ -2,16 +2,18 @@ import 'package:ecommerce_app/src/features/products/domain/product.dart'; import 'package:ecommerce_app/src/features/reviews/application/reviews_service.dart'; import 'package:ecommerce_app/src/features/reviews/domain/review.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'; -class LeaveReviewController extends StateNotifier> { - LeaveReviewController({ - required this.reviewsService, - required this.currentDateBuilder, - }) : super(const AsyncData(null)); - final ReviewsService reviewsService; - // * this is injected so we can easily mock the date in the tests - final DateTime Function() currentDateBuilder; +part 'leave_review_controller.g.dart'; + +@riverpod +class LeaveReviewController extends _$LeaveReviewController { + bool mounted = true; + @override + FutureOr build() { + ref.onDispose(() => mounted = false); + // nothing to do + } Future submitReview({ Review? previousReview, @@ -24,6 +26,8 @@ class LeaveReviewController extends StateNotifier> { if (previousReview == null || rating != previousReview.rating || comment != previousReview.comment) { + final currentDateBuilder = ref.read(currentDateBuilderProvider); + final reviewsService = ref.read(reviewsServiceProvider); final review = Review( rating: rating, comment: comment, @@ -44,12 +48,3 @@ class LeaveReviewController extends StateNotifier> { } } } - -final leaveReviewControllerProvider = - StateNotifierProvider.autoDispose>( - (ref) { - return LeaveReviewController( - reviewsService: ref.watch(reviewsServiceProvider), - currentDateBuilder: ref.watch(currentDateBuilderProvider), - ); -}); diff --git a/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_controller.g.dart b/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_controller.g.dart new file mode 100644 index 00000000..3e9042cf --- /dev/null +++ b/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_controller.g.dart @@ -0,0 +1,27 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'leave_review_controller.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$leaveReviewControllerHash() => + r'5f71662fcc7a950a82967bb17abc735d3793b180'; + +/// See also [LeaveReviewController]. +@ProviderFor(LeaveReviewController) +final leaveReviewControllerProvider = + AutoDisposeAsyncNotifierProvider.internal( + LeaveReviewController.new, + name: r'leaveReviewControllerProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$leaveReviewControllerHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef _$LeaveReviewController = AutoDisposeAsyncNotifier; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member