blob: ba78d443692e1b7a4b8e19cfc01278cc0cb3681b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import 'package:shared_preferences/shared_preferences.dart';
import 'package:device_apps/device_apps.dart';
import 'dart:async';
const String FAVORITES = 'favorites';
const String SEPERATOR = ',';
StreamController<List<Application>> favorites_$ = StreamController<List<Application>>.broadcast();
Future<Application> getApplication(String pkg) async {
if (pkg == null || pkg.length == 0) return null;
try {
return DeviceApps.getApp(pkg);
} catch(e) {
return null;
}
}
Future<List<Application>> getFavorites() async {
final prefs = await SharedPreferences.getInstance();
List<String> packageNames = prefs.getStringList(FAVORITES) ?? [];
Iterable<Future<Application>> futures = packageNames.map(getApplication);
List<Application> result = await Future.wait(futures);
return result.where((a) => a != null).map((a) => a as Application).toList() as List<Application>;
}
void refreshFavorites() async {
List<Application> fs = await getFavorites();
favorites_$.add(fs);
}
Stream<List<Application>> getFavorites$() {
return favorites_$.stream;
}
void setFavorites(List<Application> favs) async {
final prefs = await SharedPreferences.getInstance();
List<String> favList = favs.map((Application app) => app.packageName).toList();
prefs.setStringList(FAVORITES, favList);
favorites_$.add(await getFavorites());
}
void addToFavorites(Application app) async {
List<Application> fs = await getFavorites();
var matching = fs.where((a) => a.packageName == app.packageName);
if(matching.length == 0) {
fs.add(app);
}
await setFavorites(fs);
}
void removeFromFavorites(Application app) async {
List<Application> fs = await getFavorites();
fs.removeWhere((a) => a.packageName == app.packageName);
await setFavorites(fs);
}
|