From 9b74452b942b87c7d2361a7fe1885badc5804e69 Mon Sep 17 00:00:00 2001
From: Mikkel Troels Kongsted <mtkongsted@gmail.com>
Date: Thu, 30 Jan 2025 13:32:28 +0100
Subject: [PATCH] cart icon badge

---
 mobile/lib/pages/cart_page.dart    |  2 +-
 mobile/lib/pages/dashboard.dart    | 11 +++++++---
 mobile/lib/pages/product_page.dart |  7 ++++++-
 mobile/lib/repos/cart.dart         | 32 ++++++++++++++++++++++++++----
 4 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/mobile/lib/pages/cart_page.dart b/mobile/lib/pages/cart_page.dart
index edc8cd1..ae9573c 100644
--- a/mobile/lib/pages/cart_page.dart
+++ b/mobile/lib/pages/cart_page.dart
@@ -75,7 +75,7 @@ class CartItemView extends StatelessWidget {
           ),
           IconButton(
               onPressed: () {
-                cartRepo.removeCartItem(cartRepo.withProductId(productId));
+                cartRepo.removeCartItem(productId);
               },
               icon: const Icon(Icons.delete_outline)),
           Image(width: 100, image: AssetImage(imagePath))
diff --git a/mobile/lib/pages/dashboard.dart b/mobile/lib/pages/dashboard.dart
index fa36dbe..e41715a 100644
--- a/mobile/lib/pages/dashboard.dart
+++ b/mobile/lib/pages/dashboard.dart
@@ -3,6 +3,7 @@ import 'package:mobile/pages/all_products_page.dart';
 import 'package:mobile/pages/cart_page.dart';
 import 'package:mobile/pages/receipts_page.dart';
 import 'package:mobile/repos/bottom_navigation_bar.dart';
+import 'package:mobile/repos/cart.dart';
 import 'package:provider/provider.dart';
 
 class Dashboard extends StatelessWidget {
@@ -18,6 +19,7 @@ class Dashboard extends StatelessWidget {
   Widget build(BuildContext context) {
     final pageIndexProvider = Provider.of<BottomNavigationBarRepo>(context);
     int currentIndex = pageIndexProvider.currentIndex;
+    final CartRepo cartRepo = context.watch<CartRepo>();
 
     return Scaffold(
       bottomNavigationBar: BottomNavigationBar(
@@ -28,9 +30,12 @@ class Dashboard extends StatelessWidget {
               icon: Icon(currentIndex == 0 ? Icons.home : Icons.home_outlined),
               label: "Home"),
           BottomNavigationBarItem(
-              icon: Icon(currentIndex == 1
-                  ? Icons.shopping_cart
-                  : Icons.shopping_cart_outlined),
+              icon: Badge.count(
+                count: cartRepo.totalItemsInCart(),
+                child: Icon(currentIndex == 1
+                    ? Icons.shopping_cart
+                    : Icons.shopping_cart_outlined),
+              ),
               label: "Cart"),
           BottomNavigationBarItem(
               icon: Icon(currentIndex == 2
diff --git a/mobile/lib/pages/product_page.dart b/mobile/lib/pages/product_page.dart
index d58258c..3c82bae 100644
--- a/mobile/lib/pages/product_page.dart
+++ b/mobile/lib/pages/product_page.dart
@@ -1,6 +1,8 @@
 import 'package:flutter/material.dart';
+import 'package:mobile/repos/cart.dart';
 import 'package:mobile/repos/product.dart';
 import 'package:mobile/widgets/primary_button.dart';
+import 'package:provider/provider.dart';
 
 class ProductPage extends StatelessWidget {
   final Product product;
@@ -70,7 +72,10 @@ class ProductPage extends StatelessWidget {
                   PrimaryButton(
                       onPressed: () {}, child: const Text("Find i butik")),
                   PrimaryButton(
-                      onPressed: () {},
+                      onPressed: () {
+                        var cartRepo = context.read<CartRepo>();
+                        cartRepo.addToCart(product);
+                      },
                       child: const Text("Tilføj til indkøbskurv")),
                 ],
               ),
diff --git a/mobile/lib/repos/cart.dart b/mobile/lib/repos/cart.dart
index 2da99dc..615fb89 100644
--- a/mobile/lib/repos/cart.dart
+++ b/mobile/lib/repos/cart.dart
@@ -68,33 +68,57 @@ class CartRepo extends ChangeNotifier {
     return cart;
   }
 
-  CartItem withProductId(int productId) {
+  CartItem? withProductId(int productId) {
     for (var i = 0; i < cart.length; i++) {
       if (cart[i].product.id == productId) {
         return cart[i];
       }
     }
-    throw ProductIdException();
+    return null;
   }
 
   void incrementAmount(int productId) {
     var cartItem = withProductId(productId);
+    if (cartItem == null) {
+      throw ProductIdException();
+    }
     cartItem.amount++;
     notifyListeners();
   }
 
   void decrementAmount(int productId) {
     var cartItem = withProductId(productId);
+    if (cartItem == null) {
+      throw ProductIdException();
+    }
     if (--cartItem.amount <= 0) {
-      removeCartItem(cartItem);
+      cart.remove(cartItem);
     }
     notifyListeners();
   }
 
-  void removeCartItem(CartItem cartItem) {
+  void removeCartItem(int productId) {
+    var cartItem = withProductId(productId);
+    if (cartItem == null) {
+      throw ProductIdException();
+    }
     cart.remove(cartItem);
     notifyListeners();
   }
+
+  addToCart(Product product) {
+    var cartItem = withProductId(product.id);
+    if (cartItem == null) {
+      cart.add(CartItem(product: product, amount: 1));
+    } else {
+      cartItem.amount++;
+    }
+    notifyListeners();
+  }
+
+  totalItemsInCart() {
+    return cart.fold<int>(0, (prev, cartItem) => prev + cartItem.amount);
+  }
 }
 
 class CartItem {