From 96c7c72dbd991cc6c086ab9566ca7cf451cee985 Mon Sep 17 00:00:00 2001 From: Affan Shaikhsurab <51104750+AffanShaikhsurab@users.noreply.github.com> Date: Fri, 24 Jan 2025 14:21:44 +0530 Subject: [PATCH] feat: Add excludedItems parameter to autocomplete function with dynamic 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. --- lib/src/open_food_search_api_client.dart | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/src/open_food_search_api_client.dart b/lib/src/open_food_search_api_client.dart index f298c896c0..7b815a599c 100644 --- a/lib/src/open_food_search_api_client.dart +++ b/lib/src/open_food_search_api_client.dart @@ -33,6 +33,7 @@ class OpenFoodSearchAPIClient { final int size = 10, final Fuzziness fuzziness = Fuzziness.none, final UriProductHelper uriHelper = uriHelperFoodProd, + final List? excludedItems, }) async { query = query.trim(); if (query.isEmpty) { @@ -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, }, ); @@ -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; } }