Skip to content

Commit

Permalink
fix: 5576 - first step towards multi product types (#5593)
Browse files Browse the repository at this point in the history
Impacted files:
* `add_basic_details_page.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `background_task.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `background_task_download_products.dart`: saved new field `Product.productType`
* `dao_product.dart`: now we're storing the `Product.productType` field
* `edit_new_packagings.dart`: now using new field `Product.productType`
* `edit_new_packagings_component.dart`: now using new field `Product.productType`
* `edit_product_page.dart`: specific icon for "food" categories; no nutrition for "beauty" and "product"
* `forgot_password_page.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `lazy_counter.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `login_result.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `newsfeed_provider.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `ocr_ingredients_helper.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `ocr_packaging_helper.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `onboarding_data_product.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `ordered_nutrients_cache.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `paged_product_query.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `product_cards_helper.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `product_image_crop_button.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `product_image_gallery_other_view.dart`: now using new field `Product.productType`
* `product_image_other_page.dart`: now using new field `Product.productType`
* `product_image_server_button.dart`: now using new field `Product.productType`
* `product_image_widget.dart`: now using new field `Product.productType`
* `product_list_page.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `product_query.dart`: now when we look for a barcode (or refresh a product), we use all productTypes if needed
* `product_refresher.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `pubspec.lock`: wtf
* `pubspec.yaml`: upgraded `openfoodfacts` to `3.15.0` for new `Product.productType` field
* `sign_up_page.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `simple_input_page_helpers.dart`: new class dedicated to non-food categories
* `simple_input_text_field.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `simple_input_widget.dart`: now using new field `Product.productType`
* `temp_product_list_share_helper.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
* `uploaded_image_gallery.dart`: now using new field `Product.productType`
* `user_preferences_dev_debug_info.dart`: replaced getter `ProductQuery.uriProductHelper` with method `ProductQuery.getUriProductHelper`
  • Loading branch information
monsieurtanuki authored Sep 25, 2024
1 parent 42d0201 commit 93fc899
Show file tree
Hide file tree
Showing 33 changed files with 278 additions and 98 deletions.
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/background/background_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ abstract class BackgroundTask {

// TODO(monsieurtanuki): store the uriProductHelper as well
@protected
UriProductHelper get uriProductHelper => ProductQuery.uriProductHelper;
UriProductHelper get uriProductHelper => ProductQuery.getUriProductHelper();

/// Returns true if tasks with the same stamp would overwrite each-other.
bool isDeduplicable() => true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,15 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
throw Exception('Something bad happened downloading products');
}
final DaoProduct daoProduct = DaoProduct(localDatabase);
final ProductType? productType =
ProductQuery.extractProductType(uriProductHelper);
for (final Product product in downloadedProducts) {
if (await _shouldBeUpdated(daoProduct, product.barcode!)) {
await daoProduct.put(product, language);
await daoProduct.put(
product,
language,
productType: productType,
);
}
}
final int deleted = await daoWorkBarcode.deleteBarcodes(work, barcodes);
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/data_models/login_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LoginResult {
try {
final LoginStatus? loginStatus = await OpenFoodAPIClient.login2(
user,
uriHelper: ProductQuery.uriProductHelper,
uriHelper: ProductQuery.getUriProductHelper(),
);
if (loginStatus == null) {
return const LoginResult(LoginResultType.serverIssue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ class AppNewsProvider extends ChangeNotifier {
/// or [https://world.openfoodfacts.[org/net]/resources/files/tagline-off-android-v3.json]
Future<String?> _fetchJSON() async {
try {
final UriProductHelper uriProductHelper = ProductQuery.uriProductHelper;
final UriProductHelper uriProductHelper =
ProductQuery.getUriProductHelper();
final Map<String, String> headers = <String, String>{};
final Uri uri;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class OnboardingDataProduct extends AbstractOnboardingData<Product> {
AbstractOnboardingData.barcode,
ProductQuery.getLanguage(),
),
uriHelper: ProductQuery.uriProductHelper,
uriHelper: ProductQuery.getUriProductHelper(
productType: ProductType.food,
),
).timeout(SnackBarDuration.long);

@override
Expand Down
31 changes: 20 additions & 11 deletions packages/smooth_app/lib/database/dao_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,34 @@ class DaoProduct extends AbstractSqlDao implements BulkDeletable {

Future<void> put(
final Product product,
final OpenFoodFactsLanguage language,
) async =>
final OpenFoodFactsLanguage language, {
final ProductType? productType,
}) async =>
putAll(
<Product>[product],
language,
productType: productType,
);

/// Replaces products in database
Future<void> putAll(
final Iterable<Product> products,
final OpenFoodFactsLanguage language,
) async =>
localDatabase.database.transaction(
(final Transaction transaction) async => _bulkReplaceLoop(
transaction,
products,
language,
),
);
final OpenFoodFactsLanguage language, {
final ProductType? productType,
}) async {
if (productType != null) {
for (final Product product in products) {
product.productType = productType;
}
}
await localDatabase.database.transaction(
(final Transaction transaction) async => _bulkReplaceLoop(
transaction,
products,
language,
),
);
}

Future<List<String>> getAllKeys() async {
final List<String> result = <String>[];
Expand Down
4 changes: 3 additions & 1 deletion packages/smooth_app/lib/helpers/product_cards_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ ProductImageData getProductImageData(
imageUrl: productImage.getUrl(
product.barcode!,
imageSize: ImageSize.DISPLAY,
uriHelper: ProductQuery.uriProductHelper,
uriHelper: ProductQuery.getUriProductHelper(
productType: product.productType,
),
),
language: language,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Uri shareProductList(List<String> barcodes) {
final String barcodesString = barcodes.join(',');

return UriHelper.replaceSubdomain(
ProductQuery.uriProductHelper.getUri(
ProductQuery.getUriProductHelper().getUri(
path: 'products/$barcodesString',
addUserAgentParameters: false,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class _RawGridGallery extends StatelessWidget {
squareSize: squareSize,
imageSize: imageSize,
heroTag: heroTag,
productType: product.productType,
),
),
);
Expand Down
14 changes: 12 additions & 2 deletions packages/smooth_app/lib/pages/image/product_image_other_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class _ProductImageOtherPageState extends State<ProductImageOtherPage> {
barcode: widget.product.barcode!,
heroTag:
widget.currentImage == image ? widget.heroTag : null,
productType: widget.product.productType,
);
},
).toList(growable: false),
Expand All @@ -89,11 +90,13 @@ class _ProductImageViewer extends StatelessWidget {
required this.image,
required this.barcode,
this.heroTag,
required this.productType,
});

final ProductImage image;
final String barcode;
final String? heroTag;
final ProductType? productType;

@override
Widget build(BuildContext context) {
Expand All @@ -111,7 +114,9 @@ class _ProductImageViewer extends StatelessWidget {
image: NetworkImage(
image.getUrl(
barcode,
uriHelper: ProductQuery.uriProductHelper,
uriHelper: ProductQuery.getUriProductHelper(
productType: productType,
),
),
),
fit: BoxFit.cover,
Expand Down Expand Up @@ -153,6 +158,7 @@ class _ProductImageViewer extends StatelessWidget {
_ProductImageDetailsButton(
image: image,
barcode: barcode,
productType: productType,
),
const Spacer(),
if (image.expired) _ProductImageOutdatedLabel(colors: colors),
Expand Down Expand Up @@ -211,18 +217,22 @@ class _ProductImageDetailsButton extends StatelessWidget {
const _ProductImageDetailsButton({
required this.image,
required this.barcode,
required this.productType,
});

final ProductImage image;
final String barcode;
final ProductType? productType;

@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
final String url = image.url ??
image.getUrl(
barcode,
uriHelper: ProductQuery.uriProductHelper,
uriHelper: ProductQuery.getUriProductHelper(
productType: productType,
),
);

return DecoratedBox(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ProductImageWidget extends StatelessWidget {
required this.productImage,
required this.barcode,
required this.squareSize,
required this.productType,
this.imageSize,
this.heroTag,
});
Expand All @@ -25,6 +26,7 @@ class ProductImageWidget extends StatelessWidget {
final String barcode;
final double squareSize;
final String? heroTag;
final ProductType? productType;

/// Allows to fetch the optimized version of the image
final ImageSize? imageSize;
Expand All @@ -45,7 +47,9 @@ class ProductImageWidget extends StatelessWidget {
imageProvider: NetworkImage(
productImage.getUrl(
barcode,
uriHelper: ProductQuery.uriProductHelper,
uriHelper: ProductQuery.getUriProductHelper(
productType: productType,
),
imageSize: imageSize,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class UploadedImageGallery extends StatelessWidget {
required this.imageField,
required this.language,
required this.isLoggedInMandatory,
required this.productType,
});

final String barcode;
final List<ProductImage> rawImages;
final ImageField imageField;
final bool isLoggedInMandatory;
final ProductType? productType;

/// Language for which we'll save the cropped image.
final OpenFoodFactsLanguage language;
Expand Down Expand Up @@ -70,7 +72,9 @@ class UploadedImageGallery extends StatelessWidget {
rawImage.getUrl(
barcode,
imageSize: ImageSize.ORIGINAL,
uriHelper: ProductQuery.uriProductHelper,
uriHelper: ProductQuery.getUriProductHelper(
productType: productType,
),
),
DaoInt(localDatabase),
);
Expand Down Expand Up @@ -102,6 +106,7 @@ class UploadedImageGallery extends StatelessWidget {
productImage: rawImage,
barcode: barcode,
squareSize: columnWidth,
productType: productType,
),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class LazyCounterUserSearch extends LazyCounter {
final SearchResult result = await OpenFoodAPIClient.searchProducts(
user,
configuration,
uriHelper: ProductQuery.uriProductHelper,
uriHelper: ProductQuery.getUriProductHelper(),
);
return result.count;
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class _UserPreferencesDebugInfoState extends State<UserPreferencesDebugInfo> {
'IsLoggedIn': ProductQuery.isLoggedIn().toString(),
'UUID': OpenFoodAPIConfiguration.uuid.toString(),
'Matomo Visitor ID': AnalyticsHelper.matomoVisitorId,
'QueryType': ProductQuery.uriProductHelper.isTestMode
'QueryType': ProductQuery.getUriProductHelper().isTestMode
? 'QueryType.TEST'
: 'QueryType.PROD',
'Domain': ProductQuery.uriProductHelper.domain,
'Domain': ProductQuery.getUriProductHelper().domain,
'UserAgent-name': '${OpenFoodAPIConfiguration.userAgent?.name}',
'UserAgent-system': '${OpenFoodAPIConfiguration.userAgent?.system}',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ class _AddBasicDetailsPageState extends State<AddBasicDetailsPage> {
user: ProductQuery.getReadUser(),
limit: 25,
fuzziness: Fuzziness.none,
uriHelper: ProductQuery.uriProductHelper,
uriHelper: ProductQuery.getUriProductHelper(
productType: widget.product.productType,
),
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ class _ProductListPageState extends State<ProductListPage>
barcodes,
language,
),
uriHelper: ProductQuery.uriProductHelper,
uriHelper: ProductQuery.getUriProductHelper(),
);
final List<Product>? freshProducts = searchResult.products;
if (freshProducts == null) {
Expand Down
Loading

0 comments on commit 93fc899

Please sign in to comment.