Skip to content

Commit f45bd49

Browse files
committed
[Feature] update all shortcuts
1 parent 6a4c435 commit f45bd49

File tree

6 files changed

+155
-26
lines changed

6 files changed

+155
-26
lines changed

android/src/main/java/com/divyanshushekhar/flutter_shortcuts/MethodCallImplementation.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.content.res.Resources;
1010
import android.graphics.drawable.Icon;
1111
import android.os.Build;
12+
import android.widget.Toast;
1213

1314
import androidx.annotation.NonNull;
1415
import androidx.annotation.RequiresApi;
@@ -48,6 +49,13 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
4849
List<Map<String, String>> arg = call.arguments();
4950
List<ShortcutInfo> shortcuts = processShortcuts(arg);
5051
shortcutManager.setDynamicShortcuts(shortcuts);
52+
Toast.makeText(context, "Shortcut Created", Toast.LENGTH_SHORT).show();
53+
break;
54+
case "updateShortcutItems":
55+
List<Map<String, String>> updateShortcutArgs = call.arguments();
56+
List<ShortcutInfo> updateShortcuts = processShortcuts(updateShortcutArgs);
57+
boolean updated = shortcutManager.updateShortcuts(updateShortcuts);
58+
Toast.makeText(context, "Shortcut Updated: " + updated, Toast.LENGTH_SHORT).show();
5159
break;
5260
case "clearShortcutItems":
5361
shortcutManager.removeAllDynamicShortcuts();
@@ -81,10 +89,11 @@ private List<ShortcutInfo> processShortcuts(List<Map<String, String>> shortcuts)
8189
final List<ShortcutInfo> shortcutList = new ArrayList<>();
8290

