Skip to content

Commit

Permalink
feat: #1060 - added an "exclude ecoscore" switch in dev mode (#1510)
Browse files Browse the repository at this point in the history
Impacted files:
* `product_cards_helper.dart`: added the exclusion of "excluded attributes"
* `smooth_matched_product.dart`: added the exclusion of "excluded attributes"
* `smooth_product_card_found.dart`: added the exclusion of "excluded attributes"
* `summary_card.dart`: added the exclusion of "excluded attributes"
* `user_preferences.dart`: added the list of "excluded attributes"
* `user_preferences_attribute_group.dart`: added the exclusion of "excluded attributes"
* `user_preferences_dev_mode.dart`: added a switch for "exclude ecoscore"; refactored
  • Loading branch information
monsieurtanuki authored Apr 7, 2022
1 parent 9bbb406 commit 2b6b0b1
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,27 @@ class SmoothProductCardFound extends StatelessWidget {
@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context)!;
final UserPreferences userPreferences = context.watch<UserPreferences>();
final ProductPreferences productPreferences =
context.watch<ProductPreferences>();
final Size screenSize = MediaQuery.of(context).size;

final List<String> excludedAttributeIds =
userPreferences.getExcludedAttributeIds();
final List<Widget> scores = <Widget>[];
final double iconSize = IconWidgetSizer.getIconSizeFromContext(context);
final List<Attribute> attributes =
getPopulatedAttributes(product, SCORE_ATTRIBUTE_IDS);
final List<Attribute> attributes = getPopulatedAttributes(
product,
SCORE_ATTRIBUTE_IDS,
excludedAttributeIds,
);
for (final Attribute attribute in attributes) {
scores.add(SvgIconChip(attribute.iconUrl!, height: iconSize));
}
final MatchedProduct matchedProduct = MatchedProduct.getMatchedProduct(
product,
context.watch<ProductPreferences>(),
context.watch<UserPreferences>(),
productPreferences,
userPreferences,
);
final ProductCompatibilityHelper helper =
ProductCompatibilityHelper(matchedProduct);
Expand Down
8 changes: 8 additions & 0 deletions packages/smooth_app/lib/data_models/user_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class UserPreferences extends ChangeNotifier {
static const String _TAG_CAMERA_DECLINE = 'declined_camera_use_once';
static const String _TAG_CRASH_REPORTS = 'crash_reports';
static const String _TAG_ANALYTICS_REPORTS = 'analytics_reports';
static const String _TAG_EXCLUDED_ATTRIBUTE_IDS = 'excluded_attributes';

Future<void> init(final ProductPreferences productPreferences) async {
if (_sharedPreferences.getBool(_TAG_INIT) != null) {
Expand Down Expand Up @@ -109,6 +110,13 @@ class UserPreferences extends ChangeNotifier {
bool? getFlag(final String key) =>
_sharedPreferences.getBool(_getFlagTag(key));

List<String> getExcludedAttributeIds() =>
_sharedPreferences.getStringList(_TAG_EXCLUDED_ATTRIBUTE_IDS) ??
<String>[];

Future<void> setExcludedAttributeIds(final List<String> value) async =>
_sharedPreferences.setStringList(_TAG_EXCLUDED_ATTRIBUTE_IDS, value);

Future<void> setDevMode(final int value) async =>
_sharedPreferences.setInt(_TAG_DEV_MODE, value);

Expand Down
4 changes: 4 additions & 0 deletions packages/smooth_app/lib/helpers/product_cards_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ Widget buildProductSmoothCard({
List<Attribute> getPopulatedAttributes(
final Product product,
final List<String> attributeIds,
final List<String> excludedAttributeIds,
) {
final List<Attribute> result = <Attribute>[];
final Map<String, Attribute> attributes = product.getAttributes(attributeIds);
for (final String attributeId in attributeIds) {
if (excludedAttributeIds.contains(attributeId)) {
continue;
}
Attribute? attribute = attributes[attributeId];
// Some attributes selected in the user preferences might be unavailable for some products
if (attribute == null) {
Expand Down
33 changes: 27 additions & 6 deletions packages/smooth_app/lib/helpers/smooth_matched_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,25 @@ abstract class MatchedProduct {
final Product product,
final ProductPreferencesManager productPreferencesManager,
final UserPreferences userPreferences,
) =>
userPreferences.getFlag(
UserPreferencesDevMode.userPreferencesFlagLenientMatching) ??
false
? _StrongMatchedProduct(product, productPreferencesManager)
: _LenientMatchedProduct(product, productPreferencesManager);
) {
final List<String> excludedAttributeIds =
userPreferences.getExcludedAttributeIds();
if (userPreferences.getFlag(
UserPreferencesDevMode.userPreferencesFlagStrongMatching,
) ??
false) {
return _StrongMatchedProduct(
product,
productPreferencesManager,
excludedAttributeIds,
);
}
return _LenientMatchedProduct(
product,
productPreferencesManager,
excludedAttributeIds,
);
}

final Product product;
double get score;
Expand Down Expand Up @@ -61,6 +74,7 @@ class _StrongMatchedProduct extends MatchedProduct {
_StrongMatchedProduct(
final Product product,
final ProductPreferencesManager productPreferencesManager,
final List<String> excludedAttributeIds,
) : super(product) {
final List<AttributeGroup>? attributeGroups = product.attributeGroups;
if (attributeGroups == null) {
Expand All @@ -71,6 +85,9 @@ class _StrongMatchedProduct extends MatchedProduct {
for (final AttributeGroup group in attributeGroups) {
if (group.attributes != null) {
for (final Attribute attribute in group.attributes!) {
if (excludedAttributeIds.contains(attribute.id)) {
continue;
}
final PreferenceImportance? preferenceImportance =
productPreferencesManager.getPreferenceImportanceFromImportanceId(
productPreferencesManager.getImportanceIdForAttributeId(
Expand Down Expand Up @@ -126,13 +143,17 @@ class _LenientMatchedProduct extends MatchedProduct {
_LenientMatchedProduct(
final Product product,
final ProductPreferencesManager productPreferencesManager,
final List<String> excludedAttributeIds,
) : super(product) {
_score = 0.0;
int numAttributesComputed = 0;
if (product.attributeGroups != null) {
for (final AttributeGroup group in product.attributeGroups!) {
if (group.attributes != null) {
for (final Attribute attribute in group.attributes!) {
if (excludedAttributeIds.contains(attribute.id)) {
continue;
}
final String importanceLevel = productPreferencesManager
.getImportanceIdForAttributeId(attribute.id!);
// Check whether any mandatory attribute is incompatible
Expand Down
11 changes: 9 additions & 2 deletions packages/smooth_app/lib/pages/product/summary_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,15 @@ class _SummaryCardState extends State<SummaryCard> {
Widget _buildSummaryCardContent(BuildContext context) {
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final AppLocalizations localizations = AppLocalizations.of(context)!;
final List<Attribute> scoreAttributes =
getPopulatedAttributes(widget._product, SCORE_ATTRIBUTE_IDS);
final UserPreferences userPreferences = context.read<UserPreferences>();

final List<String> excludedAttributeIds =
userPreferences.getExcludedAttributeIds();
final List<Attribute> scoreAttributes = getPopulatedAttributes(
widget._product,
SCORE_ATTRIBUTE_IDS,
excludedAttributeIds,
);

// Header takes 1 row.
// Product Title Tile takes 2 rows to render.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ class UserPreferencesAttributeGroup extends AbstractUserPreferences {
),
);
}
final List<Attribute> attributes = group.attributes!;
result.addAll(
List<Widget>.generate(
attributes.length,
(int index) => AttributeButton(attributes[index], productPreferences),
),
);
final List<String> excludedAttributeIds =
userPreferences.getExcludedAttributeIds();
for (final Attribute attribute in group.attributes!) {
if (excludedAttributeIds.contains(attribute.id)) {
continue;
}
result.add(AttributeButton(attribute, productPreferences));
}
return result;
}
}
25 changes: 21 additions & 4 deletions packages/smooth_app/lib/pages/user_preferences_dev_mode.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/Attribute.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/product_list.dart';
Expand Down Expand Up @@ -37,7 +38,7 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
static const String userPreferencesFlagProd = '__devWorkingOnProd';
static const String userPreferencesTestEnvHost = '__testEnvHost';
static const String userPreferencesFlagUseMLKit = '__useMLKit';
static const String userPreferencesFlagLenientMatching = '__lenientMatching';
static const String userPreferencesFlagStrongMatching = '__lenientMatching';
static const String userPreferencesFlagAdditionalButton =
'__additionalButtonOnProductPage';
static const String userPreferencesFlagEditIngredients = '__editIngredients';
Expand Down Expand Up @@ -189,12 +190,12 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
title: const Text('Switch between strong and lenient matching'),
subtitle: Text(
'Current matching level is '
'${(userPreferences.getFlag(userPreferencesFlagLenientMatching) ?? false) ? 'strong' : 'lenient'}',
'${(userPreferences.getFlag(userPreferencesFlagStrongMatching) ?? false) ? 'strong' : 'lenient'}',
),
onTap: () async {
await userPreferences.setFlag(
userPreferencesFlagLenientMatching,
!(userPreferences.getFlag(userPreferencesFlagLenientMatching) ??
userPreferencesFlagStrongMatching,
!(userPreferences.getFlag(userPreferencesFlagStrongMatching) ??
false));
setState(() {});
},
Expand Down Expand Up @@ -243,6 +244,22 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
}
},
),
SwitchListTile(
title: const Text('Exclude ecoscore'),
value: userPreferences
.getExcludedAttributeIds()
.contains(Attribute.ATTRIBUTE_ECOSCORE),
onChanged: (bool value) async {
const String tag = Attribute.ATTRIBUTE_ECOSCORE;
final List<String> list = userPreferences.getExcludedAttributeIds();
list.removeWhere((final String element) => element == tag);
if (value) {
list.add(tag);
}
await userPreferences.setExcludedAttributeIds(list);
setState(() {});
},
),
];

Future<void> _changeTestEnvHost() async {
Expand Down

0 comments on commit 2b6b0b1

Please sign in to comment.