diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 483ae035f..272541ca5 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -366,7 +366,7 @@ "@logIngredient": {}, "searchIngredient": "Search ingredient", "@searchIngredient": { - "description": "Label on ingredient search form" + "description": "Label (or Hint text) on ingredient search form" }, "nutritionalPlan": "Nutritional plan", "@nutritionalPlan": {}, @@ -378,6 +378,11 @@ "@noNutritionalPlans": { "description": "Message shown when the user has no nutritional plans" }, + "manageLists": "Manage lists", + "@manageLists": { + "description": "Label for the button to manage ingredient lists" + }, + "clearSearchTerm": "Clear search term (text)", "onlyLogging": "Only track calories", "onlyLoggingHelpText": "Check the box if you only want to log your calories and don't want to setup a detailed nutritional plan with specific meals", "goalMacro": "Macro goals", @@ -494,7 +499,9 @@ "timeStartAhead": "Start time cannot be ahead of end time", "@timeStartAhead": {}, "ingredient": "Ingredient", - "@ingredient": {}, + "@ingredient": { + "description": "Title for the ingredients screen" + }, "energy": "Energy", "@energy": { "description": "Energy in a meal, ingredient etc. e.g. in kJ" @@ -1274,6 +1281,10 @@ "@filterNutriscore": { "description": "Heading for the Nutri-Score slider in the ingredient search filter dialog" }, + "nutriscoreValue": "Nutri-Score:", + "@filterNutriscore": { + "description": "label for Nutri-Score(nutriscore) for.eg, in subtitle of ingredient custom lists" + }, "filterNutriscoreOff": "Off", "@filterNutriscoreOff": { "description": "Label for the first slider stop, which disables the Nutri-Score filter" diff --git a/lib/main.dart b/lib/main.dart index a6ad741ae..deead1b71 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -40,6 +40,8 @@ import 'package:wger/screens/form_screen.dart'; import 'package:wger/screens/gallery_screen.dart'; import 'package:wger/screens/gym_mode.dart'; import 'package:wger/screens/home_tabs_screen.dart'; +import 'package:wger/screens/ingredient_screen.dart'; +import 'package:wger/screens/ingredients_screen.dart'; import 'package:wger/screens/log_meal_screen.dart'; import 'package:wger/screens/log_meals_screen.dart'; import 'package:wger/screens/measurement_categories_screen.dart'; @@ -211,6 +213,8 @@ class MainApp extends ConsumerWidget { NutritionalPlansScreen.routeName: (ctx) => const NutritionalPlansScreen(), NutritionalDiaryScreen.routeName: (ctx) => const NutritionalDiaryScreen(), NutritionalPlanScreen.routeName: (ctx) => const NutritionalPlanScreen(), + IngredientDetailScreen.routeName: (ctx) => const IngredientDetailScreen(), + IngredientsScreen.routeName: (ctx) => const IngredientsScreen(), LogMealsScreen.routeName: (ctx) => const LogMealsScreen(), LogMealScreen.routeName: (ctx) => const LogMealScreen(), WeightScreen.routeName: (ctx) => const WeightScreen(), diff --git a/lib/models/nutrition/ingredient_filters.dart b/lib/models/nutrition/ingredient_filters.dart index 8916cd6c4..09ff4ccff 100644 --- a/lib/models/nutrition/ingredient_filters.dart +++ b/lib/models/nutrition/ingredient_filters.dart @@ -12,12 +12,14 @@ class IngredientFilters { /// `nutriscore__lte`. `null` means the filter is off (slider at the /// "No filter" position). final NutriScore? nutriscoreMax; + final String searchTerm; const IngredientFilters({ this.isVegan = false, this.isVegetarian = false, this.searchLanguage = SearchLanguage.currentAndEnglish, this.nutriscoreMax, + this.searchTerm = '', }); /// Locale-aware default filter set. @@ -37,12 +39,14 @@ class IngredientFilters { SearchLanguage? searchLanguage, NutriScore? nutriscoreMax, bool clearNutriscoreMax = false, + String? searchTerm, }) { return IngredientFilters( isVegan: isVegan ?? this.isVegan, isVegetarian: isVegetarian ?? this.isVegetarian, searchLanguage: searchLanguage ?? this.searchLanguage, nutriscoreMax: clearNutriscoreMax ? null : (nutriscoreMax ?? this.nutriscoreMax), + searchTerm: searchTerm ?? this.searchTerm, ); } diff --git a/lib/providers/ingredient_filters_notifier.dart b/lib/providers/ingredient_filters_notifier.dart index 8e7ee4156..baf319b33 100644 --- a/lib/providers/ingredient_filters_notifier.dart +++ b/lib/providers/ingredient_filters_notifier.dart @@ -75,6 +75,32 @@ class IngredientFiltersNotifier extends _$IngredientFiltersNotifier { ); await PreferenceHelper.instance.saveIngredientNutriscoreMax(value); } + + /// Updates the search term in the state without persisting it. + void setSearchTerm(String value) { + final current = _current(); + if (current.searchTerm != value) { + state = AsyncData(current.copyWith(searchTerm: value)); + } + } + + /// Resets all filters to their default values and clears persisted preferences. + Future resetFilters() async { + state = const AsyncData( + IngredientFilters( + isVegan: false, + isVegetarian: false, + searchLanguage: SearchLanguage.current, + searchTerm: '', + ), + ); + + final pref = PreferenceHelper.instance; + await pref.saveIngredientVeganFilter(false); + await pref.saveIngredientVegetarianFilter(false); + await pref.saveIngredientSearchLanguage(SearchLanguage.current); + await pref.saveIngredientNutriscoreMax(null); + } } // Synchronous helper provider to unwrap the AsyncValue produced by `ingredientFiltersProvider` diff --git a/lib/providers/ingredient_filters_notifier.g.dart b/lib/providers/ingredient_filters_notifier.g.dart index 76f5ac067..b6f59e19b 100644 --- a/lib/providers/ingredient_filters_notifier.g.dart +++ b/lib/providers/ingredient_filters_notifier.g.dart @@ -33,7 +33,7 @@ final class IngredientFiltersNotifierProvider IngredientFiltersNotifier create() => IngredientFiltersNotifier(); } -String _$ingredientFiltersNotifierHash() => r'3fb2ae558584d5ea3cce83679b6682ef5b1ba6f2'; +String _$ingredientFiltersNotifierHash() => r'c8e5577795e50643a8fdcf5b44c0a22b21830dfb'; abstract class _$IngredientFiltersNotifier extends $AsyncNotifier { FutureOr build(); diff --git a/lib/providers/ingredient_notifier.dart b/lib/providers/ingredient_notifier.dart index d216ca405..8cff65d08 100644 --- a/lib/providers/ingredient_notifier.dart +++ b/lib/providers/ingredient_notifier.dart @@ -22,17 +22,19 @@ import 'dart:async'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:logging/logging.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:wger/models/core/search_options.dart'; import 'package:wger/models/nutrition/ingredient.dart'; +import 'package:wger/providers/ingredient_filters_notifier.dart'; import 'package:wger/providers/network_provider.dart'; import 'ingredient_repository.dart'; part 'ingredient_notifier.g.dart'; -@riverpod +@Riverpod(keepAlive: true) final class IngredientNotifier extends _$IngredientNotifier { final _logger = Logger('IngredientProvider'); @@ -45,37 +47,32 @@ final class IngredientNotifier extends _$IngredientNotifier { final Map> _inflight = {}; @override - FutureOr build() async { - _repo = ref.read(ingredientRepositoryProvider); - return null; + Stream> build() { + ref.keepAlive(); + _logger.finer('Building ingredient stream'); + _repo = ref.watch(ingredientRepositoryProvider); + // Clear cache when the main list changes + // ref.onDispose(() => _cache.clear()); + + return _repo.watchAllDrift(); } /// Fetch ingredient by [id] from the DB (one-shot) and update state. /// If the id is present in the in-memory cache, the cached value is /// returned immediately without a DB read. Simultaneous calls for the - /// same id are deduplicated. The provider `state` is used only as a - /// status signal (loading/done/error) and does not carry the Ingredient - /// itself — consumers should read the data from the notifier's cache. + /// same id are deduplicated. Future fetch(int id) async { _logger.finer('Fetching ingredient id: $id'); // Return cached result if present (could be null meaning 'not found'). if (_cache.containsKey(id)) { - final cached = _cache[id]; - if (ref.mounted) { - state = const AsyncData(null); - } - return cached; + return _cache[id]; } // If a fetch for this id is already ongoing, await it. if (_inflight.containsKey(id)) { _logger.finer('Awaiting in-flight fetch for id: $id'); - final res = await _inflight[id]; - if (ref.mounted) { - state = const AsyncData(null); - } - return res; + return await _inflight[id]; } // Start a new DB read and store the future in _inflight to dedupe. @@ -91,15 +88,8 @@ final class IngredientNotifier extends _$IngredientNotifier { } Future _doFetch(int id) async { - if (ref.mounted) { - state = const AsyncLoading(); - } final item = await _repo.getById(id); _cache[id] = item; // store null as 'not found' as well - if (!ref.mounted) { - return item; - } - state = const AsyncData(null); return item; } @@ -133,3 +123,19 @@ final class IngredientNotifier extends _$IngredientNotifier { ); } } + +/// Trigger ingredient search flow by reacting to filter changes. +final searchedIngredientsProvider = FutureProvider>((ref) async { + final filters = ref.watch(ingredientFiltersSyncProvider); + + // search logic handles the Online/Offline routing + return ref + .read(ingredientProvider.notifier) + .searchIngredient( + filters.searchTerm, + searchLanguage: filters.searchLanguage, + isVegan: filters.isVegan, + isVegetarian: filters.isVegetarian, + nutriscoreMax: filters.nutriscoreMax, + ); +}); diff --git a/lib/providers/ingredient_notifier.g.dart b/lib/providers/ingredient_notifier.g.dart index 510dff73f..a1d306fdf 100644 --- a/lib/providers/ingredient_notifier.g.dart +++ b/lib/providers/ingredient_notifier.g.dart @@ -12,14 +12,15 @@ part of 'ingredient_notifier.dart'; @ProviderFor(IngredientNotifier) final ingredientProvider = IngredientNotifierProvider._(); -final class IngredientNotifierProvider extends $AsyncNotifierProvider { +final class IngredientNotifierProvider + extends $StreamNotifierProvider> { IngredientNotifierProvider._() : super( from: null, argument: null, retry: null, name: r'ingredientProvider', - isAutoDispose: true, + isAutoDispose: false, dependencies: null, $allTransitiveDependencies: null, ); @@ -32,19 +33,19 @@ final class IngredientNotifierProvider extends $AsyncNotifierProvider IngredientNotifier(); } -String _$ingredientNotifierHash() => r'40e9fa3376a25289b04a86f9303e20de77cab1d8'; +String _$ingredientNotifierHash() => r'd94756d249a15e604640f2e9a5c1802a7d4f9711'; -abstract class _$IngredientNotifier extends $AsyncNotifier { - FutureOr build(); +abstract class _$IngredientNotifier extends $StreamNotifier> { + Stream> build(); @$mustCallSuper @override void runBuild() { - final ref = this.ref as $Ref, void>; + final ref = this.ref as $Ref>, List>; final element = ref.element as $ClassProviderElement< - AnyNotifier, void>, - AsyncValue, + AnyNotifier>, List>, + AsyncValue>, Object?, Object? >; diff --git a/lib/providers/ingredient_repository.dart b/lib/providers/ingredient_repository.dart index 1a52cfd6c..6191985fd 100644 --- a/lib/providers/ingredient_repository.dart +++ b/lib/providers/ingredient_repository.dart @@ -62,6 +62,18 @@ class IngredientRepository { }); } + /// Watches all locally synced ingredients. + /// + /// Only ingredients that are used in a nutritional plan or log are + /// synced to the device for offline storage in the Drift database. + Stream> watchAllDrift() { + _logger.finer('Watching all synced ingredients'); + final query = _baseJoinedQuery()..orderBy([OrderingTerm(expression: _db.ingredientTable.name)]); + return query.watch().map((rows) { + return _hydrate(rows); + }); + } + /// Read a single ingredient by [id] once from the DB Future getById(int id) async { _logger.finer('Reading ingredient $id'); diff --git a/lib/screens/ingredient_screen.dart b/lib/screens/ingredient_screen.dart new file mode 100644 index 000000000..98db0111a --- /dev/null +++ b/lib/screens/ingredient_screen.dart @@ -0,0 +1,46 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2025 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import 'package:flutter/material.dart'; +import 'package:wger/core/wide_screen_wrapper.dart'; +import 'package:wger/models/nutrition/ingredient.dart'; +import 'package:wger/widgets/nutrition/ingredient_detail.dart'; + +class IngredientDetailScreen extends StatelessWidget { + static const routeName = '/ingredient-detail'; + + const IngredientDetailScreen({super.key}); + + @override + Widget build(BuildContext context) { + // Extract the ingredient from the route arguments + final ingredient = ModalRoute.of(context)!.settings.arguments as Ingredient; + + return Scaffold( + appBar: AppBar( + title: Text(ingredient.name), + actions: const [], + ), + body: WidescreenWrapper( + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 10), + child: IngredientDetail(ingredient), + ), + ), + ); + } +} diff --git a/lib/screens/ingredients_screen.dart b/lib/screens/ingredients_screen.dart new file mode 100644 index 000000000..44ca2aa68 --- /dev/null +++ b/lib/screens/ingredients_screen.dart @@ -0,0 +1,87 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2025 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:wger/core/wide_screen_wrapper.dart'; +import 'package:wger/l10n/generated/app_localizations.dart'; +import 'package:wger/models/nutrition/ingredient.dart'; +import 'package:wger/providers/ingredient_notifier.dart'; +import 'package:wger/widgets/core/progress_indicator.dart'; +import 'package:wger/widgets/nutrition/ingredient_filter_row.dart'; +import 'package:wger/widgets/nutrition/ingredient_list_tile.dart'; + +class IngredientsScreen extends ConsumerStatefulWidget { + const IngredientsScreen({super.key}); + + static const routeName = '/ingredients'; + + @override + ConsumerState createState() => _IngredientsScreenState(); +} + +class _IngredientsScreenState extends ConsumerState { + @override + Widget build(BuildContext context) { + final ingredientsAsync = ref.watch(searchedIngredientsProvider); + final i18n = AppLocalizations.of(context); + + return Scaffold( + appBar: AppBar( + title: Text( + '${i18n.ingredient}' + 's', + ), + ), + body: WidescreenWrapper( + child: Column( + children: [ + const IngredientFilterRow(), + Expanded( + child: ingredientsAsync.when( + data: (list) => _IngredientsList( + ingredientList: list, + ), + + loading: () => const Center(child: CenteredProgressIndicator()), + error: (e, st) => Center(child: Text('Error: $e')), + ), + ), + ], + ), + ), + ); + } +} + +class _IngredientsList extends StatelessWidget { + const _IngredientsList({required this.ingredientList}); + + final List ingredientList; + + @override + Widget build(BuildContext context) { + return ListView.separated( + separatorBuilder: (context, index) { + return const Divider(thickness: 1); + }, + itemCount: ingredientList.length, + itemBuilder: (context, index) => IngredientListTile(ingredient: ingredientList[index]), + ); + } +} diff --git a/lib/screens/nutritional_plans_screen.dart b/lib/screens/nutritional_plans_screen.dart index 60499c899..c10fc8a8d 100644 --- a/lib/screens/nutritional_plans_screen.dart +++ b/lib/screens/nutritional_plans_screen.dart @@ -22,10 +22,14 @@ import 'package:wger/core/wide_screen_wrapper.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; import 'package:wger/providers/network_provider.dart'; import 'package:wger/screens/form_screen.dart'; -import 'package:wger/widgets/core/app_bar.dart'; +import 'package:wger/screens/ingredients_screen.dart'; import 'package:wger/widgets/nutrition/forms.dart'; import 'package:wger/widgets/nutrition/nutritional_plans_list.dart'; +enum _NutritionalPlansAppBarOptions { + list, +} + class NutritionalPlansScreen extends ConsumerWidget { const NutritionalPlansScreen(); @@ -36,7 +40,28 @@ class NutritionalPlansScreen extends ConsumerWidget { final isOnline = ref.watch(networkStatusProvider); return Scaffold( - appBar: EmptyAppBar(AppLocalizations.of(context).nutritionalPlans), + appBar: AppBar( + title: Text(AppLocalizations.of(context).nutritionalPlans), + actions: [ + PopupMenuButton( + itemBuilder: (context) { + return [ + const PopupMenuItem<_NutritionalPlansAppBarOptions>( + value: _NutritionalPlansAppBarOptions.list, + child: Text('ingredient list'), // TODO: i10n + ), + ]; + }, + onSelected: (value) { + switch (value) { + case _NutritionalPlansAppBarOptions.list: + Navigator.of(context).pushNamed(IngredientsScreen.routeName); + break; + } + }, + ), + ], + ), floatingActionButton: FloatingActionButton( backgroundColor: isOnline ? null : Colors.grey, onPressed: isOnline diff --git a/lib/widgets/nutrition/ingredient_detail.dart b/lib/widgets/nutrition/ingredient_detail.dart new file mode 100644 index 000000000..6fb751ec4 --- /dev/null +++ b/lib/widgets/nutrition/ingredient_detail.dart @@ -0,0 +1,161 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2025 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:wger/models/nutrition/ingredient.dart'; +import 'package:wger/theme/theme.dart'; +import 'package:wger/widgets/nutrition/ingredient_images.dart'; + +class IngredientDetail extends StatelessWidget { + final Ingredient _ingredient; + static const PADDING = 9.0; + + const IngredientDetail(this._ingredient, {super.key}); + + @override + Widget build(BuildContext context) { + return SingleChildScrollView( + child: Container( + color: Theme.of(context).colorScheme.surfaceContainerLow, + padding: const EdgeInsets.all(PADDING), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + // Image + ...getImage(), + const SizedBox(height: 8), + + // Dietary Tags & Nutri-Score + getBadges(context), + const SizedBox(height: 16), + + // Nutritional Info per 100g + ...getNutritionInfo(context), + + const SizedBox(height: 20), + ], + ), + ), + ); + } + + List getImage() { + final List out = []; + if (_ingredient.image != null) { + out.add( + Center( + child: IngredientImageWidget( + image: _ingredient.image, + height: 250, + ), + ), + ); + out.add(const SizedBox(height: PADDING)); + } + return out; + } + + Widget getBadges(BuildContext context) { + final List out = []; + + if (_ingredient.isVegan == true) { + out.add(_buildColoredChip('Vegan', wgerTertiaryColor, Colors.white)); + } else if (_ingredient.isVegetarian == true) { + out.add(_buildColoredChip('Vegetarian', wgerPrimaryColorLight, wgerPrimaryColor)); + } + + if (_ingredient.nutriscore != null) { + out.add( + _buildColoredChip( + 'Nutri-Score: ${_ingredient.nutriscore!.name.toUpperCase()}', + wgerPrimaryColor.withAlpha(200), + Colors.white, + ), + ); + } + + return Wrap(spacing: 8, runSpacing: 8, children: out); + } + + Widget _buildColoredChip(String label, Color bg, Color text) { + return Container( + padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6), + decoration: BoxDecoration( + color: bg, + borderRadius: BorderRadius.circular(20), + ), + child: Text( + label, + style: TextStyle(color: text, fontWeight: FontWeight.bold, fontSize: 12), + ), + ); + } + + List getNutritionInfo(BuildContext context) { + final List out = []; + final macros = _ingredient.nutritionalValues; + + out.add( + Text( + 'Nutritional Values (per 100g)', + style: Theme.of(context).textTheme.headlineSmall, + ), + ); + out.add(const SizedBox(height: PADDING)); + + // Build the macro list leveraging the model's getters + out.add(_buildNutritionRow('Energy', '${macros.energy} kJ')); + out.add(_buildNutritionRow('Protein', '${macros.protein} g')); + out.add(_buildNutritionRow('Carbohydrates', '${macros.carbohydrates} g')); + if (macros.carbohydratesSugar > 0) { + out.add(_buildNutritionRow(' ↳ Sugars', '${macros.carbohydratesSugar} g', isSubItem: true)); + } + out.add(_buildNutritionRow('Fat', '${macros.fat} g')); + if (macros.fatSaturated > 0) { + out.add(_buildNutritionRow(' ↳ Saturated', '${macros.fatSaturated} g', isSubItem: true)); + } + out.add(_buildNutritionRow('Fiber', '${macros.fiber} g')); + out.add(_buildNutritionRow('Sodium', '${macros.sodium} g')); + + out.add(const SizedBox(height: PADDING)); + return out; + } + + Widget _buildNutritionRow(String label, String value, {bool isSubItem = false}) { + return Padding( + padding: EdgeInsets.symmetric( + vertical: 4, + horizontal: isSubItem ? 16.0 : 0.0, + ), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + label, + style: TextStyle( + color: isSubItem ? Colors.grey[600] : null, + fontWeight: isSubItem ? FontWeight.normal : FontWeight.w500, + ), + ), + Text(value), + ], + ), + ); + } +} diff --git a/lib/widgets/nutrition/ingredient_filter_row.dart b/lib/widgets/nutrition/ingredient_filter_row.dart new file mode 100644 index 000000000..bf68754f6 --- /dev/null +++ b/lib/widgets/nutrition/ingredient_filter_row.dart @@ -0,0 +1,337 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2025 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:wger/helpers/consts.dart'; +import 'package:wger/l10n/generated/app_localizations.dart'; +import 'package:wger/models/core/search_options.dart'; +import 'package:wger/models/nutrition/ingredient.dart'; +import 'package:wger/providers/ingredient_filters_notifier.dart'; +import 'package:wger/providers/ingredient_repository.dart'; + +class IngredientFilterRow extends ConsumerStatefulWidget { + const IngredientFilterRow({super.key}); + + @override + _IngredientFilterRowState createState() => _IngredientFilterRowState(); +} + +class _IngredientFilterRowState extends ConsumerState { + late final TextEditingController _ingredientNameController; + + @override + void initState() { + super.initState(); + + final initialSearch = ref.read(ingredientFiltersSyncProvider).searchTerm; + + _ingredientNameController = TextEditingController(text: initialSearch) + ..addListener(() { + final text = _ingredientNameController.text; + final currentFilters = ref.read(ingredientFiltersSyncProvider); + if (currentFilters.searchTerm != text) { + ref.read(ingredientFiltersProvider.notifier).setSearchTerm(text); + } + }); + } + + @override + Widget build(BuildContext context) { + final i18n = AppLocalizations.of(context); + final currentFilters = ref.watch(ingredientFiltersSyncProvider); + + return Padding( + padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 15), + child: Row( + children: [ + Expanded( + child: TextFormField( + controller: _ingredientNameController, + decoration: InputDecoration( + hintText: '${i18n.searchIngredient}...', + prefixIcon: const Icon(Icons.search), + contentPadding: const EdgeInsets.symmetric(horizontal: 10), + border: const OutlineInputBorder( + borderSide: BorderSide(color: Colors.black), + ), + suffixIcon: Row( + mainAxisSize: MainAxisSize.min, + children: [ + if (currentFilters.searchTerm.isNotEmpty) + IconButton( + icon: const Icon(Icons.clear), + tooltip: i18n.clearSearchTerm, + onPressed: () { + ref.read(ingredientFiltersProvider.notifier).setSearchTerm(''); + _ingredientNameController.clear(); + }, + ), + ], + ), + ), + ), + ), + Row( + children: [ + IconButton( + onPressed: () => showDialog( + context: context, + builder: (_) => const IngredientFilterDialog(), + ), + icon: const Icon(Icons.filter_alt), + ), + ], + ), + ], + ), + ); + } + + @override + void dispose() { + _ingredientNameController.dispose(); + super.dispose(); + } +} + +// exact code for components from main branch's ingredients search in [IngredientTypeahead] + +/// Filter dialog for the ingredient search. Shown via [showDialog] from the +/// filter icon button in the [IngredientsScreen]. +/// +/// All state is owned by [ingredientFiltersProvider]; this widget is purely a +/// view over that state. +class IngredientFilterDialog extends ConsumerWidget { + const IngredientFilterDialog({super.key}); + + @override + Widget build(BuildContext context, WidgetRef ref) { + final i18n = AppLocalizations.of(context); + + return AlertDialog( + title: Text(i18n.filter), + content: const SingleChildScrollView( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + _LanguageDropdown(), + _DietSwitches(), + _NutriscoreSliderSection(), + ], + ), + ), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context), + child: Text(MaterialLocalizations.of(context).closeButtonLabel), + ), + ], + ); + } +} + +/// Wraps the shared [SearchLanguageDropdown] and binds it to +/// [ingredientFiltersProvider]. +class _LanguageDropdown extends ConsumerWidget { + const _LanguageDropdown(); + + @override + Widget build(BuildContext context, WidgetRef ref) { + final filters = ref.watch(ingredientFiltersSyncProvider); + return SearchLanguageDropdown( + selected: filters.searchLanguage, + onChanged: (val) => ref.read(ingredientFiltersProvider.notifier).chooseLanguage(val), + ); + } +} + +/// Vegan + Vegetarian toggles, grouped because they are thematically and +/// visually paired. +class _DietSwitches extends ConsumerWidget { + const _DietSwitches(); + + @override + Widget build(BuildContext context, WidgetRef ref) { + final i18n = AppLocalizations.of(context); + final filters = ref.watch(ingredientFiltersSyncProvider); + + return Column( + mainAxisSize: MainAxisSize.min, + children: [ + SwitchListTile( + title: Text(i18n.isVegan), + value: filters.isVegan, + contentPadding: EdgeInsets.zero, + onChanged: (val) => ref.read(ingredientFiltersProvider.notifier).toggleVegan(val), + ), + SwitchListTile( + title: Text(i18n.isVegetarian), + value: filters.isVegetarian, + contentPadding: EdgeInsets.zero, + onChanged: (val) => ref.read(ingredientFiltersProvider.notifier).toggleVegetarian(val), + ), + ], + ); + } +} + +/// Section that wraps [_NutriscoreSlider] and binds it to +/// [ingredientFiltersProvider]. +class _NutriscoreSliderSection extends ConsumerWidget { + const _NutriscoreSliderSection(); + + @override + Widget build(BuildContext context, WidgetRef ref) { + final filters = ref.watch(ingredientFiltersSyncProvider); + return _NutriscoreSlider( + value: filters.nutriscoreMax, + onChanged: (grade) => ref.read(ingredientFiltersProvider.notifier).chooseNutriscoreMax(grade), + ); + } +} + +/// Discrete slider that lets the user pick the worst acceptable [NutriScore] +/// grade. Index 0 is the "Off" position (no filter, `null` value) and +/// indices 1..N map to [NutriScore.values]. +class _NutriscoreSlider extends StatelessWidget { + final NutriScore? value; + final ValueChanged onChanged; + + const _NutriscoreSlider({required this.value, required this.onChanged}); + + static const int _offIndex = 0; + + int _valueToIndex(NutriScore? v) => v == null ? _offIndex : NutriScore.values.indexOf(v) + 1; + + NutriScore? _indexToValue(int i) => i == _offIndex ? null : NutriScore.values[i - 1]; + + @override + Widget build(BuildContext context) { + final i18n = AppLocalizations.of(context); + final index = _valueToIndex(value); + final maxIndex = NutriScore.values.length; // 0=Off, then A..E + final helperText = value == null + ? i18n.filterNutriscoreNoFilter + : i18n.filterNutriscoreOrBetter(value!.name.toUpperCase()); + return Padding( + padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + i18n.filterNutriscore, + style: Theme.of(context).textTheme.bodyMedium, + ), + Text( + helperText, + style: Theme.of(context).textTheme.bodySmall, + ), + Slider( + value: index.toDouble(), + min: 0, + max: maxIndex.toDouble(), + divisions: maxIndex, + label: value == null ? i18n.filterNutriscoreOff : value!.name.toUpperCase(), + onChanged: (v) => onChanged(_indexToValue(v.round())), + ), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 12), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + i18n.filterNutriscoreOff, + style: Theme.of(context).textTheme.labelSmall, + ), + ...NutriScore.values.map( + (score) => Text( + score.name.toUpperCase(), + style: Theme.of(context).textTheme.labelSmall, + ), + ), + ], + ), + ), + ], + ), + ); + } +} + +/// Dropdown that lets the user pick which language(s) to search in. +/// +/// Shared between exercise and ingredient search filters. The widget is +/// purely presentational — the caller passes the currently [selected] value +/// and an [onChanged] callback to react to user input. +class SearchLanguageDropdown extends StatelessWidget { + final SearchLanguage selected; + final ValueChanged onChanged; + + const SearchLanguageDropdown({ + super.key, + required this.selected, + required this.onChanged, + }); + + @override + Widget build(BuildContext context) { + final i18n = AppLocalizations.of(context); + final languageCode = Localizations.localeOf(context).languageCode; + final isEnglish = languageCode == LANGUAGE_SHORT_ENGLISH; + + final items = >[ + DropdownMenuItem( + value: SearchLanguage.current, + child: Text(i18n.searchLanguageCurrent(languageCode)), + ), + if (!isEnglish) + DropdownMenuItem( + value: SearchLanguage.currentAndEnglish, + child: Text(i18n.searchLanguageEnglish(languageCode)), + ), + DropdownMenuItem( + value: SearchLanguage.all, + child: Text(i18n.searchLanguageAll), + ), + ]; + + // If the persisted value isn't offered for the current locale (e.g. + // currentAndEnglish on an English locale), fall back to null so the + // dropdown shows a valid state until the auto-switch corrects it. + SearchLanguage? value = selected; + if (!items.any((it) => it.value == value)) { + value = null; + } + + return ListTile( + contentPadding: EdgeInsets.zero, + title: Text(i18n.language), + subtitle: DropdownButton( + value: value, + isExpanded: true, + items: items, + onChanged: (val) { + if (val != null) { + onChanged(val); + } + }, + ), + ); + } +} diff --git a/lib/widgets/nutrition/ingredient_images.dart b/lib/widgets/nutrition/ingredient_images.dart new file mode 100644 index 000000000..9aa5cd1aa --- /dev/null +++ b/lib/widgets/nutrition/ingredient_images.dart @@ -0,0 +1,51 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2025 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:wger/models/nutrition/ingredient_image.dart'; +import 'package:wger/widgets/core/wger_image.dart'; + +/// Renders an ingredient image using WgerImage for disk + memory caching, falling back to +/// a tinted asset placeholder when [image] is null or the network load +/// fails. Optional [height] constrains the rendered box; width is always +/// derived from the parent. +class IngredientImageWidget extends StatelessWidget { + const IngredientImageWidget({super.key, this.image, this.height}); + + final IngredientImage? image; + final double? height; + + static const _placeholder = Image( + image: AssetImage('assets/images/placeholder.png'), + color: Color.fromRGBO(255, 255, 255, 0.3), + colorBlendMode: BlendMode.modulate, + semanticLabel: 'Placeholder', + ); + + @override + Widget build(BuildContext context) { + return WgerImage( + mediaPath: image?.image, + height: height, + // No width — let the parent decide, matching the previous Image.network + // behaviour where the natural aspect ratio drove sizing. + fit: BoxFit.contain, + errorWidget: _placeholder, + ); + } +} diff --git a/lib/widgets/nutrition/ingredient_list_tile.dart b/lib/widgets/nutrition/ingredient_list_tile.dart new file mode 100644 index 000000000..966364a8d --- /dev/null +++ b/lib/widgets/nutrition/ingredient_list_tile.dart @@ -0,0 +1,60 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2025 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:wger/models/nutrition/ingredient.dart'; +import 'package:wger/screens/ingredient_screen.dart'; +import 'package:wger/widgets/nutrition/ingredient_images.dart'; + +class IngredientListTile extends StatelessWidget { + const IngredientListTile({super.key, required this.ingredient}); + + final Ingredient ingredient; + + @override + Widget build(BuildContext context) { + const double IMG_SIZE = 60; + final String macros = + 'P: ${ingredient.protein}g / C: ${ingredient.carbohydrates}g / F: ${ingredient.fat}g'; + + return ListTile( + leading: SizedBox( + height: IMG_SIZE, + width: IMG_SIZE, + child: CircleAvatar( + backgroundColor: const Color(0x00ffffff), + child: ClipOval( + child: SizedBox( + height: IMG_SIZE, + width: IMG_SIZE, + child: IngredientImageWidget(image: ingredient.image), + ), + ), + ), + ), + title: Text(ingredient.name, overflow: TextOverflow.ellipsis, maxLines: 2), + subtitle: Text( + '${ingredient.energy} kJ • $macros', + overflow: TextOverflow.ellipsis, + ), + onTap: () { + Navigator.pushNamed(context, IngredientDetailScreen.routeName, arguments: ingredient); + }, + ); + } +}