Files
flutter/lib/helpers/shared_preferences.dart
kiranannadatha8 b7f7e43fa3 refactor(nutrition): collapse Nutri-Score toggle into single slider
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.
2026-04-20 10:33:08 -04:00

93 lines
3.4 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences/util/legacy_to_async_migration_util.dart';
import 'package:wger/models/nutrition/ingredient.dart';
import 'package:wger/providers/nutrition.dart';
/// A helper class that manages preferences using SharedPreferencesAsync
/// and handles migration from the legacy SharedPreferences to
/// SharedPreferencesAsync.
class PreferenceHelper {
SharedPreferencesAsync _asyncPref = SharedPreferencesAsync();
PreferenceHelper._instantiate();
static final PreferenceHelper _instance = PreferenceHelper._instantiate();
static SharedPreferencesAsync get asyncPref => _instance._asyncPref;
static PreferenceHelper get instance => _instance;
/// Migration function that ensures any legacy data stored in
/// SharedPreferences is migrated to SharedPreferencesAsync. This migration
/// only happens once, as checked by the migrationCompletedKey.
///
/// [migrationCompletedKey] is used to track if the migration has been
/// completed.
Future<void> migrationSupportFunctionForSharedPreferences() async {
const SharedPreferencesOptions sharedPreferencesOptions = SharedPreferencesOptions();
final SharedPreferences prefs = await SharedPreferences.getInstance();
await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary(
legacySharedPreferencesInstance: prefs,
sharedPreferencesAsyncOptions: sharedPreferencesOptions,
migrationCompletedKey: 'migrationCompleted',
);
_asyncPref = SharedPreferencesAsync();
}
//ingredients filters
//1.vegan
Future<void> saveIngredientVeganFilter(bool value) async {
await PreferenceHelper.asyncPref.setBool('ingredientVeganFilter', value);
}
Future<bool> getIngredientVeganFilter() async {
return await PreferenceHelper.asyncPref.getBool('ingredientVeganFilter') ?? false;
}
//2.vegetarian
Future<void> saveIngredientVegetarianFilter(bool value) async {
await PreferenceHelper.asyncPref.setBool('ingredientVegetarianFilter', value);
}
Future<bool> getIngredientVegetarianFilter() async {
return await PreferenceHelper.asyncPref.getBool('ingredientVegetarianFilter') ?? false;
}
//3.language
Future<void> saveIngredientSearchLanguage(IngredientSearchLanguage language) async {
await PreferenceHelper.asyncPref.setString('search_language', language.name);
}
Future<IngredientSearchLanguage> 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,
);
}
}
//4.nutri-score worst acceptable grade (null means the filter is off)
Future<void> saveIngredientNutriscoreMax(NutriScore? value) async {
if (value == null) {
await PreferenceHelper.asyncPref.remove('ingredientNutriscoreMax');
} else {
await PreferenceHelper.asyncPref.setString('ingredientNutriscoreMax', value.name);
}
}
Future<NutriScore?> getIngredientNutriscoreMax() async {
final value = await PreferenceHelper.asyncPref.getString('ingredientNutriscoreMax');
if (value == null) {
return null;
}
return NutriScore.values.firstWhere(
(e) => e.name == value,
orElse: () => NutriScore.c,
);
}
}