From e67c1a22acd8f0ce9a5ae1a994c2028c4c41c4aa Mon Sep 17 00:00:00 2001 From: Tom Bursch Date: Wed, 14 Aug 2024 20:08:29 +0200 Subject: [PATCH] fix remaining bugs --- kitchenowl/lib/cubits/shoppinglist_cubit.dart | 5 +++-- kitchenowl/lib/models/item.dart | 13 +++++++++++++ .../lib/services/transactions/shoppinglist.dart | 10 +++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/kitchenowl/lib/cubits/shoppinglist_cubit.dart b/kitchenowl/lib/cubits/shoppinglist_cubit.dart index 81cda69f..a1c5c5a8 100644 --- a/kitchenowl/lib/cubits/shoppinglist_cubit.dart +++ b/kitchenowl/lib/cubits/shoppinglist_cubit.dart @@ -106,7 +106,7 @@ class ShoppinglistCubit extends Cubit { l.add(item); ShoppinglistSorting.sortShoppinglistItems(l, state.sorting); final recent = List.of(shoppinglist.recentItems); - recent.removeWhere((e) => e.id == item.id); + recent.removeWhere((e) => e.name == item.name); if (_state is SearchShoppinglistCubitState) { final result = List.of(_state.result); final index = result.indexWhere((e) => e.id == item.id); @@ -151,8 +151,9 @@ class ShoppinglistCubit extends Cubit { if (shoppinglist == null) return; final l = List.of(shoppinglist.items); - l.remove(item); + l.removeWhere((e) => e.name == item.name); final recent = List.of(shoppinglist.recentItems); + recent.removeWhere((e) => e.name == item.name); recent.insert(0, ItemWithDescription.fromItem(item: item)); if (recent.length > recentItemCountProvider()) { recent.removeLast(); diff --git a/kitchenowl/lib/models/item.dart b/kitchenowl/lib/models/item.dart index e229f81c..7085fb54 100644 --- a/kitchenowl/lib/models/item.dart +++ b/kitchenowl/lib/models/item.dart @@ -24,6 +24,19 @@ class Item extends Model { this.defaultKey, }); + factory Item.fromItem({ + required Item item, + }) => + Item( + id: item.id, + name: item.name, + icon: item.icon, + category: item.category, + ordering: item.ordering, + isDefault: item.isDefault, + defaultKey: item.defaultKey, + ); + factory Item.fromJson(Map map) => Item( id: map['id'], name: map['name'], diff --git a/kitchenowl/lib/services/transactions/shoppinglist.dart b/kitchenowl/lib/services/transactions/shoppinglist.dart index 66b712f3..cd9fc902 100644 --- a/kitchenowl/lib/services/transactions/shoppinglist.dart +++ b/kitchenowl/lib/services/transactions/shoppinglist.dart @@ -31,7 +31,7 @@ class TransactionShoppingListGet extends Transaction> { final lists = await ApiService.getInstance().getShoppingLists( household, sorting: sorting, - recentItemlimit: recentItemlimit, + recentItemlimit: recentItemlimit + 3, ); if (lists != null) { MemStorage.getInstance().writeShoppingLists(household, lists); @@ -59,8 +59,11 @@ class TransactionShoppingListSearchItem extends Transaction> { final shoppingLists = await MemStorage.getInstance().readShoppingLists(household); return (shoppingLists - ?.map( - (shoppingList) => shoppingList.recentItems + shoppingList.items) + ?.map((shoppingList) => + shoppingList.recentItems + .map((e) => Item.fromItem(item: e)) + .toList() + + shoppingList.items) .fold>( [], (p, e) => p + e, @@ -191,6 +194,7 @@ class TransactionShoppingListRemoveItem extends Transaction { shoppingLists.where((e) => e.id == shoppinglist.id).firstOrNull; if (latestShoppingList == null) return false; latestShoppingList.items.removeWhere((e) => e.name == item.name); + latestShoppingList.recentItems.removeWhere((e) => e.name == item.name); latestShoppingList.recentItems.insert(0, item); MemStorage.getInstance().writeShoppingLists(household, shoppingLists);