Skip to content

Commit

Permalink
Update for Swift 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
sberrevoets committed Sep 24, 2018
1 parent 52e31fa commit dc3ca3c
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 82 deletions.
12 changes: 6 additions & 6 deletions Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@
TargetAttributes = {
D2515E6E1BC1A47F00ED606F = {
CreatedOnToolsVersion = 7.0.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 1000;
ProvisioningStyle = Manual;
};
D2C0AFCC1BE7236A00F31D0F = {
CreatedOnToolsVersion = 7.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 1000;
ProvisioningStyle = Manual;
TestTargetID = D2515E6E1BC1A47F00ED606F;
};
Expand Down Expand Up @@ -390,7 +390,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -408,7 +408,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.ScottyDoesntCode.Example;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -422,7 +422,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.ScottyDoesntCode.SDCAlertView-UI-Tests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
TEST_TARGET_NAME = Example;
USES_XCTRUNNER = YES;
};
Expand All @@ -437,7 +437,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.ScottyDoesntCode.SDCAlertView-UI-Tests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
TEST_TARGET_NAME = Example;
USES_XCTRUNNER = YES;
};
Expand Down
4 changes: 2 additions & 2 deletions Example/DemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class DemoViewController: UITableViewController {
switch self.contentControl.selectedSegmentIndex {
case 1:
let contentView = alert.contentView
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
let spinner = UIActivityIndicatorView(style: .gray)
spinner.translatesAutoresizingMaskIntoConstraints = false
spinner.startAnimating()
contentView.addSubview(spinner)
Expand Down Expand Up @@ -105,7 +105,7 @@ final class DemoViewController: UITableViewController {
private func presentUIAlertController() {
let title = self.titleTextField.content
let message = self.messageTextField.content
let style = UIAlertControllerStyle(rawValue: self.styleControl.selectedSegmentIndex)!
let style = UIAlertController.Style(rawValue: self.styleControl.selectedSegmentIndex)!
let alert = UIAlertController(title: title, message: message, preferredStyle: style)

let textFields = Int(self.textFieldCountTextField.content ?? "0")!
Expand Down
4 changes: 2 additions & 2 deletions Example/TestsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TestsViewController: UITableViewController {
case 8:
let alert = AlertController(title: "Title", message: "Message")
let contentView = alert.contentView
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
let spinner = UIActivityIndicatorView(style: .gray)
spinner.translatesAutoresizingMaskIntoConstraints = false
spinner.startAnimating()
contentView.addSubview(spinner)
Expand All @@ -81,7 +81,7 @@ class TestsViewController: UITableViewController {
case 11:
let alert = AlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
let contentView = alert.contentView
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
let spinner = UIActivityIndicatorView(style: .gray)
spinner.translatesAutoresizingMaskIntoConstraints = false
spinner.startAnimating()
contentView.addSubview(spinner)
Expand Down
6 changes: 3 additions & 3 deletions SDCAlertView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
TargetAttributes = {
D2515EC01BC1A7D500ED606F = {
CreatedOnToolsVersion = 7.0.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 1000;
ProvisioningStyle = Manual;
};
};
Expand Down Expand Up @@ -302,7 +302,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down Expand Up @@ -332,7 +332,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.ScottyDoesntCode.SDCAlertView;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down
58 changes: 26 additions & 32 deletions Source/Actions/AlertAction.swift
Original file line number Diff line number Diff line change
@@ -1,47 +1,28 @@
import UIKit

/**
The action's style

- normal: The action will have default font and text color
- preferred: The action will take a style that indicates it's the preferred option
- destructive: The action will convey that this action will do something destructive
*/
@objc(SDCAlertActionStyle)
public enum AlertActionStyle: Int {
case normal
case preferred
case destructive
}

@objc(SDCAlertAction)
public class AlertAction: NSObject {

/**
Creates an action with a plain title.

- parameter title: An optional title for the action
- parameter style: The action's style
- parameter handler: An optional closure that's called when the user taps on this action
*/
/// Creates an action with a plain title.
///
/// parameter title: An optional title for the action
/// parameter style: The action's style
/// parameter handler: An optional closure that's called when the user taps on this action
@objc
public convenience init(title: String?, style: AlertActionStyle, handler: ((AlertAction) -> Void)? = nil)
public convenience init(title: String?, style: AlertAction.Style, handler: ((AlertAction) -> Void)? = nil)
{
self.init()
self.title = title
self.style = style
self.handler = handler
}

/**
Creates an action with a stylized title.

- parameter attributedTitle: An optional stylized title
- parameter style: The action's style
- parameter handler: An optional closure that is called when the user taps on this action
*/
@objc
public convenience init(attributedTitle: NSAttributedString?, style: AlertActionStyle,
/// Creates an action with a stylized title.
///
/// - parameter attributedTitle: An optional stylized title
/// - parameter style: The action's style
/// - parameter handler: An optional closure that is called when the user taps on this action
public convenience init(attributedTitle: NSAttributedString?, style: AlertAction.Style,
handler: ((AlertAction) -> Void)? = nil)
{
self.init()
Expand All @@ -67,7 +48,7 @@ public class AlertAction: NSObject {

/// The action's style.
@objc
internal(set) public var style: AlertActionStyle = .normal
internal(set) public var style: AlertAction.Style = .normal

/// The action's button accessibility identifier
@objc
Expand All @@ -83,3 +64,16 @@ public class AlertAction: NSObject {
didSet { self.actionView?.isEnabled = self.isEnabled }
}
}

extension AlertAction {
/// The action's style
@objc(SDCAlertActionStyle)
public enum Style: Int {
/// The action will have default font and text color
case normal
/// The action will take a style that indicates it's the preferred option
case preferred
/// The action will convey that this action will do something destructive
case destructive
}
}
62 changes: 30 additions & 32 deletions Source/AlertController.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import UIKit

/// The alert controller's style
///
/// - actionSheet: An action sheet style alert that slides in from the bottom and presents the user with a
/// list of possible actions to perform. Does not show as expected on iPad.
/// - alert: The standard alert style that asks the user for information or confirmation.
@objc(SDCAlertControllerStyle)
public enum AlertControllerStyle: Int {
// An action sheet style alert that slides in from the bottom and presents the user with a list of
// possible actions to perform. Does not show as expected on iPad.
case actionSheet
// The standard alert style that asks the user for information or confirmation.
case alert
}


/// The layout of the alert's actions. Only applies to AlertControllerStyle.alert, not .actionSheet (see
/// `AlertControllerStyle`).
///
/// - automatic: If the alert has 2 actions, display them horizontally. Otherwise, display them vertically.
/// - vertical: Display the actions vertically.
/// - horizontal: Display the actions horizontally.
/// The layout of the alert's actions. Only applies to AlertControllerStyle.alert, not .actionSheet.
@objc(SDCActionLayout)
public enum ActionLayout: Int {
/// If the alert has 2 actions, display them horizontally. Otherwise, display them vertically.
case automatic
/// Display the actions vertically.
case vertical
/// Display the actions horizontally.
case horizontal
}

Expand Down Expand Up @@ -96,7 +92,7 @@ public final class AlertController: UIViewController {
}
}

/// The layout of the actions in the alert.
/// The layout of the actions in the alert, or `.automatic` for action sheets.
@objc
public var actionLayout: ActionLayout {
get { return (self.alert as? AlertView)?.actionLayout ?? .automatic }
Expand All @@ -114,7 +110,7 @@ public final class AlertController: UIViewController {
/// on an action. If it returns false, the AlertAction handler will not be executed.
@objc
public var shouldDismissHandler: ((AlertAction?) -> Bool)?

/// A closure called when the alert is dismissed after an outside tap (when `dismissOnOutsideTap` behavior
/// is enabled)
@objc
Expand All @@ -126,7 +122,7 @@ public final class AlertController: UIViewController {

/// The alert's presentation style.
@objc
private(set) public var preferredStyle: AlertControllerStyle = .alert
public let preferredStyle: AlertControllerStyle

private let alert: UIView & AlertControllerViewRepresentable
private lazy var transitionDelegate: Transition = Transition(alertStyle: self.preferredStyle)
Expand All @@ -145,7 +141,6 @@ public final class AlertController: UIViewController {
preferredStyle: AlertControllerStyle = .alert)
{
self.init(preferredStyle: preferredStyle)
self.preferredStyle = preferredStyle
self.commonInit()

self.attributedTitle = attributedTitle
Expand All @@ -159,9 +154,9 @@ public final class AlertController: UIViewController {
/// - parameter message: An optional message
/// - parameter preferredStyle: The preferred presentation style of the alert. Default is `alert`.
@objc
public convenience init(title: String?, message: String?, preferredStyle: AlertControllerStyle = .alert) {
public convenience init(title: String?, message: String?, preferredStyle: AlertControllerStyle = .alert)
{
self.init(preferredStyle: preferredStyle)
self.preferredStyle = preferredStyle
self.commonInit()

self.title = title
Expand All @@ -183,6 +178,7 @@ public final class AlertController: UIViewController {
}
}

self.preferredStyle = preferredStyle
super.init(nibName: nil, bundle: nil)
}

Expand Down Expand Up @@ -259,12 +255,21 @@ public final class AlertController: UIViewController {
self.textFields?.first?.resignFirstResponder()
}

public override func becomeFirstResponder() -> Bool {
if self.behaviors.contains(.automaticallyFocusTextField) {
return self.textFields?.first?.becomeFirstResponder() ?? super.becomeFirstResponder()
}

return super.becomeFirstResponder()
}

public override var preferredStatusBarStyle: UIStatusBarStyle {
return self.presentingViewController?.preferredStatusBarStyle ?? .default
}

public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return self.presentingViewController?.supportedInterfaceOrientations ?? super.supportedInterfaceOrientations
return self.presentingViewController?.supportedInterfaceOrientations
?? super.supportedInterfaceOrientations
}

// MARK: - Private
Expand All @@ -277,13 +282,14 @@ public final class AlertController: UIViewController {
}

private func listenForKeyboardChanges() {
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardChange),
name: .UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default
.addObserver(self, selector: #selector(self.keyboardChange),
name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}

@objc
private func keyboardChange(_ notification: Notification) {
let newFrameValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue
let newFrameValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue
guard let newFrame = newFrameValue?.cgRectValue else {
return
}
Expand All @@ -292,14 +298,6 @@ public final class AlertController: UIViewController {
self.alert.layoutIfNeeded()
}

public override func becomeFirstResponder() -> Bool {
if self.behaviors.contains(.automaticallyFocusTextField) {
return self.textFields?.first?.becomeFirstResponder() ?? super.becomeFirstResponder()
}

return super.becomeFirstResponder()
}

private func configureAlertView() {
self.alert.translatesAutoresizingMaskIntoConstraints = false
self.alert.visualStyle = self.visualStyle
Expand Down Expand Up @@ -362,10 +360,10 @@ public final class AlertController: UIViewController {
}

let textFieldsViewController = TextFieldsViewController(textFields: textFields)
textFieldsViewController.willMove(toParentViewController: self)
self.addChildViewController(textFieldsViewController)
textFieldsViewController.willMove(toParent: self)
self.addChild(textFieldsViewController)
alert.textFieldsViewController = textFieldsViewController
textFieldsViewController.didMove(toParentViewController: self)
textFieldsViewController.didMove(toParent: self)
}

private func addChromeTapHandlerIfNecessary() {
Expand Down
2 changes: 1 addition & 1 deletion Source/AlertVisualStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ open class AlertVisualStyle: NSObject {
switch alertStyle {
case .alert:
if kIsIphoneX {
self.margins = .zero
self.margins = UIEdgeInsets()
} else {
self.margins = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Text Fields/TextFieldCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class TextFieldCell: UITableViewCell {
container?.addSubview(textField)
textField.translatesAutoresizingMaskIntoConstraints = false

let insets = self.visualStyle?.textFieldMargins ?? UIEdgeInsets.zero
let insets = self.visualStyle?.textFieldMargins ?? UIEdgeInsets()

let leading = textField.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: insets.left)
let trailing = textField.trailingAnchor.constraint(equalTo: self.trailingAnchor,
Expand Down
2 changes: 1 addition & 1 deletion Source/Views/ActionSheetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class ActionSheetView: UIView, AlertControllerViewRepresentable {
self.cancelButton.isHighlighted = cancelIsSelected

if cancelIsSelected && sender.state == .ended {
self.cancelButton.sendActions(for: .touchUpInside)
self.cancelButton.sendActions(for: UIControl.Event.touchUpInside)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Views/AlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ final class AlertView: UIView, AlertControllerViewRepresentable {

override var intrinsicContentSize: CGSize {
let totalHeight = self.contentHeight + self.actionsCollectionView.displayHeight
return CGSize(width: UIViewNoIntrinsicMetric, height: totalHeight)
return CGSize(width: UIView.noIntrinsicMetric, height: totalHeight)
}

@objc
Expand Down
Loading

0 comments on commit dc3ca3c

Please sign in to comment.