From 7388527c43b343bbe3b8112e250b479d30743cb7 Mon Sep 17 00:00:00 2001 From: esche Date: Tue, 4 Jul 2023 22:07:00 +0200 Subject: [PATCH] merged a simple logic for the basket --- Frontend-user/lib/basket.dart | 17 +++--- Frontend-user/lib/finance.dart | 10 ++-- Frontend-user/lib/sample_data.dart | 92 ++++++++++++++++++------------ Frontend-user/pubspec.lock | 24 ++++++++ Frontend-user/pubspec.yaml | 1 + 5 files changed, 94 insertions(+), 50 deletions(-) diff --git a/Frontend-user/lib/basket.dart b/Frontend-user/lib/basket.dart index e704de2..3bb4a69 100644 --- a/Frontend-user/lib/basket.dart +++ b/Frontend-user/lib/basket.dart @@ -3,13 +3,12 @@ import 'package:flutter/services.dart'; import 'sample_data.dart'; class ShowBasket extends StatelessWidget { - final int index; + final Basket basket; final bool editable; - const ShowBasket(this.index, {this.editable = false, super.key}); + const ShowBasket(this.basket, {this.editable = false, super.key}); @override Widget build(BuildContext context) { - final List basket = SampleData().transactions[index].basket!; return Column( children: [ Expanded( @@ -24,7 +23,7 @@ class ShowBasket extends StatelessWidget { 'Kosten:', ), subtitle: Text( - '${SampleData().transactions[index].amount / 100}', + '${basket.price}', ), trailing: editable ? Row( @@ -47,19 +46,19 @@ class ShowBasket extends StatelessWidget { ); } - ListView getBasket(List basket) { + ListView getBasket(Basket basket) { return ListView.builder( - itemCount: basket.length, + itemCount: basket.purchases.length, itemBuilder: (context, index) { return Card( child: ListTile( leading: Text( - basket[index].products.category.icon, + basket.purchases.keys.elementAt(index).category.icon, style: const TextStyle(fontSize: 24), ), - title: Text(basket[index].products.name), + title: Text(basket.purchases.keys.elementAt(index).name), subtitle: Text( - '${basket[index].amount} ${basket[index].products.unit}'), + '${basket.purchases.values.elementAt(index)} ${basket.purchases.keys.elementAt(index).unit}'), trailing: editable ? SizedBox( width: 100, diff --git a/Frontend-user/lib/finance.dart b/Frontend-user/lib/finance.dart index edaecd4..bea638f 100644 --- a/Frontend-user/lib/finance.dart +++ b/Frontend-user/lib/finance.dart @@ -43,7 +43,9 @@ class Finance extends StatelessWidget { showBottomSheet( context: context, builder: (BuildContext context) { - return ShowBasket(index); + return ShowBasket(SampleData() + .transactions[index] + .basket!); }); } : null)); @@ -68,8 +70,7 @@ class Finance extends StatelessWidget { Text getSubtitle(Transaction transaction) { String text = '${transaction.amount / 100}'; text += '€ '; - text += DateFormat("EEEE, dd. MMMM yyyy HH:mm") - .format(transaction.date); + text += DateFormat("EEEE, dd. MMMM yyyy HH:mm").format(transaction.date); if (transaction.description != null) { (text += '\n${transaction.description}'); } @@ -98,7 +99,8 @@ class Finance extends StatelessWidget { showBottomSheet( context: context, builder: (BuildContext context) { - return ShowBasket(index, editable: true); + return ShowBasket(SampleData().transactions[index].basket!, + editable: true); }); }, ); diff --git a/Frontend-user/lib/sample_data.dart b/Frontend-user/lib/sample_data.dart index 61ce01d..33d0414 100644 --- a/Frontend-user/lib/sample_data.dart +++ b/Frontend-user/lib/sample_data.dart @@ -1,3 +1,5 @@ +import 'package:uuid/uuid.dart'; + final DateTime now = DateTime.now(); enum TransaktionArt { monatlBeitrag, aufladung, einkauf, korrektur } @@ -15,7 +17,7 @@ class Transaction { int amount; TransaktionArt type; DateTime date; - List? basket; + Basket? basket; String? description; Transaction( {required this.type, @@ -28,17 +30,31 @@ class Transaction { class Product { final int id = 0; final String name; - final Unit unit; - final double price; //pro Kilogramm oder Stück + final Unit unit; //pro Kg oder Stück + final double price; final double vat; final Category category; const Product(this.name, this.unit, this.price, this.vat, this.category); } class Basket { - Product products; - int amount; - Basket(this.products, this.amount); + Map purchases; + double price; + String guid; + Basket(this.purchases, this.price) : guid = const Uuid().v4(); + + void addItem(Product product, int quantity) { + if (purchases.containsKey(product)) { + purchases.update( + product, (existingQuantity) => existingQuantity + quantity); + } else { + purchases[product] = quantity; + } + } + + void removeItem(Product product) { + purchases.remove(product); + } } class SampleData { @@ -63,39 +79,41 @@ class SampleData { Product('Flaschenpfand', Unit.stueck, -0.15, 0, categories[3]) ]; - static List basket = [ - Basket(products[0], 20), - Basket(products[1], 2200), - Basket(products[2], 2), - Basket(products[3], 1), - Basket(products[4], 1), - Basket(products[5], 2), - Basket(products[6], 222), - Basket(products[0], 20), - Basket(products[1], 2200), - Basket(products[2], 2), - Basket(products[3], 1), - Basket(products[4], 1), - Basket(products[5], 2), - Basket(products[6], 222) - ]; + //such a basket can not exist later. It is for testing purposes + // when scrolling through a long basket + static Basket basket = Basket({ + products[0]: 20, + products[1]: 2200, + products[2]: 2, + products[3]: 1, + products[4]: 1, + products[5]: 2, + products[6]: 222, + products[0]: 20, + products[1]: 2200, + products[2]: 2, + products[3]: 1, + products[4]: 1, + products[5]: 2, + products[6]: 222 + }, 27.9); - static List basket2 = [ - Basket(products[0], 22), - Basket(products[1], 2241), - Basket(products[3], 2), - Basket(products[4], 4), - Basket(products[6], 2), - Basket(products[7], 5) - ]; + static Basket basket2 = Basket({ + products[0]: 22, + products[1]: 2241, + products[3]: 2, + products[4]: 4, + products[6]: 2, + products[7]: 5, + }, 34); - static List basket3 = [ - Basket(products[0], -2), - Basket(products[1], 21), - Basket(products[3], -4), - Basket(products[4], 1), - Basket(products[6], 5) - ]; + static Basket basket3 = Basket({ + products[0]: 2, + products[1]: 21, + products[3]: 4, + products[4]: 1, + products[6]: 5, + }, -1); List transactions = [ Transaction( diff --git a/Frontend-user/pubspec.lock b/Frontend-user/pubspec.lock index dd54301..d3bb239 100644 --- a/Frontend-user/pubspec.lock +++ b/Frontend-user/pubspec.lock @@ -41,6 +41,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.17.1" + crypto: + dependency: transitive + description: + name: crypto + sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab + url: "https://pub.dev" + source: hosted + version: "3.0.3" cupertino_icons: dependency: "direct main" description: @@ -184,6 +192,22 @@ packages: url: "https://pub.dev" source: hosted version: "0.5.1" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c + url: "https://pub.dev" + source: hosted + version: "1.3.2" + uuid: + dependency: "direct main" + description: + name: uuid + sha256: "648e103079f7c64a36dc7d39369cabb358d377078a051d6ae2ad3aa539519313" + url: "https://pub.dev" + source: hosted + version: "3.0.7" vector_math: dependency: transitive description: diff --git a/Frontend-user/pubspec.yaml b/Frontend-user/pubspec.yaml index 23ea054..b070662 100644 --- a/Frontend-user/pubspec.yaml +++ b/Frontend-user/pubspec.yaml @@ -36,6 +36,7 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 intl: ^0.18.1 + uuid: ^3.0.7 dev_dependencies: flutter_test: