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