Skip to content

Commit

Permalink
feat: openfoodfacts#2248 - user agent distinction between scan and ba…
Browse files Browse the repository at this point in the history
…rcode search (openfoodfacts#2408)

Impacted files:
* `barcode_product_query.dart`: new parameter `isScanned` to distinguish scan and barcode search via the user agent comment field
* `continuous_scan_model.dart`: "this IS a scan"
* `product_dialog_helper.dart`: "this is NOT a scan"
* `product_query.dart`: new method to set the user agent comment field
  • Loading branch information
monsieurtanuki authored Jun 28, 2022
1 parent 603e3a4 commit c1664d6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class ContinuousScanModel with ChangeNotifier {
BarcodeProductQuery(
barcode: barcode,
daoProduct: _daoProduct,
isScanned: true,
).getFetchedProduct();

Future<void> _loadBarcode(
Expand Down
5 changes: 5 additions & 0 deletions packages/smooth_app/lib/database/barcode_product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<FetchedProduct> getFetchedProduct() async {
final ProductQueryConfiguration configuration = ProductQueryConfiguration(
Expand All @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions packages/smooth_app/lib/database/product_query.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ProductDialogHelper {
future: BarcodeProductQuery(
barcode: barcode,
daoProduct: DaoProduct(localDatabase),
isScanned: false,
).getFetchedProduct(),
title: refresh
? AppLocalizations.of(context).refreshing_product
Expand Down

0 comments on commit c1664d6

Please sign in to comment.