Skip to content

Commit 3f3f834

Browse files
authored
[quick_actions] 3/3 Quick actions federated migration (flutter#3736)
1 parent e73402d commit 3f3f834

6 files changed

Lines changed: 68 additions & 151 deletions

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ Aleksandr Yurkovskiy <[email protected]>
6464
Anton Borries <[email protected]>
6565
6666
Rahul Raj <[email protected]>
67+
Daniel Roek <[email protected]>

packages/quick_actions/quick_actions/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.0
2+
3+
* Migrate to federated architecture.
4+
15
## 0.5.0+1
26

37
* Updated example app implementation.

packages/quick_actions/quick_actions/example/integration_test/quick_actions_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void main() {
1212

1313
testWidgets('Can set shortcuts', (WidgetTester tester) async {
1414
final QuickActions quickActions = QuickActions();
15-
quickActions.initialize(null);
15+
await quickActions.initialize(null);
1616

1717
const ShortcutItem shortCutItem = ShortcutItem(
1818
type: 'action_one',

packages/quick_actions/quick_actions/lib/quick_actions.dart

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,24 @@
44

55
import 'dart:async';
66

7-
import 'package:flutter/services.dart';
8-
import 'package:meta/meta.dart';
7+
import 'package:quick_actions_platform_interface/platform_interface/quick_actions_platform.dart';
8+
import 'package:quick_actions_platform_interface/types/types.dart';
99

10-
const MethodChannel _kChannel =
11-
MethodChannel('plugins.flutter.io/quick_actions');
12-
13-
/// Handler for a quick action launch event.
14-
///
15-
/// The argument [type] corresponds to the [ShortcutItem]'s field.
16-
typedef void QuickActionHandler(String type);
17-
18-
/// Home screen quick-action shortcut item.
19-
class ShortcutItem {
20-
/// Constructs an instance with the given [type], [localizedTitle], and
21-
/// [icon].
22-
///
23-
/// Only [icon] should be nullable. It will remain `null` if unset.
24-
const ShortcutItem({
25-
required this.type,
26-
required this.localizedTitle,
27-
this.icon,
28-
});
29-
30-
/// The identifier of this item; should be unique within the app.
31-
final String type;
32-
33-
/// Localized title of the item.
34-
final String localizedTitle;
35-
36-
/// Name of native resource (xcassets etc; NOT a Flutter asset) to be
37-
/// displayed as the icon for this item.
38-
final String? icon;
39-
}
10+
export 'package:quick_actions_platform_interface/types/types.dart';
4011

4112
/// Quick actions plugin.
4213
class QuickActions {
43-
/// Gets an instance of the plugin with the default methodChannel.
44-
///
45-
/// [initialize] should be called before using any other methods.
46-
factory QuickActions() => _instance;
47-
48-
/// This is a test-only constructor. Do not call this, it can break at any
49-
/// time.
50-
@visibleForTesting
51-
QuickActions.withMethodChannel(this.channel);
52-
53-
static final QuickActions _instance =
54-
QuickActions.withMethodChannel(_kChannel);
55-
56-
/// This is a test-only accessor. Do not call this, it can break at any time.
57-
@visibleForTesting
58-
final MethodChannel channel;
59-
6014
/// Initializes this plugin.
6115
///
6216
/// Call this once before any further interaction with the the plugin.
63-
void initialize(QuickActionHandler handler) async {
64-
channel.setMethodCallHandler((MethodCall call) async {
65-
assert(call.method == 'launch');
66-
handler(call.arguments);
67-
});
68-
final String? action =
69-
await channel.invokeMethod<String?>('getLaunchAction');
70-
if (action != null) {
71-
handler(action);
72-
}
73-
}
17+
Future<void> initialize(QuickActionHandler handler) async =>
18+
QuickActionsPlatform.instance.initialize(handler);
7419

7520
/// Sets the [ShortcutItem]s to become the app's quick actions.
76-
Future<void> setShortcutItems(List<ShortcutItem> items) async {
77-
final List<Map<String, String?>> itemsList =
78-
items.map(_serializeItem).toList();
79-
await channel.invokeMethod<void>('setShortcutItems', itemsList);
80-
}
21+
Future<void> setShortcutItems(List<ShortcutItem> items) async =>
22+
QuickActionsPlatform.instance.setShortcutItems(items);
8123

8224
/// Removes all [ShortcutItem]s registered for the app.
8325
Future<void> clearShortcutItems() =>
84-
channel.invokeMethod<void>('clearShortcutItems');
85-
86-
Map<String, String?> _serializeItem(ShortcutItem item) {
87-
return <String, String?>{
88-
'type': item.type,
89-
'localizedTitle': item.localizedTitle,
90-
'icon': item.icon,
91-
};
92-
}
26+
QuickActionsPlatform.instance.clearShortcutItems();
9327
}

packages/quick_actions/quick_actions/pubspec.yaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: quick_actions
22
description: Flutter plugin for creating shortcuts on home screen, also known as
33
Quick Actions on iOS and App Shortcuts on Android.
44
homepage: https://github.com/flutter/plugins/tree/master/packages/quick_actions
5-
version: 0.5.0+1
5+
version: 0.6.0
66

77
flutter:
88
plugin:
@@ -17,15 +17,18 @@ dependencies:
1717
flutter:
1818
sdk: flutter
1919
meta: ^1.3.0
20+
quick_actions_platform_interface: ^1.0.0
2021

2122
dev_dependencies:
22-
test: ^1.16.3
2323
flutter_test:
2424
sdk: flutter
2525
integration_test:
2626
sdk: flutter
27-
pedantic: ^1.10.0
27+
mockito: ^5.0.0-nullsafety.7
28+
pedantic: ^1.11.0
29+
plugin_platform_interface: ^2.0.0
30+
2831

2932
environment:
30-
sdk: ">=2.12.0-259.9.beta <3.0.0"
33+
sdk: ">=2.12.0 <3.0.0"
3134
flutter: ">=1.12.13+hotfix.5"

packages/quick_actions/quick_actions/test/quick_actions_test.dart

Lines changed: 47 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,65 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:async';
6-
7-
import 'package:flutter/services.dart';
85
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:mockito/mockito.dart';
7+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
98
import 'package:quick_actions/quick_actions.dart';
9+
import 'package:quick_actions_platform_interface/platform_interface/quick_actions_platform.dart';
10+
import 'package:quick_actions_platform_interface/quick_actions_platform_interface.dart';
11+
import 'package:quick_actions_platform_interface/types/shortcut_item.dart';
1012

1113
void main() {
12-
TestWidgetsFlutterBinding.ensureInitialized();
14+
group('$QuickActions', () {
15+
setUp(() {
16+
QuickActionsPlatform.instance = MockQuickActionsPlatform();
17+
});
1318

14-
late QuickActions quickActions;
15-
final List<MethodCall> log = <MethodCall>[];
19+
test('initialize() PlatformInterface', () async {
20+
QuickActions quickActions = QuickActions();
21+
QuickActionHandler handler = (type) {};
1622

17-
setUp(() {
18-
quickActions = QuickActions();
19-
quickActions.channel.setMockMethodCallHandler(
20-
(MethodCall methodCall) async {
21-
log.add(methodCall);
22-
return 'non empty response';
23-
},
24-
);
25-
});
23+
await quickActions.initialize(handler);
24+
verify(QuickActionsPlatform.instance.initialize(handler)).called(1);
25+
});
2626

27-
test('setShortcutItems with demo data', () async {
28-
const String type = 'type';
29-
const String localizedTitle = 'localizedTitle';
30-
const String icon = 'icon';
31-
await quickActions.setShortcutItems(
32-
const <ShortcutItem>[
33-
ShortcutItem(type: type, localizedTitle: localizedTitle, icon: icon)
34-
],
35-
);
36-
expect(
37-
log,
38-
<Matcher>[
39-
isMethodCall(
40-
'setShortcutItems',
41-
arguments: <Map<String, String>>[
42-
<String, String>{
43-
'type': type,
44-
'localizedTitle': localizedTitle,
45-
'icon': icon,
46-
}
47-
],
48-
),
49-
],
50-
);
51-
log.clear();
52-
});
27+
test('setShortcutItems() PlatformInterface', () {
28+
QuickActions quickActions = QuickActions();
29+
QuickActionHandler handler = (type) {};
30+
quickActions.initialize(handler);
31+
quickActions.setShortcutItems([]);
5332

54-
test('clearShortcutItems', () {
55-
quickActions.clearShortcutItems();
56-
expect(
57-
log,
58-
<Matcher>[
59-
isMethodCall('clearShortcutItems', arguments: null),
60-
],
61-
);
62-
log.clear();
63-
});
33+
verify(QuickActionsPlatform.instance.initialize(handler)).called(1);
34+
verify(QuickActionsPlatform.instance.setShortcutItems([])).called(1);
35+
});
6436

65-
test('initialize', () async {
66-
final Completer<bool> quickActionsHandler = Completer<bool>();
67-
quickActions.initialize((_) => quickActionsHandler.complete(true));
68-
expect(
69-
log,
70-
<Matcher>[
71-
isMethodCall('getLaunchAction', arguments: null),
72-
],
73-
);
74-
log.clear();
37+
test('clearShortcutItems() PlatformInterface', () {
38+
QuickActions quickActions = QuickActions();
39+
QuickActionHandler handler = (type) {};
7540

76-
expect(quickActionsHandler.future, completion(isTrue));
41+
quickActions.initialize(handler);
42+
quickActions.clearShortcutItems();
43+
44+
verify(QuickActionsPlatform.instance.initialize(handler)).called(1);
45+
verify(QuickActionsPlatform.instance.clearShortcutItems()).called(1);
46+
});
7747
});
48+
}
7849

79-
test('Shortcut item can be constructed', () {
80-
const String type = 'type';
81-
const String localizedTitle = 'title';
82-
const String icon = 'foo';
50+
class MockQuickActionsPlatform extends Mock
51+
with MockPlatformInterfaceMixin
52+
implements QuickActionsPlatform {
53+
@override
54+
Future<void> clearShortcutItems() async =>
55+
super.noSuchMethod(Invocation.method(#clearShortcutItems, []));
8356

84-
const ShortcutItem item =
85-
ShortcutItem(type: type, localizedTitle: localizedTitle, icon: icon);
57+
@override
58+
Future<void> initialize(QuickActionHandler? handler) async =>
59+
super.noSuchMethod(Invocation.method(#initialize, [handler]));
8660

87-
expect(item.type, type);
88-
expect(item.localizedTitle, localizedTitle);
89-
expect(item.icon, icon);
90-
});
61+
@override
62+
Future<void> setShortcutItems(List<ShortcutItem>? items) async =>
63+
super.noSuchMethod(Invocation.method(#setShortcutItems, [items]));
9164
}
65+
66+
class MockQuickActions extends QuickActions {}

0 commit comments

Comments
 (0)