Skip to content

Commit

Permalink
feat: 3526 - added "add extra photo" button for both edit packaging p…
Browse files Browse the repository at this point in the history
…ages (#3560)

Impacted files:
* `app_en.arb`: added a label for the new "add extra packaging photo" button
* `edit_new_packagings.dart`: added an "add extra packaging photo" button
* `ocr_helper.dart`: added an "has add extra photo?" method
* `ocr_ingredients_helper.dart`: implemented the new "has add extra photo?" method (`false`)
* `ocr_packaging_helper.dart`: implemented the new "has add extra photo?" method (`true`)
* `ocr_widget.dart`: added an "add extra packaging photo" button; refactored as `StatefulWidget` as needed by new button
  • Loading branch information
monsieurtanuki authored Jan 14, 2023
1 parent 21dfb29 commit 9f93cee
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
4 changes: 4 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,10 @@
"@add_photo_button_label": {
"description": "Label for the add photo button"
},
"add_packaging_photo_button_label": "Take photos of any packaging/recycling information",
"@add_packaging_photo_button_label": {
"description": "Label for the add PACKAGING photo button"
},
"choose_image_source_title": "Choose image source",
"@choose_image_source_title": {
"description": "Title for the image source chooser"
Expand Down
32 changes: 28 additions & 4 deletions packages/smooth_app/lib/pages/product/edit_new_packagings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_card.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/pages/image_crop_page.dart';
import 'package:smooth_app/pages/product/explanation_widget.dart';
import 'package:smooth_app/pages/product/may_exit_page_helper.dart';
import 'package:smooth_app/pages/product/simple_input_widget.dart';
Expand Down Expand Up @@ -169,15 +171,37 @@ class _EditNewPackagingsState extends State<EditNewPackagings> {
);
children.add(
Padding(
padding: const EdgeInsets.all(VERY_LARGE_SPACE),
child: ElevatedButton.icon(
label: Text(appLocalizations.edit_packagings_element_add),
icon: const Icon(Icons.add),
padding: const EdgeInsets.only(
top: VERY_LARGE_SPACE,
left: SMALL_SPACE,
right: SMALL_SPACE,
),
child: addPanelButton(
appLocalizations.edit_packagings_element_add.toUpperCase(),
iconData: Icons.add,
onPressed: () =>
setState(() => _addPackagingToControllers(ProductPackaging())),
),
),
);
children.add(
Padding(
padding: const EdgeInsets.only(
bottom: VERY_LARGE_SPACE,
left: SMALL_SPACE,
right: SMALL_SPACE,
),
child: addPanelButton(
appLocalizations.add_packaging_photo_button_label.toUpperCase(),
onPressed: () async => confirmAndUploadNewPicture(
this,
imageField: ImageField.OTHER,
barcode: widget.product.barcode!,
),
iconData: Icons.add_a_photo,
),
),
);
children.add(
Padding(
padding: const EdgeInsets.symmetric(horizontal: SMALL_SPACE),
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 @@ -49,6 +49,9 @@ abstract class OcrHelper {
/// Stamp to identify similar updates on the same product.
BackgroundTaskDetailsStamp getStamp();

/// Returns true if we need to put an "add extra photos" button.
bool hasAddExtraPhotoButton();

@protected
OpenFoodFactsLanguage getLanguage() => ProductQuery.getLanguage()!;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ class OcrIngredientsHelper extends OcrHelper {
@override
BackgroundTaskDetailsStamp getStamp() =>
BackgroundTaskDetailsStamp.ocrIngredients;

@override
bool hasAddExtraPhotoButton() => false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,7 @@ class OcrPackagingHelper extends OcrHelper {
@override
BackgroundTaskDetailsStamp getStamp() =>
BackgroundTaskDetailsStamp.ocrPackaging;

@override
bool hasAddExtraPhotoButton() => true;
}
59 changes: 43 additions & 16 deletions packages/smooth_app/lib/pages/product/ocr_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import 'package:smooth_app/data_models/product_image_data.dart';
import 'package:smooth_app/database/transient_file.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/pages/image_crop_page.dart';
import 'package:smooth_app/pages/product/explanation_widget.dart';
import 'package:smooth_app/pages/product/ocr_helper.dart';

/// Widget dedicated to OCR, with 3 actions: upload image, extract data, save.
class OcrWidget extends StatelessWidget {
///
/// Potential extra action: add extra photos.
class OcrWidget extends StatefulWidget {
const OcrWidget({
required this.controller,
required this.onSubmitField,
Expand All @@ -28,6 +32,11 @@ class OcrWidget extends StatelessWidget {
final Product product;
final OcrHelper helper;

@override
State<OcrWidget> createState() => _OcrWidgetState();
}

class _OcrWidgetState extends State<OcrWidget> {
@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
Expand All @@ -49,12 +58,12 @@ class OcrWidget extends StatelessWidget {
child: SmoothActionButtonsBar(
positiveAction: SmoothActionButton(
text: (TransientFile.isImageAvailable(
productImageData,
product.barcode!,
widget.productImageData,
widget.product.barcode!,
))
? helper.getActionRefreshPhoto(appLocalizations)
? widget.helper.getActionRefreshPhoto(appLocalizations)
: appLocalizations.upload_image,
onPressed: () async => onTapNewImage(),
onPressed: () async => widget.onTapNewImage(),
),
),
),
Expand All @@ -75,24 +84,25 @@ class OcrWidget extends StatelessWidget {
child: Column(
children: <Widget>[
if (TransientFile.isServerImage(
productImageData,
product.barcode!,
widget.productImageData,
widget.product.barcode!,
))
SmoothActionButtonsBar.single(
action: SmoothActionButton(
text: helper.getActionExtractText(appLocalizations),
onPressed: () async => onTapExtractData(),
text: widget.helper
.getActionExtractText(appLocalizations),
onPressed: () async => widget.onTapExtractData(),
),
)
else if (TransientFile.isImageAvailable(
productImageData,
product.barcode!,
widget.productImageData,
widget.product.barcode!,
))
// TODO(monsieurtanuki): what if slow upload? text instead?
const CircularProgressIndicator.adaptive(),
const SizedBox(height: MEDIUM_SPACE),
TextField(
controller: controller,
controller: widget.controller,
decoration: InputDecoration(
fillColor: Colors.white.withOpacity(0.2),
filled: true,
Expand All @@ -103,12 +113,26 @@ class OcrWidget extends StatelessWidget {
maxLines: null,
textInputAction: TextInputAction.done,
onSubmitted: (_) =>
onSubmitField(helper.getImageField()),
widget.onSubmitField(widget.helper.getImageField()),
),
const SizedBox(height: SMALL_SPACE),
ExplanationWidget(
helper.getInstructions(appLocalizations),
widget.helper.getInstructions(appLocalizations),
),
if (widget.helper.hasAddExtraPhotoButton())
Padding(
padding: const EdgeInsets.only(top: SMALL_SPACE),
child: addPanelButton(
appLocalizations.add_packaging_photo_button_label
.toUpperCase(),
onPressed: () async => confirmAndUploadNewPicture(
this,
imageField: ImageField.OTHER,
barcode: widget.product.barcode!,
),
iconData: Icons.add_a_photo,
),
),
const SizedBox(height: MEDIUM_SPACE),
SmoothActionButtonsBar(
axis: Axis.horizontal,
Expand All @@ -119,8 +143,11 @@ class OcrWidget extends StatelessWidget {
positiveAction: SmoothActionButton(
text: appLocalizations.save,
onPressed: () async {
await onSubmitField(helper.getImageField());
////ignore: use_build_context_synchronously
await widget
.onSubmitField(widget.helper.getImageField());
if (!mounted) {
return;
}
Navigator.pop(context);
},
),
Expand Down

0 comments on commit 9f93cee

Please sign in to comment.