Skip to content

Commit

Permalink
feat: 3418 - 7 new KP action handled (#3510)
Browse files Browse the repository at this point in the history
Deleted files:
* `add_category_button.dart`
* `add_ingredients_button.dart`

New files:
* `add_ocr_button.dart`: "Add OCR image" button for user contribution.
* `add_simple_input_button.dart`: "Add simple input" button for user contribution.

Impacted files:
* `app_en.arb`: added 6 labels for 6 new buttons
* `knowledge_panel_action_card.dart`: added 7 new action cases for "Add" buttons; refactored in OOP
* `knowledge_panels_builder.dart`: now use generic `AddOcrButton`; refactored around `ProductState`
* `ocr_helper.dart`: added an abstract method for the "Add" button label
* `ocr_ingredients_helper.dart`: implemented the new "Add" button label method
* `ocr_packaging_helper.dart`: implemented the new "Add" button label method
* `simple_input_page_helpers.dart`: added an abstract method for the "Add" button label; implemented it in all offspring classes
* `summary_card.dart`: now use generic `AddSimpleInputButton`; refactored around `ProductState`
  • Loading branch information
monsieurtanuki authored Jan 3, 2023
1 parent bd3f7b8 commit 6321df0
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import 'package:flutter/material.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/smooth_html_widget.dart';
import 'package:smooth_app/pages/product/add_category_button.dart';
import 'package:smooth_app/pages/product/add_ingredients_button.dart';
import 'package:smooth_app/pages/product/add_nutrition_button.dart';
import 'package:smooth_app/pages/product/add_ocr_button.dart';
import 'package:smooth_app/pages/product/add_simple_input_button.dart';
import 'package:smooth_app/pages/product/ocr_helper.dart';
import 'package:smooth_app/pages/product/ocr_ingredients_helper.dart';
import 'package:smooth_app/pages/product/ocr_packaging_helper.dart';
import 'package:smooth_app/pages/product/simple_input_page_helpers.dart';
import 'package:smooth_app/services/smooth_services.dart';

/// "Contribute Actions" for the knowledge panels.
Expand All @@ -14,17 +18,41 @@ class KnowledgePanelActionCard extends StatelessWidget {
final KnowledgePanelActionElement element;
final Product product;

// TODO(monsieurtanuki): move to off-dart's knowledge_panel_element.dart
static const String _ACTION_ADD_ORIGINS = 'add_origins';
static const String _ACTION_ADD_STORES = 'add_stores';
static const String _ACTION_ADD_LABELS = 'add_labels';
static const String _ACTION_ADD_COUNTRIES = 'add_countries';
static const String _ACTION_ADD_PACKAGING_IMAGE = 'add_packaging_image';
static const String _ACTION_ADD_PACKAGING_TEXT = 'add_packaging_text';
static const String _ACTION_ADD_INGREDIENTS_IMAGE = 'add_ingredients_image';

@override
Widget build(BuildContext context) {
final List<Widget> actionWidgets = <Widget>[];
for (final String action in element.actions) {
final AbstractSimpleInputPageHelper? simpleInputPageHelper =
_getSimpleInputPageHelper(action);
if (simpleInputPageHelper != null) {
actionWidgets.add(
AddSimpleInputButton(
product: product,
helper: simpleInputPageHelper,
),
);
continue;
}
final OcrHelper? ocrHelper = _getOcrHelper(action);
if (ocrHelper != null) {
actionWidgets.add(
AddOCRButton(
product: product,
helper: ocrHelper,
),
);
continue;
}
switch (action) {
case KnowledgePanelActionElement.ACTION_ADD_CATEGORIES:
actionWidgets.add(AddCategoryButton(product));
break;
case KnowledgePanelActionElement.ACTION_ADD_INGREDIENTS_TEXT:
actionWidgets.add(AddIngredientsButton(product));
break;
case KnowledgePanelActionElement.ACTION_ADD_NUTRITION_FACTS:
actionWidgets.add(AddNutritionButton(product));
break;
Expand All @@ -42,4 +70,36 @@ class KnowledgePanelActionCard extends StatelessWidget {
],
);
}

AbstractSimpleInputPageHelper? _getSimpleInputPageHelper(
final String action,
) {
switch (action) {
case KnowledgePanelActionElement.ACTION_ADD_CATEGORIES:
return SimpleInputPageCategoryHelper();
case _ACTION_ADD_ORIGINS:
return SimpleInputPageOriginHelper();
case _ACTION_ADD_STORES:
return SimpleInputPageStoreHelper();
case _ACTION_ADD_LABELS:
return SimpleInputPageLabelHelper();
case _ACTION_ADD_COUNTRIES:
return SimpleInputPageCountryHelper();
}
return null;
}

OcrHelper? _getOcrHelper(
final String action,
) {
switch (action) {
case KnowledgePanelActionElement.ACTION_ADD_INGREDIENTS_TEXT:
case _ACTION_ADD_INGREDIENTS_IMAGE:
return OcrIngredientsHelper();
case _ACTION_ADD_PACKAGING_IMAGE:
case _ACTION_ADD_PACKAGING_TEXT:
return OcrPackagingHelper();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/knowledge_panel/knowledge_panels/knowledge_panel_element_card.dart';
import 'package:smooth_app/pages/preferences/user_preferences_dev_mode.dart';
import 'package:smooth_app/pages/product/add_ingredients_button.dart';
import 'package:smooth_app/pages/product/add_nutrition_button.dart';
import 'package:smooth_app/pages/product/add_ocr_button.dart';
import 'package:smooth_app/pages/product/ocr_ingredients_helper.dart';

/// "Knowledge Panel" widget.
class KnowledgePanelWidget extends StatelessWidget {
Expand Down Expand Up @@ -51,8 +52,8 @@ class KnowledgePanelWidget extends StatelessWidget {
}
if (!onboardingMode) {
if (panelId == 'health_card') {
final bool nutritionAddOrUpdate = product.statesTags
?.contains('en:nutrition-facts-to-be-completed') ??
final bool nutritionAddOrUpdate = product.statesTags?.contains(
ProductState.NUTRITION_FACTS_COMPLETED.toBeCompletedTag) ??
false;
if (nutritionAddOrUpdate) {
children.add(AddNutritionButton(product));
Expand All @@ -68,7 +69,12 @@ class KnowledgePanelWidget extends StatelessWidget {
needEditIngredients) {
// When the flag is removed, this should be the following:
// if (product.statesTags?.contains('en:ingredients-to-be-completed') ?? false) {
children.add(AddIngredientsButton(product));
children.add(
AddOCRButton(
product: product,
helper: OcrIngredientsHelper(),
),
);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,14 @@
"description": "Message when a new picture is being uploaded to the server"
},
"score_add_missing_ingredients": "Add missing ingredients",
"score_add_missing_packaging_image": "Add missing packaging image",
"score_add_missing_nutrition_facts": "Add missing nutrition facts",
"score_add_missing_product_category": "Add missing product category",
"score_add_missing_product_countries": "Add missing product countries",
"score_add_missing_product_emb": "Add missing product traceability codes",
"score_add_missing_product_labels": "Add missing product labels",
"score_add_missing_product_origins": "Add missing product origins",
"score_add_missing_product_stores": "Add missing product stores",
"score_update_nutrition_facts": "Update nutrition facts",
"nutrition_page_title": "Nutrition Facts",
"nutrition_page_unspecified": "Nutrition facts are not specified on the product",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/pages/product/common/product_refresher.dart';
import 'package:smooth_app/pages/product/edit_ingredients_page.dart';
import 'package:smooth_app/pages/product/ocr_ingredients_helper.dart';
import 'package:smooth_app/pages/product/ocr_helper.dart';

/// "Add ingredients" button for user contribution.
class AddIngredientsButton extends StatelessWidget {
const AddIngredientsButton(this.product);
/// "Add OCR image" button for user contribution.
class AddOCRButton extends StatelessWidget {
const AddOCRButton({
required this.product,
required this.helper,
});

final Product product;
final OcrHelper helper;

@override
Widget build(BuildContext context) => addPanelButton(
AppLocalizations.of(context).score_add_missing_ingredients,
helper.getAddButtonLabel(AppLocalizations.of(context)),
onPressed: () async {
if (!await ProductRefresher().checkIfLoggedIn(context)) {
return;
Expand All @@ -24,7 +28,7 @@ class AddIngredientsButton extends StatelessWidget {
MaterialPageRoute<void>(
builder: (BuildContext context) => EditOcrPage(
product: product,
helper: OcrIngredientsHelper(),
helper: helper,
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import 'package:smooth_app/pages/product/common/product_refresher.dart';
import 'package:smooth_app/pages/product/simple_input_page.dart';
import 'package:smooth_app/pages/product/simple_input_page_helpers.dart';

/// "Add category" button for user contribution.
class AddCategoryButton extends StatelessWidget {
const AddCategoryButton(this.product);
/// "Add simple input" button for user contribution.
class AddSimpleInputButton extends StatelessWidget {
const AddSimpleInputButton({
required this.product,
required this.helper,
});

final Product product;
final AbstractSimpleInputPageHelper helper;

@override
Widget build(BuildContext context) => addPanelButton(
AppLocalizations.of(context).score_add_missing_product_category,
helper.getAddButtonLabel(AppLocalizations.of(context)),
onPressed: () async {
if (!await ProductRefresher().checkIfLoggedIn(context)) {
return;
Expand All @@ -23,7 +27,7 @@ class AddCategoryButton extends StatelessWidget {
context,
MaterialPageRoute<void>(
builder: (BuildContext context) => SimpleInputPage(
helper: SimpleInputPageCategoryHelper(),
helper: helper,
product: product,
),
fullscreenDialog: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/smooth_app/lib/pages/product/ocr_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ abstract class OcrHelper {
/// Returns the page title.
String getTitle(final AppLocalizations appLocalizations);

/// Returns the label of the corresponding "add" button.
String getAddButtonLabel(final AppLocalizations appLocalizations);

/// Returns the image field we try to run OCR on.
ImageField getImageField();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class OcrIngredientsHelper extends OcrHelper {
String getTitle(final AppLocalizations appLocalizations) =>
appLocalizations.ingredients_editing_title;

@override
String getAddButtonLabel(final AppLocalizations appLocalizations) =>
appLocalizations.score_add_missing_ingredients;

@override
ImageField getImageField() => ImageField.INGREDIENTS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class OcrPackagingHelper extends OcrHelper {
String getTitle(final AppLocalizations appLocalizations) =>
appLocalizations.packaging_editing_title;

@override
String getAddButtonLabel(final AppLocalizations appLocalizations) =>
appLocalizations.score_add_missing_packaging_image;

@override
ImageField getImageField() => ImageField.PACKAGING;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ abstract class AbstractSimpleInputPageHelper extends ChangeNotifier {
/// Returns the subtitle on the main "edit product" page.
String? getSubtitle(final AppLocalizations appLocalizations) => null;

/// Returns the label of the corresponding "add" button.
String getAddButtonLabel(final AppLocalizations appLocalizations);

/// Returns the hint of the "add" text field.
String getAddHint(final AppLocalizations appLocalizations);

Expand Down Expand Up @@ -141,6 +144,10 @@ class SimpleInputPageStoreHelper extends AbstractSimpleInputPageHelper {
String getTitle(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_stores_title;

@override
String getAddButtonLabel(final AppLocalizations appLocalizations) =>
appLocalizations.score_add_missing_product_stores;

@override
String getAddHint(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_stores_hint;
Expand All @@ -165,6 +172,10 @@ class SimpleInputPageOriginHelper extends AbstractSimpleInputPageHelper {
String getTitle(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_origins_title;

@override
String getAddButtonLabel(final AppLocalizations appLocalizations) =>
appLocalizations.score_add_missing_product_origins;

@override
String getAddHint(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_origins_hint;
Expand Down Expand Up @@ -195,6 +206,10 @@ class SimpleInputPageEmbCodeHelper extends AbstractSimpleInputPageHelper {
String getTitle(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_emb_codes_title;

@override
String getAddButtonLabel(final AppLocalizations appLocalizations) =>
appLocalizations.score_add_missing_product_emb;

@override
String getAddHint(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_emb_codes_hint;
Expand Down Expand Up @@ -233,6 +248,10 @@ class SimpleInputPageLabelHelper extends AbstractSimpleInputPageHelper {
String getSubtitle(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_labels_subtitle;

@override
String getAddButtonLabel(final AppLocalizations appLocalizations) =>
appLocalizations.score_add_missing_product_labels;

@override
String getAddHint(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_labels_hint;
Expand Down Expand Up @@ -263,6 +282,10 @@ class SimpleInputPageCategoryHelper extends AbstractSimpleInputPageHelper {
String getTitle(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_categories_title;

@override
String getAddButtonLabel(final AppLocalizations appLocalizations) =>
appLocalizations.score_add_missing_product_category;

@override
String? getAddExplanations(final AppLocalizations appLocalizations) =>
'${appLocalizations.edit_product_form_item_categories_explainer_1}'
Expand Down Expand Up @@ -301,6 +324,10 @@ class SimpleInputPageCountryHelper extends AbstractSimpleInputPageHelper {
String getTitle(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_countries_title;

@override
String getAddButtonLabel(final AppLocalizations appLocalizations) =>
appLocalizations.score_add_missing_product_countries;

@override
String getAddHint(final AppLocalizations appLocalizations) =>
appLocalizations.edit_product_form_item_countries_hint;
Expand Down
19 changes: 14 additions & 5 deletions packages/smooth_app/lib/pages/product/summary_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import 'package:smooth_app/knowledge_panel/knowledge_panels/knowledge_panel_page
import 'package:smooth_app/knowledge_panel/knowledge_panels_builder.dart';
import 'package:smooth_app/pages/preferences/user_preferences_page.dart';
import 'package:smooth_app/pages/product/add_basic_details_page.dart';
import 'package:smooth_app/pages/product/add_category_button.dart';
import 'package:smooth_app/pages/product/add_simple_input_button.dart';
import 'package:smooth_app/pages/product/common/product_query_page_helper.dart';
import 'package:smooth_app/pages/product/hideable_container.dart';
import 'package:smooth_app/pages/product/product_questions_widget.dart';
import 'package:smooth_app/pages/product/simple_input_page_helpers.dart';
import 'package:smooth_app/query/category_product_query.dart';
import 'package:smooth_app/query/product_query.dart';

Expand Down Expand Up @@ -293,8 +294,14 @@ class _SummaryCardState extends State<SummaryCard> {

if (widget.isFullVersion) {
// Complete category
if (statesTags.contains('en:categories-to-be-completed')) {
summaryCardButtons.add(AddCategoryButton(_product));
if (statesTags
.contains(ProductState.CATEGORIES_COMPLETED.toBeCompletedTag)) {
summaryCardButtons.add(
AddSimpleInputButton(
product: _product,
helper: SimpleInputPageCategoryHelper(),
),
);
}

// Compare to category
Expand All @@ -314,8 +321,10 @@ class _SummaryCardState extends State<SummaryCard> {
}

// Complete basic details
if (statesTags.contains('en:product-name-to-be-completed') ||
statesTags.contains('en:quantity-to-be-completed')) {
if (statesTags
.contains(ProductState.PRODUCT_NAME_COMPLETED.toBeCompletedTag) ||
statesTags
.contains(ProductState.QUANTITY_COMPLETED.toBeCompletedTag)) {
summaryCardButtons.add(
addPanelButton(
localizations.completed_basic_details_btn_text,
Expand Down

0 comments on commit 6321df0

Please sign in to comment.