Skip to content

Commit

Permalink
Merge branch 'master' into feat/openfoodfacts#531
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurtanuki authored Aug 3, 2022
2 parents ff90d51 + 645d903 commit 9bcfc74
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/auto-assign-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
assign-author:
runs-on: ubuntu-latest
steps:
- uses: toshimaru/auto-author-assign@v1.5.1
- uses: toshimaru/auto-author-assign@v1.6.1
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ dependencies:

dev_dependencies:
test: any
lints: ^1.0.1
lints: ^2.0.0
131 changes: 54 additions & 77 deletions lib/model/IngredientsAnalysisTags.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,82 @@ enum VeganStatus {
VEGAN_STATUS_UNKNOWN,
}

extension VeganStatusExtension on VeganStatus {
static const Map<VeganStatus, String> _tags = {
VeganStatus.VEGAN: 'en:vegan',
VeganStatus.NON_VEGAN: 'en:non-vegan',
VeganStatus.MAYBE_VEGAN: 'en:maybe-vegan',
VeganStatus.VEGAN_STATUS_UNKNOWN: 'en:vegan-status-unknown',
};
String get tag => _tags[this]!;
}

enum VegetarianStatus {
VEGETARIAN,
NON_VEGETARIAN,
MAYBE_VEGETARIAN,
VEGETARIAN_STATUS_UNKNOWN,
}

extension VegetarianStatusExtension on VegetarianStatus {
static const Map<VegetarianStatus, String> _tags = {
VegetarianStatus.VEGETARIAN: 'en:vegetarian',
VegetarianStatus.NON_VEGETARIAN: 'en:non-vegetarian',
VegetarianStatus.MAYBE_VEGETARIAN: 'en:maybe-vegetarian',
VegetarianStatus.VEGETARIAN_STATUS_UNKNOWN: 'en:vegetarian-status-unknown',
};
String get tag => _tags[this]!;
}

enum PalmOilFreeStatus {
PALM_OIL_FREE,
PALM_OIL,
MAY_CONTAIN_PALM_OIL,
PALM_OIL_CONTENT_UNKNOWN,
}

extension PalmOilFreeStatusExtension on PalmOilFreeStatus {
static const Map<PalmOilFreeStatus, String> _tags = {
PalmOilFreeStatus.PALM_OIL_FREE: 'en:palm-oil-free',
PalmOilFreeStatus.PALM_OIL: 'en:palm-oil',
PalmOilFreeStatus.MAY_CONTAIN_PALM_OIL: 'en:may-contain-palm-oil',
PalmOilFreeStatus.PALM_OIL_CONTENT_UNKNOWN: 'en:palm-oil-content-unknown',
};
String get tag => _tags[this]!;
}

