diff --git a/lib/model/Product.dart b/lib/model/Product.dart index c461714419..5c0e312b42 100644 --- a/lib/model/Product.dart +++ b/lib/model/Product.dart @@ -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 @@ -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 getProductImprovements() { + final Set 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; + } } diff --git a/test/api_getProduct_test.dart b/test/api_getProduct_test.dart index 41a48c23b2..b8687c4c5f 100644 --- a/test/api_getProduct_test.dart +++ b/test/api_getProduct_test.dart @@ -662,6 +662,13 @@ void main() { group = result.product!.attributeGroups! .singleWhere((element) => element.id == 'labels'); + + final Set 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; ', @@ -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 improvements = + result.product!.getProductImprovements(); + expect(improvements.contains(ProductImprovement.LABELS_TO_BE_COMPLETED), + false); + expect(improvements.contains(ProductImprovement.ORIGINS_TO_BE_COMPLETED), + true); }); test(