Skip to content

Commit

Permalink
feat: 2484 - now when editing we go to the full image with the croppe…
Browse files Browse the repository at this point in the history
…d area on top

Impacted files:
* `app_en.arb`: added a label for the "send image" button (instead of vague "confirm")
* `background_task_crop.dart`: added a product change where we store the crop parameters
* `background_task_image.dart`: added a fake product change where we remove the crop parameters
* `new_crop_page.dart`: added constructor parameters `initialCropRect` and `initialRotation`; removed the "new photo" button for UI/UX consistency
* `product_image_viewer.dart`: refactored with 2 or 4 buttons and cleaner code
* `product_query.dart`: added `IMAGES` product field
* `pubspec.lock`: wtf
* `pubspec.yaml`: upgraded `openfoodfacts` to `2.2.1` (about `ProductImage`)
* `rotation.dart`: added method `fromDegrees`
* `up_to_date_changes.dart`: added `images` to "overwritable" product fields; now accepts all operation types
  • Loading branch information
monsieurtanuki committed Jan 30, 2023
1 parent 7121b0e commit 326bcc1
Show file tree
Hide file tree
Showing 10 changed files with 392 additions and 193 deletions.
59 changes: 41 additions & 18 deletions packages/smooth_app/lib/background/background_task_crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,44 @@ class BackgroundTaskCrop extends AbstractBackgroundTask {
);

