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

Commit fd9968e

Browse files
committed
do not crash if no type or title
1 parent cc00bd7 commit fd9968e

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

packages/quick_actions/quick_actions_ios/example/ios/RunnerTests/DefaultShortcutItemParserTests.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import XCTest
99

1010
class DefaultShortcutItemParserTests: XCTestCase {
1111

12-
func testparseShortcutItems() {
12+
func testParseShortcutItems() {
1313
let rawItem = [
1414
"type": "SearchTheThing",
1515
"localizedTitle": "Search the thing",
@@ -27,7 +27,7 @@ class DefaultShortcutItemParserTests: XCTestCase {
2727
XCTAssertEqual(parser.parseShortcutItems([rawItem]), [expectedItem])
2828
}
2929

30-
func testparseShortcutItems_noIcon() {
30+
func testParseShortcutItems_noIcon() {
3131
let rawItem: [String: Any] = [
3232
"type": "SearchTheThing",
3333
"localizedTitle": "Search the thing",
@@ -44,4 +44,24 @@ class DefaultShortcutItemParserTests: XCTestCase {
4444
let parser = DefaultShortcutItemParser()
4545
XCTAssertEqual(parser.parseShortcutItems([rawItem]), [expectedItem])
4646
}
47+
48+
func testParseShortcutItems_noType() {
49+
let rawItem = [
50+
"localizedTitle": "Search the thing",
51+
"icon": "search_the_thing.png",
52+
]
53+
54+
let parser = DefaultShortcutItemParser()
55+
XCTAssertEqual(parser.parseShortcutItems([rawItem]), [])
56+
}
57+
58+
func testParseShortcutItems_noLocalizedTitle() {
59+
let rawItem = [
60+
"type": "SearchTheThing",
61+
"icon": "search_the_thing.png",
62+
]
63+
64+
let parser = DefaultShortcutItemParser()
65+
XCTAssertEqual(parser.parseShortcutItems([rawItem]), [])
66+
}
4767
}

packages/quick_actions/quick_actions_ios/ios/Classes/ShortcutItemParser.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@ final class DefaultShortcutItemParser: ShortcutItemParser {
2222
return items.compactMap { deserializeShortcutItem(with: $0) }
2323
}
2424

25-
private func deserializeShortcutItem(with serialized: [String: Any]) -> UIApplicationShortcutItem
25+
private func deserializeShortcutItem(with serialized: [String: Any]) -> UIApplicationShortcutItem?
2626
{
27+
guard
28+
let type = serialized["type"] as? String,
29+
let localizedTitle = serialized["localizedTitle"] as? String
30+
else {
31+
return nil
32+
}
2733

2834
let icon = (serialized["icon"] as? String).map {
2935
UIApplicationShortcutIcon(templateImageName: $0)
3036
}
3137

3238
// type and localizedTitle are required.
3339
return UIApplicationShortcutItem(
34-
type: serialized["type"] as! String,
35-
localizedTitle: serialized["localizedTitle"] as! String,
40+
type: type,
41+
localizedTitle: localizedTitle,
3642
localizedSubtitle: nil,
3743
icon: icon,
3844
userInfo: nil)

0 commit comments

Comments
 (0)