Skip to content

Commit

Permalink
modified addAction functions and finally action.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgr-ksmt committed Jun 24, 2017
1 parent f77ef61 commit 6f68007
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 75 deletions.
6 changes: 4 additions & 2 deletions Sources/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extension Alertift {
/// - destructive: Destructive action(action title)
/// - cancel: Cancel description(action title)
public enum Action {
typealias Handler = (UIAlertAction) -> Void

case `default`(String?)
case destructive(String?)
case cancel(String?)
Expand All @@ -41,8 +43,8 @@ extension Alertift {
///
/// - Parameter actionHandler: Action handler for **UIAlertAction**
/// - Returns: Instance of **UIAlertAction**
func buildAlertAction(handler actionHandler: @escaping (UIAlertAction) -> Void) -> UIAlertAction {
return UIAlertAction(title: title, style: style, handler: { actionHandler($0) })
func buildAlertAction(handler actionHandler: @escaping Action.Handler) -> UIAlertAction {
return UIAlertAction(title: title, style: style, handler: actionHandler)
}
}
}
8 changes: 4 additions & 4 deletions Sources/ActionHandlerBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ final class ActionHandlerBuilder {
private init() {}

/// Build closure using two closure.
static func build<T: UIAlertAction>(_ h1: @escaping () -> Void, _ h2: @escaping (T) -> Void) -> (T) -> Void {
return { action in
h1()
h2(action)
static func build<T: UIAlertAction>(_ h1: @escaping (T) -> Void, _ h2: @escaping (T) -> Void) -> (T) -> Void {
return {
h1($0)
h2($0)
}
}
}
Expand Down
25 changes: 21 additions & 4 deletions Sources/ActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import Foundation
extension Alertift {
/// ActionSheet
final public class ActionSheet: AlertType, _AlertType {

public typealias Handler = (UIAlertAction, Int) -> Void

var _alertController: InnerAlertController!
public var alertController: UIAlertController {
return _alertController as UIAlertController
Expand All @@ -33,12 +34,20 @@ extension Alertift {
}

/// Add action to alertController
@available(*, unavailable, message: "")
public func action(_ action: Alertift.Action, handler: @escaping Alertift.ActionHandler = {}) -> Self {
_alertController.addAction(buildAlertAction(action, handler: handler))
public func action(_ action: Alertift.Action, handler: @escaping Handler = { _ in}) -> Self {
_alertController.addAction(buildAlertAction(action, handler: merge(_alertController.actionHandler, handler)))
return self
}

/// Add finally handler.
///
/// - Parameter handler: The handler to execute after either alert selected.
/// - Returns: Myself
public func finally(handler: @escaping Handler) -> Self {
_alertController.finallyHandler = { handler($0.0, $0.1) }
return self
}

/// Add sourceView and sourceRect to **popoverPresentationController**.
///
/// If you want to use action sheet on iPad, you have to use this method.
Expand Down Expand Up @@ -81,3 +90,11 @@ extension Alertift {
}
}
}

/// Deprecations
extension Alertift.ActionSheet {
@available(*, unavailable, message: "use new 'action(_:handler)'")
public func action(_ action: Alertift.Action, handler: @escaping () -> Void = {}) -> Self {
fatalError("")
}
}
61 changes: 34 additions & 27 deletions Sources/Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Foundation
extension Alertift {
/// Alert
final public class Alert: AlertType, _AlertType {
public typealias Handler = (UIAlertAction, Int, [UITextField]?) -> Void

/// TextFieldHandler
public typealias TextFieldHandler = ((UITextField, Int) -> Void)

Expand All @@ -37,44 +39,24 @@ extension Alertift {
buildAlertControlelr(title: title, message: message, style: .alert)
}

/// Add alertAction to alertController
///
/// - Parameters:
/// - alertAction: UIAlertAction
/// - isPreferred: If isPreferred is true, alertAction becomes preferredAction.
private func addActionToAlertController(_ alertAction: UIAlertAction, isPreferred: Bool) {
_alertController.addAction(alertAction)
if isPreferred {
_alertController.preferredAction = alertAction
}
}

/// 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
@available(*, unavailable, message: "")
public func action(_ action: Alertift.Action, isPreferred: Bool = false, handler: @escaping Alertift.ActionHandler = {}) -> Self {
addActionToAlertController(buildAlertAction(action, handler: handler), isPreferred: isPreferred)
public func action(_ action: Alertift.Action, isPreferred: Bool = false, handler: @escaping Handler = { _ in }) -> Self {
addActionToAlertController(buildAlertAction(action, handler: merge(_alertController.actionWithTextFieldsHandler, handler)), isPreferred: isPreferred)
return self
}

/// Add action to Alert
/// Add finally handler.
///
/// - Parameters:
/// - action: Alert action.
/// - isPreferred: If you want to change this action to preferredAction, set true. Default is false.
/// - textFieldsHandler: The block that returns array of UITextFields to execute after this action performed.
/// - Parameter handler: The handler to execute after either alert selected.
/// - Returns: Myself
@available(*, unavailable, message: "")
final public func action(_ action: Alertift.Action, isPreferred: Bool = false, textFieldsHandler handler: @escaping ActionWithTextFieldsHandler) -> Self {
addActionToAlertController(
buildAlertAction(action, handler: merge(_alertController.actionWithTextFieldsHandler, handler)),
isPreferred: isPreferred
)
public func finally(handler: @escaping Handler) -> Self {
_alertController.finallyHandler = handler
return self
}

Expand Down Expand Up @@ -105,8 +87,33 @@ extension Alertift {
return self
}

/// Add alertAction to alertController
///
/// - Parameters:
/// - alertAction: UIAlertAction
/// - isPreferred: If isPreferred is true, alertAction becomes preferredAction.
private func addActionToAlertController(_ alertAction: UIAlertAction, isPreferred: Bool) {
_alertController.addAction(alertAction)
if isPreferred {
_alertController.preferredAction = alertAction
}
}

deinit {
Debug.log()
}
}
}

/// Deprecations
extension Alertift.Alert {
@available(*, unavailable, message: "use new 'action(_:isPreferred:handler)'")
public func action(_ action: Alertift.Action, isPreferred: Bool = false, handler: @escaping () -> Void = {}) -> Self {
fatalError("")
}

@available(*, unavailable, message: "use new 'action(_:isPreferred:handler)'")
final public func action(_ action: Alertift.Action, isPreferred: Bool = false, textFieldsHandler handler: @escaping ActionWithTextFieldsHandler) -> Self {
fatalError("")
}
}
20 changes: 9 additions & 11 deletions Sources/AlertType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,10 @@ extension AlertType {
/// - action: action
/// - handler: The handler to execute after the action selected.
/// - Returns: **UIAlertAction**
@available(*, unavailable, message: "")
func buildAlertAction(_ action: Alertift.Action, handler: @escaping Alertift.ActionHandler) -> UIAlertAction {
func buildAlertAction(_ action: Alertift.Action, handler: @escaping Alertift.Action.Handler) -> UIAlertAction {
return action.buildAlertAction(handler: ActionHandlerBuilder.build(handler, _alertController.finallyExecutor))
}

/// Add finally handler.
///
/// - Parameter handler: The handler to execute after either alert selected.
/// - Returns: Myself
public func finally(handler: @escaping Alertift.FinallyHandler) -> Self {
_alertController.finallyHandler = handler
return self
}

/// Change background color
///
/// - Parameter color: UIColor
Expand Down Expand Up @@ -137,3 +127,11 @@ extension AlertType {
viewController?.present(_alertController, animated: true, completion: completion)
}
}

/// Deprecations
extension AlertType {
@available(*, unavailable, message: "use new 'buildAlertAction(_:handler:)'")
func buildAlertAction(_ action: Alertift.Action, handler: @escaping () -> Void) -> UIAlertAction {
fatalError("")
}
}
18 changes: 0 additions & 18 deletions Sources/Alertift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,3 @@ public final class Alertift {
return actionSheet(title: title, message: message).popover(anchorView: anchorView)
}
}

extension Alertift {
/// Action handler
@available(*, unavailable, message: "")
public typealias ActionHandler = () -> Void

/// Finally handler
public typealias FinallyHandler = (UIAlertAction, Int) -> Void

public typealias AlertHandler = (UIAlertAction, Int, [UITextField]?) -> Void

public typealias ActionSheetHandler = (UIAlertAction, Int) -> Void
}


/// Internal typealiases
typealias _Alert = Alertift.Alert
typealias _ActionSheet = Alertift.ActionSheet
21 changes: 12 additions & 9 deletions Sources/InnerAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import UIKit
/// subclass of **UIAlertController**
class InnerAlertController: UIAlertController {
/// textFieldTextDidChangeHandler: ((UITextField, Int) -> Void)
var textFieldTextDidChangeHandler: _Alert.TextFieldHandler?
var textFieldTextDidChangeHandler: Alertift.Alert.TextFieldHandler?

/// finallyHandler: (UIAlertAction, Int) -> Void
var finallyHandler: Alertift.FinallyHandler?
public typealias FinallyHandler = (UIAlertAction, Int, [UITextField]?) -> Void
var finallyHandler: FinallyHandler?

var alertBackgroundColor: UIColor?
var titleTextColor: UIColor? = .black
Expand Down Expand Up @@ -49,18 +49,21 @@ class InnerAlertController: UIAlertController {
}
textFieldTextDidChangeHandler?(textField, index)
}


/// Returns actionHandler
var actionHandler: (UIAlertAction) -> (UIAlertAction, Int) {
return { [weak self] action in (action, self?.actions.index(of: action) ?? -1) }
}

/// Returns actionWithTextFieldsHandler
var actionWithTextFieldsHandler: () -> ([UITextField]?) {
return { [weak self] in
self?.textFields
}
var actionWithTextFieldsHandler: (UIAlertAction) -> (UIAlertAction, Int, [UITextField]?) {
return { [weak self] action in (action, self?.actions.index(of: action) ?? -1, self?.textFields) }
}

/// Returns finallyExecutor
var finallyExecutor: (UIAlertAction) -> Void {
return { [weak self] action in
self?.finallyHandler?(action, self?.actions.index(of: action) ?? -1)
self?.finallyHandler?(action, self?.actions.index(of: action) ?? -1, self?.textFields)
}
}

Expand Down

0 comments on commit 6f68007

Please sign in to comment.