Skip to content

Commit

Permalink
fix remaining bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch committed Aug 14, 2024
1 parent 200010e commit e67c1a2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
5 changes: 3 additions & 2 deletions kitchenowl/lib/cubits/shoppinglist_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class ShoppinglistCubit extends Cubit<ShoppinglistCubitState> {
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);
Expand Down Expand Up @@ -151,8 +151,9 @@ class ShoppinglistCubit extends Cubit<ShoppinglistCubitState> {
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();
Expand Down
13 changes: 13 additions & 0 deletions kitchenowl/lib/models/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> map) => Item(
id: map['id'],
name: map['name'],
Expand Down
10 changes: 7 additions & 3 deletions kitchenowl/lib/services/transactions/shoppinglist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TransactionShoppingListGet extends Transaction<List<ShoppingList>> {
final lists = await ApiService.getInstance().getShoppingLists(
household,
sorting: sorting,
recentItemlimit: recentItemlimit,
recentItemlimit: recentItemlimit + 3,
);
if (lists != null) {
MemStorage.getInstance().writeShoppingLists(household, lists);
Expand Down Expand Up @@ -59,8 +59,11 @@ class TransactionShoppingListSearchItem extends Transaction<List<Item>> {
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<List<Item>>(
[],
(p, e) => p + e,
Expand Down Expand Up @@ -191,6 +194,7 @@ class TransactionShoppingListRemoveItem extends Transaction<bool> {
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);

Expand Down

0 comments on commit e67c1a2

Please sign in to comment.