diff --git a/mobile/lib/pages/finish_shopping_page.dart b/mobile/lib/pages/finish_shopping_page.dart index 2ee9bb5..70c1f40 100644 --- a/mobile/lib/pages/finish_shopping_page.dart +++ b/mobile/lib/pages/finish_shopping_page.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:mobile/repos/cart.dart'; +import 'package:mobile/repos/receipt.dart'; import 'package:mobile/widgets/primary_button.dart'; import 'package:mobile/widgets/receipt_item.dart'; import 'package:provider/provider.dart'; @@ -10,6 +11,7 @@ class FinishShoppingPage extends StatelessWidget { @override Widget build(BuildContext context) { final CartRepo cartRepo = context.read(); + final ReceiptRepo receiptRepo = context.read(); final cart = cartRepo.allCartItems(); return Scaffold( @@ -43,8 +45,17 @@ class FinishShoppingPage extends StatelessWidget { )), ), Center( - child: - PrimaryButton(onPressed: () {}, child: const Text("Betal"))) + child: PrimaryButton( + onPressed: () { + receiptRepo.createReceipt(cart); + cartRepo.clearCart(); + Navigator.pop(context); + }, + child: const Text("Betal"))), + //const CircularProgressIndicator( + // valueColor: AlwaysStoppedAnimation(Colors.blue), + // strokeWidth: 6.0, + //) ], ), ); diff --git a/mobile/lib/repos/cart.dart b/mobile/lib/repos/cart.dart index 5d04576..1138c8d 100644 --- a/mobile/lib/repos/cart.dart +++ b/mobile/lib/repos/cart.dart @@ -22,45 +22,10 @@ class CartRepo extends ChangeNotifier { amount: 6), CartItem( product: Product( - id: 1, - name: "Letmælk", - price: 13, - description: "Konventionel letmælk med fedtprocent på 1,5%"), - amount: 1), - CartItem( - product: Product( - id: 1, - name: "Letmælk", - price: 13, - description: "Konventionel letmælk med fedtprocent på 1,5%"), - amount: 1), - CartItem( - product: Product( - id: 1, - name: "Letmælk", - price: 13, - description: "Konventionel letmælk med fedtprocent på 1,5%"), - amount: 1), - CartItem( - product: Product( - id: 1, - name: "Letmælk", - price: 13, - description: "Konventionel letmælk med fedtprocent på 1,5%"), - amount: 1), - CartItem( - product: Product( - id: 1, - name: "Letmælk", - price: 13, - description: "Konventionel letmælk med fedtprocent på 1,5%"), - amount: 1), - CartItem( - product: Product( - id: 1, - name: "Letmælk", - price: 13, - description: "Konventionel letmælk med fedtprocent på 1,5%"), + id: 3, + name: "Minimælk", + price: 12, + description: "Konventionel minimælk med fedtprocent på 0,4%"), amount: 1), ]; @@ -115,7 +80,7 @@ class CartRepo extends ChangeNotifier { notifyListeners(); } - addToCart(Product product) { + void addToCart(Product product) { final cartItem = withProductId(product.id); if (cartItem == null) { cart.add(CartItem(product: product, amount: 1)); @@ -125,14 +90,19 @@ class CartRepo extends ChangeNotifier { notifyListeners(); } - totalItemsInCart() { + int totalItemsInCart() { return cart.fold(0, (prev, cartItem) => prev + cartItem.amount); } - totalPrice() { + int totalPrice() { return cart.fold( 0, (prev, cartItem) => prev + cartItem.amount * cartItem.product.price); } + + void clearCart() { + cart.clear(); + notifyListeners(); + } } class CartItem { diff --git a/mobile/lib/repos/receipt.dart b/mobile/lib/repos/receipt.dart index b12b0e5..bd7dcde 100644 --- a/mobile/lib/repos/receipt.dart +++ b/mobile/lib/repos/receipt.dart @@ -1,7 +1,9 @@ import 'package:flutter/material.dart'; +import 'package:mobile/repos/cart.dart'; import 'package:mobile/repos/product.dart'; class ReceiptRepo extends ChangeNotifier { + int nextId = 0; final List receipts = [ Receipt( id: 0, @@ -9,35 +11,38 @@ class ReceiptRepo extends ChangeNotifier { receiptItems: [ ReceiptItem( product: Product( - id: 1, + id: 1243, name: "Letmælk", price: 13, description: "Konventionel minimælk med fedtprocent på 0,4%"), amount: 1), ReceiptItem( product: Product( - id: 0, + id: 340, name: "Minimælk", price: 12, description: "Konventionel minimælk med fedtprocent på 0,4%"), amount: 3), ]), - Receipt(id: 1, date: DateTime.now(), receiptItems: [ - ReceiptItem( - product: Product( - id: 1, - name: "Letmælk", - price: 13, - description: "Konventionel minimælk med fedtprocent på 0,4%"), - amount: 3), - ReceiptItem( - product: Product( - id: 0, - name: "Minimælk", - price: 12, - description: "Konventionel minimælk med fedtprocent på 0,4%"), - amount: 1), - ]) + Receipt( + id: 1, + date: DateTime.fromMillisecondsSinceEpoch(1735031200000), + receiptItems: [ + ReceiptItem( + product: Product( + id: 12341, + name: "Letmælk", + price: 13, + description: "Konventionel minimælk med fedtprocent på 0,4%"), + amount: 3), + ReceiptItem( + product: Product( + id: 1234443, + name: "Minimælk", + price: 12, + description: "Konventionel minimælk med fedtprocent på 0,4%"), + amount: 1), + ]) ]; List allReceipts() { @@ -52,6 +57,18 @@ class ReceiptRepo extends ChangeNotifier { } return null; } + + void createReceipt(List cartItems) { + List receiptItems = []; + for (var i = 0; i < cartItems.length; i++) { + final ReceiptItem receiptItem = ReceiptItem( + amount: cartItems[i].amount, product: cartItems[i].product); + receiptItems.add(receiptItem); + } + receipts.add(Receipt( + date: DateTime.now(), receiptItems: receiptItems, id: nextId++)); + notifyListeners(); + } } class Receipt {