-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: autocomplete manager for both TagTypes and TaxonomyNames (#851)
* feat: autocomplete manager for both TagTypes and TaxonomyNames New files: * `autocomplete_manager.dart`: Manager that returns the suggestions for the latest input. Code was largely moved from `suggestion_manager.dart` as refactoring. Manager that returns the suggestions for the latest input. * `autocompleter.dart`: Interface that provides autocomplete suggestions. * `tag_type_autocompleter.dart`: Autocomplete suggestions for TagType. * `taxonomy_name_autocompleter.dart`: Autocomplete suggestions for TaxonomyNames. Impacted files: * `api_suggestion_manager_test.dart`: added test for elastic search * `fuzziness.dart`: renamed from `fuzziness_level.dart` * `open_food_search_api_client.dart`: minor refactoring * `openfoodfacts.dart`: added the new 4 files and impacted the file renaming * `suggestion_manager.dart`: deprecated it in favor of new classes `TagTypeAutocompleter` and `AutocompleteManager` * fix after self-review
- Loading branch information
1 parent
7a1f4e6
commit 6bb8deb
Showing
9 changed files
with
227 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'dart:async'; | ||
|
||
import '../model/user.dart'; | ||
import '../open_food_search_api_client.dart'; | ||
import '../utils/autocompleter.dart'; | ||
import '../utils/language_helper.dart'; | ||
import '../utils/open_food_api_configuration.dart'; | ||
import '../utils/uri_helper.dart'; | ||
import 'autocomplete_search_result.dart'; | ||
import 'autocomplete_single_result.dart'; | ||
import 'fuzziness.dart'; | ||
import 'taxonomy_name.dart'; | ||
|
||
/// Autocomplete suggestions for [TaxonomyName]s. | ||
class TaxonomyNameAutocompleter implements Autocompleter { | ||
const TaxonomyNameAutocompleter({ | ||
required this.taxonomyNames, | ||
required this.language, | ||
this.limit = 25, | ||
this.uriHelper = uriHelperFoodProd, | ||
this.user, | ||
this.fuzziness = Fuzziness.none, | ||
}); | ||
|
||
final List<TaxonomyName> taxonomyNames; | ||
final OpenFoodFactsLanguage language; | ||
final int limit; | ||
final UriProductHelper uriHelper; | ||
final User? user; | ||
final Fuzziness fuzziness; | ||
|
||
@override | ||
Future<List<String>> getSuggestions( | ||
final String input, | ||
) async { | ||
final AutocompleteSearchResult results = | ||
await OpenFoodSearchAPIClient.autocomplete( | ||
language: language, | ||
query: input, | ||
taxonomyNames: taxonomyNames, | ||
size: limit, | ||
user: user, | ||
uriHelper: uriHelper, | ||
fuzziness: fuzziness, | ||
); | ||
final List<String> result = <String>[]; | ||
if (results.options == null) { | ||
return result; | ||
} | ||
for (final AutocompleteSingleResult item in results.options!) { | ||
result.add(item.text); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:meta/meta.dart'; | ||
import 'autocompleter.dart'; | ||
|
||
/// Manager that returns the suggestions for the latest input. | ||
/// | ||
/// Typical use case: the user types one character (which triggers a call to | ||
/// suggestions), then another character (which triggers another call). | ||
/// What if the second call is much faster than the first one, for server or | ||
/// connection reasons? The autocomplete widget will get the second suggestions, | ||
/// then the first suggestions will override them. | ||
/// And the user should get the suggestions that match the latest input. | ||
class AutocompleteManager implements Autocompleter { | ||
AutocompleteManager(this.autocompleter); | ||
|
||
final Autocompleter autocompleter; | ||
|
||
final List<String> _inputs = <String>[]; | ||
final Map<String, List<String>> _cache = <String, List<String>>{}; | ||
|
||
@override | ||
Future<List<String>> getSuggestions( | ||
final String input, | ||
) async { | ||
_inputs.add(input); | ||
final List<String>? cached = _cache[input]; | ||
if (cached != null) { | ||
return cached; | ||
} | ||
await waitForTestPurpose(); | ||
_cache[input] = await autocompleter.getSuggestions(input); | ||
// meanwhile there might have been some calls to this method, adding inputs. | ||
for (final String latestInput in _inputs.reversed) { | ||
final List<String>? cached = _cache[latestInput]; | ||
if (cached != null) { | ||
return cached; | ||
} | ||
} | ||
// not supposed to happen, as we should have downloaded for "input". | ||
return <String>[]; | ||
} | ||
|
||
@protected | ||
@visibleForTesting | ||
Future<void> waitForTestPurpose() async {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/// Interface that provides autocomplete suggestions. | ||
abstract class Autocompleter { | ||
Future<List<String>> getSuggestions(final String input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'dart:async'; | ||
|
||
import '../model/user.dart'; | ||
import '../open_food_api_client.dart'; | ||
import 'autocompleter.dart'; | ||
import 'country_helper.dart'; | ||
import 'language_helper.dart'; | ||
import 'open_food_api_configuration.dart'; | ||
import 'uri_helper.dart'; | ||
import 'tag_type.dart'; | ||
|
||
/// Autocomplete suggestions for [TagType]. | ||
class TagTypeAutocompleter implements Autocompleter { | ||
const TagTypeAutocompleter({ | ||
required this.tagType, | ||
required this.language, | ||
this.country, | ||
this.categories, | ||
this.shape, | ||
this.limit = 25, | ||
this.uriHelper = uriHelperFoodProd, | ||
this.user, | ||
}); | ||
|
||
final TagType tagType; | ||
final OpenFoodFactsLanguage language; | ||
final OpenFoodFactsCountry? country; | ||
final String? categories; | ||
final String? shape; | ||
final int limit; | ||
final UriProductHelper uriHelper; | ||
final User? user; | ||
|
||
@override | ||
Future<List<String>> getSuggestions( | ||
final String input, | ||
) async => | ||
OpenFoodAPIClient.getSuggestions( | ||
tagType, | ||
input: input, | ||
language: language, | ||
country: country, | ||
categories: categories, | ||
shape: shape, | ||
limit: limit, | ||
uriHelper: uriHelper, | ||
user: user, | ||
); | ||
} |
Oops, something went wrong.