Moved from category list to category map

This was done to make category selection easier

NOTE: this also fixes the bug, where the wrong category was selected,
or the category id returned out-of-bounds
This commit is contained in:
Jakob Meier 2023-04-05 10:07:42 +02:00
parent eba0790f7c
commit 86dac312a5
No known key found for this signature in database
GPG key ID: 66BDC7E6A01A6152

View file

@ -26,7 +26,7 @@ class _ShoppingListPageState extends State<ShoppingListPage> {
List<RoomItem> list = []; List<RoomItem> list = [];
List<RoomItem> cart = []; List<RoomItem> cart = [];
Map<int, int> weights = {}; Map<int, int> weights = {};
List<RoomCategory> categories = []; Map<int?, RoomCategory> categories = {};
List<RoomProduct> products = []; List<RoomProduct> products = [];
void fetchItems() { void fetchItems() {
@ -124,14 +124,16 @@ class _ShoppingListPageState extends State<ShoppingListPage> {
.toList(); .toList();
Map<int, int> map = {}; Map<int, int> map = {};
Map<int?, RoomCategory> cat = {};
for (int i = 0; i < resp.length; i++) { for (int i = 0; i < resp.length; i++) {
map[resp[i].id] = i; map[resp[i].id] = i;
cat[resp[i].id] = resp[i];
} }
if (mounted) { if (mounted) {
setState(() { setState(() {
weights = map; weights = map;
categories = resp; categories = cat;
sortAll(); sortAll();
}); });
} }
@ -199,12 +201,8 @@ class _ShoppingListPageState extends State<ShoppingListPage> {
itemCount: list.length, itemCount: list.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final item = list[index]; final item = list[index];
final cat =
RoomCategory cat = RoomCategory.other(context); categories[item.category] ?? RoomCategory.other(context);
try {
cat = categories[item.category!];
}catch(_){}
return ShoppingListItem( return ShoppingListItem(
name: item.name, name: item.name,
description: item.description, description: item.description,
@ -239,7 +237,7 @@ class _ShoppingListPageState extends State<ShoppingListPage> {
context: context, context: context,
builder: (context) => ShoppingListItemInfo( builder: (context) => ShoppingListItemInfo(
products: products, products: products,
categories: categories, category: cat,
item: item, item: item,
room: widget.room!.id, room: widget.room!.id,
server: widget.room!.serverTag)); server: widget.room!.serverTag));
@ -253,13 +251,13 @@ class _ShoppingListPageState extends State<ShoppingListPage> {
physics: const ClampingScrollPhysics(), physics: const ClampingScrollPhysics(),
itemBuilder: (context, index) { itemBuilder: (context, index) {
final item = cart[index]; final item = cart[index];
final cat =
categories[item.category] ?? RoomCategory.other(context);
return ShoppingListItem( return ShoppingListItem(
name: item.name, name: item.name,
description: item.description, description: item.description,
category: (item.category != null) category: cat,
? categories[item.category!]
: RoomCategory.other(context),
key: Key(item.id.toString()), key: Key(item.id.toString()),
inCart: item.state != 0, inCart: item.state != 0,
onDismiss: () { onDismiss: () {
@ -278,7 +276,7 @@ class _ShoppingListPageState extends State<ShoppingListPage> {
changeItemState(item); changeItemState(item);
}, },
onTap: () { onTap: () {
// TODO: show modal bottom sheet // show modal bottom sheet
// containing // containing
// - item info // - item info
// - option to view linked product (if available) // - option to view linked product (if available)
@ -289,7 +287,7 @@ class _ShoppingListPageState extends State<ShoppingListPage> {
context: context, context: context,
builder: (context) => ShoppingListItemInfo( builder: (context) => ShoppingListItemInfo(
products: products, products: products,
categories: categories, category: cat,
item: item, item: item,
room: widget.room!.id, room: widget.room!.id,
server: widget.room!.serverTag)); server: widget.room!.serverTag));
@ -360,8 +358,9 @@ class ShoppingListItem extends StatelessWidget {
}, },
background: background:
Icon(!inCart ? Icons.shopping_cart : Icons.remove_shopping_cart), Icon(!inCart ? Icons.shopping_cart : Icons.remove_shopping_cart),
child: Opacity(
opacity: inCart?0.5:1.0,
child: ListTile( child: ListTile(
enabled: !inCart,
title: Text(name), title: Text(name),
subtitle: Text(description), subtitle: Text(description),
trailing: CategoryChip( trailing: CategoryChip(
@ -372,23 +371,24 @@ class ShoppingListItem extends StatelessWidget {
onTap!(); onTap!();
} }
}, },
), )),
); );
} }
} }
class ShoppingListItemInfo extends StatelessWidget { class ShoppingListItemInfo extends StatelessWidget {
RoomItem item; final RoomItem item;
String server; final String server;
String room; final String room;
List<RoomCategory> categories = []; final RoomCategory category;
List<RoomProduct> products = []; final List<RoomProduct> products;
ShoppingListItemInfo( const ShoppingListItemInfo(
{required this.item, {super.key,
required this.item,
required this.server, required this.server,
required this.room, required this.room,
required this.categories, required this.category,
required this.products}); required this.products});
@override @override
@ -404,11 +404,7 @@ class ShoppingListItemInfo extends StatelessWidget {
child: Column(children: [ child: Column(children: [
Text(item.name, style: textTheme.headlineLarge), Text(item.name, style: textTheme.headlineLarge),
Text(item.description, style: textTheme.titleMedium), Text(item.description, style: textTheme.titleMedium),
CategoryPicker( CategoryChip(category: category,),
label: AppLocalizations.of(context)!.selectCategoryLabel,
categories: categories,
selected: item.category,
enabled: false),
ProductPicker( ProductPicker(
label: label:
AppLocalizations.of(context)!.selectLinkedProductLabel, AppLocalizations.of(context)!.selectLinkedProductLabel,