Skip to content

Commit

Permalink
feat: 5643 - edit of products of any product type (#5661)
Browse files Browse the repository at this point in the history
Impacted files:
* `add_basic_details_page.dart`: added product type
* `add_new_product_page.dart`: added a "set product type page" for new products; added product type; minor refactoring
* `add_other_details_page.dart`: added product type
* `app_en.arb`: added 1 label per product type, and 4 labels for product type input
* `background_task_barcode.dart`: added product type
* `background_task_crop.dart`: added product type
* `background_task_details.dart`: added product type
* `background_task_hunger_games.dart`: added product type
* `background_task_image.dart`: added product type
* `background_task_refresh_later.dart`: added product type
* `background_task_unselect.dart`: added product type
* `background_task_upload.dart`: added product type
* `edit_new_packagings.dart`: added product type
* `edit_ocr_page.dart`: added product type
* `image_crop_page.dart`: added product type
* `nutrition_page_loaded.dart`: added product type
* `product_crop_helper.dart`: added product type
* `product_dialog_helper.dart`: removed irrelevant nutriscore and ecoscore logos for new found products, as we cannot say already if it's food
* `product_image_button.dart`: added product type
* `product_image_carousel_item.dart`: added product type
* `product_image_crop_button.dart`: added product type
* `product_image_gallery_view.dart`: added product type
* `product_image_local_button.dart`: added product type
* `product_image_unselect_button.dart`: added product type
* `product_image_viewer.dart`: added product type
* `product_query.dart`: localized labels for product type
* `simple_input_page.dart`: added product type
* `simple_input_page_helpers.dart`: added product type
* `up_to_date_changes.dart`: added product type
* `uploaded_image_gallery.dart`: added product type
  • Loading branch information
monsieurtanuki authored Oct 2, 2024
1 parent 5b7db8a commit 899cb0f
Show file tree
Hide file tree
Showing 30 changed files with 210 additions and 65 deletions.
15 changes: 15 additions & 0 deletions packages/smooth_app/lib/background/background_task_barcode.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:flutter/foundation.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/background/background_task.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/pages/product/common/product_refresher.dart';
import 'package:smooth_app/query/product_query.dart';

/// Abstract background task that involves a single barcode.
abstract class BackgroundTaskBarcode extends BackgroundTask {
Expand All @@ -11,20 +13,28 @@ abstract class BackgroundTaskBarcode extends BackgroundTask {
super.language,
required super.stamp,
required this.barcode,
required this.productType,
});

BackgroundTaskBarcode.fromJson(super.json)
: barcode = json[_jsonTagBarcode] as String,
productType =
ProductType.fromOffTag(json[_jsonTagProductType] as String?) ??
// for legacy reason (not refreshed products = no product type)
ProductType.food,
super.fromJson();

final String barcode;
final ProductType productType;

static const String _jsonTagBarcode = 'barcode';
static const String _jsonTagProductType = 'productType';

@override
Map<String, dynamic> toJson() {
final Map<String, dynamic> result = super.toJson();
result[_jsonTagBarcode] = barcode;
result[_jsonTagProductType] = productType.offTag;
return result;
}

Expand All @@ -45,4 +55,9 @@ abstract class BackgroundTaskBarcode extends BackgroundTask {
barcode: barcode,
localDatabase: localDatabase,
);

@override
UriProductHelper get uriProductHelper => ProductQuery.getUriProductHelper(
productType: productType,
);
}
6 changes: 6 additions & 0 deletions packages/smooth_app/lib/background/background_task_crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
required super.processName,
required super.uniqueId,
required super.barcode,
required super.productType,
required super.language,
required super.stamp,
required super.imageField,
Expand Down Expand Up @@ -48,6 +49,7 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
/// Adds the background task about uploading a product image.
static Future<void> addTask(
final String barcode, {
required final ProductType? productType,
required final OpenFoodFactsLanguage language,
required final int imageId,
required final ImageField imageField,
Expand All @@ -67,6 +69,7 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
final BackgroundTaskBarcode task = _getNewTask(
language,
barcode,
productType ?? ProductType.food,
imageId,
imageField,
croppedFile,
Expand Down Expand Up @@ -95,6 +98,7 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
static BackgroundTaskCrop _getNewTask(
final OpenFoodFactsLanguage language,
final String barcode,
final ProductType productType,
final int imageId,
final ImageField imageField,
final File croppedFile,
Expand All @@ -108,6 +112,7 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
BackgroundTaskCrop._(
uniqueId: uniqueId,
barcode: barcode,
productType: productType,
processName: _operationType.processName,
imageId: imageId,
imageField: imageField.offTag,
Expand Down Expand Up @@ -158,6 +163,7 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
await BackgroundTaskRefreshLater.addTask(
barcode,
localDatabase: localDatabase,
productType: productType,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:smooth_app/database/local_database.dart';
///
/// With that stamp, we can de-duplicate similar tasks.
enum BackgroundTaskDetailsStamp {
productType('product_type'),
basicDetails('basic_details'),
otherDetails('other_details'),
ocrIngredients('ocr_ingredients'),
Expand All @@ -38,6 +39,7 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode
required super.processName,
required super.uniqueId,
required super.barcode,
required super.productType,
required super.stamp,
required this.inputMap,
});
Expand Down Expand Up @@ -70,6 +72,7 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode
required final BuildContext context,
required final BackgroundTaskDetailsStamp stamp,
final bool showSnackBar = true,
required final ProductType? productType,
}) async {
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final String uniqueId = await _operationType.getNewKey(
Expand All @@ -80,6 +83,7 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode
minimalistProduct,
uniqueId,
stamp,
productType ?? ProductType.food,
);
if (!context.mounted) {
return;
Expand All @@ -104,11 +108,13 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode
final Product minimalistProduct,
final String uniqueId,
final BackgroundTaskDetailsStamp stamp,
final ProductType productType,
) =>
BackgroundTaskDetails._(
uniqueId: uniqueId,
processName: _operationType.processName,
barcode: minimalistProduct.barcode!,
productType: productType,
inputMap: jsonEncode(minimalistProduct.toJson()),
stamp: getStamp(minimalistProduct.barcode!, stamp.tag),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class BackgroundTaskHungerGames extends BackgroundTaskBarcode {
required super.processName,
required super.uniqueId,
required super.barcode,
required super.productType,
required super.stamp,
required this.insightId,
required this.insightAnnotation,
Expand Down Expand Up @@ -80,6 +81,8 @@ class BackgroundTaskHungerGames extends BackgroundTaskBarcode {
processName: _operationType.processName,
uniqueId: uniqueId,
barcode: barcode,
// not really relevant for Robotoff
productType: ProductType.food,
stamp: _getStamp(barcode, insightId),
insightId: insightId,
insightAnnotation: insightAnnotation,
Expand Down
6 changes: 6 additions & 0 deletions packages/smooth_app/lib/background/background_task_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
required super.processName,
required super.uniqueId,
required super.barcode,
required super.productType,
required super.language,
required super.stamp,
required super.imageField,
Expand Down Expand Up @@ -58,6 +59,7 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
/// Adds the background task about uploading a product image.
static Future<void> addTask(
final String barcode, {
required final ProductType? productType,
required final OpenFoodFactsLanguage language,
required final ImageField imageField,
required final File fullFile,
Expand All @@ -77,6 +79,7 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
final BackgroundTaskBarcode task = _getNewTask(
language,
barcode,
productType ?? ProductType.food,
imageField,
fullFile,
croppedFile,
Expand Down Expand Up @@ -105,6 +108,7 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
static BackgroundTaskImage _getNewTask(
final OpenFoodFactsLanguage language,
final String barcode,
final ProductType productType,
final ImageField imageField,
final File fullFile,
final File croppedFile,
Expand All @@ -118,6 +122,7 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
BackgroundTaskImage._(
uniqueId: uniqueId,
barcode: barcode,
productType: productType,
processName: _operationType.processName,
imageField: imageField.offTag,
fullPath: fullFile.path,
Expand Down Expand Up @@ -176,6 +181,7 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
await BackgroundTaskRefreshLater.addTask(
barcode,
localDatabase: localDatabase,
productType: productType,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/painting.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/background/background_task_barcode.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/local_database.dart';
Expand All @@ -14,6 +15,7 @@ class BackgroundTaskRefreshLater extends BackgroundTaskBarcode {
required super.processName,
required super.uniqueId,
required super.barcode,
required super.productType,
required super.stamp,
required this.timestamp,
});
Expand Down Expand Up @@ -49,12 +51,17 @@ class BackgroundTaskRefreshLater extends BackgroundTaskBarcode {
static Future<void> addTask(
final String barcode, {
required final LocalDatabase localDatabase,
required final ProductType productType,
}) async {
final String uniqueId = await _operationType.getNewKey(
localDatabase,
barcode: barcode,
);
final BackgroundTaskBarcode task = _getNewTask(barcode, uniqueId);
final BackgroundTaskBarcode task = _getNewTask(
barcode,
uniqueId,
productType,
);
await task.addToManager(localDatabase);
}

Expand All @@ -67,11 +74,13 @@ class BackgroundTaskRefreshLater extends BackgroundTaskBarcode {
static BackgroundTaskRefreshLater _getNewTask(
final String barcode,
final String uniqueId,
final ProductType productType,
) =>
BackgroundTaskRefreshLater._(
uniqueId: uniqueId,
processName: _operationType.processName,
barcode: barcode,
productType: productType,
timestamp: LocalDatabase.nowInMillis(),
stamp: _getStamp(barcode),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class BackgroundTaskUnselect extends BackgroundTaskBarcode
required super.uniqueId,
required OpenFoodFactsLanguage super.language,
required super.barcode,
required super.productType,
required super.stamp,
required this.imageField,
});
Expand All @@ -43,6 +44,7 @@ class BackgroundTaskUnselect extends BackgroundTaskBarcode
/// Adds the background task about unselecting a product image.
static Future<void> addTask(
final String barcode, {
required final ProductType? productType,
required final ImageField imageField,
required final BuildContext context,
required final OpenFoodFactsLanguage language,
Expand All @@ -54,6 +56,7 @@ class BackgroundTaskUnselect extends BackgroundTaskBarcode
);
final BackgroundTaskBarcode task = _getNewTask(
barcode,
productType ?? ProductType.food,
imageField,
uniqueId,
language,
Expand All @@ -75,13 +78,15 @@ class BackgroundTaskUnselect extends BackgroundTaskBarcode
/// Returns a new background task about unselecting a product image.
static BackgroundTaskUnselect _getNewTask(
final String barcode,
final ProductType productType,
final ImageField imageField,
final String uniqueId,
final OpenFoodFactsLanguage language,
) =>
BackgroundTaskUnselect._(
uniqueId: uniqueId,
barcode: barcode,
productType: productType,
language: language,
processName: _operationType.processName,
imageField: imageField.offTag,
Expand Down Expand Up @@ -116,6 +121,7 @@ class BackgroundTaskUnselect extends BackgroundTaskBarcode
await BackgroundTaskRefreshLater.addTask(
barcode,
localDatabase: localDatabase,
productType: productType,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract class BackgroundTaskUpload extends BackgroundTaskBarcode
required super.processName,
required super.uniqueId,
required super.barcode,
required super.productType,
required super.language,
required super.stamp,
required this.imageField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class _ProductImageCarouselItemState extends State<ProductImageCarouselItem> {
onPressed: () async => confirmAndUploadNewPicture(
context,
barcode: widget.product.barcode!,
productType: widget.product.productType,
imageField: widget.productImageData.imageField,
language: ProductQuery.getLanguage(),
isLoggedInMandatory: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/smooth_app/lib/data_models/up_to_date_changes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class UpToDateChanges {
/// * [BackgroundTaskDetails]
/// * [BackgroundTaskImage]
Product _overwrite(final Product initial, final Product change) {
if (change.productType != null) {
initial.productType = change.productType;
}
if (change.productName != null) {
initial.productName = change.productName;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,25 @@
}
}
},
"product_type_label_food": "Food",
"product_type_label_beauty": "Personal care",
"product_type_label_pet_food": "Pet food",
"product_type_label_product": "Other",
"product_type_selection_title": "Product type",
"product_type_selection_subtitle": "Select the type of this product",
"product_type_selection_empty": "You need to select a product type first!",
"@product_type_selection_empty": {
"description": "Error message about product type that needs to be set"
},
"product_type_selection_already": "You cannot change the product type ({productType})!",
"@product_type_selection_already": {
"description": "Error message about product type that cannot be set again",
"placeholders": {
"productType": {
"type": "String"
}
}
},
"prices_app_dev_mode_flag": "Shortcut to Prices app on product page",
"prices_app_button": "Go to Prices app",
"prices_generic_title": "Prices",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class UploadedImageGallery extends StatelessWidget {
isLoggedInMandatory: isLoggedInMandatory,
cropHelper: ProductCropAgainHelper(
barcode: barcode,
productType: productType,
imageField: imageField,
imageId: int.parse(rawImage.imgid!),
language: language,
Expand Down
2 changes: 2 additions & 0 deletions packages/smooth_app/lib/pages/image_crop_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ Future<CropParameters?> confirmAndUploadNewPicture(
final BuildContext context, {
required final ImageField imageField,
required final String barcode,
required final ProductType? productType,
required final OpenFoodFactsLanguage language,
required final bool isLoggedInMandatory,
}) async =>
Expand All @@ -266,6 +267,7 @@ Future<CropParameters?> confirmAndUploadNewPicture(
imageField: imageField,
language: language,
barcode: barcode,
productType: productType,
),
isLoggedInMandatory: isLoggedInMandatory,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class _AddBasicDetailsPageState extends State<AddBasicDetailsPage> {
minimalistProduct,
context: context,
stamp: BackgroundTaskDetailsStamp.basicDetails,
productType: _product.productType,
);

return true;
Expand Down
Loading

0 comments on commit 899cb0f

Please sign in to comment.