class IngredientsAnalysisTags {
IngredientsAnalysisTags(List<dynamic> data) {
veganStatus = _getVeganStatus(data);
vegetarianStatus = _getVegetarianStatus(data);
palmOilFreeStatus = _getPalmOilFreeStatus(data);
}

static const List<String> _VEGAN_TAGS = [
'en:vegan',
'en:non-vegan',
'en:maybe-vegan',
'en:vegan-status-unknown',
];

static const List<VeganStatus> _VEGAN_STATUSES = [
VeganStatus.VEGAN,
VeganStatus.NON_VEGAN,
VeganStatus.MAYBE_VEGAN,
VeganStatus.VEGAN_STATUS_UNKNOWN,
];

static const List<String> _VEGETARIAN_TAGS = [
'en:vegetarian',
'en:non-vegetarian',
'en:maybe-vegetarian',
'en:vegetarian-status-unknown',
];

static const List<VegetarianStatus> _VEGETARIAN_STATUSES = [
VegetarianStatus.VEGETARIAN,
VegetarianStatus.NON_VEGETARIAN,
VegetarianStatus.MAYBE_VEGETARIAN,
VegetarianStatus.VEGETARIAN_STATUS_UNKNOWN,
];

static const List<String> _PALM_OIL_FREE_TAGS = [
'en:palm-oil-free',
'en:palm-oil',
'en:may-contain-palm-oil',
'en:palm-oil-content-unknown',
];

static const List<PalmOilFreeStatus> _PALM_OIL_FREE_STATUSES = [
PalmOilFreeStatus.PALM_OIL_FREE,
PalmOilFreeStatus.PALM_OIL,
PalmOilFreeStatus.MAY_CONTAIN_PALM_OIL,
PalmOilFreeStatus.PALM_OIL_CONTENT_UNKNOWN,
];

static VeganStatus? _getVeganStatus(List<dynamic> data) {
VeganStatus? result;
_VEGAN_TAGS.asMap().forEach((key, value) {
if (data.contains(value)) {
result = _VEGAN_STATUSES[key];
for (final VeganStatus status in VeganStatus.values) {
if (data.contains(status.tag)) {
return status;
}
});
return result;
}
return null;
}

static VegetarianStatus? _getVegetarianStatus(List<dynamic> data) {
VegetarianStatus? result;
_VEGETARIAN_TAGS.asMap().forEach((key, value) {
if (data.contains(value)) {
result = _VEGETARIAN_STATUSES[key];
for (final VegetarianStatus status in VegetarianStatus.values) {
if (data.contains(status.tag)) {
return status;
}
});
return result;
}
return null;
}

static PalmOilFreeStatus? _getPalmOilFreeStatus(List<dynamic> data) {
PalmOilFreeStatus? result;
_PALM_OIL_FREE_TAGS.asMap().forEach((key, value) {
if (data.contains(value)) {
result = _PALM_OIL_FREE_STATUSES[key];
for (final PalmOilFreeStatus status in PalmOilFreeStatus.values) {
if (data.contains(status.tag)) {
return status;
}
});
return result;
}
return null;
}

VeganStatus? veganStatus;
Expand All @@ -113,23 +98,15 @@ class IngredientsAnalysisTags {
return result;
}

_VEGAN_STATUSES.asMap().forEach((key, value) {
if (ingredientsAnalysisTags.veganStatus == value) {
result.add(_VEGAN_TAGS[key]);
}
});

_VEGETARIAN_STATUSES.asMap().forEach((key, value) {
if (ingredientsAnalysisTags.vegetarianStatus == value) {
result.add(_VEGETARIAN_TAGS[key]);
}
});

_PALM_OIL_FREE_STATUSES.asMap().forEach((key, value) {
if (ingredientsAnalysisTags.palmOilFreeStatus == value) {
result.add(_PALM_OIL_FREE_TAGS[key]);
}
});
if (ingredientsAnalysisTags.veganStatus != null) {
result.add(ingredientsAnalysisTags.veganStatus!.tag);
}
if (ingredientsAnalysisTags.vegetarianStatus != null) {
result.add(ingredientsAnalysisTags.vegetarianStatus!.tag);
}
if (ingredientsAnalysisTags.palmOilFreeStatus != null) {
result.add(ingredientsAnalysisTags.palmOilFreeStatus!.tag);
}

return result;
}
Expand Down
36 changes: 36 additions & 0 deletions lib/model/parameter/IngredientsAnalysisParameter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:openfoodfacts/interface/Parameter.dart';
import 'package:openfoodfacts/model/IngredientsAnalysisTags.dart';

/// Ingredients Analysis search API parameter.
class IngredientsAnalysisParameter extends Parameter {
@override
String getName() => 'ingredients_analysis_tags';

@override
String getValue() {
final List<String> result = <String>[];
if (veganStatus != null) {
result.add(veganStatus!.tag);
}
if (vegetarianStatus != null) {
result.add(vegetarianStatus!.tag);
}
if (palmOilFreeStatus != null) {
result.add(palmOilFreeStatus!.tag);
}
if (result.isEmpty) {
return '';
}
return result.join(',');
}

final VeganStatus? veganStatus;
final VegetarianStatus? vegetarianStatus;
final PalmOilFreeStatus? palmOilFreeStatus;

const IngredientsAnalysisParameter({
this.veganStatus,
this.vegetarianStatus,
this.palmOilFreeStatus,
});
}
4 changes: 4 additions & 0 deletions lib/model/parameter/SortBy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class SortBy extends Parameter {
SortOption.EDIT: 'last_modified_t',
SortOption.POPULARITY: 'unique_scans_n',
SortOption.NOTHING: 'nothing',
SortOption.ECOSCORE: 'ecoscore_score',
SortOption.NUTRISCORE: 'nutriscore_score',
};

@override
Expand All @@ -28,4 +30,6 @@ enum SortOption {
CREATED,
EDIT,
NOTHING,
ECOSCORE,
NUTRISCORE,
}
24 changes: 14 additions & 10 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import 'utils/HttpHelper.dart';
import 'utils/LanguageHelper.dart';
import 'utils/ProductHelper.dart';
import 'utils/ProductQueryConfigurations.dart';
import 'utils/ProductSearchQueryConfiguration.dart';

export 'events.dart';
export 'folksonomy.dart';
Expand Down Expand Up @@ -293,14 +292,6 @@ class OpenFoodAPIClient {
/// Returns the list of products as SearchResult.
/// Query the language specific host from OpenFoodFacts.
static Future<SearchResult> searchProducts(
final User? user,
final ProductSearchQueryConfiguration configuration, {
final QueryType? queryType,
}) async =>
getProducts(user, configuration, queryType: queryType);

/// Returns products searched according to a [configuration].
static Future<SearchResult> getProducts(
final User? user,
final AbstractQueryConfiguration configuration, {
final QueryType? queryType,
Expand All @@ -312,7 +303,20 @@ class OpenFoodAPIClient {
return result;
}

/// Returns products searched according to a [configuration].
// TODO: deprecated from 2022-07-26; remove when old enough
@Deprecated('Use searchProducts instead')
static Future<SearchResult> getProducts(
final User? user,
final AbstractQueryConfiguration configuration, {
final QueryType? queryType,
}) async =>
searchProducts(user, configuration, queryType: queryType);

/// Search the OpenFoodFacts product database: multiple barcodes in input.
// TODO: deprecated from 2022-07-26; remove when old enough
@Deprecated(
'Use method searchProducts with ProductListQueryConfiguration instead')
static Future<SearchResult> getProductList(
final User? user,
final ProductListQueryConfiguration configuration, {
Expand All @@ -328,7 +332,7 @@ class OpenFoodAPIClient {
final OpenFoodFactsCountry? country,
final QueryType? queryType,
}) async {
final SearchResult searchResult = await getProductList(
final SearchResult searchResult = await searchProducts(
user,
ProductListQueryConfiguration(
barcodes,
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/ProductListQueryConfiguration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ProductListQueryConfiguration extends AbstractQueryConfiguration {
Map<String, String> getParametersMap() {
final Map<String, String> result = super.getParametersMap();

result['code'] = '${barcodes.join(',')}.json';
result['code'] = barcodes.join(',');

return result;
}
Expand Down
10 changes: 5 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:
json_annotation: ^4.5.0
json_annotation: ^4.6.0
http: ^0.13.4
path: ^1.8.2
image: ^3.1.3
image: ^3.2.0
meta: ^1.8.0

dev_dependencies:
analyzer: ">=4.0.0 <5.0.0"
build_runner: ^2.1.11
json_serializable: ^6.2.0
build_runner: ^2.2.0
json_serializable: ^6.3.1
lints: ^2.0.0
test: ^1.21.1
test: ^1.21.4
coverage: ^1.3.2
6 changes: 5 additions & 1 deletion test/api_getProduct_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,11 @@ void main() {
expect(result.product?.packaging, 'de:In einer Plastikflasche');
expect(result.product?.packagingTags, ['de:in-einer-plastikflasche']);
expect(result.product?.quantity, '5.5 Liter');
});
},
timeout: Timeout(
// some tests can be slow here
Duration(seconds: 90),
));

test('get new product fields', () async {
final ProductQueryConfiguration configuration = ProductQueryConfiguration(
Expand Down
2 changes: 1 addition & 1 deletion test/api_getToBeCompletedProducts_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void main() {

final SearchResult result;
try {
result = await OpenFoodAPIClient.getProducts(
result = await OpenFoodAPIClient.searchProducts(
OpenFoodAPIConfiguration.globalUser,
configuration,
queryType: OpenFoodAPIConfiguration.globalQueryType,
Expand Down
2 changes: 1 addition & 1 deletion test/api_getUserProducts_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void main() {

final SearchResult result;
try {
result = await OpenFoodAPIClient.getProducts(
result = await OpenFoodAPIClient.searchProducts(
OpenFoodAPIConfiguration.globalUser,
configuration,
queryType: OpenFoodAPIConfiguration.globalQueryType,
Expand Down
2 changes: 1 addition & 1 deletion test/api_matchedProductV2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void main() {
BARCODE_CHORIZO,
];

final SearchResult result = await OpenFoodAPIClient.getProductList(
final SearchResult result = await OpenFoodAPIClient.searchProducts(
OpenFoodAPIConfiguration.globalUser,
ProductListQueryConfiguration(
inputBarcodes,
Expand Down
2 changes: 1 addition & 1 deletion test/api_saveProduct_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void main() {
);

testProductResult1(result2);
});
}, skip: 'Random results');

/// Returns a timestamp up to the minute level.
String _getMinuteTimestamp() =>
Expand Down
Loading

0 comments on commit 9bcfc74

Please sign in to comment.