@override
Future<void> preExecute(final LocalDatabase localDatabase) async =>
TransientFile.putImage(
ImageField.fromOffTag(imageField)!,
barcode,
localDatabase,
File(croppedPath),
Future<void> preExecute(final LocalDatabase localDatabase) async {
await localDatabase.upToDate.addChange(
uniqueId,
Product(
barcode: barcode,
images: <ProductImage>[_getProductImage()],
),
);
TransientFile.putImage(
ImageField.fromOffTag(imageField)!,
barcode,
localDatabase,
File(croppedPath),
);
}

/// Returns the actual crop parameters.
///
/// cf. [UpToDateChanges._overwrite] regarding `images` field.
ProductImage _getProductImage() => ProductImage(
field: ImageField.fromOffTag(imageField)!,
language: getLanguage(),
size: ImageSize.ORIGINAL,
angle: ImageAngleExtension.fromInt(rotationDegrees),
imgid: '$imageId',
x1: cropX1,
y1: cropY1,
x2: cropX2,
y2: cropY2,
coordinatesImageSize: ImageSize.ORIGINAL.number,
);

@override
Future<void> postExecute(
final LocalDatabase localDatabase,
final bool success,
) async {
localDatabase.upToDate.terminate(uniqueId);
try {
File(croppedPath).deleteSync();
} catch (e) {
Expand All @@ -206,20 +231,18 @@ class BackgroundTaskCrop extends AbstractBackgroundTask {
/// Uploads the product image.
@override
Future<void> upload() async {
final ImageField imageField = ImageField.fromOffTag(this.imageField)!;
final OpenFoodFactsLanguage language = getLanguage();
final User user = getUser();
final ProductImage productImage = _getProductImage();
final String? imageUrl = await OpenFoodAPIClient.setProductImageCrop(
barcode: barcode,
imageField: imageField,
language: language,
imgid: '$imageId',
angle: ImageAngleExtension.fromInt(rotationDegrees)!,
x1: cropX1,
y1: cropY1,
x2: cropX2,
y2: cropY2,
user: user,
imageField: productImage.field,
language: getLanguage(),
imgid: productImage.imgid!,
angle: productImage.angle!,
x1: productImage.x1!,
y1: productImage.y1!,
x2: productImage.x2!,
y2: productImage.y2!,
user: getUser(),
);
if (imageUrl == null) {
throw Exception('Could not select picture');
Expand Down
34 changes: 28 additions & 6 deletions packages/smooth_app/lib/background/background_task_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:provider/provider.dart';
import 'package:smooth_app/background/abstract_background_task.dart';
import 'package:smooth_app/background/background_task_refresh_later.dart';
import 'package:smooth_app/data_models/operation_type.dart';
import 'package:smooth_app/data_models/up_to_date_changes.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/database/transient_file.dart';
import 'package:smooth_app/query/product_query.dart';
Expand Down Expand Up @@ -192,12 +193,32 @@ class BackgroundTaskImage extends AbstractBackgroundTask {
stamp.contains(';image;${ImageField.OTHER.offTag};');

@override
Future<void> preExecute(final LocalDatabase localDatabase) async =>
TransientFile.putImage(
ImageField.fromOffTag(imageField)!,
barcode,
localDatabase,
File(croppedPath),
Future<void> preExecute(final LocalDatabase localDatabase) async {
await localDatabase.upToDate.addChange(
uniqueId,
Product(
barcode: barcode,
images: <ProductImage>[_getProductImage()],
),
);
TransientFile.putImage(
ImageField.fromOffTag(imageField)!,
barcode,
localDatabase,
File(croppedPath),
);
}

/// Returns a fake value that means: "remove the previous value when merging".
///
/// If we use this task, it means that we took a brand new picture. Therefore,
/// all previous crop parameters are attached to a different imageid, and
/// to avoid confusion we need to clear them.
/// cf. [UpToDateChanges._overwrite] regarding `images` field.
ProductImage _getProductImage() => ProductImage(
field: ImageField.fromOffTag(imageField)!,
language: getLanguage(),
size: ImageSize.ORIGINAL,
);

// TODO(monsieurtanuki): we may also need to remove old files that were not removed from some reason
Expand All @@ -206,6 +227,7 @@ class BackgroundTaskImage extends AbstractBackgroundTask {
final LocalDatabase localDatabase,
final bool success,
) async {
localDatabase.upToDate.terminate(uniqueId);
try {
File(fullPath).deleteSync();
} catch (e) {
Expand Down
33 changes: 26 additions & 7 deletions packages/smooth_app/lib/data_models/up_to_date_changes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ class UpToDateChanges {
/// Returns all the actions related to a barcode, sorted by id.
Iterable<TransientOperation> getSortedOperations(final String barcode) {
final List<TransientOperation> result = <TransientOperation>[];
for (final TransientOperation transientProduct
in _daoTransientProduct.getAll(barcode)) {
if (OperationType.details.matches(transientProduct) ||
OperationType.unselect.matches(transientProduct)) {
result.add(transientProduct);
}
}
result.addAll(_daoTransientProduct.getAll(barcode));
result.sort(OperationType.sort);
return result;
}
Expand Down Expand Up @@ -160,6 +154,31 @@ class UpToDateChanges {
change.imagePackagingSmallUrl!,
);
}
if (change.images != null && change.images!.isNotEmpty) {
initial.images ??= <ProductImage>[];
// let's remove similar entries first
final Set<int> removeIndices = <int>{};
for (final ProductImage changeProductImage in change.images!) {
int i = 0;
for (final ProductImage initialProductImage in initial.images!) {
if (changeProductImage.field == initialProductImage.field &&
changeProductImage.size == initialProductImage.size &&
changeProductImage.language == initialProductImage.language) {
removeIndices.add(i);
}
i++;
}
}
final List<int> sorted = List<int>.from(removeIndices);
sorted.reversed.forEach(initial.images!.removeAt);
// then add the correct new entries
for (final ProductImage changeProductImage in change.images!) {
// null imgid means just deletion, no adding
if (changeProductImage.imgid != null) {
initial.images!.add(changeProductImage);
}
}
}
if (change.website != null) {
initial.website = change.website;
}
Expand Down
1 change: 1 addition & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@
"description": "Button clicking on which confirms the picture of the front of product that user just took."
},
"confirm_button_label": "Confirm",
"send_image_button_label": "Send image",
"front_packaging_photo_title": "Front Packaging Photo",
"ingredients_photo_title": "Ingredients Photo",
"nutritional_facts_photo_title": "Nutrition Facts Photo",
Expand Down
Loading

0 comments on commit 326bcc1

Please sign in to comment.