Skip to content

Commit

Permalink
feat: 3897 - now we display localized images (#3900)
Browse files Browse the repository at this point in the history
Impacted files:
* `background_task_crop.dart`: added language to transient file key
* `background_task_image.dart`: added language to transient file key
* `background_task_unselect.dart`: unrelated removal of not used fields
* `edit_ingredients_page.dart`: now we display localized images
* `image_upload_card.dart`: added language to transient file key
* `ocr_widget.dart`: added language to transient file key
* `product_cards_helper.dart`: now returns a localized image; refactored; new method `getLocalizedProductImageUrl`
* `product_image_carousel.dart`: now we display localized images
* `product_image_gallery_view.dart`: now we display localized images
* `product_image_swipeable_view.dart`: now we display localized images
* `product_image_viewer.dart`: now we display localized images
* `product_query.dart`: unrelated removal of not used field
* `product_refresher.dart`: unrelated minor refactoring
* `smooth_product_image.dart`: now we display localized images
* `transient_file.dart`: added language to transient file key
* `up_to_date_changes.dart`: unrelated removal of not used fields
  • Loading branch information
monsieurtanuki committed Apr 24, 2023
1 parent 896dea3 commit 9679de7
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 73 deletions.
2 changes: 2 additions & 0 deletions packages/smooth_app/lib/background/background_task_crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class BackgroundTaskCrop extends AbstractBackgroundTask {
TransientFile.putImage(
ImageField.fromOffTag(imageField)!,
barcode,
getLanguage(),
localDatabase,
File(croppedPath),
);
Expand Down Expand Up @@ -217,6 +218,7 @@ class BackgroundTaskCrop extends AbstractBackgroundTask {
TransientFile.removeImage(
ImageField.fromOffTag(imageField)!,
barcode,
getLanguage(),
localDatabase,
);
localDatabase.notifyListeners();
Expand Down
2 changes: 2 additions & 0 deletions packages/smooth_app/lib/background/background_task_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class BackgroundTaskImage extends AbstractBackgroundTask {
TransientFile.putImage(
ImageField.fromOffTag(imageField)!,
barcode,
getLanguage(),
localDatabase,
File(croppedPath),
);
Expand Down Expand Up @@ -253,6 +254,7 @@ class BackgroundTaskImage extends AbstractBackgroundTask {
TransientFile.removeImage(
ImageField.fromOffTag(imageField)!,
barcode,
getLanguage(),
localDatabase,
);
localDatabase.notifyListeners();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,15 @@ class BackgroundTaskUnselect extends AbstractBackgroundTask {
switch (ImageField.fromOffTag(imageField)!) {
case ImageField.FRONT:
result.imageFrontUrl = '';
result.imageFrontSmallUrl = '';
break;
case ImageField.INGREDIENTS:
result.imageIngredientsUrl = '';
result.imageIngredientsSmallUrl = '';
break;
case ImageField.NUTRITION:
result.imageNutritionUrl = '';
result.imageNutritionSmallUrl = '';
break;
case ImageField.PACKAGING:
result.imagePackagingUrl = '';
result.imagePackagingSmallUrl = '';
break;
case ImageField.OTHER:
// We do nothing. Actually we're not supposed to unselect other images.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:smooth_app/database/transient_file.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/pages/image_crop_page.dart';
import 'package:smooth_app/pages/product/product_image_gallery_view.dart';
import 'package:smooth_app/query/product_query.dart';

// TODO(monsieurtanuki): rename that class, like `ProductImageCarouselItem`
/// Displays a product image in the carousel: access to gallery, or new image.
Expand Down Expand Up @@ -36,6 +37,7 @@ class _ImageUploadCardState extends State<ImageUploadCard> {
final ImageProvider? imageProvider = TransientFile.getImageProvider(
widget.productImageData,
widget.product.barcode!,
ProductQuery.getLanguage(),
);

if (imageProvider == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/cards/data_cards/image_upload_card.dart';
import 'package:smooth_app/data_models/product_image_data.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/query/product_query.dart';

class ProductImageCarousel extends StatelessWidget {
/// Carousel of product images, or of just an [alternateImageUrl].
Expand Down Expand Up @@ -31,7 +32,10 @@ class ProductImageCarousel extends StatelessWidget {
),
];
} else {
productImagesData = getProductMainImagesData(product);
productImagesData = getProductMainImagesData(
product,
ProductQuery.getLanguage(),
);
}
return SizedBox(
height: height,
Expand Down
14 changes: 7 additions & 7 deletions packages/smooth_app/lib/data_models/product_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ProductList {
final String keywords, {
required int pageSize,
required int pageNumber,
required OpenFoodFactsLanguage? language,
required OpenFoodFactsLanguage language,
required OpenFoodFactsCountry? country,
}) : this._(
listType: ProductListType.HTTP_SEARCH_KEYWORDS,
Expand All @@ -88,7 +88,7 @@ class ProductList {
final String category, {
required int pageSize,
required int pageNumber,
required OpenFoodFactsLanguage? language,
required OpenFoodFactsLanguage language,
required OpenFoodFactsCountry? country,
}) : this._(
listType: ProductListType.HTTP_SEARCH_CATEGORY,
Expand All @@ -103,7 +103,7 @@ class ProductList {
final String userId, {
required int pageSize,
required int pageNumber,
required OpenFoodFactsLanguage? language,
required OpenFoodFactsLanguage language,
}) : this._(
listType: ProductListType.HTTP_USER_CONTRIBUTOR,
parameters: userId,
Expand All @@ -116,7 +116,7 @@ class ProductList {
final String userId, {
required int pageSize,
required int pageNumber,
required OpenFoodFactsLanguage? language,
required OpenFoodFactsLanguage language,
}) : this._(
listType: ProductListType.HTTP_USER_INFORMER,
parameters: userId,
Expand All @@ -129,7 +129,7 @@ class ProductList {
final String userId, {
required int pageSize,
required int pageNumber,
required OpenFoodFactsLanguage? language,
required OpenFoodFactsLanguage language,
}) : this._(
listType: ProductListType.HTTP_USER_PHOTOGRAPHER,
parameters: userId,
Expand All @@ -142,7 +142,7 @@ class ProductList {
final String userId, {
required int pageSize,
required int pageNumber,
required OpenFoodFactsLanguage? language,
required OpenFoodFactsLanguage language,
}) : this._(
listType: ProductListType.HTTP_USER_TO_BE_COMPLETED,
parameters: userId,
Expand All @@ -154,7 +154,7 @@ class ProductList {
ProductList.allToBeCompleted({
required int pageSize,
required int pageNumber,
required OpenFoodFactsLanguage? language,
required OpenFoodFactsLanguage language,
required OpenFoodFactsCountry? country,
}) : this._(
listType: ProductListType.HTTP_ALL_TO_BE_COMPLETED,
Expand Down
20 changes: 0 additions & 20 deletions packages/smooth_app/lib/data_models/up_to_date_changes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,21 @@ class UpToDateChanges {
change.imageFrontUrl!,
);
}
if (change.imageFrontSmallUrl != null) {
initial.imageFrontSmallUrl = emptyMeansNull(
change.imageFrontSmallUrl!,
);
}
if (change.imageIngredientsUrl != null) {
initial.imageIngredientsUrl = emptyMeansNull(
change.imageIngredientsUrl!,
);
}
if (change.imageIngredientsSmallUrl != null) {
initial.imageIngredientsSmallUrl = emptyMeansNull(
change.imageIngredientsSmallUrl!,
);
}
if (change.imageNutritionUrl != null) {
initial.imageNutritionUrl = emptyMeansNull(
change.imageNutritionUrl!,
);
}
if (change.imageNutritionSmallUrl != null) {
initial.imageNutritionSmallUrl = emptyMeansNull(
change.imageNutritionSmallUrl!,
);
}
if (change.imagePackagingUrl != null) {
initial.imagePackagingUrl = emptyMeansNull(
change.imagePackagingUrl!,
);
}
if (change.imagePackagingSmallUrl != null) {
initial.imagePackagingSmallUrl = emptyMeansNull(
change.imagePackagingSmallUrl!,
);
}
if (change.images != null && change.images!.isNotEmpty) {
initial.images ??= <ProductImage>[];
// let's remove similar entries first
Expand Down
33 changes: 26 additions & 7 deletions packages/smooth_app/lib/database/transient_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,42 @@ class TransientFile {
static void putImage(
final ImageField imageField,
final String barcode,
final OpenFoodFactsLanguage language,
final LocalDatabase localDatabase,
final File file,
) {
_transientFiles[_getImageKey(imageField, barcode)] = file.path;
_transientFiles[_getImageKey(
imageField,
barcode,
language,
)] = file.path;
localDatabase.notifyListeners();
}

/// Removes the current transient image for [imageField] and [barcode].
static void removeImage(
final ImageField imageField,
final String barcode,
final OpenFoodFactsLanguage language,
final LocalDatabase localDatabase,
) =>
_transientFiles.remove(_getImageKey(imageField, barcode));
_transientFiles.remove(_getImageKey(
imageField,
barcode,
language,
));

/// Returns the transient image for [imageField] and [barcode].
static File? getImage(
final ImageField imageField,
final String barcode,
final OpenFoodFactsLanguage language,
) {
final String? path = _transientFiles[_getImageKey(imageField, barcode)];
final String? path = _transientFiles[_getImageKey(
imageField,
barcode,
language,
)];
if (path == null) {
return null;
}
Expand All @@ -47,15 +62,17 @@ class TransientFile {
static String _getImageKey(
final ImageField imageField,
final String barcode,
final OpenFoodFactsLanguage language,
) =>
'$barcode;$imageField';
'$barcode;$imageField;${language.code}';

/// Returns a way to display the image, either locally or from the server.
static ImageProvider? getImageProvider(
final ProductImageData imageData,
final String barcode,
final OpenFoodFactsLanguage language,
) {
final File? file = getImage(imageData.imageField, barcode);
final File? file = getImage(imageData.imageField, barcode, language);
if (file != null) {
return FileImage(file);
}
Expand All @@ -72,8 +89,9 @@ class TransientFile {
static bool isImageAvailable(
final ProductImageData imageData,
final String barcode,
final OpenFoodFactsLanguage language,
) =>
getImage(imageData.imageField, barcode) != null ||
getImage(imageData.imageField, barcode, language) != null ||
imageData.imageUrl != null;

/// Returns true if the displayed image comes from the server.
Expand All @@ -87,7 +105,8 @@ class TransientFile {
static bool isServerImage(
final ProductImageData imageData,
final String barcode,
final OpenFoodFactsLanguage language,
) =>
getImage(imageData.imageField, barcode) == null &&
getImage(imageData.imageField, barcode, language) == null &&
imageData.imageUrl != null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/database/transient_file.dart';
import 'package:smooth_app/generic_lib/widgets/images/smooth_image.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/query/product_query.dart';

/// Main product image on a product card.
class SmoothMainProductImage extends StatelessWidget {
Expand All @@ -21,9 +22,15 @@ class SmoothMainProductImage extends StatelessWidget {
@override
Widget build(BuildContext context) {
context.watch<LocalDatabase>();
final OpenFoodFactsLanguage language = ProductQuery.getLanguage();
final ImageProvider? imageProvider = TransientFile.getImageProvider(
getProductImageData(product, ImageField.FRONT),
getProductImageData(
product,
ImageField.FRONT,
language,
),
product.barcode!,
language,
);

return SmoothImage(
Expand Down

0 comments on commit 9679de7

Please sign in to comment.