Skip to content

Commit d4c6886

Browse files
committed
push shortcut item
1 parent dfd03ba commit d4c6886

File tree

11 files changed

+173
-62
lines changed

11 files changed

+173
-62
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.divyanshushekhar.flutter_shortcuts;
22

3-
import android.annotation.TargetApi;
43
import android.app.Activity;
54
import android.content.Context;
65
import android.content.Intent;
@@ -49,6 +48,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
4948
case "setShortcutItems":
5049
setShortcutItems(call,shortcutManager);
5150
break;
51+
case "pushShortcutItem":
52+
pushShortcutItem(call,shortcutManager);
53+
break;
5254
case "updateAllShortcutItems":
5355
updateAllShortcutItems(call,shortcutManager);
5456
break;
@@ -84,8 +86,6 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
8486
result.success(null);
8587
}
8688

87-
88-
8989
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
9090
private void setShortcutItems(MethodCall call,ShortcutManager shortcutManager) {
9191
List<Map<String, String>> setShortcutItemsArgs = call.arguments();
@@ -94,6 +94,13 @@ private void setShortcutItems(MethodCall call,ShortcutManager shortcutManager) {
9494
Toast.makeText(context, "Shortcut Created", Toast.LENGTH_SHORT).show();
9595
}
9696

97+
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
98+
private void pushShortcutItem(MethodCall call, ShortcutManager shortcutManager) {
99+
final List<Map<String, String>> args = call.arguments();
100+
List<ShortcutInfo> shortcutInfo = processShortcuts(args);
101+
shortcutManager.addDynamicShortcuts(shortcutInfo);
102+
}
103+
97104
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
98105
private void updateAllShortcutItems(MethodCall call, ShortcutManager shortcutManager) {
99106
List<Map<String, String>> updateAllShortcutArgs = call.arguments();
@@ -106,10 +113,11 @@ private void updateAllShortcutItems(MethodCall call, ShortcutManager shortcutMan
106113
private void updateShortcutItem(MethodCall call, ShortcutManager shortcutManager) {
107114
final List<Map<String, String>> args = call.arguments();
108115
Map<String, String> info = args.get(0);
116+
final String refId = info.get("id");
109117
List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
110118
final List<ShortcutInfo> shortcutList = new ArrayList<>();
111119
for(ShortcutInfo si : dynamicShortcuts) {
112-
if(si.getId().equalsIgnoreCase(info.get("id"))) {
120+
if(si.getId().equalsIgnoreCase(refId)) {
113121
ShortcutInfo shortcutInfo = createShortcutInfo(info);
114122
shortcutList.add(shortcutInfo);
115123
continue;
@@ -170,7 +178,8 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
170178
final String id = shortcut.get("id");
171179
final String icon = shortcut.get("icon");
172180
final String action = shortcut.get("action");
173-
final String title = shortcut.get("title");
181+
final String shortLabel = shortcut.get("shortLabel");
182+
final String longLabel = shortcut.get("LongLabel");
174183
final ShortcutInfo.Builder shortcutBuilder;
175184
shortcutBuilder = new ShortcutInfo.Builder(context, id);
176185

@@ -183,9 +192,13 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
183192
}
184193
}
185194

195+
if(longLabel != null) {
196+
shortcutBuilder.setLongLabel(longLabel);
197+
}
198+
199+
assert shortLabel!=null;
186200
return shortcutBuilder
187-
.setLongLabel(title)
188-
.setShortLabel(title)
201+
.setShortLabel(shortLabel)
189202
.setIntent(intent)
190203
.build();
191204
}
@@ -194,6 +207,7 @@ private int loadResourceId(Context context, String icon) {
194207
if (icon == null) {
195208
return 0;
196209
}
210+
197211
final String packageName = context.getPackageName();
198212
final Resources res = context.getResources();
199213
final int resourceId = res.getIdentifier(icon, "drawable", packageName);

example/lib/main.dart

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ class _MyAppState extends State<MyApp> {
2525
}
2626
});
2727
});
28-
29-
/*
30-
flutterShortcuts.staticShortcutsInitialize((String id) {
31-
if(id == 'Homepage') {
32-
Navigator.pushNamed(context, '/Homepage');
33-
} else if(id == 'Secondpage') {
34-
Navigator.pushNamed(context, '/Secondpage');
35-
}
36-
});
37-
*/
3828
}
3929

