You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.8 KiB
54 lines
1.8 KiB
3 years ago
|
import 'package:riverpod/riverpod.dart';
|
||
|
import 'package:thesis_shop/models/cart_item.dart';
|
||
|
import 'package:thesis_shop/models/product.dart';
|
||
|
import 'package:thesis_shop/stores/product_list_provider.dart';
|
||
|
|
||
|
class CartStore extends StateNotifier<Map<String, int>> {
|
||
|
CartStore() : super({});
|
||
|
|
||
|
void _addAmountOfProduct(Product product, int amount) {
|
||
|
final value = state[product.title] ?? 0;
|
||
|
if (value + amount < 0) {
|
||
|
return;
|
||
|
} else if (value + amount == 0) {
|
||
|
state.remove(product.title);
|
||
|
state = {...state};
|
||
|
return;
|
||
|
}
|
||
|
state = {...state, product.title: value + amount};
|
||
|
}
|
||
|
|
||
|
void increaseAmountOfProduct(Product product) =>
|
||
|
_addAmountOfProduct(product, 1);
|
||
|
void decreaseAmountOfProduct(Product product) =>
|
||
|
_addAmountOfProduct(product, -1);
|
||
|
}
|
||
|
|
||
|
final totalProductInCartCountProvider = Provider(
|
||
|
(ref) => ref.watch(cartItemListProvider).length,
|
||
|
dependencies: [cartItemListProvider],
|
||
|
);
|
||
|
|
||
|
final totalPriceProvider = Provider(
|
||
|
(ref) => ref.watch(cartItemListProvider).fold<double>(
|
||
|
0.0,
|
||
|
(previousValue, element) =>
|
||
|
previousValue + element.amount * element.product.price),
|
||
|
dependencies: [cartItemListProvider]);
|
||
|
|
||
|
final cartStoreProvider =
|
||
|
StateNotifierProvider<CartStore, Map<String, int>>((ref) => CartStore());
|
||
|
|
||
|
final cartItemListProvider = Provider((ref) {
|
||
|
final cart = ref.watch(cartStoreProvider);
|
||
|
final productsByTitle = ref.watch(productListProvider).whenOrNull(
|
||
|
data: (products) =>
|
||
|
{for (final product in products) product.title: product});
|
||
|
if (productsByTitle == null) return <CartItem>[];
|
||
|
return cart.entries
|
||
|
.where((entry) => productsByTitle.containsKey(entry.key))
|
||
|
.map((entry) =>
|
||
|
CartItem(product: productsByTitle[entry.key]!, amount: entry.value))
|
||
|
.toList();
|
||
|
}, dependencies: [cartStoreProvider, productListProvider]);
|