Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/quick_actions/quick_actions_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.0+1

* Removes custom modulemap file with "Test" submodule and private headers for Swift migration.
* Migrates `FLTQuickActionsPlugin` class to Swift.

## 1.0.0

* Updates version to 1.0 to reflect current status.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

@import Flutter;
@import quick_actions_ios;
@import quick_actions_ios.Test;
@import XCTest;
#import <OCMock/OCMock.h>

Expand All @@ -26,9 +25,9 @@ - (void)testHandleMethodCall_setShortcutItems {

FLTShortcutStateManager *mockShortcutStateManager = OCMClassMock([FLTShortcutStateManager class]);

FLTQuickActionsPlugin *plugin =
[[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:mockShortcutStateManager];
QuickActionsPlugin *plugin =
[[QuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:mockShortcutStateManager];
XCTestExpectation *resultExpectation =
[self expectationWithDescription:@"result block must be called."];
[plugin handleMethodCall:call
Expand All @@ -45,9 +44,9 @@ - (void)testHandleMethodCall_clearShortcutItems {
FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"clearShortcutItems"
arguments:nil];
FLTShortcutStateManager *mockShortcutStateManager = OCMClassMock([FLTShortcutStateManager class]);
FLTQuickActionsPlugin *plugin =
[[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:mockShortcutStateManager];
QuickActionsPlugin *plugin =
[[QuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:mockShortcutStateManager];
XCTestExpectation *resultExpectation =
[self expectationWithDescription:@"result block must be called."];
[plugin handleMethodCall:call
Expand All @@ -63,9 +62,9 @@ - (void)testHandleMethodCall_getLaunchAction {
FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"getLaunchAction"
arguments:nil];

FLTQuickActionsPlugin *plugin =
[[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:OCMClassMock([FLTShortcutStateManager class])];
QuickActionsPlugin *plugin =
[[QuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:OCMClassMock([FLTShortcutStateManager class])];
XCTestExpectation *resultExpectation =
[self expectationWithDescription:@"result block must be called."];
[plugin handleMethodCall:call
Expand All @@ -79,9 +78,9 @@ - (void)testHandleMethodCall_getLaunchAction {
- (void)testHandleMethodCall_nonExistMethods {
FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"nonExist" arguments:nil];

FLTQuickActionsPlugin *plugin =
[[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:OCMClassMock([FLTShortcutStateManager class])];
QuickActionsPlugin *plugin =
[[QuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:OCMClassMock([FLTShortcutStateManager class])];
XCTestExpectation *resultExpectation =
[self expectationWithDescription:@"result must be called."];
[plugin
Expand All @@ -97,9 +96,9 @@ - (void)testHandleMethodCall_nonExistMethods {

- (void)testApplicationPerformActionForShortcutItem {
id mockChannel = OCMClassMock([FlutterMethodChannel class]);
FLTQuickActionsPlugin *plugin =
[[FLTQuickActionsPlugin alloc] initWithChannel:mockChannel
shortcutStateManager:OCMClassMock([FLTShortcutStateManager class])];
QuickActionsPlugin *plugin =
[[QuickActionsPlugin alloc] initWithChannel:mockChannel
shortcutStateManager:OCMClassMock([FLTShortcutStateManager class])];

UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc]
initWithType:@"SearchTheThing"
Expand All @@ -118,9 +117,9 @@ - (void)testApplicationPerformActionForShortcutItem {

- (void)testApplicationDidFinishLaunchingWithOptions_launchWithShortcut {
id mockShortcutStateManager = OCMClassMock([FLTShortcutStateManager class]);
FLTQuickActionsPlugin *plugin =
[[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:mockShortcutStateManager];
QuickActionsPlugin *plugin =
[[QuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:mockShortcutStateManager];

UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc]
initWithType:@"SearchTheThing"
Expand All @@ -138,9 +137,9 @@ - (void)testApplicationDidFinishLaunchingWithOptions_launchWithShortcut {
}

- (void)testApplicationDidFinishLaunchingWithOptions_launchWithoutShortcut {
FLTQuickActionsPlugin *plugin =
[[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:OCMClassMock([FLTShortcutStateManager class])];
QuickActionsPlugin *plugin =
[[QuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])
shortcutStateManager:OCMClassMock([FLTShortcutStateManager class])];
BOOL launchResult = [plugin application:[UIApplication sharedApplication]
didFinishLaunchingWithOptions:@{}];
XCTAssertTrue(launchResult,
Expand All @@ -150,9 +149,9 @@ - (void)testApplicationDidFinishLaunchingWithOptions_launchWithoutShortcut {
- (void)testApplicationDidBecomeActive_launchWithoutShortcut {
id mockChannel = OCMClassMock([FlutterMethodChannel class]);
id mockShortcutStateManager = OCMClassMock([FLTShortcutStateManager class]);
FLTQuickActionsPlugin *plugin =
[[FLTQuickActionsPlugin alloc] initWithChannel:mockChannel
shortcutStateManager:mockShortcutStateManager];
QuickActionsPlugin *plugin =
[[QuickActionsPlugin alloc] initWithChannel:mockChannel
shortcutStateManager:mockShortcutStateManager];

[plugin application:[UIApplication sharedApplication] didFinishLaunchingWithOptions:@{}];
[plugin applicationDidBecomeActive:[UIApplication sharedApplication]];
Expand All @@ -162,9 +161,9 @@ - (void)testApplicationDidBecomeActive_launchWithoutShortcut {
- (void)testApplicationDidBecomeActive_launchWithShortcut {
id mockChannel = OCMClassMock([FlutterMethodChannel class]);
id mockShortcutStateManager = OCMClassMock([FLTShortcutStateManager class]);
FLTQuickActionsPlugin *plugin =
[[FLTQuickActionsPlugin alloc] initWithChannel:mockChannel
shortcutStateManager:mockShortcutStateManager];
QuickActionsPlugin *plugin =
[[QuickActionsPlugin alloc] initWithChannel:mockChannel
shortcutStateManager:mockShortcutStateManager];

UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc]
initWithType:@"SearchTheThing"
Expand All @@ -183,9 +182,9 @@ - (void)testApplicationDidBecomeActive_launchWithShortcut {
- (void)testApplicationDidBecomeActive_launchWithShortcut_becomeActiveTwice {
id mockChannel = OCMClassMock([FlutterMethodChannel class]);
id mockShortcutStateManager = OCMClassMock([FLTShortcutStateManager class]);
FLTQuickActionsPlugin *plugin =
[[FLTQuickActionsPlugin alloc] initWithChannel:mockChannel
shortcutStateManager:mockShortcutStateManager];
QuickActionsPlugin *plugin =
[[QuickActionsPlugin alloc] initWithChannel:mockChannel
shortcutStateManager:mockShortcutStateManager];

UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc]
initWithType:@"SearchTheThing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

@import quick_actions_ios;
@import quick_actions_ios.Test;
@import XCTest;
#import <OCMock/OCMock.h>

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import Flutter

public final class QuickActionsPlugin: NSObject, FlutterPlugin {

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(
name: "plugins.flutter.io/quick_actions_ios",
binaryMessenger: registrar.messenger())
let instance = QuickActionsPlugin(channel: channel)
registrar.addMethodCallDelegate(instance, channel: channel)
registrar.addApplicationDelegate(instance)
}

private let channel: FlutterMethodChannel
private let shortcutStateManager: FLTShortcutStateManager
/// The type of the shortcut item selected when launching the app.
private var launchingShortcutType: String? = nil

// TODO: (hellohuanlin) remove `@objc` attribute and make it non-public after migrating tests to Swift.
@objc
public init(
channel: FlutterMethodChannel,
shortcutStateManager: FLTShortcutStateManager = FLTShortcutStateManager()
) {
self.channel = channel
self.shortcutStateManager = shortcutStateManager
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "setShortcutItems":
// `arguments` must be a list of dictionaries
let items = call.arguments as! [[String: Any]]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force unwrap it to keep the same behavior as ObjC. Whether to crash on platform code or to surface an error is discussed here in this proposal.

Copy link
Contributor

@cyanglaz cyanglaz Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If items is nil, it doesn't crash on Objective-C(here). It actually sets the [UIApplication sharedApplication].shortcutItems to an empty array, which results the same behavior as "clearShortcutItems". On the other hand, it would crash in this swift version due to force unwrapping.

I think the current Objective-C behavior is more like?

let items = call.arguments as [[String: Any]] ?? []
shortcutStateManager.setShortcutItems(items)
result(nil)

I'm not sure the original design of an implicit "clearShortcutItems" is intended but for migration I feel like we should keep the behavior the same if possible and have a follow up PR to handle the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If wrong type it should crash:

NSNumber *arr = @1;
for (NSDictionary *item in (NSArray *)arr) { // crash here
  NSLog(@"%@", item);
}

But I see your point about nil value. The equivalent swift should be this instead:

if let arguments = call.arguments {
  // crash of wrong type
  items = arguments as! [[String:Any]]
} else {
  // no crash if nil
  items = []
}

Not sure if it's worth to do tho. The corresponding dart side guarantees that the argument is non-null. So the else block actually never run.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

shortcutStateManager.setShortcutItems(items)
result(nil)
case "clearShortcutItems":
shortcutStateManager.setShortcutItems([])
result(nil)
case "getLaunchAction":
result(nil)
case _:
result(FlutterMethodNotImplemented)
}
}

public func application(
_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: @escaping (Bool) -> Void
) -> Bool {
handleShortcut(shortcutItem.type)
return true
}

public func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [AnyHashable: Any] = [:]
) -> Bool {
if let shortcutItem = launchOptions[UIApplication.LaunchOptionsKey.shortcutItem]
as? UIApplicationShortcutItem
{
// Keep hold of the shortcut type and handle it in the
// `applicationDidBecomeActure:` method once the Dart MethodChannel
// is initialized.
launchingShortcutType = shortcutItem.type

// Return false to indicate we handled the quick action to ensure
// the `application:performActionFor:` method is not called (as
// per Apple's documentation:
// https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622935-application).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these comments are copied over from objc.

return false
}
return true
}

public func applicationDidBecomeActive(_ application: UIApplication) {
if let shortcutType = launchingShortcutType {
handleShortcut(shortcutType)
launchingShortcutType = nil
}
}

private func handleShortcut(_ shortcut: String) {
channel.invokeMethod("launch", arguments: shortcut)
}

}
Loading