-
-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ProductImageData to contain all image links
- Loading branch information
1 parent
dde3f44
commit 1a02022
Showing
6 changed files
with
97 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 31 additions & 3 deletions
34
packages/smooth_app/lib/data_models/product_image_data.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,43 @@ | ||
import 'package:openfoodfacts/model/ProductImage.dart'; | ||
import 'package:collection/collection.dart'; | ||
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
|
||
class ProductImageData { | ||
const ProductImageData({ | ||
required this.imageField, | ||
required this.title, | ||
required this.buttonText, | ||
this.imageUrl, | ||
required this.imageSizeToUrlMap, | ||
}); | ||
|
||
ProductImageData.from({ | ||
required this.imageField, | ||
required this.title, | ||
required this.buttonText, | ||
required String? barcode, | ||
required Iterable<ProductImage> productImages, | ||
}) : imageSizeToUrlMap = _mapProductImages(barcode, productImages); | ||
|
||
final ImageField imageField; | ||
final String title; | ||
final String buttonText; | ||
final String? imageUrl; | ||
final Map<ImageSize?, String> imageSizeToUrlMap; | ||
|
||
static Map<ImageSize?, String> _mapProductImages( | ||
String? barcode, Iterable<ProductImage> images) { | ||
final Map<ImageSize?, String?> map = images | ||
.groupListsBy((ProductImage element) => element.size) | ||
.map((ImageSize? size, List<ProductImage> images) => | ||
MapEntry<ImageSize?, ProductImage>(size, images.first)) | ||
.map((ImageSize? size, ProductImage image) { | ||
final String? url = image.url ?? ImageHelper.buildUrl(barcode, image); | ||
return MapEntry<ImageSize?, String?>(size, url); | ||
}); | ||
map.removeWhere((_, String? image) => image == null); | ||
return map.cast(); | ||
} | ||
|
||
String? getImageUrl(ImageSize preferredSize) { | ||
return imageSizeToUrlMap[preferredSize] ?? | ||
imageSizeToUrlMap.values.firstOrNull; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters