Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 7b9ac6b

Browse files
authored
[quick_actions] 2/3 Quick actions federated platform interface (#3735)
* Moved quickactions to a subfolder * Added platform interface with tests * Added exports * Formatted, made initialize return Future<void> instead of void * Fixed formatting * formatting * Fixed analyze issue with import * Fixed formatting * Fixed formatting * Added license in files * Removed accidental \\\ * changed license to Flutter 2017 * Moved quickactions to a subfolder * Added platform interface with tests * Added exports * Formatted, made initialize return Future<void> instead of void * Fixed formatting * formatting * Fixed analyze issue with import * Fixed formatting * Fixed formatting * Added license in files * Removed accidental \\\ * changed license to Flutter 2017 * Implemented feedback * Changed 2017 to 2013 * Changed 2017 to 2013 * Implemented feedback.
1 parent a85f397 commit 7b9ac6b

14 files changed

+458
-66
lines changed

packages/quick_actions/AUTHORS

Lines changed: 0 additions & 66 deletions
This file was deleted.

packages/quick_actions/quick_actions/test/quick_actions_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4+
45
import 'dart:async';
56

67
import 'package:flutter/services.dart';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 1.0.0
2+
3+
* Initial release of quick_actions_platform_interface
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright 2013 The Chromium Authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without modification,
4+
are permitted provided that the following conditions are met:
5+
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above
9+
copyright notice, this list of conditions and the following
10+
disclaimer in the documentation and/or other materials provided
11+
with the distribution.
12+
* Neither the name of Google Inc. nor the names of its
13+
contributors may be used to endorse or promote products derived
14+
from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# quick_actions_platform_interface
2+
3+
A common platform interface for the [`quick_actions`][1] plugin.
4+
5+
This interface allows platform-specific implementations of the `quick_actions`
6+
plugin, as well as the plugin itself, to ensure they are supporting the
7+
same interface.
8+
9+
# Usage
10+
11+
To implement a new platform-specific implementation of `quick_actions`, extend
12+
[`QuickActionsPlatform`][2] with an implementation that performs the
13+
platform-specific behavior, and when you register your plugin, set the default
14+
`QuickActionsPlatform` by calling
15+
`QuickActionsPlatform.instance = MyPlatformQuickActions()`.
16+
17+
# Note on breaking changes
18+
19+
Strongly prefer non-breaking changes (such as adding a method to the interface)
20+
over breaking changes for this package.
21+
22+
See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
23+
on why a less-clean interface is preferable to a breaking change.
24+
25+
[1]: ../quick_actions
26+
[2]: lib/quick_actions_platform_interface.dart
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/foundation.dart';
6+
import 'package:flutter/services.dart';
7+
import 'package:meta/meta.dart' show visibleForTesting;
8+
import 'package:quick_actions_platform_interface/types/types.dart';
9+
10+
import '../platform_interface/quick_actions_platform.dart';
11+
12+
final MethodChannel _channel =
13+
MethodChannel('plugins.flutter.io/quick_actions');
14+
15+
/// An implementation of [QuickActionsPlatform] that uses method channels.
16+
class MethodChannelQuickActions extends QuickActionsPlatform {
17+
/// The MethodChannel that is being used by this implementation of the plugin.
18+
@visibleForTesting
19+
MethodChannel get channel => _channel;
20+
21+
@override
22+
Future<void> initialize(QuickActionHandler handler) async {
23+
channel.setMethodCallHandler((MethodCall call) async {
24+
assert(call.method == 'launch');
25+
handler(call.arguments);
26+
});
27+
final String? action =
28+
await channel.invokeMethod<String?>('getLaunchAction');
29+
if (action != null) {
30+
handler(action);
31+
}
32+
}
33+
34+
@override
35+
Future<void> setShortcutItems(List<ShortcutItem> items) async {
36+
final List<Map<String, String?>> itemsList =
37+
items.map(_serializeItem).toList();
38+
await channel.invokeMethod<void>('setShortcutItems', itemsList);
39+
}
40+
41+
@override
42+
Future<void> clearShortcutItems() =>
43+
channel.invokeMethod<void>('clearShortcutItems');
44+
45+
Map<String, String?> _serializeItem(ShortcutItem item) {
46+
return <String, String?>{
47+
'type': item.type,
48+
'localizedTitle': item.localizedTitle,
49+
'icon': item.icon,
50+
};
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
6+
import 'package:quick_actions_platform_interface/types/types.dart';
7+
8+
import '../method_channel/method_channel_quick_actions.dart';
9+
10+
/// The interface that implementations of quick_actions must implement.
11+
///
12+
/// Platform implementations should extend this class rather than implement it as `quick_actions`
13+
/// does not consider newly added methods to be breaking changes. Extending this class
14+
/// (using `extends`) ensures that the subclass will get the default implementation, while
15+
/// platform implementations that `implements` this interface will be broken by newly added
16+
/// [QuickActionsPlatform] methods.
17+
abstract class QuickActionsPlatform extends PlatformInterface {
18+
/// Constructs a QuickActionsPlatform.
19+
QuickActionsPlatform() : super(token: _token);
20+
21+
static final Object _token = Object();
22+
23+
static QuickActionsPlatform _instance = MethodChannelQuickActions();
24+
25+
/// The default instance of [QuickActionsPlatform] to use.
26+
///
27+
/// Defaults to [MethodChannelQuickActions].
28+
static QuickActionsPlatform get instance => _instance;
29+
30+
/// Platform-specific plugins should set this with their own platform-specific
31+
/// class that extends [QuickActionsPlatform] when they register themselves.
32+
// TODO(amirh): Extract common platform interface logic.
33+
// https://github.com/flutter/flutter/issues/43368
34+
static set instance(QuickActionsPlatform instance) {
35+
PlatformInterface.verifyToken(instance, _token);
36+
_instance = instance;
37+
}
38+
39+
/// Initializes this plugin.
40+
///
41+
/// Call this once before any further interaction with the the plugin.
42+
Future<void> initialize(QuickActionHandler handler) async {
43+
throw UnimplementedError("initialize() has not been implemented.");
44+
}
45+
46+
/// Sets the [ShortcutItem]s to become the app's quick actions.
47+
Future<void> setShortcutItems(List<ShortcutItem> items) async {
48+
throw UnimplementedError("setShortcutItems() has not been implemented.");
49+
}
50+
51+
/// Removes all [ShortcutItem]s registered for the app.
52+
Future<void> clearShortcutItems() {
53+
throw UnimplementedError("clearShortcutItems() has not been implemented.");
54+
}
55+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
export 'package:quick_actions_platform_interface/platform_interface/quick_actions_platform.dart';
6+
export 'package:quick_actions_platform_interface/types/types.dart';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Handler for a quick action launch event.
6+
///
7+
/// The argument [type] corresponds to the [ShortcutItem]'s field.
8+
typedef void QuickActionHandler(String type);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Home screen quick-action shortcut item.
6+
class ShortcutItem {
7+
/// Constructs an instance with the given [type], [localizedTitle], and
8+
/// [icon].
9+
///
10+
/// Only [icon] should be nullable. It will remain `null` if unset.
11+
const ShortcutItem({
12+
required this.type,
13+
required this.localizedTitle,
14+
this.icon,
15+
});
16+
17+
/// The identifier of this item; should be unique within the app.
18+
final String type;
19+
20+
/// Localized title of the item.
21+
final String localizedTitle;
22+
23+
/// Name of native resource (xcassets etc; NOT a Flutter asset) to be
24+
/// displayed as the icon for this item.
25+
final String? icon;
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
export 'quick_action_handler.dart';
6+
export 'shortcut_item.dart';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: quick_actions_platform_interface
2+
description: A common platform interface for the quick_actions plugin.
3+
homepage: https://github.com/flutter/plugins/tree/master/packages/quick_actions/quick_actions_platform_interface
4+
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
5+
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
6+
version: 1.0.0
7+
8+
dependencies:
9+
flutter:
10+
sdk: flutter
11+
meta: ^1.3.0
12+
plugin_platform_interface: ^2.0.0
13+
14+
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
17+
mockito: ^5.0.1
18+
pedantic: ^1.11.0
19+
20+
environment:
21+
sdk: ">=2.12.0-259.9.beta <3.0.0"
22+
flutter: ">=1.22.0"

0 commit comments

Comments
 (0)