Skip to content

Commit

Permalink
Rename alertController to _alertController as internal.
Browse files Browse the repository at this point in the history
Added new alertController as UIAlertController's instance
  • Loading branch information
sgr-ksmt committed Apr 30, 2017
1 parent 983a7fe commit c41f7d4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 42 deletions.
30 changes: 15 additions & 15 deletions Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,22 @@ class ViewController: UIViewController {
.show()
}
private func showActionSheet(source: UIView, frame: CGRect) {
Alertift.actionSheet(message: "Which food do you like?")
.popover(sourceView: source, sourceRect: frame)
.action(.default("🍣"))
.action(.default("🍎"))
.action(.default("🍖"))
.action(.default("🍅"))
.action(.cancel("None of them"))
.finally { action, index in
if action.style == .cancel {
return
Alertift.actionSheet(message: "Which food do you like?")
.popover(sourceView: source, sourceRect: frame)
.action(.default("🍣"))
.action(.default("🍎"))
.action(.default("🍖"))
.action(.default("🍅"))
.action(.cancel("None of them"))
.finally { action, index in
if action.style == .cancel {
return
}
Alertift.alert(message: "\(index). \(action.title!)")
.action(.default("OK"))
.show()
}
Alertift.alert(message: "\(index). \(action.title!)")
.action(.default("OK"))
.show()
}
.show()
.show()
}
}

