Skip to content

Commit adc2e7c

Browse files
committed
Simplify action handling
1 parent 8986c0c commit adc2e7c

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

targets/ShieldAction/ShieldActionExtension.swift

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ func sendNotification(){
4141
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
4242
}
4343

44-
func handleAction(dict: [String: Any]) -> ShieldActionResponse {
44+
func handleAction(configForSelectedAction: [String: Any]) -> ShieldActionResponse {
4545
logger.log("handleAction")
46-
if let type = dict["type"] as? String {
46+
if let type = configForSelectedAction["type"] as? String {
4747
if(type == "unblockAll"){
4848
unblockAllApps()
4949
}
5050
}
5151

52-
if let behaviour = dict["behavior"] as? String {
52+
if let behaviour = configForSelectedAction["behavior"] as? String {
5353
if(behaviour == "defer"){
5454
return .defer
5555
}
@@ -58,52 +58,37 @@ func handleAction(dict: [String: Any]) -> ShieldActionResponse {
5858
return .close
5959
}
6060

61+
func handleAction(action: ShieldAction, completionHandler: @escaping (ShieldActionResponse) -> Void) {
62+
if let shieldActionConfig = userDefaults?.dictionary(forKey: "shieldActions") {
63+
if let configForSelectedAction = shieldActionConfig[action == .primaryButtonPressed ? "primary" : "secondary"] as? [String: Any] {
64+
let response = handleAction(configForSelectedAction: configForSelectedAction)
65+
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
66+
completionHandler(response);
67+
}
68+
} else {
69+
completionHandler(.close)
70+
}
71+
} else {
72+
completionHandler(.close)
73+
}
74+
}
75+
6176
// Override the functions below to customize the shield actions used in various situations.
6277
// The system provides a default response for any functions that your subclass doesn't override.
6378
// Make sure that your class name matches the NSExtensionPrincipalClass in your Info.plist.
6479
class ShieldActionExtension: ShieldActionDelegate {
6580
override func handle(action: ShieldAction, for application: ApplicationToken, completionHandler: @escaping (ShieldActionResponse) -> Void) {
6681
logger.log("handle application")
67-
if let obj = userDefaults?.dictionary(forKey: "shieldActions") {
68-
if let dict = obj[action == .primaryButtonPressed ? "primary" : "secondary"] as? [String: Any] {
69-
let response = handleAction(dict: dict)
70-
completionHandler(response)
71-
} else {
72-
completionHandler(.close)
73-
}
74-
} else {
75-
completionHandler(.close)
76-
}
82+
handleAction(action: action, completionHandler: completionHandler)
7783
}
7884

7985
override func handle(action: ShieldAction, for webDomain: WebDomainToken, completionHandler: @escaping (ShieldActionResponse) -> Void) {
8086
logger.log("handle domain")
81-
if let obj = userDefaults?.dictionary(forKey: "shieldActions") {
82-
if let dict = obj[action == .primaryButtonPressed ? "primary" : "secondary"] as? [String: Any] {
83-
let response = handleAction(dict: dict)
84-
completionHandler(response)
85-
} else {
86-
completionHandler(.close)
87-
}
88-
} else {
89-
completionHandler(.close)
90-
}
87+
handleAction(action: action, completionHandler: completionHandler)
9188
}
9289

9390
override func handle(action: ShieldAction, for category: ActivityCategoryToken, completionHandler: @escaping (ShieldActionResponse) -> Void) {
9491
logger.log("handle category")
95-
// openUrl(urlString: "device-activity://")
96-
97-
if let obj = userDefaults?.dictionary(forKey: "shieldActions") {
98-
if let dict = obj[action == .primaryButtonPressed ? "primary" : "secondary"] as? [String: Any] {
99-
let response = handleAction(dict: dict)
100-
completionHandler(response)
101-
} else {
102-
completionHandler(.close)
103-
}
104-
} else {
105-
completionHandler(.close)
106-
}
107-
// sendNotification()
92+
handleAction(action: action, completionHandler: completionHandler)
10893
}
10994
}

0 commit comments

Comments
 (0)