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

fix: 5576 - first step towards multi product types #5593

Merged
merged 6 commits into from
Sep 25, 2024
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
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
Loading