-
Notifications
You must be signed in to change notification settings - Fork 336
Improve UIAlertController APIs in the SwiftCatalog sample app. #87
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
github "facebook/facebook-ios-sdk" ~> 4.14 | ||
github "facebook/facebook-ios-sdk" ~> 4.16 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
github "BoltsFramework/Bolts-ObjC" "1.8.4" | ||
github "facebook/facebook-ios-sdk" "sdk-version-4.15.0" | ||
github "facebook/facebook-ios-sdk" "sdk-version-4.16.0" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,94 @@ | |
import Foundation | ||
import UIKit | ||
|
||
extension UIAlertAction { | ||
|
||
/// Action types most commonly used | ||
public enum actionType{ | ||
|
||
///Ok Option | ||
case ok | ||
|
||
/// Default Cancel Option | ||
case cancel | ||
|
||
/// Destructive action with custom title | ||
case destructive(String) | ||
|
||
/// Custom action with title and style | ||
case custom(String,UIAlertActionStyle) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Space between |
||
|
||
/** | ||
Creates the action instance for UIAlertController | ||
- parameter handler: Call Back function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Add empty line between description and first parameter. |
||
- returns UIAlertAction Instance | ||
*/ | ||
public func action(handler:((String) -> Void)? = nil) -> UIAlertAction { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Space after |
||
|
||
//Default value | ||
var actionStyle = UIAlertActionStyle.default | ||
var title = "" | ||
|
||
// Action configuration based on the action type | ||
switch self { | ||
|
||
case .cancel: | ||
actionStyle = .cancel | ||
title = "Cancel" | ||
|
||
case .destructive(let optionTitle): | ||
title = optionTitle | ||
actionStyle = .destructive | ||
|
||
case .custom(let optionTitle, let style): | ||
title = optionTitle | ||
actionStyle = style | ||
|
||
default: | ||
title = "Ok" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
} | ||
|
||
//Creating UIAlertAction instance | ||
let action = UIAlertAction(title:title,style:actionStyle, handler: {(nativeAction) in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Reformat using this style There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Since the closure is the last argument, you can use the trailing closure syntax aka return UIAlertAction(title: title, style: actionStyle) { nativeAction in
if let handler = handle {
handler(title)
}
} |
||
if (handler != nil){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified and be more Swifty using the following:
Or even |
||
handler!(title) | ||
} | ||
}) | ||
|
||
return action | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't seem to be re-using |
||
} | ||
} | ||
} | ||
|
||
extension UIAlertController { | ||
convenience init(title: String, message: String) { | ||
self.init(title: title, message: message, preferredStyle: .alert) | ||
let action = UIAlertAction(title: NSLocalizedString("OK", comment: "OK action"), | ||
style: .default, | ||
handler: nil) | ||
addAction(action) | ||
|
||
/** | ||
Creates the alert view controller using the actions specified | ||
|
||
- parameter title: Title of the alert. | ||
- parameter message: Alert message body. | ||
- parameter actions: Variable numbre of actions as an Array of actionType values. | ||
- parameter style: UIAlertControllerStyle enum value | ||
- parameter handler: Handler block/closure for the clicked option. | ||
|
||
- let alert = UIAlertController.init(title:"Confirm",message:"Do you want to quit sign up and login with other account ?",actions:.custom("Yes,.default),.custom("Cancel",.destructive)){ (buttonTitle) in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Please line break this line or trim the |
||
if buttonTitle == "Yes"{ | ||
print("Yes Clicked") | ||
} | ||
} | ||
|
||
self.present(alert, animated: true, completion: nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this line is relevant, since it suggests presentation from inside a |
||
|
||
*/ | ||
convenience init(title: String, message: String ,actions:[UIAlertAction.actionType] = [.ok],style: UIAlertControllerStyle = .alert,handler:((String) -> Swift.Void)? = nil) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please line break and style as follows: convenience init(title: String,
message: String,
actions: [UIAlertAction.actionType] = [.ok],
style: UIAlertControllerStyle = .alert,
handler: ((String) -> Swift.Void)? = nil) { |
||
|
||
//initialize the contoller (self) instance | ||
self.init(title: title, message: message, preferredStyle:style) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
|
||
//Fetching actions specidied by the user and adding actions accordingly | ||
for actionType in actions { | ||
addAction(actionType.action(handler: handler)) | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Space after
actionType
and before{