Skip to content

Commit

Permalink
Migrate example to Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron McTavish committed Oct 14, 2016
1 parent f78abef commit 22fc36d
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 52 deletions.
6 changes: 3 additions & 3 deletions Example/AppDelegate.swift
Expand Up @@ -19,12 +19,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// MARK: - UIApplicationDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let viewController = FormViewController()
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.navigationBar.translucent = false
navigationController.navigationBar.isTranslucent = false

window = UIWindow(frame: UIScreen.mainScreen().bounds)
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()

Expand Down
42 changes: 21 additions & 21 deletions Example/FormEntryView.swift
Expand Up @@ -21,7 +21,7 @@ final class FormEntryView<V: Validator>: UIView, ValidatorControlDelegate, UITex

let errorLabel = UILabel()

private let stackView = UIStackView()
fileprivate let stackView = UIStackView()


// MARK: - Initializers
Expand All @@ -36,23 +36,23 @@ final class FormEntryView<V: Validator>: UIView, ValidatorControlDelegate, UITex

// Setup

stackView.axis = .Vertical
stackView.distribution = .Fill
stackView.alignment = .Fill
stackView.axis = .vertical
stackView.distribution = .fill
stackView.alignment = .fill
addSubview(stackView)

textLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
textLabel.textAlignment = .Center
textLabel.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)
textLabel.textAlignment = .center
stackView.addArrangedSubview(textLabel)

textField.borderStyle = .Line
textField.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
textField.borderStyle = .line
textField.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)
textField.setValidatorDelegate(self)
stackView.addArrangedSubview(textField)

errorLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
errorLabel.hidden = true
errorLabel.lineBreakMode = .ByWordWrapping
errorLabel.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)
errorLabel.isHidden = true
errorLabel.lineBreakMode = .byWordWrapping
errorLabel.numberOfLines = 0
stackView.addArrangedSubview(errorLabel)

Expand All @@ -66,10 +66,10 @@ final class FormEntryView<V: Validator>: UIView, ValidatorControlDelegate, UITex

let stackViewMargin: CGFloat = 0.0
stackView.translatesAutoresizingMaskIntoConstraints = false
addConstraint(NSLayoutConstraint(item: stackView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: -stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: -stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: -stackViewMargin))
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -79,32 +79,32 @@ final class FormEntryView<V: Validator>: UIView, ValidatorControlDelegate, UITex

// MARK: - ValidatorControlDelegate

