Skip to content
Prev Previous commit
Next Next commit
remove parser class
  • Loading branch information
tarrinneal committed Oct 20, 2023
commit 20796bb00899621d3efaf5fabddca5b87d64ef28
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class DefaultShortcutItemParserTests: XCTestCase {
icon: UIApplicationShortcutIcon(templateImageName: "search_the_thing.png"),
userInfo: nil)

let parser = DefaultShortcutItemParser()
XCTAssertEqual(parser.parseShortcutItems([rawItem]), [expectedItem])
XCTAssertEqual(QuickActionsPlugin.parseShortcutItems([rawItem]), [expectedItem])
}

func testParseShortcutItems_noIcon() {
Expand All @@ -40,7 +39,6 @@ class DefaultShortcutItemParserTests: XCTestCase {
icon: nil,
userInfo: nil)

let parser = DefaultShortcutItemParser()
XCTAssertEqual(parser.parseShortcutItems([rawItem]), [expectedItem])
XCTAssertEqual(QuickActionsPlugin.parseShortcutItems([rawItem]), [expectedItem])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ public final class QuickActionsPlugin: NSObject, FlutterPlugin, IOSQuickActionsA

public static func register(with registrar: FlutterPluginRegistrar) {
let messenger = registrar.messenger()
let instance = QuickActionsPlugin(messenger: messenger)
let flutterApi = IOSQuickActionsFlutterApi(binaryMessenger: messenger)
let instance = QuickActionsPlugin(flutterApi: flutterApi)
IOSQuickActionsApiSetup.setUp(binaryMessenger: messenger, api: instance)
registrar.addApplicationDelegate(instance)
}

private let shortcutItemProvider: ShortcutItemProviding
private let shortcutItemParser: ShortcutItemParser = DefaultShortcutItemParser()
private let flutterApi: IOSQuickActionsFlutterApiProtocol
/// The type of the shortcut item selected when launching the app.
private var launchingShortcutType: String? = nil

// This init is meant for unit testing only.
init(
flutterApi: IOSQuickActionsFlutterApiProtocol,
shortcutItemProvider: ShortcutItemProviding = UIApplication.shared
Expand All @@ -28,27 +27,19 @@ public final class QuickActionsPlugin: NSObject, FlutterPlugin, IOSQuickActionsA
self.shortcutItemProvider = shortcutItemProvider
}

convenience init(
messenger: FlutterBinaryMessenger,
shortcutItemProvider: ShortcutItemProviding = UIApplication.shared
) {
let flutterApi = IOSQuickActionsFlutterApi(binaryMessenger: messenger)
self.init(flutterApi: flutterApi, shortcutItemProvider: shortcutItemProvider)
}

func setShortcutItems(itemsList: [ShortcutItemMessage]) {
shortcutItemProvider.shortcutItems = shortcutItemParser.parseShortcutItems(itemsList)
self.shortcutItemProvider.shortcutItems = QuickActionsPlugin.parseShortcutItems(itemsList)
}

func clearShortcutItems() {
shortcutItemProvider.shortcutItems = []
self.shortcutItemProvider.shortcutItems = []
}

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

Expand All @@ -75,14 +66,38 @@ public final class QuickActionsPlugin: NSObject, FlutterPlugin, IOSQuickActionsA

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

func handleShortcut(_ shortcut: String) {
flutterApi.launchAction(action: shortcut) { _ in
self.flutterApi.launchAction(action: shortcut) { _ in
// noop
}
}

static func parseShortcutItems(_ items: [ShortcutItemMessage]) -> [UIApplicationShortcutItem] {
Copy link
Contributor

@hellohuanlin hellohuanlin Oct 20, 2023

Choose a reason for hiding this comment

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

this doesn't seem to belong to a static member of QuickActionsPlugin. I suggest 2 alternatives -

  1. make them private non-static members of QuickActionsPlugin (so they become implementation details QuickActionsPlugin).

  2. A more swifty way I would do is to create class extension of either UIApplicationShortcutItem or ShortcutItemMessage, outside of QuickActionsPlugin

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, thanks for the feedback!

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh also avoid the naming "parse" since it's not parsing anymore. Something like:

extension ShortcutItemMessage {
  var item: UIApplicationShortcutItem {
    return ...
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we need the tests that I was using this for any longer, since this is now an implementation detail and my understanding is that the swifty thing to do is consider the converter tested, since it is used in code that is already tested.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes if it's implementation detail, it can only be tested via non-private members. You can delete those tests if it's already covered elsewhere

return items.compactMap { deserializeShortcutItem(with: $0) }
}

static private func deserializeShortcutItem(with serialized: ShortcutItemMessage)
-> UIApplicationShortcutItem?
{

let type = serialized.type
let localizedTitle = serialized.localizedTitle

let icon = (serialized.icon).map {
UIApplicationShortcutIcon(templateImageName: $0)
}

// type and localizedTitle are required.
return UIApplicationShortcutItem(
type: type,
localizedTitle: localizedTitle,
localizedSubtitle: nil,
icon: icon,
userInfo: nil)
}
}

This file was deleted.