Skip to content

Commit

Permalink
feat: #307 - added 21 enum values for advanced search parameters (#313)
Browse files Browse the repository at this point in the history
Impacted files:
* `api_searchProduct_test.dart`: additional tests on all `TagFilterType`s; refactoring
* `TagFilter.dart`: added more explicit `enum TagFilterType` as filter types
  • Loading branch information
monsieurtanuki authored Dec 12, 2021
1 parent 2da4d27 commit 55f3fa5
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 43 deletions.
75 changes: 75 additions & 0 deletions lib/model/parameter/TagFilter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
import 'package:openfoodfacts/interface/Parameter.dart';

/// Filter types for advanced search parameters
enum TagFilterType {
BRANDS,
CATEGORIES,
PACKAGING,
LABELS,
ORIGINS,
MANUFACTURING_PLACES,
EMB_CODES,
PURCHASE_PLACES,
STORES,
COUNTRIES,
ADDITIVES,
ALLERGENS,
TRACES,
NUTRITION_GRADES,
STATES,
INGREDIENTS,
NOVA_GROUPS,
LANGUAGES,
CREATOR,
EDITORS,
LANG,
}

extension TagFilterTypeExtension on TagFilterType {
static const Map<TagFilterType, String> _map = <TagFilterType, String>{
TagFilterType.BRANDS: 'brands',
TagFilterType.CATEGORIES: 'categories',
TagFilterType.PACKAGING: 'packaging',
TagFilterType.LABELS: 'labels',
TagFilterType.ORIGINS: 'origins',
TagFilterType.MANUFACTURING_PLACES: 'manufacturing_places',
TagFilterType.EMB_CODES: 'emb_codes',
TagFilterType.PURCHASE_PLACES: 'purchase_places',
TagFilterType.STORES: 'stores',
TagFilterType.COUNTRIES: 'countries',
TagFilterType.ADDITIVES: 'additives',
TagFilterType.ALLERGENS: 'allergens',
TagFilterType.TRACES: 'traces',
TagFilterType.NUTRITION_GRADES: 'nutrition_grades',
TagFilterType.STATES: 'states',
TagFilterType.INGREDIENTS: 'ingredients',
TagFilterType.NOVA_GROUPS: 'nova_groups',
TagFilterType.LANGUAGES: 'languages',
TagFilterType.CREATOR: 'creator',
TagFilterType.EDITORS: 'editors',
TagFilterType.LANG: 'lang',
};

String get key => _map[this]!;
}

/// Tag filter ("LIST contains/without ITEM") search API parameter
///
/// Eg. 'nutrition_grades' contains 'A'
Expand All @@ -25,9 +78,31 @@ class TagFilter extends Parameter {
final bool contains;
final String tagName;

// TODO: deprecated from 2021-12-12 (#307); remove when old enough
@Deprecated('Use TagFilter.fromType instead')
const TagFilter({
required final String tagType,
required final bool contains,
required final String tagName,
}) : this._(
tagType: tagType,
contains: contains,
tagName: tagName,
);

const TagFilter._({
required this.tagType,
required this.contains,
required this.tagName,
});

TagFilter.fromType({
required final TagFilterType tagFilterType,
required final String tagName,
final bool contains = true,
}) : this._(
tagType: tagFilterType.key,
contains: contains,
tagName: tagName,
);
}
Loading

0 comments on commit 55f3fa5

Please sign in to comment.