Skip to content

feat(nutrition): filter ingredient search by Nutri-Score - #1179

Merged
rolandgeider merged 2 commits into
wger-project:masterfrom
kiranannadatha8:feature/2295-nutriscore-filter
Apr 20, 2026
Merged

rolandgeider merged 2 commits into
wger-project:masterfrom
kiranannadatha8:feature/2295-nutriscore-filter

Conversation

@kiranannadatha8

Copy link
Copy Markdown
Contributor

Closes part of wger-project/wger#2295 for the Flutter client.

Summary

Adds a "Filter by Nutri-Score" switch + A–E discrete slider to the ingredient search filter popover, mirroring the React PR (wger-project/react#1237). When enabled, the selected worst-acceptable grade is sent to the backend as nutriscore__lte=<a|b|c|d|e> — the range lookup introduced in backend PR wger-project/wger#2305.

What changed

  • IngredientFilters: adds filterNutriscore (bool) + nutriscoreMax (NutriScore enum, default e)
  • PreferenceHelper: persists both settings across sessions
  • IngredientFiltersNotifier (riverpod): toggleNutriscore + chooseNutriscoreMax
  • NutritionPlansProvider.searchIngredient: forwards nutriscoreMax as the nutriscore__lte query arg
  • IngredientTypeahead filter popover: new SwitchListTile + 5-division Slider (A = 0, E = 4); slider is hidden until the switch is on
  • i18n: English strings filterNutriscore and filterNutriscoreMax added to app_en.arb
  • Tests: 3 new widget tests in test/nutrition/ingredient_typeahead_test.dart covering (1) slider hidden until switch flips, (2) query emitted without the filter, (3) nutriscoreMax threaded through when the filter is on
  • Generated Mockito overrides updated across nutritional_meal_form_test.mocks.dart, nutritional_plan_form_test.mocks.dart, settings_test.mocks.dart, and weight_screen_test.mocks.dart so existing stubs still match the extended searchIngredient signature

UI

Same layout as the React PR (wger-project/react#1237 has screenshots of the equivalent UI): the existing vegan / vegetarian filter popover now has a third SwitchListTile labelled Filter by Nutri-Score. Flipping it reveals a Slider with 5 discrete stops (A · B · C · D · E). The current max grade renders in the slider label. State persists across app restarts via SharedPreferencesAsync.

Test plan

  • flutter analyze clean on changed files
  • flutter test test/nutrition/ — 27 passing including the 3 new widget tests
  • flutter test test/core/settings_test.dart test/weight/weight_screen_test.dart — mockito overrides still match
  • Manual: backend running PR #2305 + search "ap", flip switch, drag slider to B — confirm results exclude C/D/E-rated items

Related

Adds a "Filter by Nutri-Score" switch and an A–E discrete slider to
the ingredient search filter popover. The worst acceptable grade is
sent to the backend as `nutriscore__lte=<a|b|c|d|e>`, matching the
new filter exposed by wger-project/wger#2295.

- Extend `IngredientFilters` with `filterNutriscore` + `nutriscoreMax`
- Persist both settings through `PreferenceHelper`
- Expose `toggleNutriscore` and `chooseNutriscoreMax` on the riverpod
  notifier
- Propagate `nutriscoreMax` through `NutritionPlansProvider.searchIngredient`
- Add English strings `filterNutriscore` / `filterNutriscoreMax`
- Widget tests cover: slider hidden until switch flipped, query emitted
  with/without the filter
- Update stale generated Mockito overrides across the four test files
  that mock `NutritionPlansProvider` so the new named arg lines up

Refs wger-project/wger#2295
Mirrors the React refactor in wger-project/react#1237 addressing
Roland's review — the SwitchListTile + conditional Slider pair
expressed one semantic decision ("what grade ceiling?") through two
controls. Folded "Off" into the slider itself:

- Slider stops are now Off | A | B | C | D | E (was A..E, gated by
  a SwitchListTile).
- Off position sends no `nutriscoreMax`; A..E map as before.
- Heading changed from "Filter by Nutri-Score" to "Nutri-Score
  filter", with a dynamic helper ("No filter" / "<grade> or better").
- Model: `IngredientFilters.nutriscoreMax` is now nullable; removed
  `filterNutriscore`. `copyWith` takes a `clearNutriscoreMax` flag
  because Dart's `??` cannot distinguish "no argument" from "null".
- Storage collapsed: `saveIngredientNutriscoreMax(null)` removes the
  key; `getIngredientNutriscoreMax()` returns `null` when absent.
- Riverpod notifier: removed `toggleNutriscore`; `chooseNutriscoreMax`
  now takes `NutriScore?`.
- l10n: removed `filterNutriscoreMax`; added `filterNutriscoreOff`,
  `filterNutriscoreNoFilter`, `filterNutriscoreOrBetter` (placeholder).

Tests updated: 61/61 pass.
@kiranannadatha8

Copy link
Copy Markdown
Contributor Author

Pushed b7f7e43 mirroring the React refactor in wger-project/react#1237: collapsed the Switch + Slider pair into a single Off | A | B | C | D | E slider per @rolandgeider's feedback. All 61 nutrition tests pass.

@rolandgeider
rolandgeider merged commit 568d6f3 into wger-project:master Apr 20, 2026
1 check passed
@rolandgeider

Copy link
Copy Markdown
Member

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants