Created
November 26, 2015 13:18
-
-
Save radex/6c4f8da03ba57422574b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Option 1: if..elseif + optional unwrapping conditions | |
private func handleNotificationAction(id: String?, userInfo: [NSObject: AnyObject], responseInfo: [NSObject: AnyObject]?, completion: () -> Void) { | |
let taskId = userInfo["extra"]?["task_hash"] as? String | |
let projectId = userInfo["extra"]?["project_hash"] as? String | |
let comment = responseInfo?["UIUserNotificationActionResponseTypedTextKey"] as? String | |
if id == "comment", let task = taskId, comment = comment where !comment.isEmpty { | |
} else if id == "invitation_accept", let project = projectId { | |
} else if id == "invitation_decline", let project = projectId { | |
} else if id == "star", let task = taskId { | |
} else if id == "complete", let task = taskId { | |
} else if id == "show" || id == "comment" || id == "delegate", let task = taskId { | |
} else { | |
// assertionFailure | |
} | |
} | |
// Option 2: switch + force unwrap | |
private func handleNotificationAction(id: String?, userInfo: [NSObject: AnyObject], responseInfo: [NSObject: AnyObject]?, completion: () -> Void) { | |
let taskId = userInfo["extra"]?["task_hash"] as? String | |
let projectId = userInfo["extra"]?["project_hash"] as? String | |
let comment = responseInfo?["UIUserNotificationActionResponseTypedTextKey"] as? String | |
switch id { | |
case "comment"? where taskId != nil && comment != nil && !comment.isEmpty: | |
case "invitation_accept"? where projectId != nil: | |
case "invitation_decline"? where projectId != nil: | |
case "star"? where taskId != nil: | |
case "complete"? where taskId != nil: | |
case "show"?, "comment"?, "delegate"? where taskId != nil: | |
default: | |
// assertionFailure | |
} | |
} |
How about going completely left field and doing something like the following:
typealias NotifyActionSuggestionHandler = (taskId : String?, projectId : String?, comment: String?, completion: () -> Void) -> ()
func handleComment(taskId taskId: String?, projectId : String?, comment: String?, completion: () -> Void) {
guard let tid = taskId, let c = comment where !c.isEmpty else {
print("Comment Data Error")
return
}
print("Comment: task(\(tid))")
completion()
}
func handleInvitationAccept(taskId taskId: String?, projectId : String?, comment: String?, completion: () -> Void) {
print("Invitation Accept")
completion()
}
var handleStar : NotifyActionSuggestionHandler = {(tid,pid,com,comp) in
print("Handle Star")
comp()
}
let commands = ["comment" : handleComment, "invitation_accept" : handleInvitationAccept, "star" : handleStar]
func handleNotificationActionSuggestion(id: String?, userInfo: [NSObject: AnyObject], responseInfo: [NSObject: AnyObject]?, completion: () -> Void) {
guard let actionId = id, let action = commands[actionId] else {return} // Or assertion failure (fail fast)
action(taskId: userInfo["extra"]?["task_hash"] as? String, projectId: userInfo["extra"]?["project_hash"] as? String, comment: responseInfo?["UIUserNotificationActionResponseTypedTextKey"] as? String, completion: completion)
}
let user : [NSObject:AnyObject] = [:]
let response : [NSObject:AnyObject] = [:]
handleNotificationActionSuggestion("comment", userInfo: user, responseInfo: response, completion: {print("Comment Done")})
handleNotificationActionSuggestion("invitation_accept", userInfo: user, responseInfo: response, completion: {print("Invitation Done")})
handleNotificationActionSuggestion("star", userInfo: user, responseInfo: response, completion: {print("Star Done")})
Keep functions small, easily extendible and avoids complex conditionals/switch on strings - also brings testing benefits. (Although I do like narfdotpl suggestion)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh, a slight improvement over the previous one:
...and it works! I think that matching can be a good addition to the standard extensions :P