Skip to content

Commit

Permalink
refactor using associatedtype
Browse files Browse the repository at this point in the history
  • Loading branch information
sgr-ksmt committed Jun 24, 2017
1 parent b5e7f34 commit 13d7478
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Sources/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension Alertift {
///
/// - Parameter actionHandler: Action handler for **UIAlertAction**
/// - Returns: Instance of **UIAlertAction**
func buildAlertAction(handler actionHandler: @escaping Action.Handler) -> UIAlertAction {
func buildAlertAction(handler actionHandler: Action.Handler?) -> UIAlertAction {
return UIAlertAction(title: title, style: style, handler: actionHandler)
}
}
Expand Down
21 changes: 3 additions & 18 deletions Sources/ActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,11 @@ extension Alertift {
}

/// Add action to alertController
public func action(_ action: Alertift.Action, handler: @escaping Handler = { _ in}) -> Self {
_alertController.addAction(buildAlertAction(action, handler: merge(_alertController.actionHandler, handler)))
public func action(_ action: Alertift.Action, handler: Handler? = nil) -> Self {
_alertController.addAction(buildAlertAction(action, handler: handler.map { merge(_alertController.actionHandler, $0) }))
return self
}

/// Add actions to Alert
///
/// - Parameters:
/// - actions: Alert actions.
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func actions(_ actions: [Alertift.Action], handler: @escaping Handler = { _ in }) -> Self {
actions.forEach { _ = action($0, handler: handler) }
return self
}

public func actions(_ actions: [String?], handler: @escaping Handler = { _ in }) -> Self {
return self.actions(actions.map(Alertift.Action.init(title:)), handler: handler)
}


/// Add finally handler.
///
/// - Parameter handler: The handler to execute after either alert selected.
Expand Down
24 changes: 7 additions & 17 deletions Sources/Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,22 @@ extension Alertift {
buildAlertControlelr(title: title, message: message, style: .alert)
}

/// Add action to Alert
///
/// - Parameters:
/// - action: Alert action.
/// - isPreferred: If you want to change this action to preferredAction, set true. Default is false.
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, isPreferred: Bool = false, handler: @escaping Handler = { _ in }) -> Self {
addActionToAlertController(buildAlertAction(action, handler: merge(_alertController.actionWithTextFieldsHandler, handler)), isPreferred: isPreferred)
public func action(_ action: Alertift.Action, handler: Handler? = nil) -> Self {
addActionToAlertController(buildAlertAction(action, handler: handler.map { merge(_alertController.actionWithTextFieldsHandler, $0) }), isPreferred: false)
return self
}

/// Add actions to Alert
/// Add action to Alert
///
/// - Parameters:
/// - actions: Alert actions.
/// - action: Alert action.
/// - isPreferred: If you want to change this action to preferredAction, set true. Default is false.
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func actions(_ actions: [Alertift.Action], handler: @escaping Handler = { _ in }) -> Self {
actions.forEach { _ = action($0, handler: handler) }
public func action(_ action: Alertift.Action, isPreferred: Bool, handler: Handler? = nil) -> Self {
addActionToAlertController(buildAlertAction(action, handler: handler.map { merge(_alertController.actionWithTextFieldsHandler, $0) }), isPreferred: isPreferred)
return self
}

public func actions(_ actions: [String?], handler: @escaping Handler = { _ in }) -> Self {
return self.actions(actions.map(Alertift.Action.init(title:)), handler: handler)
}

/// Add finally handler.
///
Expand Down
37 changes: 31 additions & 6 deletions Sources/AlertType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ extension _AlertType where Self: AlertType {

/// AlertType protocol
public protocol AlertType: class {
associatedtype Handler

/// Add action to Alert
///
/// - Parameters:
/// - action: Alert action.
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
func action(_ action: Alertift.Action, handler: Handler?) -> Self

/// UIAlertController
var alertController: UIAlertController { get }
/// default background color of Alert(ActionSheet).
Expand All @@ -48,14 +58,19 @@ extension AlertType {
return alertController as! InnerAlertController
}

/// Build **UIAlertAction** using **Alertift.Action** and handler.
/// Add actions to Alert
///
/// - Parameters:
/// - action: action
/// - handler: The handler to execute after the action selected.
/// - Returns: **UIAlertAction**
func buildAlertAction(_ action: Alertift.Action, handler: @escaping Alertift.Action.Handler) -> UIAlertAction {
return action.buildAlertAction(handler: ActionHandlerBuilder.build(handler, _alertController.finallyExecutor))
/// - actions: Alert actions.
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func actions(_ actions: [Alertift.Action], handler: Handler? = nil) -> Self {
actions.forEach { _ = action($0, handler: handler) }
return self
}

public func actions(_ actions: [String?], handler: Handler? = nil) -> Self {
return self.actions(actions.map(Alertift.Action.init(title:)), handler: handler)
}

/// Change background color
Expand Down Expand Up @@ -126,6 +141,16 @@ extension AlertType {
}
viewController?.present(_alertController, animated: true, completion: completion)
}

/// Build **UIAlertAction** using **Alertift.Action** and handler.
///
/// - Parameters:
/// - action: action
/// - handler: The handler to execute after the action selected.
/// - Returns: **UIAlertAction**
func buildAlertAction(_ action: Alertift.Action, handler: Alertift.Action.Handler?) -> UIAlertAction {
return action.buildAlertAction(handler: handler.map { ActionHandlerBuilder.build($0, _alertController.finallyExecutor) })
}
}

/// Deprecations
Expand Down

0 comments on commit 13d7478

Please sign in to comment.