Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 2484 - now when editing we go to the full image with the cropped area on top #3658

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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