addToSearchHistory method
Add the searchTerm
to a search history with the given historyKey
. If
there are already a maximum number of items in history, this will be
capped. Oldest items will be discarded in that scenario.
Implementation
void addToSearchHistory({
required String historyKey,
required String searchTerm,
}) async {
if (searchTerm.trim().isEmpty) {
return;
}
_database.writeTxnSync(() {
SearchHistoryItem searchHistoryItem = SearchHistoryItem(
searchTerm: searchTerm,
historyKey: historyKey,
);
_database.searchHistoryItems
.deleteByUniqueKeySync(searchHistoryItem.uniqueKey);
_database.searchHistoryItems.putSync(searchHistoryItem);
int countInSameHistory = _database.searchHistoryItems
.filter()
.historyKeyEqualTo(historyKey)
.countSync();
if (maximumSearchHistoryItems < countInSameHistory) {
int surplus = countInSameHistory - maximumSearchHistoryItems;
_database.searchHistoryItems
.filter()
.historyKeyEqualTo(historyKey)
.limit(surplus)
.build()
.deleteAllSync();
}
});
}