searchDictionary method
Gets the raw unprocessed entries straight from a dictionary database given a search term. This will be processed later for user viewing.
Implementation
Future<DictionarySearchResult> searchDictionary({
required String searchTerm,
required bool searchWithWildcards,
bool useCache = true,
}) async {
if (_dictionarySearchCache[searchTerm] != null && useCache) {
return _dictionarySearchCache[searchTerm]!;
}
searchTerm = searchTerm.replaceAll('\n', ' ');
searchTerm = _removeEmoji.removemoji(searchTerm, ' ', false);
/// Strip lone surrogates that may crash the search.
RegExp loneSurrogate = RegExp(
'[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]',
);
searchTerm = searchTerm.replaceAll(loneSurrogate, ' ');
ReceivePort receivePort = ReceivePort();
receivePort.listen((message) {
debugPrint(message.toString());
});
DictionarySearchParams params = DictionarySearchParams(
searchTerm: searchTerm,
directoryPath: _databaseDirectory.path,
maximumDictionarySearchResults: maximumDictionarySearchResults,
maximumDictionaryTermsInResult: maximumTerms,
searchWithWildcards: searchWithWildcards,
enabledDictionaryIds: [],
sendPort: receivePort.sendPort,
);
if (params.searchTerm.trim().isEmpty) {
return DictionarySearchResult(searchTerm: searchTerm);
}
/// Searching also persists the result in the database. This is useful for
/// dictionary search history, as well as allowing a result to be linked
/// to the actual data, rather than duplicating that data within the
/// database, which is not ideal for storage purposes.
_searchOperation =
cancelable.compute(targetLanguage.prepareSearchResults, params);
int? id = await _searchOperation?.value;
if (id == null) {
return DictionarySearchResult(searchTerm: searchTerm);
}
DictionarySearchResult? result =
_database.dictionarySearchResults.getSync(id);
if (result != null) {
_dictionarySearchCache[searchTerm] = result;
return result;
} else {
return DictionarySearchResult(searchTerm: searchTerm);
}
}