diff --git a/packages/smooth_app/lib/data_models/continuous_scan_model.dart b/packages/smooth_app/lib/data_models/continuous_scan_model.dart index 4c598944fc7..9ea2ba8db4e 100644 --- a/packages/smooth_app/lib/data_models/continuous_scan_model.dart +++ b/packages/smooth_app/lib/data_models/continuous_scan_model.dart @@ -181,6 +181,7 @@ class ContinuousScanModel with ChangeNotifier { BarcodeProductQuery( barcode: barcode, daoProduct: _daoProduct, + isScanned: true, ).getFetchedProduct(); Future _loadBarcode( diff --git a/packages/smooth_app/lib/database/barcode_product_query.dart b/packages/smooth_app/lib/database/barcode_product_query.dart index 5e9ebeb62f8..ffa48ba1506 100644 --- a/packages/smooth_app/lib/database/barcode_product_query.dart +++ b/packages/smooth_app/lib/database/barcode_product_query.dart @@ -9,10 +9,12 @@ class BarcodeProductQuery { BarcodeProductQuery({ required this.barcode, required this.daoProduct, + required this.isScanned, }); final String barcode; final DaoProduct daoProduct; + final bool isScanned; Future getFetchedProduct() async { final ProductQueryConfiguration configuration = ProductQueryConfiguration( @@ -24,10 +26,13 @@ class BarcodeProductQuery { final ProductResult result; try { + ProductQuery.setUserAgentComment(isScanned ? 'scan' : 'search'); result = await OpenFoodAPIClient.getProduct(configuration); } catch (e) { + ProductQuery.setUserAgentComment(''); return FetchedProduct.error(FetchedProductStatus.internetError); } + ProductQuery.setUserAgentComment(''); if (result.status == 1) { final Product? product = result.product; diff --git a/packages/smooth_app/lib/database/product_query.dart b/packages/smooth_app/lib/database/product_query.dart index 786cb4f9c7b..d37e07d3551 100644 --- a/packages/smooth_app/lib/database/product_query.dart +++ b/packages/smooth_app/lib/database/product_query.dart @@ -1,3 +1,4 @@ +import 'package:openfoodfacts/model/UserAgent.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; import 'package:openfoodfacts/utils/CountryHelper.dart'; import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart'; @@ -42,6 +43,23 @@ abstract class ProductQuery { '_' '${getCountry()!.iso2Code.toUpperCase()}'; + /// Sets a comment for the user agent. + /// + /// cf. https://github.com/openfoodfacts/smooth-app/issues/2248 + static void setUserAgentComment(final String comment) { + final UserAgent? previous = OpenFoodAPIConfiguration.userAgent; + if (previous == null) { + return; + } + OpenFoodAPIConfiguration.userAgent = UserAgent( + name: previous.name, + version: previous.version, + system: previous.system, + url: previous.url, + comment: comment, + ); + } + static const String _UUID_NAME = 'UUID_NAME_REV_1'; /// Sets the uuid id as "final variable", for instance for API queries. diff --git a/packages/smooth_app/lib/pages/product/common/product_dialog_helper.dart b/packages/smooth_app/lib/pages/product/common/product_dialog_helper.dart index f1bf2f61c01..6c67547e6a8 100644 --- a/packages/smooth_app/lib/pages/product/common/product_dialog_helper.dart +++ b/packages/smooth_app/lib/pages/product/common/product_dialog_helper.dart @@ -37,6 +37,7 @@ class ProductDialogHelper { future: BarcodeProductQuery( barcode: barcode, daoProduct: DaoProduct(localDatabase), + isScanned: false, ).getFetchedProduct(), title: refresh ? AppLocalizations.of(context).refreshing_product