update carton form and add recent search

This commit is contained in:
tzw
2024-09-23 18:36:23 +06:30
parent 458884bf70
commit 3629a8312c
16 changed files with 292 additions and 96 deletions

View File

@@ -88,4 +88,24 @@ class SharedPref {
final prefs = await SharedPreferences.getInstance();
prefs.remove(key);
}
static Future<void> saveRecentSearch(String key, String query) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Use `Set` to avoid duplication of recentSearches
Set<String> allSearches = prefs.getStringList(key)?.toSet() ?? {};
//Place it at first in the set
allSearches = {query, ...allSearches};
prefs.setStringList(key, allSearches.toList());
}
static Future<List<String>?> getRecentSearch(String key, String query) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final allSearches = prefs.getStringList(key);
return allSearches!.where((search) => search.startsWith(query)).toList();
}
static Future<void> clearRecentSearch(String key) async {
return await _remove(key);
}
}