4030
@override
@@ -52,20 +42,22 @@ class _MyAppState extends State<MyApp> {
5242
ElevatedButton(
5343
child: Text("Set Shortcuts"),
5444
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) {
45+
await flutterShortcuts.setShortcutItems(
46+
shortcutItems: <FlutterShortcutItem>[
47+
const FlutterShortcutItem(
48+
id: "1",
49+
action: 'Homepage',
50+
shortLabel: 'Play Ludo',
51+
icon: 'ic_launcher',
52+
),
53+
const FlutterShortcutItem(
54+
id: "2",
55+
action: 'Secondpage',
56+
shortLabel: 'Play Snake Ladder',
57+
icon: 'ic_launcher',
58+
),
59+
],
60+
).then((value) {
6961
setState(() {
7062
if (action == 'No Action') {
7163
action = 'Flutter Shortcuts Ready';
@@ -80,6 +72,18 @@ class _MyAppState extends State<MyApp> {
8072
flutterShortcuts.clearShortcutItems();
8173
},
8274
),
75+
ElevatedButton(
76+
child: Text("push Shortcut Item"),
77+
onPressed: () async {
78+
await flutterShortcuts.pushShortcutItem(
79+
shortcut: FlutterShortcutItem(
80+
id: "5",
81+
action: "fifthaction",
82+
shortLabel: "shortLabel",
83+
),
84+
);
85+
},
86+
),
8387
ElevatedButton(
8488
child: Text("Update all shortcuts"),
8589
onPressed: () async {
@@ -88,13 +92,13 @@ class _MyAppState extends State<MyApp> {
8892
const FlutterShortcutItem(
8993
id: "1",
9094
action: 'Homepage',
91-
title: 'Home Page 1',
95+
shortLabel: 'Home Page 1',
9296
icon: 'ic_launcher',
9397
),
9498
const FlutterShortcutItem(
9599
id: "2",
96100
action: 'Secondpage',
97-
title: 'Second Page 1',
101+
shortLabel: 'Second Page 1',
98102
icon: 'ic_launcher',
99103
),
100104
]);
@@ -108,7 +112,7 @@ class _MyAppState extends State<MyApp> {
108112
shortcut: FlutterShortcutItem(
109113
id: "1",
110114
action: 'Fourthpage',
111-
title: 'Fourth Page 4',
115+
shortLabel: 'Fourth Page 4',
112116
icon: 'ic_launcher',
113117
),
114118
);

lib/flutter_shortcuts.dart

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,61 @@
1+
/*
2+
3+
Copyright (c) 2021 divshekhar (Divyanshu Shekhar).
4+
All rights reserved.
5+
6+
The plugin is governed by the BSD-3-clause License. Please see the LICENSE file
7+
for more details.
8+
9+
*/
10+
111
import 'dart:async';
212

313
import 'package:flutter_shortcuts/src/platform/flutter_shortcuts_platform.dart';
4-
import 'package:flutter_shortcuts/src/types/types.dart';
14+
import 'package:flutter_shortcuts/src/helper/helper.dart';
515

6-
export 'package:flutter_shortcuts/src/types/types.dart';
16+
export 'package:flutter_shortcuts/src/helper/helper.dart';
717

818
class FlutterShortcuts {
19+
/// [initialize()] performs action when shortcuts is opened.
920
Future<void> initialize(FlutterShortcutAction action) async {
1021
FlutterShortcutsPlatform.instance.initialize(action);
1122
}
1223

13-
Future<void> setShortcutItems(List<FlutterShortcutItem> items) async {
14-
return FlutterShortcutsPlatform.instance.setShortcutItems(items);
24+
/// [setShortcutItems()] will set all the shortcut items.
25+
Future<void> setShortcutItems(
26+
{List<FlutterShortcutItem> shortcutItems}) async {
27+
return FlutterShortcutsPlatform.instance.setShortcutItems(shortcutItems);
1528
}
1629

30+
/// [clearShortcutItems()] will remove all the shortcut items.
1731
Future<void> clearShortcutItems() async {
1832
return FlutterShortcutsPlatform.instance.clearShortcutItems();
1933
}
2034

21-
/// updateShortcutItems() will update all the shortcut items.
35+
/// [pushShortcutItem()] will push a new shortcut item.
36+
/// If there is already a dynamic or pinned shortcut with the same ID,
37+
/// the shortcut will be updated.
38+
Future<void> pushShortcutItem({FlutterShortcutItem shortcut}) async {
39+
return FlutterShortcutsPlatform.instance.pushShortcutItem(shortcut);
40+
}
41+
42+
/// [updateShortcutItems()] will update all the shortcut items.
2243
Future<void> updateAllShortcutItems({
2344
List<FlutterShortcutItem> shortcutList,
2445
}) async {
2546
return FlutterShortcutsPlatform.instance
2647
.updateAllShortcutItems(shortcutList);
2748
}
2849

29-
/// updateShortcutItems() will update a single shortcut item based on id.
50+
/// [updateShortcutItem()] will update a single shortcut item based on id.
3051
Future<void> updateShortcutItem({
3152
String id,
3253
FlutterShortcutItem shortcut,
3354
}) async {
3455
return FlutterShortcutsPlatform.instance.updateShortcutItem(id, shortcut);
3556
}
3657

58+
/// [changeShortcutItemIcon()] will change the icon of the shortcut based on id.
3759
Future<void> changeShortcutItemIcon({String id, String icon}) async {
3860
return FlutterShortcutsPlatform.instance.changeShortcutItemIcon(id, icon);
3961
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
3+
Copyright (c) 2021 divshekhar (Divyanshu Shekhar).
4+
All rights reserved.
5+
6+
The plugin is governed by the BSD-3-clause License. Please see the LICENSE file
7+
for more details.
8+
9+
*/
10+
11+
typedef void FlutterShortcutAction(String action);

lib/src/helper/helper.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
3+
Copyright (c) 2021 divshekhar (Divyanshu Shekhar).
4+
All rights reserved.
5+
6+
The plugin is governed by the BSD-3-clause License. Please see the LICENSE file
7+
for more details.
8+
9+
*/
10+
11+
export 'action/flutter_shortcut_action.dart';
12+
export 'model/flutter_shortcut_item.dart';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
Copyright (c) 2021 divshekhar (Divyanshu Shekhar).
4+
All rights reserved.
5+
6+
The plugin is governed by the BSD-3-clause License. Please see the LICENSE file
7+
for more details.
8+
9+
*/
10+
11+
import 'package:flutter/material.dart';
12+
13+
class FlutterShortcutItem {
14+
const FlutterShortcutItem({
15+
@required this.id,
16+
@required this.action,
17+
@required this.shortLabel,
18+
this.longLabel,
19+
this.icon,
20+
});
21+
22+
// Id of the shortcut
23+
final String id;
24+
25+
// Action performed by the shortcut
26+
final String action;
27+
28+
// Short label of the shortcut
29+
final String shortLabel;
30+
31+
// Long label of the shortcut
32+
final String longLabel;
33+
34+
// Icon of the shortcut
35+
final String icon;
36+
}

lib/src/method_call/flutter_shortcuts_method_call_handler.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
/*
2+
3+
Copyright (c) 2021 divshekhar (Divyanshu Shekhar).
4+
All rights reserved.
5+
6+
The plugin is governed by the BSD-3-clause License. Please see the LICENSE file
7+
for more details.
8+
9+
*/
10+
111
import 'package:flutter/services.dart';
212
import 'package:flutter_shortcuts/src/platform/flutter_shortcuts_platform.dart';
3-
import 'package:flutter_shortcuts/src/types/types.dart';
13+
import 'package:flutter_shortcuts/src/helper/helper.dart';
414

515
class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
616
final MethodChannel _channel =
@@ -32,6 +42,12 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
3242
await channel.invokeMethod<void>('clearShortcutItems');
3343
}
3444

45+
@override
46+
Future<void> pushShortcutItem(FlutterShortcutItem shortcut) async {
47+
final Map<String, String> item = _serializeItem(shortcut);
48+
await channel.invokeMethod<void>('pushShortcutItem', [item]);
49+
}
50+
3551
@override
3652
Future<void> updateAllShortcutItems(List<FlutterShortcutItem> items) async {
3753
final List<Map<String, String>> itemsList =
@@ -55,7 +71,8 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
5571
return <String, String>{
5672
'id': item.id,
5773
'action': item.action,
58-
'title': item.title,
74+
'shortLabel': item.shortLabel,
75+
'longLabel': item.longLabel,
5976
'icon': item.icon,
6077
};
6178
}

lib/src/platform/flutter_shortcuts_platform.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
/*
2+
3+
Copyright (c) 2021 divshekhar (Divyanshu Shekhar).
4+
All rights reserved.
5+
6+
The plugin is governed by the BSD-3-clause License. Please see the LICENSE file
7+
for more details.
8+
9+
*/
10+
111
import 'package:flutter_shortcuts/src/method_call/flutter_shortcuts_method_call_handler.dart';
2-
import 'package:flutter_shortcuts/src/types/types.dart';
12+
import 'package:flutter_shortcuts/src/helper/helper.dart';
313
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
414

515
abstract class FlutterShortcutsPlatform extends PlatformInterface {
@@ -30,6 +40,10 @@ abstract class FlutterShortcutsPlatform extends PlatformInterface {
3040
throw UnimplementedError("clearShortcutItems() has not been implemented.");
3141
}
3242

43+
Future<void> pushShortcutItem(FlutterShortcutItem shortcut) async {
44+
throw UnimplementedError("pushShortcutItem() has not been implemented.");
45+
}
46+
3347
Future<void> updateAllShortcutItems(List<FlutterShortcutItem> items) async {
3448
throw UnimplementedError("updateShortcutItems() has not been implemented.");
3549
}

lib/src/types/action/flutter_shortcut_action.dart

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)