From efeeea85ab8bb96784d0c4451bdb01bdd3bd3deb Mon Sep 17 00:00:00 2001 From: Saraswathi Date: Thu, 5 Mar 2026 16:48:52 +0530 Subject: [PATCH] enhancement:persist-ingredient-filters in nutrition --- .gitignore | 3 + lib/helpers/shared_preferences.dart | 37 +++++ lib/main.dart | 17 +- lib/models/nutrition/ingredient_filters.dart | 25 +++ lib/providers/nutrition.dart | 5 +- ...nutrition_ingredient_filters_riverpod.dart | 48 ++++++ lib/widgets/nutrition/widgets.dart | 146 ++++++++++-------- 7 files changed, 214 insertions(+), 67 deletions(-) create mode 100644 lib/models/nutrition/ingredient_filters.dart create mode 100644 lib/providers/nutrition_ingredient_filters_riverpod.dart diff --git a/.gitignore b/.gitignore index b3dc06861..282de5549 100644 --- a/.gitignore +++ b/.gitignore @@ -65,3 +65,6 @@ ios/fastlane/output ios/Runner.ipa ios/Runner.app.dSYM.zip +#Flutter Version Management +.fvm/ + diff --git a/lib/helpers/shared_preferences.dart b/lib/helpers/shared_preferences.dart index 92ef435e6..cdab8a537 100644 --- a/lib/helpers/shared_preferences.dart +++ b/lib/helpers/shared_preferences.dart @@ -1,5 +1,6 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/util/legacy_to_async_migration_util.dart'; +import 'package:wger/providers/nutrition.dart'; /// A helper class that manages preferences using SharedPreferencesAsync /// and handles migration from the legacy SharedPreferences to @@ -31,4 +32,40 @@ class PreferenceHelper { ); _asyncPref = SharedPreferencesAsync(); } + + //ingredients filters + //1.vegan + Future saveIngredientVeganFilter(bool value) async { + await PreferenceHelper.asyncPref.setBool('ingredientVeganFilter', value); + } + + Future getIngredientVeganFilter() async { + return await PreferenceHelper.asyncPref.getBool('ingredientVeganFilter') ?? false; + } + + //2.vegetarian + Future saveIngredientVegetarianFilter(bool value) async { + await PreferenceHelper.asyncPref.setBool('ingredientVegetarianFilter', value); + } + + Future getIngredientVegetarianFilter() async { + return await PreferenceHelper.asyncPref.getBool('ingredientVegetarianFilter') ?? false; + } + + //3.language + Future saveIngredientSearchLanguage(IngredientSearchLanguage language) async { + await PreferenceHelper.asyncPref.setString('search_language', language.name); + } + + Future getIngredientSearchLanguage() async { + final value = await PreferenceHelper.asyncPref.getString('search_language'); + if (value == null) { + return IngredientSearchLanguage.currentAndEnglish; + } else { + return IngredientSearchLanguage.values.firstWhere( + (e) => e.name == value, + orElse: () => IngredientSearchLanguage.currentAndEnglish, + ); + } + } } diff --git a/lib/main.dart b/lib/main.dart index 956c4874b..fadadbb9e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -33,6 +33,7 @@ import 'package:wger/providers/exercises.dart'; import 'package:wger/providers/gallery.dart'; import 'package:wger/providers/measurement.dart'; import 'package:wger/providers/nutrition.dart'; +import 'package:wger/providers/nutrition_ingredient_filters_riverpod.dart'; import 'package:wger/providers/routines.dart'; import 'package:wger/providers/user.dart'; import 'package:wger/providers/wger_base_riverpod.dart'; @@ -79,6 +80,9 @@ void _setupLogging() { } final GlobalKey navigatorKey = GlobalKey(); +late final bool initialVegan; +late final bool initialVegetarian; +late final IngredientSearchLanguage initialSearchLanguage; void main() async { // Needs to be called before runApp @@ -94,7 +98,10 @@ void main() async { // SharedPreferences to SharedPreferencesAsync migration function await PreferenceHelper.instance.migrationSupportFunctionForSharedPreferences(); - + //sharedpreference for filters in nutrition + initialVegan = await PreferenceHelper.instance.getIngredientVeganFilter(); + initialVegetarian = await PreferenceHelper.instance.getIngredientVegetarianFilter(); + initialSearchLanguage = await PreferenceHelper.instance.getIngredientSearchLanguage(); // Catch errors from Flutter itself (widget build, layout, paint, etc.) // // NOTE: it seems this sometimes makes problems and even freezes the flutter @@ -222,11 +229,17 @@ class MainApp extends StatelessWidget { child: Consumer( builder: (ctx, auth, _) { final baseInstance = WgerBaseProvider(Provider.of(ctx, listen: false)); - return Consumer( builder: (ctx, user, _) => riverpod.ProviderScope( overrides: [ wgerBaseProvider.overrideWithValue(baseInstance), + ingredientFiltersNotifierProvider.overrideWith( + () => IngredientFiltersNotifier( + initialVegan: initialVegan, + initialVegetarian: initialVegetarian, + initialSearchLanguage: initialSearchLanguage, + ), + ), ], child: riverpod.Consumer( builder: (rpCtx, ref, _) { diff --git a/lib/models/nutrition/ingredient_filters.dart b/lib/models/nutrition/ingredient_filters.dart new file mode 100644 index 000000000..bb34415c1 --- /dev/null +++ b/lib/models/nutrition/ingredient_filters.dart @@ -0,0 +1,25 @@ +//model is used to store ingredient filters values +import 'package:wger/providers/nutrition.dart'; + +class IngredientFilters { + final bool isVegan; + final bool isVegetarian; + final IngredientSearchLanguage searchLanguage; + IngredientFilters({ + required this.isVegan, + required this.isVegetarian, + required this.searchLanguage, + }); + + IngredientFilters copyWith({ + bool? isVegan, + bool? isVegetarian, + IngredientSearchLanguage? searchLanguage, + }) { + return IngredientFilters( + isVegan: isVegan ?? this.isVegan, + isVegetarian: isVegetarian ?? this.isVegetarian, + searchLanguage: searchLanguage ?? this.searchLanguage, + ); + } +} diff --git a/lib/providers/nutrition.dart b/lib/providers/nutrition.dart index d13b66901..3761dfe28 100644 --- a/lib/providers/nutrition.dart +++ b/lib/providers/nutrition.dart @@ -400,7 +400,8 @@ class NutritionPlansProvider with ChangeNotifier { if (name.length <= 1) { return []; } - + print("isVegan in search result is $isVegan"); + print("isVegetarian in search result is $isVegetarian"); final List languages = []; switch (searchLanguage) { @@ -434,7 +435,7 @@ class NutritionPlansProvider with ChangeNotifier { if (isVegetarian) { query['is_vegetarian'] = 'true'; } - + print("query is $query"); // Send the request _logger.info('Fetching ingredients from server'); final response = await baseProvider.fetch( diff --git a/lib/providers/nutrition_ingredient_filters_riverpod.dart b/lib/providers/nutrition_ingredient_filters_riverpod.dart new file mode 100644 index 000000000..c7c9cbe29 --- /dev/null +++ b/lib/providers/nutrition_ingredient_filters_riverpod.dart @@ -0,0 +1,48 @@ +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:wger/helpers/shared_preferences.dart'; +import 'package:wger/models/nutrition/ingredient_filters.dart'; +import 'package:wger/providers/nutrition.dart'; + +class IngredientFiltersNotifier extends Notifier { + final bool initialVegan; + final bool initialVegetarian; + final IngredientSearchLanguage initialSearchLanguage; + IngredientFiltersNotifier({ + required this.initialVegan, + required this.initialVegetarian, + required this.initialSearchLanguage, + }); + @override + IngredientFilters build() { + return IngredientFilters( + isVegan: initialVegan, + isVegetarian: initialVegetarian, + searchLanguage: initialSearchLanguage, + ); + } + + Future toggleVegan(bool value) async { + state = state.copyWith(isVegan: value); + await PreferenceHelper.instance.saveIngredientVeganFilter(value); + } + + Future toggleVegetarian(bool value) async { + state = state.copyWith(isVegetarian: value); + await PreferenceHelper.instance.saveIngredientVegetarianFilter(value); + } + + Future chooseLanguage(IngredientSearchLanguage value) async { + state = state.copyWith(searchLanguage: value); + await PreferenceHelper.instance.saveIngredientSearchLanguage(value); + } +} + +// provider for notifier +final ingredientFiltersNotifierProvider = + NotifierProvider( + () => IngredientFiltersNotifier( + initialVegan: false, + initialVegetarian: false, + initialSearchLanguage: IngredientSearchLanguage.current, + ), + ); diff --git a/lib/widgets/nutrition/widgets.dart b/lib/widgets/nutrition/widgets.dart index 524a1779e..f97ab1248 100644 --- a/lib/widgets/nutrition/widgets.dart +++ b/lib/widgets/nutrition/widgets.dart @@ -18,17 +18,19 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_typeahead/flutter_typeahead.dart'; import 'package:flutter_zxing/flutter_zxing.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:logging/logging.dart'; -import 'package:provider/provider.dart'; +import 'package:provider/provider.dart' as legacy_provider; import 'package:wger/helpers/consts.dart'; import 'package:wger/helpers/misc.dart'; import 'package:wger/helpers/platform.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; import 'package:wger/models/nutrition/ingredient.dart'; import 'package:wger/providers/nutrition.dart'; +import 'package:wger/providers/nutrition_ingredient_filters_riverpod.dart'; import 'package:wger/widgets/core/core.dart'; import 'package:wger/widgets/nutrition/helpers.dart'; import 'package:wger/widgets/nutrition/ingredient_dialogs.dart'; @@ -53,7 +55,7 @@ class ScanReader extends StatelessWidget { ); } -class IngredientTypeahead extends StatefulWidget { +class IngredientTypeahead extends ConsumerStatefulWidget { final _logger = Logger('IngredientTypeahead'); final TextEditingController _ingredientController; @@ -79,15 +81,11 @@ class IngredientTypeahead extends StatefulWidget { }); @override - _IngredientTypeaheadState createState() => _IngredientTypeaheadState(); + ConsumerState createState() => _IngredientTypeaheadState(); } -class _IngredientTypeaheadState extends State { +class _IngredientTypeaheadState extends ConsumerState { late String barcode; - var _searchLanguage = IngredientSearchLanguage.currentAndEnglish; - var _isVegan = false; - var _isVegetarian = false; - @override void initState() { super.initState(); @@ -116,6 +114,7 @@ class _IngredientTypeaheadState extends State { @override Widget build(BuildContext context) { + final filters = ref.watch(ingredientFiltersNotifierProvider); return Column( children: [ TypeAheadField( @@ -154,12 +153,15 @@ class _IngredientTypeaheadState extends State { widget.onUpdateSearchQuery(pattern); widget.onDeselectIngredient(); - return Provider.of(context, listen: false).searchIngredient( + return legacy_provider.Provider.of( + context, + listen: false, + ).searchIngredient( pattern, languageCode: Localizations.localeOf(context).languageCode, - searchLanguage: _searchLanguage, - isVegan: _isVegan, - isVegetarian: _isVegetarian, + searchLanguage: filters.searchLanguage, + isVegan: filters.isVegan, + isVegetarian: filters.isVegetarian, ); }, itemBuilder: (context, ingredient) { @@ -196,7 +198,10 @@ class _IngredientTypeaheadState extends State { ), onSelected: (suggestion) async { // Cache selected ingredient - final provider = Provider.of(context, listen: false); + final provider = legacy_provider.Provider.of( + context, + listen: false, + ); await provider.cacheIngredient(suggestion); widget.selectIngredient(suggestion.id, suggestion.name, null); }, @@ -220,7 +225,7 @@ class _IngredientTypeaheadState extends State { showDialog( context: context, builder: (context) => FutureBuilder( - future: Provider.of( + future: legacy_provider.Provider.of( context, listen: false, ).searchIngredientWithBarcode(barcode), @@ -234,9 +239,9 @@ class _IngredientTypeaheadState extends State { } Widget filterButton() { + final filters = ref.watch(ingredientFiltersNotifierProvider); final i18n = AppLocalizations.of(context); final languageCode = Localizations.localeOf(context).languageCode; - // If we are in English, we don't need the "Current & English" option final isEnglish = languageCode == LANGUAGE_SHORT_ENGLISH; final dropdownItems = [ @@ -255,8 +260,12 @@ class _IngredientTypeaheadState extends State { ), ]; - if (isEnglish && _searchLanguage == IngredientSearchLanguage.currentAndEnglish) { - _searchLanguage = IngredientSearchLanguage.current; + if (isEnglish && filters.searchLanguage == IngredientSearchLanguage.currentAndEnglish) { + WidgetsBinding.instance.addPostFrameCallback((_) { + ref + .read(ingredientFiltersNotifierProvider.notifier) + .chooseLanguage(IngredientSearchLanguage.current); + }); } return IconButton( @@ -267,53 +276,64 @@ class _IngredientTypeaheadState extends State { builder: (context) { return StatefulBuilder( builder: (context, setDialogState) { - return AlertDialog( - title: Text(i18n.filter), - content: Column( - mainAxisSize: MainAxisSize.min, - children: [ - ListTile( - contentPadding: EdgeInsets.zero, - title: Text(i18n.language), - subtitle: DropdownButton( - value: _searchLanguage, - isExpanded: true, - onChanged: (IngredientSearchLanguage? newValue) { - setDialogState(() { - _searchLanguage = newValue!; - }); - }, - items: dropdownItems, + return Consumer( + builder: (context, ref, _) { + final filters = ref.watch(ingredientFiltersNotifierProvider); + return AlertDialog( + title: Text(i18n.filter), + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ + ListTile( + contentPadding: EdgeInsets.zero, + title: Text(i18n.language), + subtitle: DropdownButton( + value: filters.searchLanguage, + isExpanded: true, + onChanged: (IngredientSearchLanguage? newValue) { + setDialogState(() { + ref + .read(ingredientFiltersNotifierProvider.notifier) + .chooseLanguage(newValue!); + }); + }, + items: dropdownItems, + ), + ), + SwitchListTile( + title: Text(i18n.isVegan), + value: filters.isVegan, + contentPadding: EdgeInsets.zero, + onChanged: (val) { + setDialogState(() { + ref + .read(ingredientFiltersNotifierProvider.notifier) + .toggleVegan(val); + }); + }, + ), + SwitchListTile( + title: Text(i18n.isVegetarian), + value: filters.isVegetarian, + contentPadding: EdgeInsets.zero, + onChanged: (val) { + setDialogState(() { + ref + .read(ingredientFiltersNotifierProvider.notifier) + .toggleVegetarian(val); + }); + }, + ), + ], + ), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context), + child: Text(MaterialLocalizations.of(context).closeButtonLabel), ), - ), - SwitchListTile( - title: Text(i18n.isVegan), - value: _isVegan, - contentPadding: EdgeInsets.zero, - onChanged: (val) { - setDialogState(() { - _isVegan = val; - }); - }, - ), - SwitchListTile( - title: Text(i18n.isVegetarian), - value: _isVegetarian, - contentPadding: EdgeInsets.zero, - onChanged: (val) { - setDialogState(() { - _isVegetarian = val; - }); - }, - ), - ], - ), - actions: [ - TextButton( - onPressed: () => Navigator.pop(context), - child: Text(MaterialLocalizations.of(context).closeButtonLabel), - ), - ], + ], + ); + }, ); }, );