diff --git a/packages/smooth_app/lib/helpers/product_cards_helper.dart b/packages/smooth_app/lib/helpers/product_cards_helper.dart index 8b9ab838be0..147d166f2bd 100644 --- a/packages/smooth_app/lib/helpers/product_cards_helper.dart +++ b/packages/smooth_app/lib/helpers/product_cards_helper.dart @@ -109,8 +109,9 @@ Widget addPanelButton( List getProductMainImagesData( Product product, - AppLocalizations appLocalizations, -) => + AppLocalizations appLocalizations, { + final bool includeOther = true, +}) => [ ProductImageData( imageField: ImageField.FRONT, @@ -136,10 +137,11 @@ List getProductMainImagesData( title: appLocalizations.packaging_information, buttonText: appLocalizations.packaging_information_photo, ), - ProductImageData( - imageField: ImageField.OTHER, - imageUrl: null, - title: appLocalizations.more_photos, - buttonText: appLocalizations.more_photos, - ), + if (includeOther) + ProductImageData( + imageField: ImageField.OTHER, + imageUrl: null, + title: appLocalizations.more_photos, + buttonText: appLocalizations.more_photos, + ), ]; diff --git a/packages/smooth_app/lib/pages/product/product_image_gallery_view.dart b/packages/smooth_app/lib/pages/product/product_image_gallery_view.dart index 087d6617d06..1acfaee64f8 100644 --- a/packages/smooth_app/lib/pages/product/product_image_gallery_view.dart +++ b/packages/smooth_app/lib/pages/product/product_image_gallery_view.dart @@ -1,6 +1,5 @@ import 'dart:io'; -import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; @@ -9,7 +8,6 @@ import 'package:smooth_app/data_models/product_image_data.dart'; import 'package:smooth_app/database/local_database.dart'; import 'package:smooth_app/generic_lib/design_constants.dart'; import 'package:smooth_app/generic_lib/duration_constants.dart'; -import 'package:smooth_app/generic_lib/widgets/images/smooth_images_sliver_grid.dart'; import 'package:smooth_app/generic_lib/widgets/images/smooth_images_sliver_list.dart'; import 'package:smooth_app/generic_lib/widgets/smooth_back_button.dart'; import 'package:smooth_app/helpers/picture_capture_helper.dart'; @@ -17,7 +15,6 @@ import 'package:smooth_app/helpers/product_cards_helper.dart'; import 'package:smooth_app/pages/image_crop_page.dart'; import 'package:smooth_app/pages/product/common/product_refresher.dart'; import 'package:smooth_app/pages/product/product_image_viewer.dart'; -import 'package:smooth_app/query/product_query.dart'; import 'package:smooth_app/widgets/smooth_app_bar.dart'; import 'package:smooth_app/widgets/smooth_scaffold.dart'; @@ -41,14 +38,9 @@ class ProductImageGalleryView extends StatefulWidget { class _ProductImageGalleryViewState extends State { late final LocalDatabase _localDatabase; - Map _selectedImages = - ?>{}; - - final Map _unselectedImages = - {}; + late Map _selectedImages; bool _isRefreshed = false; - bool _isLoadingMore = true; ImageProvider? _provideImage(ProductImageData imageData) => imageData.imageUrl == null ? null : NetworkImage(imageData.imageUrl!); @@ -79,48 +71,22 @@ class _ProductImageGalleryViewState extends State { product = refreshedProduct; } final List allProductImagesData = - getProductMainImagesData(product, appLocalizations); + getProductMainImagesData( + product, + appLocalizations, + includeOther: false, + ); _selectedImages = Map.fromIterables( allProductImagesData, allProductImagesData.map(_provideImage), ); - _getProductImages().then( - (Iterable? loadedData) { - if (loadedData == null) { - return; - } - - final Map?> newMap = - Map.fromIterables( - loadedData, - loadedData.map(_provideImage), - ); - if (mounted) { - setState( - () { - _unselectedImages.clear(); - _unselectedImages.addAll(newMap); - _isLoadingMore = false; - }, - ); - } - }, - ); - if (_selectedImages.isEmpty) { - return SmoothScaffold( - body: Center( - child: Text(appLocalizations.error), - ), - ); - } return SmoothScaffold( appBar: SmoothAppBar( - title: Text(appLocalizations.edit_product_form_item_photos_title), - subTitle: widget.product.productName != null + title: widget.product.productName != null ? Text( widget.product.productName!, - maxLines: 1, + maxLines: 2, overflow: TextOverflow.ellipsis, ) : null, @@ -136,18 +102,15 @@ class _ProductImageGalleryViewState extends State { child: Scrollbar( child: CustomScrollView( slivers: [ - _buildTitle(appLocalizations.selected_images, theme: theme), + _buildTitle( + appLocalizations.edit_product_form_item_photos_title, + theme: theme, + ), SmoothImagesSliverList( imagesData: _selectedImages, onTap: (ProductImageData data, _) => data.imageUrl != null ? _openImage(data) : _newImage(data), ), - _buildTitle(appLocalizations.all_images, theme: theme), - SmoothImagesSliverGrid( - imagesData: _unselectedImages, - loading: _isLoadingMore, - onTap: (ProductImageData data, _) => _openImage(data), - ), ], ), ), @@ -182,21 +145,19 @@ class _ProductImageGalleryViewState extends State { if (croppedImageFile == null) { return; } - if (mounted) { - setState(() { - final FileImage fileImage = FileImage(croppedImageFile); - if (_selectedImages.containsKey(data)) { - _selectedImages[data] = fileImage; - } else if (_unselectedImages.containsKey(data)) { - _unselectedImages[data] = fileImage; - } else { - throw ArgumentError('Could not find the type of $data'); - } - }); - } if (!mounted) { return; } + setState(() { + final FileImage fileImage = FileImage(croppedImageFile); + final ImageField imageField = data.imageField; + for (final ProductImageData productImageData in _selectedImages.keys) { + if (productImageData.imageField == imageField) { + _selectedImages[productImageData] = fileImage; + return; + } + } + }); final bool isUploaded = await uploadCapturedPicture( widget: this, barcode: _barcode, @@ -222,51 +183,4 @@ class _ProductImageGalleryViewState extends State { ); } } - - Future?> _getProductImages() async { - final ProductQueryConfiguration configuration = ProductQueryConfiguration( - _barcode, - fields: [ProductField.IMAGES], - language: ProductQuery.getLanguage(), - country: ProductQuery.getCountry(), - ); - - final ProductResult result; - try { - result = await OpenFoodAPIClient.getProduct(configuration); - } catch (e) { - return null; - } - - if (result.status != 1) { - return null; - } - - final Product? resultProduct = result.product; - if (resultProduct == null || resultProduct.images == null) { - return null; - } - - return _deduplicateImages(resultProduct.images!) - .map((ProductImage image) => ProductImageData.from(image, _barcode)); - } - - /// Groups the list of [ProductImage] by [ProductImage.imgid] - /// and returns the first of every group - Iterable _deduplicateImages(Iterable images) => - images - .groupListsBy((ProductImage element) => element.imgid) - .values - .map(_findBestProductImage) - .whereNotNull(); - - ProductImage? _findBestProductImage(Iterable images) { - final Map map = images - .groupListsBy((ProductImage image) => image.size) - .map((ImageSize? key, List value) => - MapEntry(key, value.first)); - return map[ImageSize.DISPLAY] ?? - map[ImageSize.SMALL] ?? - map[ImageSize.THUMB]; - } }