aboutsummaryrefslogtreecommitdiff
path: root/lib/data
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2020-06-03 23:14:35 +0530
committerAkshay Nair <phenax5@gmail.com>2020-06-03 23:14:35 +0530
commit51dae022dcf9b4dcea7e7709ad0915c10977326a (patch)
tree53d1dab344fd157e0a8ea4238bb98df976e3f547 /lib/data
parent620f9ea0516d951e1d21793a5bb22e05a5c56949 (diff)
downloaddaft-launcher-51dae022dcf9b4dcea7e7709ad0915c10977326a.tar.gz
daft-launcher-51dae022dcf9b4dcea7e7709ad0915c10977326a.zip
Add to favorite feature complete
Diffstat (limited to 'lib/data')
-rw-r--r--lib/data/favorites.dart24
1 files changed, 21 insertions, 3 deletions
diff --git a/lib/data/favorites.dart b/lib/data/favorites.dart
index aad98ee..2e389b8 100644
--- a/lib/data/favorites.dart
+++ b/lib/data/favorites.dart
@@ -7,13 +7,26 @@ const String SEPERATOR = ',';
StreamController<List<Application>> favorites_$ = StreamController<List<Application>>.broadcast();
+Future<Application> getApplication(String pkg) async {
+ if (pkg.length == 0) return null;
+ try {
+ return DeviceApps.getApp(pkg);
+ } catch(e) {
+ return null;
+ }
+}
+
Future<List<Application>> getFavorites() async {
final prefs = await SharedPreferences.getInstance();
String rawFavoritesList = prefs.getString(FAVORITES);
List<String> packageNames = rawFavoritesList?.split(SEPERATOR) ?? <String>[];
- return Future.wait(packageNames.map((String pkg) => DeviceApps.getApp(pkg)));
+ Iterable<Future<Application>> futures = packageNames.map(getApplication);
+
+ List<Application> result = await Future.wait(futures);
+
+ return result.where((a) => a != null).toList();
}
void initFavorites() async {
@@ -28,7 +41,7 @@ Stream<List<Application>> getFavorites$() {
void setFavorites(List<Application> favs) async {
final prefs = await SharedPreferences.getInstance();
- String rawFavoritesList = favs.map((Application app) => app.packageName).join(SEPERATOR);
+ String rawFavoritesList = favs.map((Application app) => app.packageName).toList().join(SEPERATOR);
prefs.setString(FAVORITES, rawFavoritesList);
@@ -37,7 +50,12 @@ void setFavorites(List<Application> favs) async {
void addToFavorites(Application app) async {
List<Application> fs = await getFavorites();
- fs.add(app);
+
+ var matching = fs.where((a) => a.packageName == app.packageName);
+ if(matching.length == 0) {
+ fs.add(app);
+ }
+
await setFavorites(fs);
}