Skip to content

Commit

Permalink
Add support to automatically dismiss .Cancel style alerts
Browse files Browse the repository at this point in the history
[#128850809]
  • Loading branch information
jwfriese committed Aug 26, 2016
1 parent 28ba8ae commit 95ebb4d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 8 additions & 1 deletion Fleet/CoreExtensions/UIAlertController+Fleet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ public extension UIAlertController {
}

if let actionWithTitle = filteredActions.first {
let isCancelStyle = actionWithTitle.style == .Cancel
if let handler = actionWithTitle.handler {
handler(actionWithTitle)
} else {
Logger.logWarning("Action with title \"\(title)\" has no handler")
if !isCancelStyle {
Logger.logWarning("Action with title \"\(title)\" has no handler and is not Cancel style")
}
}

if isCancelStyle {
presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
} else {
Logger.logWarning("No action with title \"\(title)\" found on alert")
Expand Down
5 changes: 1 addition & 4 deletions FleetTestApp/Storyboards/SpottedTurtleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ class SpottedTurtleViewController: UIViewController {

alertController.addAction(alertAction)

let dismissAction = UIAlertAction(title: "Cower", style: .Cancel) { action in
self.dismissViewControllerAnimated(true, completion: nil)
}

let dismissAction = UIAlertAction(title: "Cower", style: .Cancel, handler: nil)
alertController.addAction(dismissAction)

presentViewController(alertController, animated: true, completion: nil)
Expand Down
11 changes: 11 additions & 0 deletions FleetTests/CoreExtensions/UIAlertController+FleetSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ class UIAlertController_FleetSpec: XCTestCase {
expect(self.viewControllerThatPresentsAlerts.presentedViewController).to(beNil())
expect(self.viewControllerThatPresentsAlerts.informationalLabel?.text).to(equal("WAIT NO PUT ME DOWN"))
}

func test_tapAlertActionWithTitle_whenActionWithThatTitleExistsOnAlert_whenActionIsCancelStyle_dismissesAlert() {
let alertWithCancelAction = UIAlertController()
alertWithCancelAction.addAction(UIAlertAction(title: "Go Away", style: .Cancel, handler: nil))
viewControllerThatPresentsAlerts.presentViewController(alertWithCancelAction, animated: false, completion: nil)

let alertController = Fleet.getCurrentScreen()?.presentedAlert
alertController?.tapAlertActionWithTitle("Go Away")

expect(self.viewControllerThatPresentsAlerts.presentedViewController).to(beNil())
}
}

0 comments on commit 95ebb4d

Please sign in to comment.