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: When an image is unavailable -> find in another language #6297

Merged
merged 2 commits into from
Jan 28, 2025
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
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 @@ -167,6 +167,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
Loading