aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2020-06-05 21:55:06 +0530
committerAkshay Nair <phenax5@gmail.com>2020-06-05 21:55:06 +0530
commitf4fc9576aa77045d3b6c26263a7200af0c8b6151 (patch)
tree5d139bb258ae60fb3d1371652ad6c884ead3abc9
parent83be047e15efd91a26ac27da1710b0e273706b19 (diff)
downloaddaft-launcher-f4fc9576aa77045d3b6c26263a7200af0c8b6151.tar.gz
daft-launcher-f4fc9576aa77045d3b6c26263a7200af0c8b6151.zip
refactors application data to use streams
-rw-r--r--lib/components/SearchableAppList.dart31
-rw-r--r--lib/data/applications.dart22
-rw-r--r--lib/data/favorites.dart1
-rw-r--r--lib/main.dart12
-rw-r--r--lib/pages/Apps.dart11
5 files changed, 45 insertions, 32 deletions
diff --git a/lib/components/SearchableAppList.dart b/lib/components/SearchableAppList.dart
index 249b763..d9c7a17 100644
--- a/lib/components/SearchableAppList.dart
+++ b/lib/components/SearchableAppList.dart
@@ -8,9 +8,6 @@ class _SearchableAppListState extends State<SearchableAppList> {
void initState() {
super.initState();
- _inputController.addListener(() {
-
- });
}
void dispose() {
@@ -40,6 +37,11 @@ class _SearchableAppListState extends State<SearchableAppList> {
Widget build(BuildContext ctx) {
ThemeData theme = Theme.of(context);
+ List<Application> results = widget.appList
+ .where(_filterApp)
+ .toList();
+ results.sort(_appSorter);
+
return Column(
children: [
Container(
@@ -47,13 +49,10 @@ class _SearchableAppListState extends State<SearchableAppList> {
child: Align(
alignment: Alignment.topRight,
child: TextField(
- //onChanged: onInput,
enableSuggestions: false,
controller: _inputController,
decoration: InputDecoration(
contentPadding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 8.0),
- //prefix: Container(padding: , child: Text('')),
- //prefixStyle: TextStyle(color: theme.primaryColor),
hintStyle: TextStyle(color: Color(0x88D8DEE9)),
hintText: 'Search',
suffix: IconButton(
@@ -64,24 +63,10 @@ class _SearchableAppListState extends State<SearchableAppList> {
)
),
),
- Expanded(child: FutureBuilder(
- future: widget.appListF,
- builder: (ctx, AsyncSnapshot<List<Application>> snap) {
- if (!snap.hasData) {
- return Text('Loading...');
- }
-
- List<Application> results = snap.data
- .where(_filterApp)
- .toList();
- results.sort(_appSorter);
-
- return AppList(
+ Expanded(child: AppList(
appList: results,
openApp: widget.openApp,
openOptionsMenu: widget.openOptionsMenu,
- );
- }
)),
],
);
@@ -89,11 +74,11 @@ class _SearchableAppListState extends State<SearchableAppList> {
}
class SearchableAppList extends StatefulWidget {
- Future<List<Application>> appListF;
+ List<Application> appList;
void Function(Application) openApp;
void Function(Application) openOptionsMenu;
- SearchableAppList({ this.appListF, this.openApp, this.openOptionsMenu }): super();
+ SearchableAppList({ this.appList, this.openApp, this.openOptionsMenu }): super();
@override
_SearchableAppListState createState() => _SearchableAppListState();
diff --git a/lib/data/applications.dart b/lib/data/applications.dart
new file mode 100644
index 0000000..c0f8f32
--- /dev/null
+++ b/lib/data/applications.dart
@@ -0,0 +1,22 @@
+import 'package:device_apps/device_apps.dart';
+import 'dart:async';
+
+StreamController<List<Application>> applications_$ = StreamController<List<Application>>.broadcast();
+
+Future<List<Application>> getApplications() {
+ return DeviceApps.getInstalledApplications(
+ includeSystemApps: true,
+ onlyAppsWithLaunchIntent: true,
+ includeAppIcons: false,
+ );
+}
+
+void refreshApplications() async {
+ List<Application> apps = await getApplications();
+ applications_$.add(apps);
+}
+
+Stream<List<Application>> getApplications$() {
+ return applications_$.stream;
+}
+
diff --git a/lib/data/favorites.dart b/lib/data/favorites.dart
index ba78d44..e07a43d 100644
--- a/lib/data/favorites.dart
+++ b/lib/data/favorites.dart
@@ -3,7 +3,6 @@ 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();
diff --git a/lib/main.dart b/lib/main.dart
index a8cd8ab..fa48706 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -11,6 +11,7 @@ import 'components/AppContextMenu.dart';
import 'data/config.dart';
import 'data/favorites.dart';
+import 'data/applications.dart';
import 'helpers/StreamState.dart';
void main() {
@@ -27,16 +28,26 @@ class MyAppState extends StreamState<MyApp> {
stream$: getFavorites$(),
value: [],
);
+ final applications = StreamStateValue<List<Application>>(
+ stream$: getApplications$(),
+ value: [],
+ );
final dateTime = StreamStateValue<DateTime>(
stream$: Stream.periodic(Duration(seconds: 1), (_x) => DateTime.now()).asBroadcastStream(),
value: DateTime.now(),
);
void initState() {
super.initState();
+
initStateValue(config);
refreshConfig();
+
initStateValue(favoriteApps);
refreshFavorites();
+
+ initStateValue(applications);
+ refreshApplications();
+
initStateValue(dateTime);
}
@@ -103,6 +114,7 @@ class MyAppState extends StreamState<MyApp> {
openOptionsMenu: openOptionsMenu,
),
AppsView(
+ appList: applications.value,
openApp: openApp,
openOptionsMenu: openOptionsMenu,
),
diff --git a/lib/pages/Apps.dart b/lib/pages/Apps.dart
index 97d2dbc..f221d27 100644
--- a/lib/pages/Apps.dart
+++ b/lib/pages/Apps.dart
@@ -9,21 +9,16 @@ import '../data/favorites.dart';
class AppsView extends StatelessWidget {
void Function(Application) openApp;
void Function(BuildContext, Application) openOptionsMenu;
-
- AppsView({ this.openApp, this.openOptionsMenu }): super();
+ List<Application> appList;
- Future<List<Application>> appListF = DeviceApps.getInstalledApplications(
- includeSystemApps: true,
- onlyAppsWithLaunchIntent: true,
- includeAppIcons: false,
- );
+ AppsView({ this.appList, this.openApp, this.openOptionsMenu }): super();
@override
Widget build(BuildContext ctx) {
return FixedContainer(
padding: const EdgeInsets.symmetric(vertical: 36.0, horizontal: 16.0),
child: SearchableAppList(
- appListF: appListF,
+ appList: appList,
openApp: openApp,
openOptionsMenu: (app) => openOptionsMenu(ctx, app),
),