Skip to content

Commit

Permalink
chore: additional fix around url field in product image
Browse files Browse the repository at this point in the history
Impacted files:
* `api_json_to_from_test.dart`: minor change to make the test more relevant
* `json_helper.dart`: added field `url` in the to/fromJSON image conversion methods
* `product_helper.dart`: minor fix
  • Loading branch information
monsieurtanuki committed Sep 27, 2023
1 parent 351f407 commit 0336305
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/src/utils/json_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ class JsonHelper {
}
final int? width = JsonObject.parseInt(numberObject['w']);
final int? height = JsonObject.parseInt(numberObject['h']);
final String? url = numberObject['url'];

// TODO(monsieurtanuki): add field "url"?
var image = ProductImage(
field: field,
size: size,
Expand All @@ -154,6 +154,7 @@ class JsonHelper {
y2: y2,
width: width,
height: height,
url: url,
);
imageList.add(image);
}
Expand Down Expand Up @@ -191,19 +192,22 @@ class JsonHelper {
continue;
}
final Map<String, dynamic> item = <String, dynamic>{};
item['sizes'] = <String, Map<String, int>>{};
item['sizes'] = <String, Map<String, dynamic>>{};
bool first = true;
for (final ProductImage productImage in list) {
if (productImage.size == null) {
continue;
}
final Map<String, int> size = <String, int>{};
final Map<String, dynamic> size = <String, dynamic>{};
if (productImage.width != null) {
size['w'] = productImage.width!;
}
if (productImage.height != null) {
size['h'] = productImage.height!;
}
if (productImage.url != null) {
size['url'] = productImage.url!;
}
item['sizes']![productImage.size!.number] = size;
if (first) {
first = false;
Expand Down
4 changes: 4 additions & 0 deletions lib/src/utils/product_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class ProductHelper {
return;
}

if (product.barcode == null) {
return;
}

for (ProductImage image in product.images!) {
image.url = ImageHelper.getLocalizedProductImageUrl(
product.barcode!,
Expand Down
2 changes: 1 addition & 1 deletion test/api_json_to_from_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void main() {
await OpenFoodAPIClient.getProductV3(
ProductQueryConfiguration(
BARCODE_DANISH_BUTTER_COOKIES,
fields: [ProductField.IMAGES],
fields: [ProductField.IMAGES, ProductField.BARCODE],
version: ProductQueryVersion.v3,
),
);
Expand Down

0 comments on commit 0336305

Please sign in to comment.