Skip to content

Commit

Permalink
feature/openfoodfacts#191 - added and computed possible product impro…
Browse files Browse the repository at this point in the history
…vements

Impacted files:
* `api_getProduct_test.dart`: added checks for `getProductImprovements`
* `Product.dart`: create `enum ProductImprovement` and `Product.getProductImprovements()`
  • Loading branch information
monsieurtanuki committed Aug 20, 2021
1 parent 29e22ef commit e20fa4d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/model/Product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ import 'Nutriments.dart';

part 'Product.g.dart';

/// Possible improvements on a [Product] given its current data
enum ProductImprovement {
/// * Possible message:
/// “The Eco-Score takes into account environmental labels. Please take them
/// into photo or edit the product so that they can be taken into account.”
/// * Asking your users for a photo should be enough.
/// * You can otherwise add toggles for explicit labels (please add a photo
/// of them to avoid mistakes)
LABELS_TO_BE_COMPLETED,

/// Possible message:
/// “The Eco-Score takes into account the origins of the ingredients.
/// Please take them into a photo (ingredient list and/or any geographic claim
/// or edit the product so that they can be taken into account. If it is not
/// clear, you can contact the food producer.”
ORIGINS_TO_BE_COMPLETED,
}

/// This class contains most of the data about a specific product.
///
/// Please read the language mechanics explanation if you intend to display
Expand Down Expand Up @@ -461,4 +479,23 @@ class Product extends JsonObject {
}
return result;
}

/// Returns all the potential improvements given the quality of the data
///
/// For apps with contribution mode.
/// A typical use-case is to alert the end-users that they can improve
/// the quality of the OFF data by taking pictures (or something like that),
/// when displaying a [Product].
Set<ProductImprovement> getProductImprovements() {
final Set<ProductImprovement> result = {};
if (statesTags != null) {
if (statesTags!.contains('en:labels-to-be-completed')) {
result.add(ProductImprovement.LABELS_TO_BE_COMPLETED);
}
if (statesTags!.contains('en:origins-to-be-completed')) {
result.add(ProductImprovement.ORIGINS_TO_BE_COMPLETED);
}
}
return result;
}
}
14 changes: 14 additions & 0 deletions test/api_getProduct_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,13 @@ void main() {

group = result.product!.attributeGroups!
.singleWhere((element) => element.id == 'labels');

final Set<ProductImprovement> improvements =
result.product!.getProductImprovements();
expect(improvements.contains(ProductImprovement.LABELS_TO_BE_COMPLETED),
false);
expect(improvements.contains(ProductImprovement.ORIGINS_TO_BE_COMPLETED),
false);
});

test('get product without setting OpenFoodFactsLanguage or ProductField; ',
Expand Down Expand Up @@ -818,6 +825,13 @@ void main() {
image.language == OpenFoodFactsLanguage.GERMAN)
.url,
'https://static.openfoodfacts.net/images/products/500/011/254/8167/ingredients_de.7.400.jpg');

final Set<ProductImprovement> improvements =
result.product!.getProductImprovements();
expect(improvements.contains(ProductImprovement.LABELS_TO_BE_COMPLETED),
false);
expect(improvements.contains(ProductImprovement.ORIGINS_TO_BE_COMPLETED),
true);
});

test(
Expand Down

0 comments on commit e20fa4d

Please sign in to comment.