Skip to content

Commit

Permalink
When an image is unavailable, we try to find in another language (#6297)
Browse files Browse the repository at this point in the history
  • Loading branch information
g123k authored Jan 28, 2025
1 parent aab292a commit b9e0a23
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class ProductTitleCard extends StatelessWidget {
product: product,
imageField: ImageField.FRONT,
fallbackUrl: product.imageFrontUrl,
allowAlternativeLanguage: true,
size: imageSize,
showObsoleteIcon: true,
imageFoundBorder: 1.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class _SmoothProductItemPicture extends StatelessWidget {
product: product,
imageField: ImageField.FRONT,
fallbackUrl: product.imageFrontUrl,
allowAlternativeLanguage: true,
size: Size(size, size - (scoreWidget != null ? 25.0 : 5.0)),
borderRadius: BorderRadius.vertical(
top: hasScore ? Radius.zero : const Radius.circular(08.0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ProductPicture extends StatefulWidget {
required ImageField imageField,
required Size size,
OpenFoodFactsLanguage? language,
bool allowAlternativeLanguage = false,
String? fallbackUrl,
VoidCallback? onTap,
String? heroTag,
Expand All @@ -43,6 +44,7 @@ class ProductPicture extends StatefulWidget {
product: product,
imageField: imageField,
language: language ?? ProductQuery.getLanguage(),
allowAlternativeLanguage: allowAlternativeLanguage,
size: size,
fallbackUrl: fallbackUrl,
heroTag: heroTag,
Expand All @@ -61,6 +63,7 @@ class ProductPicture extends StatefulWidget {
required TransientFile transientFile,
required Size size,
OpenFoodFactsLanguage? language,
bool allowAlternativeLanguage = false,
Product? product,
ImageField? imageField,
String? fallbackUrl,
Expand All @@ -79,6 +82,7 @@ class ProductPicture extends StatefulWidget {
product: product,
imageField: imageField,
language: language,
allowAlternativeLanguage: allowAlternativeLanguage,
size: size,
fallbackUrl: fallbackUrl,
heroTag: heroTag,
Expand All @@ -97,6 +101,7 @@ class ProductPicture extends StatefulWidget {
required this.product,
required this.imageField,
required this.language,
required this.allowAlternativeLanguage,
required this.transientFile,
required this.size,
required this.blurFilter,
Expand Down Expand Up @@ -127,6 +132,10 @@ class ProductPicture extends StatefulWidget {

final String? heroTag;

/// When an image is unavailable in the [language] or product main language,
/// we try to find another one in an alternative language.
final bool allowAlternativeLanguage;

/// Show the obsolete icon on top of the image
final bool showObsoleteIcon;

Expand Down Expand Up @@ -161,6 +170,7 @@ class _ProductPictureState extends State<ProductPicture> {
final (ImageProvider?, bool)? imageProvider = _getImageProvider(
widget.product,
widget.transientFile,
widget.allowAlternativeLanguage,
);

final Widget? inkWell = widget.onTap != null
Expand Down Expand Up @@ -264,6 +274,7 @@ class _ProductPictureState extends State<ProductPicture> {
(ImageProvider?, bool)? _getImageProvider(
Product? product,
TransientFile? transientFile,
bool allowAlternativeLanguage,
) {
if (transientFile != null) {
return (transientFile.getImageProvider(), transientFile.expired);
Expand All @@ -279,8 +290,44 @@ class _ProductPictureState extends State<ProductPicture> {

if (imageProvider != null) {
return (imageProvider, productTransientFile.expired);
} else if (widget.fallbackUrl?.isNotEmpty == true) {
}

if (widget.fallbackUrl?.isNotEmpty == true) {
return (NetworkImage(widget.fallbackUrl!), false);
} else if (allowAlternativeLanguage) {
Iterable<ProductImage>? images = widget.product?.images?.where(
(ProductImage image) =>
image.field == widget.imageField && image.url != null,
);

if (images == null || images.isEmpty) {
return null;
}

/// Let's try with English images
final Iterable<ProductImage> englishImages = images.where(
(ProductImage image) => image.language == OpenFoodFactsLanguage.ENGLISH,
);

if (englishImages.isNotEmpty) {
images = englishImages;
}

/// We prefer a display image
ProductImage? productImage;

for (final ProductImage image in images) {
if (image.size == ImageSize.DISPLAY) {
productImage = image;
break;
} else if (image.size == ImageSize.ORIGINAL) {
productImage = image;
}
}

productImage ??= images.first;

return (NetworkImage(productImage.url!), false);
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class _ImageGalleryPhotoRowState extends State<ImageGalleryPhotoRow> {
product: product,
imageField: widget.imageField,
language: widget.language,
allowAlternativeLanguage: false,
transientFile: transientFile,
size: Size(box.maxWidth, box.maxHeight),
onTap: null,
Expand Down

0 comments on commit b9e0a23

Please sign in to comment.