getRawProductImages function
Returns an id-sorted list of raw images matching the imageSize if possible.
Implementation
List<ProductImage> getRawProductImages(
Product product,
ImageSize imageSize,
) {
final List<ProductImage>? rawImages = product.getRawImages();
if (rawImages == null) {
return <ProductImage>[];
}
final Map<int, ProductImage> map = <int, ProductImage>{};
for (final ProductImage productImage in rawImages) {
final int? imageId = int.tryParse(productImage.imgid!);
if (imageId == null) {
// highly unlikely
continue;
}
final ProductImage? previous = map[imageId];
if (previous == null) {
map[imageId] = productImage;
continue;
}
final ImageSize? currentImageSize = productImage.size;
if (currentImageSize == null) {
// highly unlikely
continue;
}
final ImageSize? previousImageSize = previous.size;
if (previousImageSize == imageSize) {
// we already have the best
continue;
}
map[imageId] = productImage;
}
final List<ProductImage> result = List<ProductImage>.of(map.values);
result.sort((ProductImage a, ProductImage b) {
final int result = (a.uploaded?.millisecondsSinceEpoch ?? 0).compareTo(
b.uploaded?.millisecondsSinceEpoch ?? 0,
);
if (result != 0) {
return result;
}
return (int.tryParse(a.imgid ?? '0') ?? 0).compareTo(
int.tryParse(b.imgid ?? '0') ?? 0,
);
});
return result;
}