Skip to content

Commit

Permalink
Removed AlertBase, added AlertType
Browse files Browse the repository at this point in the history
  • Loading branch information
sgr-ksmt committed Apr 30, 2017
1 parent d21453f commit 44195ff
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 131 deletions.
8 changes: 4 additions & 4 deletions Alertift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
4D13C7B11EB22B8F007BBB43 /* Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D13C7AB1EB22B8F007BBB43 /* Action.swift */; };
4D13C7B21EB22B8F007BBB43 /* ActionSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D13C7AC1EB22B8F007BBB43 /* ActionSheet.swift */; };
4D13C7B31EB22B8F007BBB43 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D13C7AD1EB22B8F007BBB43 /* Alert.swift */; };
4D13C7B41EB22B8F007BBB43 /* AlertBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D13C7AE1EB22B8F007BBB43 /* AlertBase.swift */; };
4D13C7B51EB22B8F007BBB43 /* Alertift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D13C7AF1EB22B8F007BBB43 /* Alertift.swift */; };
4D13C7B61EB22B8F007BBB43 /* InnerAlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D13C7B01EB22B8F007BBB43 /* InnerAlertController.swift */; };
4D13C7BF1EB320D4007BBB43 /* ActionHandlerBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D13C7BE1EB320D4007BBB43 /* ActionHandlerBuilder.swift */; };
4D13C7C11EB32AE6007BBB43 /* Debug.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D13C7C01EB32AE6007BBB43 /* Debug.swift */; };
4D13C7C51EB5DFB7007BBB43 /* AlertType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D13C7C41EB5DFB7007BBB43 /* AlertType.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -25,11 +25,11 @@
4D13C7AB1EB22B8F007BBB43 /* Action.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Action.swift; sourceTree = "<group>"; };
4D13C7AC1EB22B8F007BBB43 /* ActionSheet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActionSheet.swift; sourceTree = "<group>"; };
4D13C7AD1EB22B8F007BBB43 /* Alert.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Alert.swift; sourceTree = "<group>"; };
4D13C7AE1EB22B8F007BBB43 /* AlertBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertBase.swift; sourceTree = "<group>"; };
4D13C7AF1EB22B8F007BBB43 /* Alertift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Alertift.swift; sourceTree = "<group>"; };
4D13C7B01EB22B8F007BBB43 /* InnerAlertController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InnerAlertController.swift; sourceTree = "<group>"; };
4D13C7BE1EB320D4007BBB43 /* ActionHandlerBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActionHandlerBuilder.swift; sourceTree = "<group>"; };
4D13C7C01EB32AE6007BBB43 /* Debug.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Debug.swift; sourceTree = "<group>"; };
4D13C7C41EB5DFB7007BBB43 /* AlertType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertType.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -73,13 +73,13 @@
isa = PBXGroup;
children = (
4D13C7AF1EB22B8F007BBB43 /* Alertift.swift */,
4D13C7AE1EB22B8F007BBB43 /* AlertBase.swift */,
4D13C7AD1EB22B8F007BBB43 /* Alert.swift */,
4D13C7AC1EB22B8F007BBB43 /* ActionSheet.swift */,
4D13C7AB1EB22B8F007BBB43 /* Action.swift */,
4D13C7B01EB22B8F007BBB43 /* InnerAlertController.swift */,
4D13C7BE1EB320D4007BBB43 /* ActionHandlerBuilder.swift */,
4D13C7C01EB32AE6007BBB43 /* Debug.swift */,
4D13C7C41EB5DFB7007BBB43 /* AlertType.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -167,8 +167,8 @@
4D13C7B31EB22B8F007BBB43 /* Alert.swift in Sources */,
4D13C7BF1EB320D4007BBB43 /* ActionHandlerBuilder.swift in Sources */,
4D13C7B61EB22B8F007BBB43 /* InnerAlertController.swift in Sources */,
4D13C7B41EB22B8F007BBB43 /* AlertBase.swift in Sources */,
4D13C7B11EB22B8F007BBB43 /* Action.swift in Sources */,
4D13C7C51EB5DFB7007BBB43 /* AlertType.swift in Sources */,
4D13C7C11EB32AE6007BBB43 /* Debug.swift in Sources */,
4D13C7B51EB22B8F007BBB43 /* Alertift.swift in Sources */,
4D13C7B21EB22B8F007BBB43 /* ActionSheet.swift in Sources */,
Expand Down
23 changes: 10 additions & 13 deletions Sources/ActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ import Foundation