8391
for (Map<String, String> shortcut : shortcuts) {
92+
final String id = shortcut.get("id");
8493
final String icon = shortcut.get("icon");
8594
final String action = shortcut.get("action");
8695
final String title = shortcut.get("title");
87-
final ShortcutInfo.Builder shortcutBuilder = new ShortcutInfo.Builder(context, action);
96+
final ShortcutInfo.Builder shortcutBuilder = new ShortcutInfo.Builder(context, id);
8897

8998
final int resourceId = loadResourceId(context, icon);
9099
final Intent intent = getIntentToOpenMainActivity(action);

example/lib/main.dart

Lines changed: 106 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ class MyApp extends StatefulWidget {
1212

1313
class _MyAppState extends State<MyApp> {
1414
String action = 'No Action';
15+
final FlutterShortcuts flutterShortcuts = FlutterShortcuts();
1516

1617
@override
1718
void initState() {
1819
super.initState();
19-
final FlutterShortcuts flutterShortcuts = FlutterShortcuts();
20+
2021
flutterShortcuts.initialize((String incomingAction) {
2122
setState(() {
2223
if (incomingAction != null) {
@@ -34,25 +35,6 @@ class _MyAppState extends State<MyApp> {
3435
}
3536
});
3637
*/
37-
38-
flutterShortcuts.setShortcutItems(<FlutterShortcutItem>[
39-
const FlutterShortcutItem(
40-
action: 'Homepage',
41-
title: 'Home Page',
42-
icon: 'ic_launcher',
43-
),
44-
const FlutterShortcutItem(
45-
action: 'Secondpage',
46-
title: 'Second Page',
47-
icon: 'ic_launcher',
48-
),
49-
]).then((value) {
50-
setState(() {
51-
if (action == 'No Action') {
52-
action = 'Flutter Shortcuts Ready';
53-
}
54-
});
55-
});
5638
}
5739

5840
@override
@@ -62,8 +44,110 @@ class _MyAppState extends State<MyApp> {
6244
appBar: AppBar(
6345
title: const Text('Flutter Shortcuts Plugin'),
6446
),
65-
body: Center(
66-
child: Text('ShortcutItem action : $action\n'),
47+
body: Column(
48+
children: [
49+
Center(
50+
child: Text('ShortcutItem action : $action\n'),
51+
),
52+
ElevatedButton(
53+
child: Text("Set Shortcuts"),
54+
onPressed: () async {
55+
await flutterShortcuts.setShortcutItems(<FlutterShortcutItem>[
56+
const FlutterShortcutItem(
57+
id: "1",
58+
action: 'Homepage',
59+
title: 'Play Ludo',
60+
icon: 'ic_launcher',
61+
),
62+
const FlutterShortcutItem(
63+
id: "2",
64+
action: 'Secondpage',
65+
title: 'Play Snake Ladder',
66+
icon: 'ic_launcher',
67+
),
68+
]).then((value) {
69+
setState(() {
70+
if (action == 'No Action') {
71+
action = 'Flutter Shortcuts Ready';
72+
}
73+
});
74+
});
75+
},
76+
),
77+
ElevatedButton(
78+
child: Text("Clear All Shortcuts"),
79+
onPressed: () {
80+
flutterShortcuts.clearShortcutItems();
81+
},
82+
),
83+
ElevatedButton(
84+
child: Text("Update all shortcuts"),
85+
onPressed: () async {
86+
await flutterShortcuts
87+
.updateAllShortcutItems(shortcutList: <FlutterShortcutItem>[
88+
const FlutterShortcutItem(
89+
id: "1",
90+
action: 'Homepage',
91+
title: 'Home Page 1',
92+
icon: 'ic_launcher',
93+
),
94+
const FlutterShortcutItem(
95+
id: "2",
96+
action: 'Secondpage',
97+
title: 'Second Page 1',
98+
icon: 'ic_launcher',
99+
),
100+
]);
101+
},
102+
),
103+
ElevatedButton(
104+
child: Text("Change icon of 2nd Shortcut"),
105+
onPressed: () {
106+
// flutterShortcuts.updateShortcutItemIcon(id, icon);
107+
},
108+
),
109+
ElevatedButton(
110+
child: Text("change icon color of 2nd Shortcut"),
111+
onPressed: () {
112+
// disable title
113+
// flutterShortcuts.changeIconColor(id, color);
114+
},
115+
),
116+
ElevatedButton(
117+
child: Text("change icon backgroud color of 2nd Shortcut"),
118+
onPressed: () {
119+
// disable title
120+
// flutterShortcuts.changeIconBackgroundColor(id, color);
121+
},
122+
),
123+
ElevatedButton(
124+
child: Text("set animated icon of 2nd Shortcut"),
125+
onPressed: () {
126+
// disable title
127+
// flutterShortcuts.setAnimatedIcon(id, AnimatedIcon);
128+
},
129+
),
130+
ElevatedButton(
131+
child: Text("set icon backgroud gradient of 2nd Shortcut"),
132+
onPressed: () {
133+
// disable title
134+
// flutterShortcuts.iconBackgroundGradient(id,[start,end], [start color,end color]);
135+
},
136+
),
137+
ElevatedButton(
138+
child: Text("Change title of 2nd Shortcut"),
139+
onPressed: () {
140+
// flutterShortcuts.updateShortcutItemTitle(id, title);
141+
},
142+
),
143+
ElevatedButton(
144+
child: Text("Toggle disable state of 2nd Shortcut"),
145+
onPressed: () {
146+
// disable title
147+
// flutterShortcuts.updateDisableStateShortcutItem(id, state(bool), disable title);
148+
},
149+
),
150+
],
67151
),
68152
),
69153
);

lib/flutter_shortcuts.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,22 @@ class FlutterShortcuts {
1313
Future<void> setShortcutItems(List<FlutterShortcutItem> items) async {
1414
return FlutterShortcutsPlatform.instance.setShortcutItems(items);
1515
}
16+
17+
Future<void> clearShortcutItems() async {
18+
return FlutterShortcutsPlatform.instance.clearShortcutItems();
19+
}
20+
21+
/// updateShortcutItems() will update all the shortcut items.
22+
Future<void> updateAllShortcutItems({
23+
List<FlutterShortcutItem> shortcutList,
24+
}) async {
25+
return FlutterShortcutsPlatform.instance
26+
.updateAllShortcutItems(shortcutList);
27+
}
28+
29+
/// updateShortcutItems() will update a single shortcut item based on id.
30+
Future<void> updateShortcutItem(
31+
String id,
32+
FlutterShortcutItem shortcut,
33+
) async {}
1634
}

lib/src/method_call/flutter_shortcuts_method_call_handler.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
1010

1111
@override
1212
Future<void> initialize(FlutterShortcutAction actionHandler) async {
13-
// super.initialize(action);
1413
channel.setMethodCallHandler((MethodCall call) async {
1514
assert(call.method == 'launch');
1615
actionHandler(call.arguments);
@@ -23,14 +22,26 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
2322

2423
@override
2524
Future<void> setShortcutItems(List<FlutterShortcutItem> items) async {
26-
// super.setShortcutItems(items);
2725
final List<Map<String, String>> itemsList =
2826
items.map(_serializeItem).toList();
2927
await channel.invokeMethod<void>('setShortcutItems', itemsList);
3028
}
3129

30+
@override
31+
Future<void> clearShortcutItems() async {
32+
await channel.invokeMethod<void>('clearShortcutItems');
33+
}
34+
35+
@override
36+
Future<void> updateAllShortcutItems(List<FlutterShortcutItem> items) async {
37+
final List<Map<String, String>> itemsList =
38+
items.map(_serializeItem).toList();
39+
await channel.invokeMethod<void>('updateShortcutItems', itemsList);
40+
}
41+
3242
Map<String, String> _serializeItem(FlutterShortcutItem item) {
3343
return <String, String>{
44+
'id': item.id,
3445
'action': item.action,
3546
'title': item.title,
3647
'icon': item.icon,

lib/src/platform/flutter_shortcuts_platform.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ abstract class FlutterShortcutsPlatform extends PlatformInterface {
2626
throw UnimplementedError("setShortcutItems() has not been implemented.");
2727
}
2828

29-
Future<void> updateShortcutItems(List<FlutterShortcutItem> items) async {
29+
Future<void> clearShortcutItems() async {
30+
throw UnimplementedError("clearShortcutItems() has not been implemented.");
31+
}
32+
33+
Future<void> updateAllShortcutItems(List<FlutterShortcutItem> items) async {
3034
throw UnimplementedError("updateShortcutItems() has not been implemented.");
3135
}
3236
}

lib/src/types/model/flutter_shortcut_item.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
class FlutterShortcutItem {
22
const FlutterShortcutItem({
3+
this.id,
34
this.action,
45
this.title,
56
this.icon,
67
});
78

9+
final String id;
10+
811
final String action;
912

1013
final String title;

0 commit comments

Comments
 (0)