Skip to content

Commit

Permalink
Improved building action handler. Added Logger for Debug
Browse files Browse the repository at this point in the history
  • Loading branch information
sgr-ksmt committed Apr 28, 2017
1 parent 9aa6028 commit 90cdf68
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 30 deletions.
16 changes: 12 additions & 4 deletions Alertift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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 */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -26,6 +28,8 @@
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>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -68,12 +72,14 @@
4D13C7AA1EB22B8E007BBB43 /* Sources */ = {
isa = PBXGroup;
children = (
4D13C7AB1EB22B8F007BBB43 /* Action.swift */,
4D13C7AC1EB22B8F007BBB43 /* ActionSheet.swift */,
4D13C7AD1EB22B8F007BBB43 /* Alert.swift */,
4D13C7AE1EB22B8F007BBB43 /* AlertBase.swift */,
4D13C7AF1EB22B8F007BBB43 /* Alertift.swift */,
4D13C7AE1EB22B8F007BBB43 /* AlertBase.swift */,
4D13C7AD1EB22B8F007BBB43 /* Alert.swift */,
4D13C7AC1EB22B8F007BBB43 /* ActionSheet.swift */,
4D13C7AB1EB22B8F007BBB43 /* Action.swift */,
4D13C7B01EB22B8F007BBB43 /* InnerAlertController.swift */,
4D13C7BE1EB320D4007BBB43 /* ActionHandlerBuilder.swift */,
4D13C7C01EB32AE6007BBB43 /* Debug.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -159,9 +165,11 @@
buildActionMask = 2147483647;
files = (
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 */,
4D13C7C11EB32AE6007BBB43 /* Debug.swift in Sources */,
4D13C7B51EB22B8F007BBB43 /* Alertift.swift in Sources */,
4D13C7B21EB22B8F007BBB43 /* ActionSheet.swift in Sources */,
);
Expand Down
24 changes: 24 additions & 0 deletions Sources/ActionHandlerBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// ActionHandlerBuilder.swift
// Alertift
//
// Created by Suguru Kishimoto on 4/28/17.
// Copyright © 2017 Suguru Kishimoto. All rights reserved.
//

import Foundation

final class ActionHandlerBuilder {
private init() {}

static func build<T: UIAlertAction>(_ h1: @escaping () -> Void, _ h2: @escaping (T) -> Void) -> (T) -> Void {
return { action in
h1()
h2(action)
}
}
}

func merge<A, B, C>(_ h1: @escaping (A) -> B, _ h2: @escaping (B) -> C) -> (A) -> C {
return { x in h2(h1(x)) }
}
6 changes: 5 additions & 1 deletion Sources/ActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension Alertift {

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

Expand All @@ -39,5 +39,9 @@ extension Alertift {
_alertController.popoverPresentationController?.sourceRect = rect
return self
}

deinit {
Debug.log()
}
}
}
13 changes: 7 additions & 6 deletions Sources/Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ extension Alertift {
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, isPreferred: Bool = false, handler: @escaping Alertift.ActionHandler = {}) -> Self {
addActionToAlertController(
buildAlertAction(action, handler: _alertController.combineActionHandler(handler)),
isPreferred: isPreferred
)
addActionToAlertController(buildAlertAction(action, handler: handler), isPreferred: isPreferred)
return self
}

Expand All @@ -61,9 +58,9 @@ extension Alertift {
/// - 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.
/// - Returns: Myself
final public func action(_ action: Alertift.Action, isPreferred: Bool = false, textFieldsHandler: @escaping ActionWithTextFieldsHandler) -> Self {
final public func action(_ action: Alertift.Action, isPreferred: Bool = false, textFieldsHandler handler: @escaping ActionWithTextFieldsHandler) -> Self {
addActionToAlertController(
buildAlertAction(action, handler: _alertController.combineActionWithTextFieldsHandler(textFieldsHandler)),
buildAlertAction(action, handler: merge(_alertController.actionWithTextFieldsHandler, handler)),
isPreferred: isPreferred
)
return self
Expand Down Expand Up @@ -95,5 +92,9 @@ extension Alertift {
_alertController.textFieldTextDidChangeHandler = textFieldTextDidChangeHandler
return self
}

deinit {
Debug.log()
}
}
}
4 changes: 2 additions & 2 deletions Sources/AlertBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ extension Alertift {
/// - action: action
/// - handler: The handler to execute after the action selected.
/// - Returns: **UIAlertAction**
func buildAlertAction(_ action: Alertift.Action, handler: @escaping (UIAlertAction) -> Void) -> UIAlertAction {
return action.buildAlertAction(handler: handler)
func buildAlertAction(_ action: Alertift.Action, handler: @escaping Alertift.ActionHandler) -> UIAlertAction {
return action.buildAlertAction(handler: ActionHandlerBuilder.build(handler, _alertController.finallyExecutor))
}

/// Add finally handler.
Expand Down
20 changes: 20 additions & 0 deletions Sources/Debug.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Debug.swift
// Alertift
//
// Created by Suguru Kishimoto on 4/28/17.
// Copyright © 2017 Suguru Kishimoto. All rights reserved.
//

import Foundation

// for debug
final class Debug {
static var isEnabled = false
static func log(_ msg: @autoclosure () -> String = "", _ file: @autoclosure () -> String = #file, _ line: @autoclosure () -> Int = #line, _ function: @autoclosure () -> String = #function) {
if isEnabled {
let fileName = file().components(separatedBy: "/").last ?? ""
print("[Debug] [\(fileName):\(line())]#\(function()) \(msg())")
}
}
}
23 changes: 6 additions & 17 deletions Sources/InnerAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,20 @@ class InnerAlertController: UIAlertController {
textFieldTextDidChangeHandler?(textField, index)
}

/// Combine originalHandler and finallyHandler with textFields.
///
/// - Parameter originalHandler: original action handler.
/// - Returns: New action handler.
func combineActionWithTextFieldsHandler(_ originalHandler: @escaping _Alert.ActionWithTextFieldsHandler) -> (UIAlertAction) -> Void {
return combineActionHandler { [weak self] in
originalHandler(self?.textFields)
var actionWithTextFieldsHandler: () -> ([UITextField]?) {
return { [weak self] in
self?.textFields
}
}

/// Combine originalHandler and finallyHandler.
///
/// - Parameter originalHandler: original action handler.
/// - Returns: New action handler.
func combineActionHandler(_ originalHandler: @escaping () -> Void) -> (UIAlertAction) -> Void {
var finallyExecutor: (UIAlertAction) -> Void {
return { [weak self] action in
guard let strongSelf = self else {
return
}
originalHandler()
strongSelf.finallyHandler?(action, strongSelf.actions.index(of: action) ?? -1)
self?.finallyHandler?(action, self?.actions.index(of: action) ?? -1)
}
}

deinit {
NotificationCenter.default.removeObserver(self)
Debug.log()
}
}

0 comments on commit 90cdf68

Please sign in to comment.