mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-28 17:08:29 +01:00
enhancement:persist-ingredient-filters in nutrition
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -65,3 +65,6 @@ ios/fastlane/output
|
||||
ios/Runner.ipa
|
||||
ios/Runner.app.dSYM.zip
|
||||
|
||||
#Flutter Version Management
|
||||
.fvm/
|
||||
|
||||
|
||||
@@ -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<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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
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<AuthProvider>(
|
||||
builder: (ctx, auth, _) {
|
||||
final baseInstance = WgerBaseProvider(Provider.of(ctx, listen: false));
|
||||
|
||||
return Consumer<UserProvider>(
|
||||
builder: (ctx, user, _) => riverpod.ProviderScope(
|
||||
overrides: [
|
||||
wgerBaseProvider.overrideWithValue(baseInstance),
|
||||
ingredientFiltersNotifierProvider.overrideWith(
|
||||
() => IngredientFiltersNotifier(
|
||||
initialVegan: initialVegan,
|
||||
initialVegetarian: initialVegetarian,
|
||||
initialSearchLanguage: initialSearchLanguage,
|
||||
),
|
||||
),
|
||||
],
|
||||
child: riverpod.Consumer(
|
||||
builder: (rpCtx, ref, _) {
|
||||
|
||||
25
lib/models/nutrition/ingredient_filters.dart
Normal file
25
lib/models/nutrition/ingredient_filters.dart
Normal file
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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(
|
||||
|
||||
48
lib/providers/nutrition_ingredient_filters_riverpod.dart
Normal file
48
lib/providers/nutrition_ingredient_filters_riverpod.dart
Normal file
@@ -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<IngredientFilters> {
|
||||
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<void> toggleVegan(bool value) async {
|
||||
state = state.copyWith(isVegan: value);
|
||||
await PreferenceHelper.instance.saveIngredientVeganFilter(value);
|
||||
}
|
||||
|
||||
Future<void> toggleVegetarian(bool value) async {
|
||||
state = state.copyWith(isVegetarian: value);
|
||||
await PreferenceHelper.instance.saveIngredientVegetarianFilter(value);
|
||||
}
|
||||
|
||||
Future<void> chooseLanguage(IngredientSearchLanguage value) async {
|
||||
state = state.copyWith(searchLanguage: value);
|
||||
await PreferenceHelper.instance.saveIngredientSearchLanguage(value);
|
||||
}
|
||||
}
|
||||
|
||||
// provider for notifier
|
||||
final ingredientFiltersNotifierProvider =
|
||||
NotifierProvider<IngredientFiltersNotifier, IngredientFilters>(
|
||||
() => IngredientFiltersNotifier(
|
||||
initialVegan: false,
|
||||
initialVegetarian: false,
|
||||
initialSearchLanguage: IngredientSearchLanguage.current,
|
||||
),
|
||||
);
|
||||
@@ -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<IngredientTypeahead> createState() => _IngredientTypeaheadState();
|
||||
}
|
||||
|
||||
class _IngredientTypeaheadState extends State<IngredientTypeahead> {
|
||||
class _IngredientTypeaheadState extends ConsumerState<IngredientTypeahead> {
|
||||
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<IngredientTypeahead> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final filters = ref.watch(ingredientFiltersNotifierProvider);
|
||||
return Column(
|
||||
children: [
|
||||
TypeAheadField<Ingredient>(
|
||||
@@ -154,12 +153,15 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
|
||||
widget.onUpdateSearchQuery(pattern);
|
||||
widget.onDeselectIngredient();
|
||||
|
||||
return Provider.of<NutritionPlansProvider>(context, listen: false).searchIngredient(
|
||||
return legacy_provider.Provider.of<NutritionPlansProvider>(
|
||||
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<IngredientTypeahead> {
|
||||
),
|
||||
onSelected: (suggestion) async {
|
||||
// Cache selected ingredient
|
||||
final provider = Provider.of<NutritionPlansProvider>(context, listen: false);
|
||||
final provider = legacy_provider.Provider.of<NutritionPlansProvider>(
|
||||
context,
|
||||
listen: false,
|
||||
);
|
||||
await provider.cacheIngredient(suggestion);
|
||||
widget.selectIngredient(suggestion.id, suggestion.name, null);
|
||||
},
|
||||
@@ -220,7 +225,7 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => FutureBuilder<Ingredient?>(
|
||||
future: Provider.of<NutritionPlansProvider>(
|
||||
future: legacy_provider.Provider.of<NutritionPlansProvider>(
|
||||
context,
|
||||
listen: false,
|
||||
).searchIngredientWithBarcode(barcode),
|
||||
@@ -234,9 +239,9 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
|
||||
}
|
||||
|
||||
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<IngredientTypeahead> {
|
||||
),
|
||||
];
|
||||
|
||||
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<IngredientTypeahead> {
|
||||
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<IngredientSearchLanguage>(
|
||||
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<IngredientSearchLanguage>(
|
||||
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),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user