diff options
| author | Akshay Nair <phenax5@gmail.com> | 2020-06-04 00:41:06 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2020-06-04 00:41:06 +0530 |
| commit | 5c4149b723ccf685e3a7065bebcacd6341571ed1 (patch) | |
| tree | fa304d39ec56d603a42aa97c1119879366f1b3f2 | |
| parent | 133098d9a3b220cacf3d5ee539c65252764cb967 (diff) | |
| download | daft-launcher-5c4149b723ccf685e3a7065bebcacd6341571ed1.tar.gz daft-launcher-5c4149b723ccf685e3a7065bebcacd6341571ed1.zip | |
Adds remove from favorites + context menu for favorites items
| -rw-r--r-- | TODO.md | 1 | ||||
| -rw-r--r-- | lib/data/favorites.dart | 8 | ||||
| -rw-r--r-- | lib/main.dart | 55 | ||||
| -rw-r--r-- | lib/pages/Apps.dart | 47 | ||||
| -rw-r--r-- | lib/pages/Home.dart | 12 |
5 files changed, 74 insertions, 49 deletions
@@ -16,6 +16,7 @@ - [X] Get rid of defaultTime and loading - [X] Create favorites list - [X] Get favorites list from a persistent store + - [ ] Add actions to favorite items - [ ] Remove from favorites - [ ] Change the order of favorites? diff --git a/lib/data/favorites.dart b/lib/data/favorites.dart index 45f1e00..f088a71 100644 --- a/lib/data/favorites.dart +++ b/lib/data/favorites.dart @@ -59,3 +59,11 @@ void addToFavorites(Application app) async { await setFavorites(fs); } +void removeFromFavorites(Application app) async { + List<Application> fs = await getFavorites() as List<Application>; + + fs.removeWhere((a) => a.packageName == app.packageName); + + await setFavorites(fs); +} + diff --git a/lib/main.dart b/lib/main.dart index 54a02bf..25ea6ee 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,8 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:device_apps/device_apps.dart'; +import 'package:android_intent/android_intent.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'pages/Home.dart'; import 'pages/Apps.dart'; @@ -8,7 +10,6 @@ import 'pages/Apps.dart'; import 'data/config.dart'; import 'data/favorites.dart'; import 'helpers/StreamState.dart'; -import 'package:shared_preferences/shared_preferences.dart'; void main() { runApp(MyApp()); @@ -37,6 +38,49 @@ class MyAppState extends StreamState<MyApp> { initStateValue(dateTime); } + Widget buildOptionsMenu(BuildContext ctx, Application app) { + ThemeData theme = Theme.of(ctx); + + var isFavorite = -1 != favoriteApps.value.indexWhere((a) => a.packageName == app.packageName); + + return Dialog( + child: Container( + decoration: BoxDecoration( + color: theme.backgroundColor, + ), + height: 170.0, + child: ListView( + children: [ + Option(child: Text(isFavorite ? 'Remove from favorites' : 'Add to favorites'), onTap: () async { + await isFavorite ? removeFromFavorites(app) : addToFavorites(app); + }), + Option(child: Text('App Settings'), onTap: () async { + await AndroidIntent( + action: 'action_application_details_settings', + data: 'package:${app.packageName}', + ).launch(); + }), + Option(child: Text('Uninstall'), onTap: () async { + // FIXME: Doesn't work + await AndroidIntent( + action: 'action_delete', + data: 'package:${app.packageName}', + ).launch(); + }), + ], + ), + ), + ); + } + + void openApp(Application app) { + DeviceApps.openApp(app.packageName); + } + + void openOptionsMenu(BuildContext ctx, Application app) { + showDialog(context: ctx, builder: (BuildContext ctx) => buildOptionsMenu(ctx, app)); + } + ThemeData getLightTheme() { return ThemeData(brightness: Brightness.light); } @@ -68,7 +112,7 @@ class MyAppState extends StreamState<MyApp> { // This widget is the root of your application. @override - Widget build(BuildContext context) { + Widget build(BuildContext ctx) { return MaterialApp( title: 'owyn launcher', theme: getLightTheme(), @@ -81,8 +125,13 @@ class MyAppState extends StreamState<MyApp> { HomeView( dateTime: dateTime.value, favoriteApps: favoriteApps.value, + openApp: openApp, + openOptionsMenu: openOptionsMenu, + ), + AppsView( + openApp: openApp, + openOptionsMenu: openOptionsMenu, ), - AppsView(), ], ), ), diff --git a/lib/pages/Apps.dart b/lib/pages/Apps.dart index 7049799..8c61814 100644 --- a/lib/pages/Apps.dart +++ b/lib/pages/Apps.dart @@ -1,6 +1,5 @@ import 'package:flutter/material.dart'; import 'package:device_apps/device_apps.dart'; -import 'package:android_intent/android_intent.dart'; import '../components/FixedContainer.dart'; import '../components/SearchableAppList.dart'; @@ -32,52 +31,16 @@ class Option extends StatelessWidget { } class AppsView extends StatelessWidget { + void Function(Application) openApp; + void Function(BuildContext, Application) openOptionsMenu; + + AppsView({ this.openApp, this.openOptionsMenu }): super(); + Future<List<Application>> appListF = DeviceApps.getInstalledApplications( includeSystemApps: true, onlyAppsWithLaunchIntent: true, ); - void openApp(Application app) { - DeviceApps.openApp(app.packageName); - } - - void openOptionsMenu(BuildContext ctx, Application app) { - showDialog(context: ctx, builder: (BuildContext ctx) => buildOptionsMenu(ctx, app)); - } - - Widget buildOptionsMenu(BuildContext ctx, Application app) { - ThemeData theme = Theme.of(ctx); - - return Dialog( - child: Container( - decoration: BoxDecoration( - color: theme.backgroundColor, - ), - height: 170.0, - child: ListView( - children: [ - Option(child: Text('Add to favorites'), onTap: () async { - await addToFavorites(app); - }), - Option(child: Text('App Settings'), onTap: () async { - await AndroidIntent( - action: 'action_application_details_settings', - data: 'package:${app.packageName}', - ).launch(); - }), - Option(child: Text('Uninstall'), onTap: () async { - // FIXME: Doesn't work - await AndroidIntent( - action: 'action_delete', - data: 'package:${app.packageName}', - ).launch(); - }), - ], - ), - ), - ); - } - @override Widget build(BuildContext ctx) { return FixedContainer( diff --git a/lib/pages/Home.dart b/lib/pages/Home.dart index 2930bf8..3e22f93 100644 --- a/lib/pages/Home.dart +++ b/lib/pages/Home.dart @@ -77,10 +77,10 @@ class StatusInfoCard extends StatelessWidget { class HomeView extends StatelessWidget { DateTime dateTime; List<Application> favoriteApps; + void Function(Application) openApp; + void Function(BuildContext, Application) openOptionsMenu; - HomeView({ this.dateTime, this.favoriteApps }): super(); - - void noop(Application app) {} + HomeView({ this.dateTime, this.favoriteApps, this.openApp, this.openOptionsMenu }): super(); @override Widget build(BuildContext ctx) { @@ -90,7 +90,11 @@ class HomeView extends StatelessWidget { children: [ StatusInfoCard(dateTime: dateTime), Expanded(child: Container( - child: AppList(appList: favoriteApps, openApp: noop, openOptionsMenu: noop), + child: AppList( + appList: favoriteApps, + openApp: openApp, + openOptionsMenu: (app) => openOptionsMenu(ctx, app), + ), )), ], ) |