func validatorControl(validatorControl: ValidatorControl, changedValidState validState: Bool) {
func validatorControl(_ validatorControl: ValidatorControl, changedValidState validState: Bool) {
guard let controlView = validatorControl as? UIView else {
return
}

if validState {
controlView.layer.borderColor = nil
controlView.layer.borderWidth = 0.0
errorLabel.hidden = true
errorLabel.isHidden = true
} else {
controlView.layer.borderColor = UIColor.redColor().CGColor
controlView.layer.borderColor = UIColor.red.cgColor
controlView.layer.borderWidth = 2.0
}
}

func validatorControl(validatorControl: ValidatorControl, violatedConditions conditions: [Condition]) {
func validatorControl(_ validatorControl: ValidatorControl, violatedConditions conditions: [Condition]) {
var errorText = ""
for condition in conditions {
errorText += condition.localizedViolationString
}
errorLabel.text = errorText

errorLabel.hidden = false
errorLabel.isHidden = false
}

func validatorControlDidChange(validatorControl: ValidatorControl) {
func validatorControlDidChange(_ validatorControl: ValidatorControl) {
// Not used in this example yet
}

Expand Down
30 changes: 15 additions & 15 deletions Example/FormView.swift
Expand Up @@ -19,10 +19,10 @@ final class FormView: UIView {
let nameEntry = FormEntryView<AlphabeticValidator>()
let emailEntry = FormEntryView<EmailValidator>()

let submitButton = UIButton(type: .System)
let submitButton = UIButton(type: .system)

private let bottomBufferView = UIView()
private let stackView = UIStackView()
fileprivate let bottomBufferView = UIView()
fileprivate let stackView = UIStackView()


// MARK: - Initializers
Expand All @@ -34,14 +34,14 @@ final class FormView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = UIColor.whiteColor()
backgroundColor = UIColor.white


// Setup

stackView.axis = .Vertical
stackView.distribution = .Fill
stackView.alignment = .Fill
stackView.axis = .vertical
stackView.distribution = .fill
stackView.alignment = .fill
addSubview(stackView)

nameEntry.textLabel.text = NSLocalizedString("Surname", comment: "")
Expand All @@ -52,12 +52,12 @@ final class FormView: UIView {
emailEntry.textField.validateOnFocusLossOnly = true
stackView.addArrangedSubview(emailEntry)

submitButton.titleLabel?.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
submitButton.setTitle(NSLocalizedString("Submit", comment: ""), forState: .Normal)
submitButton.titleLabel?.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)
submitButton.setTitle(NSLocalizedString("Submit", comment: ""), for: UIControlState())
stackView.addArrangedSubview(submitButton)

bottomBufferView.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, forAxis: stackView.axis)
bottomBufferView.setContentHuggingPriority(UILayoutPriorityDefaultLow, forAxis: stackView.axis)
bottomBufferView.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, for: stackView.axis)
bottomBufferView.setContentHuggingPriority(UILayoutPriorityDefaultLow, for: stackView.axis)
stackView.addArrangedSubview(bottomBufferView)


Expand All @@ -76,10 +76,10 @@ final class FormView: UIView {

let stackViewMargin: CGFloat = 20.0
stackView.translatesAutoresizingMaskIntoConstraints = false
addConstraint(NSLayoutConstraint(item: stackView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: -stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: -stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -stackViewMargin))
addConstraint(NSLayoutConstraint(item: stackView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: -stackViewMargin))

stackView.spacing = stackViewMargin
}
Expand Down
12 changes: 6 additions & 6 deletions Example/FormViewController.swift
Expand Up @@ -19,7 +19,7 @@ final class FormViewController: UIViewController {

var form = ControlForm()

private var underlyingView: FormView {
fileprivate var underlyingView: FormView {
if let myView = view as? FormView {
return myView
}
Expand All @@ -42,13 +42,13 @@ final class FormViewController: UIViewController {
form.addEntry(underlyingView.nameEntry.textField)
form.addEntry(underlyingView.emailEntry.textField)

underlyingView.submitButton.addTarget(self, action: Selector("submitButtonPressed:"), forControlEvents: .TouchUpInside)
underlyingView.submitButton.addTarget(self, action: #selector(FormViewController.submitButtonPressed(_:)), for: .touchUpInside)
}


// MARK: - Control Actions

func submitButtonPressed(sender: UIButton) {
func submitButtonPressed(_ sender: UIButton) {
let alertTitle: String
let alertMessage: String
if form.isValid {
Expand All @@ -59,13 +59,13 @@ final class FormViewController: UIViewController {
alertMessage = NSLocalizedString("Please correct your entries in the form.", comment: "")
}

let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .Alert)
let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
alertController.popoverPresentationController?.sourceView = sender

let doneAction = UIAlertAction(title: NSLocalizedString("Done", comment: ""), style: .Default, handler: nil)
let doneAction = UIAlertAction(title: NSLocalizedString("Done", comment: ""), style: .default, handler: nil)
alertController.addAction(doneAction)

presentViewController(alertController, animated: true, completion: nil)
present(alertController, animated: true, completion: nil)
}

}
6 changes: 6 additions & 0 deletions FormValidatorSwift.xcodeproj/project.pbxproj
Expand Up @@ -570,6 +570,7 @@
};
005675671C47ED32005A43F0 = {
CreatedOnToolsVersion = 7.2;
ProvisioningStyle = Manual;
};
0062E9CF1C45493E00021C0A = {
CreatedOnToolsVersion = 7.2;
Expand All @@ -578,6 +579,7 @@
0062E9D91C45493E00021C0A = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0800;
ProvisioningStyle = Manual;
};
};
};
Expand Down Expand Up @@ -897,6 +899,7 @@
005675741C47ED32005A43F0 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "Tests/Unit Tests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.ustwo.FormValidatorSwiftTests;
Expand All @@ -909,6 +912,7 @@
005675751C47ED32005A43F0 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "Tests/Unit Tests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.ustwo.FormValidatorSwiftTests;
Expand Down Expand Up @@ -1062,6 +1066,7 @@
0062E9E81C45493E00021C0A /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "Tests/Unit Tests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.ustwo.FormValidatorSwiftTests;
Expand All @@ -1073,6 +1078,7 @@
0062E9E91C45493E00021C0A /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "Tests/Unit Tests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.ustwo.FormValidatorSwiftTests;
Expand Down
8 changes: 4 additions & 4 deletions Tests/UI Tests/FormTests.swift
Expand Up @@ -68,11 +68,11 @@ class FormTests: XCTestCase {

let successAlert = app.staticTexts[NSLocalizedString("Success", comment: "")]
let exists = NSPredicate(format: "exists == true")
expectationForPredicate(exists, evaluatedWithObject: successAlert, handler: nil)
expectation(for: exists, evaluatedWith: successAlert, handler: nil)

submitButton.tap()

waitForExpectationsWithTimeout(5.0, handler: nil)
waitForExpectations(timeout: 5.0, handler: nil)
}

func testForm_Invalid() {
Expand All @@ -93,11 +93,11 @@ class FormTests: XCTestCase {

let successAlert = app.staticTexts[NSLocalizedString("Error", comment: "")]
let exists = NSPredicate(format: "exists == true")
expectationForPredicate(exists, evaluatedWithObject: successAlert, handler: nil)
expectation(for: exists, evaluatedWith: successAlert, handler: nil)

submitButton.tap()

waitForExpectationsWithTimeout(5.0, handler: nil)
waitForExpectations(timeout: 5.0, handler: nil)
}

}

0 comments on commit 22fc36d

Please sign in to comment.