11 changes: 7 additions & 4 deletions Sources/ActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ extension Alertift {
/// ActionSheet
final public class ActionSheet: AlertType, _AlertType {

public internal(set) var alertController: InnerAlertController!
var _alertController: InnerAlertController!
public var alertController: UIAlertController {
return _alertController as UIAlertController
}

public static var backgroundColor: UIColor?
public static var buttonTextColor: UIColor?
Expand All @@ -31,7 +34,7 @@ extension Alertift {

/// Add action to alertController
public func action(_ action: Alertift.Action, handler: @escaping Alertift.ActionHandler = {}) -> Self {
alertController.addAction(buildAlertAction(action, handler: handler))
_alertController.addAction(buildAlertAction(action, handler: handler))
return self
}

Expand All @@ -43,8 +46,8 @@ extension Alertift {
/// - rect: sourceRect
/// - Returns: MySelf
public func popover(sourceView view: UIView?, sourceRect rect: CGRect) -> Self {
alertController.popoverPresentationController?.sourceView = view
alertController.popoverPresentationController?.sourceRect = rect
_alertController.popoverPresentationController?.sourceView = view
_alertController.popoverPresentationController?.sourceRect = rect
return self
}

Expand Down
17 changes: 10 additions & 7 deletions Sources/Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ extension Alertift {
/// ActionWithTextFieldsHandler
public typealias ActionWithTextFieldsHandler = ([UITextField]?) -> Void

public internal(set) var alertController: InnerAlertController!
var _alertController: InnerAlertController!
public var alertController: UIAlertController {
return _alertController as UIAlertController
}

public static var backgroundColor: UIColor?
public static var buttonTextColor: UIColor?
Expand All @@ -40,9 +43,9 @@ extension Alertift {
/// - alertAction: UIAlertAction
/// - isPreferred: If isPreferred is true, alertAction becomes preferredAction.
private func addActionToAlertController(_ alertAction: UIAlertAction, isPreferred: Bool) {
alertController.addAction(alertAction)
_alertController.addAction(alertAction)
if isPreferred {
alertController.preferredAction = alertAction
_alertController.preferredAction = alertAction
}
}

Expand All @@ -67,7 +70,7 @@ extension Alertift {
/// - Returns: Myself
final public func action(_ action: Alertift.Action, isPreferred: Bool = false, textFieldsHandler handler: @escaping ActionWithTextFieldsHandler) -> Self {
addActionToAlertController(
buildAlertAction(action, handler: merge(alertController.actionWithTextFieldsHandler, handler)),
buildAlertAction(action, handler: merge(_alertController.actionWithTextFieldsHandler, handler)),
isPreferred: isPreferred
)
return self
Expand All @@ -78,12 +81,12 @@ extension Alertift {
/// - Parameter handler: Define handler if you want to customize UITextField. Default is nil.
/// - Returns: Myself
public func textField(configurationHandler handler: ((UITextField) -> Void)? = nil) -> Self {
alertController.addTextField { [weak self] textField in
_alertController.addTextField { [weak self] textField in
guard let strongSelf = self else {
return
}
handler?(textField)
strongSelf.alertController.registerTextFieldObserver(textField)
strongSelf._alertController.registerTextFieldObserver(textField)
}

return self
Expand All @@ -96,7 +99,7 @@ extension Alertift {
/// - Parameter textFieldTextDidChangeHandler: TextFieldHandler (UITextField, Int) -> Void
/// - Returns: Myself
public func handleTextFieldTextDidChange(textFieldTextDidChangeHandler: TextFieldHandler?) -> Self {
alertController.textFieldTextDidChangeHandler = textFieldTextDidChangeHandler
_alertController.textFieldTextDidChangeHandler = textFieldTextDidChangeHandler
return self
}

Expand Down
33 changes: 18 additions & 15 deletions Sources/AlertType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,67 @@ import Foundation


internal protocol _AlertType: class {
var alertController: InnerAlertController! { get set }
var _alertController: InnerAlertController! { get set }
}

extension _AlertType where Self: AlertType {
func buildAlertControlelr(title: String? = nil, message: String? = nil, style: UIAlertControllerStyle) {
alertController = InnerAlertController(title: title, message: message, preferredStyle: style)
alertController.alertBackgroundColor = type(of: self).backgroundColor
alertController.view.tintColor = type(of: self).buttonTextColor
alertController.titleTextColor = type(of: self).titleTextColor
alertController.messageTextColor = type(of: self).messageTextColor
_alertController = InnerAlertController(title: title, message: message, preferredStyle: style)
_alertController.alertBackgroundColor = type(of: self).backgroundColor
_alertController.view.tintColor = type(of: self).buttonTextColor
_alertController.titleTextColor = type(of: self).titleTextColor
_alertController.messageTextColor = type(of: self).messageTextColor
}
}

public protocol AlertType: class {
var alertController: InnerAlertController! { get }
var alertController: UIAlertController { get }
static var backgroundColor: UIColor? { get set }
static var buttonTextColor: UIColor? { get set }
static var titleTextColor: UIColor? { get set }
static var messageTextColor: UIColor? { get set }
}

extension AlertType {
extension AlertType {
private var _alertController: InnerAlertController {
return alertController as! InnerAlertController
}
/// 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: @escaping Alertift.ActionHandler) -> UIAlertAction {
return action.buildAlertAction(handler: ActionHandlerBuilder.build(handler, alertController.finallyExecutor))
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
_alertController.finallyHandler = handler
return self
}

public func backgroundColor(_ color: UIColor?) -> Self {
alertController.alertBackgroundColor = color
_alertController.alertBackgroundColor = color
return self
}

public func buttonColor(_ color: UIColor?) -> Self {
alertController.view.tintColor = color
_alertController.view.tintColor = color
return self
}

public func titleTextColor(_ color: UIColor?) -> Self {
alertController.titleTextColor = color
_alertController.titleTextColor = color
return self
}

public func messageTextColor(_ color: UIColor?) -> Self {
alertController.messageTextColor = color
_alertController.messageTextColor = color
return self
}

Expand All @@ -77,6 +80,6 @@ extension AlertType {
/// - viewController: The view controller to display over the current view controller’s content. Default is **UIApplication.shared.keyWindow?.rootViewController**
/// - completion: The block to execute after the presentation finishes. This block has no return value and takes no parameters. You may specify nil for this parameter.
final public func show(on viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController, completion: (() -> Void)? = nil) {
viewController?.present(alertController, animated: true, completion: completion)
viewController?.present(_alertController, animated: true, completion: completion)
}
}
2 changes: 1 addition & 1 deletion Sources/InnerAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit

/// InnerAlertController
/// subclass of **UIAlertController**
public class InnerAlertController: UIAlertController {
class InnerAlertController: UIAlertController {
/// textFieldTextDidChangeHandler: ((UITextField, Int) -> Void)
var textFieldTextDidChangeHandler: _Alert.TextFieldHandler?

Expand Down

0 comments on commit c41f7d4

Please sign in to comment.