Skip to content

Commit

Permalink
Convert PaymentButtonController and remaining controllers to AsyncNot…
Browse files Browse the repository at this point in the history
…ifier and Riverpod Generator
  • Loading branch information
bizz84 committed Aug 25, 2023
1 parent 0502e92 commit dd78e36
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -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<AsyncValue<int>> {
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<int> build() {
return 1;
}

void updateQuantity(int quantity) {
state = AsyncData(quantity);
}

Future<void> addItem(ProductID productId) async {
final cartService = ref.read(cartServiceProvider);
final item = Item(productId: productId, quantity: state.value!);
state = const AsyncLoading<int>().copyWithPrevious(state);
final value = await AsyncValue.guard(() => cartService.addItem(item));
Expand All @@ -22,11 +28,3 @@ class AddToCartController extends StateNotifier<AsyncValue<int>> {
}
}
}

final addToCartControllerProvider =
StateNotifierProvider.autoDispose<AddToCartController, AsyncValue<int>>(
(ref) {
return AddToCartController(
cartService: ref.watch(cartServiceProvider),
);
});

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
@@ -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<AsyncValue<void>> {
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<void> build() {
// nothing to do
}

CartService get cartService => ref.read(cartServiceProvider);

Future<void> updateItemQuantity(ProductID productId, int quantity) async {
state = const AsyncLoading();
Expand All @@ -19,11 +25,3 @@ class ShoppingCartScreenController extends StateNotifier<AsyncValue<void>> {
state = await AsyncValue.guard(() => cartService.removeItemById(productId));
}
}

final shoppingCartScreenControllerProvider =
StateNotifierProvider<ShoppingCartScreenController, AsyncValue<void>>(
(ref) {
return ShoppingCartScreenController(
cartService: ref.watch(cartServiceProvider),
);
});

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
@@ -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<AsyncValue<void>> {
PaymentButtonController({required this.checkoutService})
: super(const AsyncData(null));
final FakeCheckoutService checkoutService;
@riverpod
class PaymentButtonController extends _$PaymentButtonController {
bool mounted = true;

@override
FutureOr<void> build() {
ref.onDispose(() => mounted = false);
// nothing to do
}

Future<void> 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:
Expand All @@ -16,10 +26,3 @@ class PaymentButtonController extends StateNotifier<AsyncValue<void>> {
}
}
}

final paymentButtonControllerProvider = StateNotifierProvider.autoDispose<
PaymentButtonController, AsyncValue<void>>((ref) {
return PaymentButtonController(
checkoutService: ref.watch(checkoutServiceProvider),
);
});

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 @@ -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<AsyncValue<void>> {
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<void> build() {
ref.onDispose(() => mounted = false);
// nothing to do
}

Future<void> submitReview({
Review? previousReview,
Expand All @@ -24,6 +26,8 @@ class LeaveReviewController extends StateNotifier<AsyncValue<void>> {
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,
Expand All @@ -44,12 +48,3 @@ class LeaveReviewController extends StateNotifier<AsyncValue<void>> {
}
}
}

final leaveReviewControllerProvider =
StateNotifierProvider.autoDispose<LeaveReviewController, AsyncValue<void>>(
(ref) {
return LeaveReviewController(
reviewsService: ref.watch(reviewsServiceProvider),
currentDateBuilder: ref.watch(currentDateBuilderProvider),
);
});

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

0 comments on commit dd78e36

Please sign in to comment.