Skip to content

Commit

Permalink
feat: Add excludedItems parameter to autocomplete function with dynam…
Browse files Browse the repository at this point in the history
…ic size adjustment


**Description (optional):**  
- Added `excludedItems` parameter to filter out unwanted items from auto-suggestions.  
- Dynamically adjusted the `size` parameter to request additional items based on the length of the exclusion list.  
- Ensured safety by handling potential null values in `excludedItems`.  
- Improved overall flexibility of the autocomplete functionality.
  • Loading branch information
AffanShaikhsurab authored Jan 24, 2025
1 parent 25fd0af commit 96c7c72
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/src/open_food_search_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class OpenFoodSearchAPIClient {
final int size = 10,
final Fuzziness fuzziness = Fuzziness.none,
final UriProductHelper uriHelper = uriHelperFoodProd,
final List<String>? excludedItems,
}) async {
query = query.trim();
if (query.isEmpty) {
Expand All @@ -52,7 +53,7 @@ class OpenFoodSearchAPIClient {
'q': query,
'taxonomy_names': taxonomyTags.join(','),
'lang': language.offTag,
'size': size.toString(),
'size': (size + (excludedItems?.length ?? 0)).toString(),
'fuzziness': fuzziness.offTag,
},
);
Expand All @@ -61,8 +62,19 @@ class OpenFoodSearchAPIClient {
user: user,
uriHelper: uriHelper,
);
return AutocompleteSearchResult.fromJson(
// Parse the response
final AutocompleteSearchResult autocompleteSearchResult =
AutocompleteSearchResult.fromJson(
HttpHelper().jsonDecode(utf8.decode(response.bodyBytes)),
);
// If no exclusions are specified, return the result as-is
if (excludedItems == null || excludedItems.isEmpty) {
return autocompleteSearchResult;
}
// Filter out excluded items
autocompleteSearchResult.options?.removeWhere(
(option) => excludedItems.contains(option.text),
);
return autocompleteSearchResult;
}
}

0 comments on commit 96c7c72

Please sign in to comment.