From 29782de163f1a22640e7256580c4ca204f8faed1 Mon Sep 17 00:00:00 2001 From: Tom Bursch Date: Wed, 20 Nov 2024 14:50:36 +0100 Subject: [PATCH] feat: Optionally remember the last used shopping list (#548) --- kitchenowl/lib/cubits/settings_cubit.dart | 18 +++++++++++ kitchenowl/lib/cubits/shoppinglist_cubit.dart | 32 ++++++++++++------- kitchenowl/lib/l10n/app_en.arb | 2 ++ kitchenowl/lib/pages/settings_page.dart | 18 +++++++++++ 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/kitchenowl/lib/cubits/settings_cubit.dart b/kitchenowl/lib/cubits/settings_cubit.dart index 8424cf19..3b40b6a5 100644 --- a/kitchenowl/lib/cubits/settings_cubit.dart +++ b/kitchenowl/lib/cubits/settings_cubit.dart @@ -28,6 +28,9 @@ class SettingsCubit extends Cubit { .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(); @@ -48,6 +51,7 @@ class SettingsCubit extends Cubit { shoppingListListView: await shoppingListListView ?? false, shoppingListTapToRemove: await shoppingListTapToRemove ?? true, recentItemsCategorize: await recentItemsCategorize ?? false, + restoreLastShoppingList: await restoreLastShoppingList ?? false, )); } @@ -109,6 +113,14 @@ class SettingsCubit extends Cubit { ); 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 { @@ -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, @@ -130,6 +143,7 @@ class SettingsState extends Equatable { this.shoppingListListView = false, this.shoppingListTapToRemove = true, this.recentItemsCategorize = false, + this.restoreLastShoppingList = false, }); SettingsState copyWith({ @@ -141,6 +155,7 @@ class SettingsState extends Equatable { bool? shoppingListListView, bool? shoppingListTapToRemove, bool? recentItemsCategorize, + bool? restoreLastShoppingList, }) => SettingsState( themeMode: themeMode ?? this.themeMode, @@ -153,6 +168,8 @@ class SettingsState extends Equatable { shoppingListTapToRemove ?? this.shoppingListTapToRemove, recentItemsCategorize: recentItemsCategorize ?? this.recentItemsCategorize, + restoreLastShoppingList: + restoreLastShoppingList ?? this.restoreLastShoppingList, ); @override @@ -165,5 +182,6 @@ class SettingsState extends Equatable { shoppingListListView, shoppingListTapToRemove, recentItemsCategorize, + restoreLastShoppingList, ]; } diff --git a/kitchenowl/lib/cubits/shoppinglist_cubit.dart b/kitchenowl/lib/cubits/shoppinglist_cubit.dart index dce93f1c..5267d0ab 100644 --- a/kitchenowl/lib/cubits/shoppinglist_cubit.dart +++ b/kitchenowl/lib/cubits/shoppinglist_cubit.dart @@ -1,5 +1,3 @@ -import 'dart:convert'; - import 'package:collection/collection.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -263,10 +261,12 @@ class ShoppinglistCubit extends Cubit { } 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, )); @@ -299,10 +299,20 @@ class ShoppinglistCubit extends Cubit { .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> categories = TransactionHandler.getInstance().runTransaction( @@ -312,12 +322,12 @@ class ShoppinglistCubit extends Cubit { 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(), ); diff --git a/kitchenowl/lib/l10n/app_en.arb b/kitchenowl/lib/l10n/app_en.arb index f59162c2..e6cde4ef 100644 --- a/kitchenowl/lib/l10n/app_en.arb +++ b/kitchenowl/lib/l10n/app_en.arb @@ -277,6 +277,7 @@ "@recipesSuggested": {}, "@redo": {}, "@refresh": {}, + "@rememberLastShoppingList": {}, "@remove": {}, "@rename": {}, "@reportIssue": {}, @@ -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", diff --git a/kitchenowl/lib/pages/settings_page.dart b/kitchenowl/lib/pages/settings_page.dart index 7bc012d4..aee529e5 100644 --- a/kitchenowl/lib/pages/settings_page.dart +++ b/kitchenowl/lib/pages/settings_page.dart @@ -277,6 +277,24 @@ class _SettingsPageState extends State { ), ), ), + ListTile( + title: Text( + AppLocalizations.of(context)!.rememberLastShoppingList, + ), + leading: const Icon(Icons.restore_rounded), + onTap: () => BlocProvider.of(context) + .setRestoreLastShoppinglist( + !BlocProvider.of(context) + .state + .restoreLastShoppingList, + ), + trailing: KitchenOwlSwitch( + value: state.restoreLastShoppingList, + onChanged: (value) => + BlocProvider.of(context) + .setRestoreLastShoppinglist(value), + ), + ), ListTile( title: Text( AppLocalizations.of(context)!.itemsRecent,