Skip to content

Commit

Permalink
feat: Optionally remember the last used shopping list (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch authored Nov 20, 2024
1 parent 90b852f commit 29782de
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
18 changes: 18 additions & 0 deletions kitchenowl/lib/cubits/settings_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class SettingsCubit extends Cubit<SettingsState> {
.readBool(key: 'shoppingListTapToRemove');
final recentItemsCategorize =
PreferenceStorage.getInstance().readBool(key: 'recentItemsCategorize');
final restoreLastShoppingList = PreferenceStorage.getInstance()
.readBool(key: 'restoreLastShoppingList');

Config.deviceInfo = DeviceInfoPlugin().deviceInfo;
Config.packageInfo = PackageInfo.fromPlatform();

Expand All @@ -48,6 +51,7 @@ class SettingsCubit extends Cubit<SettingsState> {
shoppingListListView: await shoppingListListView ?? false,
shoppingListTapToRemove: await shoppingListTapToRemove ?? true,
recentItemsCategorize: await recentItemsCategorize ?? false,
restoreLastShoppingList: await restoreLastShoppingList ?? false,
));
}

Expand Down Expand Up @@ -109,6 +113,14 @@ class SettingsCubit extends Cubit<SettingsState> {
);
emit(state.copyWith(recentItemsCategorize: recentItemsCategorize));
}

void setRestoreLastShoppinglist(bool restoreLastShoppingList) {
PreferenceStorage.getInstance().writeBool(
key: 'restoreLastShoppingList',
value: restoreLastShoppingList,
);
emit(state.copyWith(restoreLastShoppingList: restoreLastShoppingList));
}
}

class SettingsState extends Equatable {
Expand All @@ -120,6 +132,7 @@ class SettingsState extends Equatable {
final bool shoppingListListView;
final bool shoppingListTapToRemove;
final bool recentItemsCategorize;
final bool restoreLastShoppingList;

const SettingsState({
this.themeMode = ThemeMode.system,
Expand All @@ -130,6 +143,7 @@ class SettingsState extends Equatable {
this.shoppingListListView = false,
this.shoppingListTapToRemove = true,
this.recentItemsCategorize = false,
this.restoreLastShoppingList = false,
});

SettingsState copyWith({
Expand All @@ -141,6 +155,7 @@ class SettingsState extends Equatable {
bool? shoppingListListView,
bool? shoppingListTapToRemove,
bool? recentItemsCategorize,
bool? restoreLastShoppingList,
}) =>
SettingsState(
themeMode: themeMode ?? this.themeMode,
Expand All @@ -153,6 +168,8 @@ class SettingsState extends Equatable {
shoppingListTapToRemove ?? this.shoppingListTapToRemove,
recentItemsCategorize:
recentItemsCategorize ?? this.recentItemsCategorize,
restoreLastShoppingList:
restoreLastShoppingList ?? this.restoreLastShoppingList,
);

@override
Expand All @@ -165,5 +182,6 @@ class SettingsState extends Equatable {
shoppingListListView,
shoppingListTapToRemove,
recentItemsCategorize,
restoreLastShoppingList,
];
}
32 changes: 21 additions & 11 deletions kitchenowl/lib/cubits/shoppinglist_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand Down Expand Up @@ -263,10 +261,12 @@ class ShoppinglistCubit extends Cubit<ShoppinglistCubitState> {
}

void setShoppingList(ShoppingList shoppingList) {
PreferenceStorage.getInstance().write(
key: 'selectedShoppinglist',
value: jsonEncode(shoppingList.toJsonWithId()),
);
if (shoppingList.id != null) {
PreferenceStorage.getInstance().writeInt(
key: 'selectedShoppinglist',
value: shoppingList.id!,
);
}
emit(state.copyWith(
selectedShoppinglistId: shoppingList.id,
));
Expand Down Expand Up @@ -299,10 +299,20 @@ class ShoppinglistCubit extends Cubit<ShoppinglistCubitState> {
.map((e) => e.id != null ? MapEntry(e.id!, e) : null)
.whereNotNull()));

final shoppinglist =
state.selectedShoppinglist ?? shoppingLists.values.firstOrNull;
ShoppingList? shoppingList = state.selectedShoppinglist;
if (await PreferenceStorage.getInstance()
.readBool(key: "restoreLastShoppingList") ??
false) {
int? id = await PreferenceStorage.getInstance()
.readInt(key: "selectedShoppinglist");
if (id != null) {
shoppingList ??=
shoppingLists.values.firstWhereOrNull((s) => s.id == id);
}
}
shoppingList ??= shoppingLists.values.firstOrNull;

if (shoppinglist == null) return;
if (shoppingList == null) return;

Future<List<Category>> categories =
TransactionHandler.getInstance().runTransaction(
Expand All @@ -312,12 +322,12 @@ class ShoppinglistCubit extends Cubit<ShoppinglistCubitState> {

final resState = LoadingShoppinglistCubitState(
shoppinglists: shoppingLists,
selectedShoppinglistId: shoppinglist.id,
selectedShoppinglistId: shoppingList.id,
categories: await categories,
sorting: state.sorting,
selectedListItems: state.selectedListItems
.map((e) =>
shoppinglist.items.firstWhereOrNull((item) => item.id == e.id))
shoppingList!.items.firstWhereOrNull((item) => item.id == e.id))
.whereNotNull()
.toList(),
);
Expand Down
2 changes: 2 additions & 0 deletions kitchenowl/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
"@recipesSuggested": {},
"@redo": {},
"@refresh": {},
"@rememberLastShoppingList": {},
"@remove": {},
"@rename": {},
"@reportIssue": {},
Expand Down Expand Up @@ -558,6 +559,7 @@
"recipesSuggested": "Suggested",
"redo": "Redo",
"refresh": "Refresh",
"rememberLastShoppingList": "Remember the last used shopping list",
"remove": "Remove",
"rename": "Rename",
"reportIssue": "Report an issue",
Expand Down
18 changes: 18 additions & 0 deletions kitchenowl/lib/pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,24 @@ class _SettingsPageState extends State<SettingsPage> {
),
),
),
ListTile(
title: Text(
AppLocalizations.of(context)!.rememberLastShoppingList,
),
leading: const Icon(Icons.restore_rounded),
onTap: () => BlocProvider.of<SettingsCubit>(context)
.setRestoreLastShoppinglist(
!BlocProvider.of<SettingsCubit>(context)
.state
.restoreLastShoppingList,
),
trailing: KitchenOwlSwitch(
value: state.restoreLastShoppingList,
onChanged: (value) =>
BlocProvider.of<SettingsCubit>(context)
.setRestoreLastShoppinglist(value),
),
),
ListTile(
title: Text(
AppLocalizations.of(context)!.itemsRecent,
Expand Down

0 comments on commit 29782de

Please sign in to comment.