extension Alertift {
/// ActionSheet
final public class ActionSheet: AlertBase {
final public class ActionSheet: AlertType {

public static var backgroundColor: UIColor? = nil
override class var _backgroundColor: UIColor? {
return backgroundColor
}
public var alertController: InnerAlertController!

public static var buttonTextColor: UIColor? = nil
override class var _buttonTextColor: UIColor? {
return buttonTextColor
}
public static var backgroundColor: UIColor?
public static var buttonTextColor: UIColor?
public static var titleTextColor: UIColor?
public static var messageTextColor: UIColor?

/// Make action sheet
///
Expand All @@ -29,12 +26,12 @@ extension Alertift {
/// - message: Descriptive text that provides additional details about the reason for the alert.
/// - Returns: Instance of **ActionSheet**
public init(title: String? = nil, message: String? = nil) {
super.init(title: title, message: message, style: .actionSheet)
buildAlertControlelr(title: title, message: message, style: .actionSheet)
}

/// 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 @@ -46,8 +43,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
29 changes: 13 additions & 16 deletions Sources/Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@ import Foundation

extension Alertift {
/// Alert
final public class Alert: AlertBase {
final public class Alert: AlertType {
/// TextFieldHandler
public typealias TextFieldHandler = ((UITextField, Int) -> Void)

/// ActionWithTextFieldsHandler
public typealias ActionWithTextFieldsHandler = ([UITextField]?) -> Void

public static var backgroundColor: UIColor? = nil
override class var _backgroundColor: UIColor? {
return backgroundColor
}
public var alertController: InnerAlertController!

public static var buttonTextColor: UIColor? = nil
override class var _buttonTextColor: UIColor? {
return buttonTextColor
}
public static var backgroundColor: UIColor?
public static var buttonTextColor: UIColor?
public static var titleTextColor: UIColor?
public static var messageTextColor: UIColor?

/// Make alert
///
Expand All @@ -34,7 +31,7 @@ extension Alertift {
/// - message: Descriptive text that provides additional details about the reason for the alert.
/// - Returns: Instance of **Alert**
public init(title: String? = nil, message: String? = nil) {
super.init(title: title, message: message, style: .alert)
buildAlertControlelr(title: title, message: message, style: .alert)
}

/// Add alertAction to alertController
Expand All @@ -43,9 +40,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 @@ -70,7 +67,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 @@ -81,12 +78,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 @@ -99,7 +96,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
95 changes: 0 additions & 95 deletions Sources/AlertBase.swift

This file was deleted.

75 changes: 75 additions & 0 deletions Sources/AlertType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// AlertType.swift
// Alertift
//
// Created by Suguru Kishimoto on 4/30/17.
// Copyright © 2017 Suguru Kishimoto. All rights reserved.
//

import Foundation

public protocol AlertType: class {
var alertController: InnerAlertController! { get set }
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 {
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
}

/// 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))
}

/// 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
}

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

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

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

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

/// Show alert (or action sheet).
///
/// - Parameters:
/// - 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)
}
}
1 change: 0 additions & 1 deletion Sources/Alertift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ extension Alertift {


/// Internal typealiases
typealias _AlertBase = Alertift.AlertBase
typealias _Alert = Alertift.Alert
typealias _ActionSheet = Alertift.ActionSheet
4 changes: 2 additions & 2 deletions 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**
class InnerAlertController: UIAlertController {
public class InnerAlertController: UIAlertController {
/// textFieldTextDidChangeHandler: ((UITextField, Int) -> Void)
var textFieldTextDidChangeHandler: _Alert.TextFieldHandler?

Expand Down Expand Up @@ -61,7 +61,7 @@ class InnerAlertController: UIAlertController {
}
}

override func viewWillLayoutSubviews() {
override public func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

adaptBackgroundColor()
Expand Down

0 comments on commit 44195ff

Please sign in to comment.