Skip to content

Commit

Permalink
Merge pull request #67 from MLSDev/swift3
Browse files Browse the repository at this point in the history
Migrate to Swift 3
  • Loading branch information
Hugo Tunius committed Dec 7, 2016
2 parents 6043831 + 2de94a3 commit 94766c0
Show file tree
Hide file tree
Showing 16 changed files with 377 additions and 383 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ before_install:
- open -a "simulator" --args -CurrentDeviceUDID $IOS_SIMULATOR_UDID

script:
- xcodebuild clean build test -project SkyFloatingLabelTextField/SkyFloatingLabelTextField.xcodeproj -scheme SkyFloatingLabelTextField -destination 'platform=iOS Simulator,name=iPhone 6,OS=9.3' GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES GCC_GENERATE_TEST_COVERAGE_FILES=YES
- xcodebuild clean build test -project SkyFloatingLabelTextField/SkyFloatingLabelTextField.xcodeproj -scheme SkyFloatingLabelTextField -destination 'platform=iOS Simulator,name=iPhone 6' GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES GCC_GENERATE_TEST_COVERAGE_FILES=YES

after_success:
- cd $TRAVIS_BUILD_DIR && slather
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = net.skyscanner.SkyFloatingLabelTextFieldExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -464,7 +464,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = net.skyscanner.SkyFloatingLabelTextFieldExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down Expand Up @@ -584,7 +584,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -603,7 +603,7 @@
PRODUCT_BUNDLE_IDENTIFIER = net.skyscanner.SkyFloatingLabelTextField;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -614,7 +614,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = net.skyscanner.SkyFloatingLabelTextFieldTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -625,7 +625,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = net.skyscanner.SkyFloatingLabelTextFieldTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit

let isLTRLanguage = UIApplication.sharedApplication().userInterfaceLayoutDirection == .LeftToRight
let isLTRLanguage = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight

class ShowcaseExampleViewController: UIViewController, UITextFieldDelegate {

Expand All @@ -28,24 +28,24 @@ class ShowcaseExampleViewController: UIViewController, UITextFieldDelegate {

self.setupThemeColors()

self.departureCityField.becomeFirstResponder()
_ = self.departureCityField.becomeFirstResponder()
}

// MARK: - Creating the form elements

func setupThemeColors() {
self.submitButton.layer.borderColor = darkGreyColor.CGColor
self.submitButton.layer.borderColor = darkGreyColor.cgColor
self.submitButton.layer.borderWidth = 1
self.submitButton.layer.cornerRadius = 5
self.submitButton.setTitleColor(overcastBlueColor, forState: .Highlighted)
self.submitButton.setTitleColor(overcastBlueColor, for: .highlighted)

self.applySkyscannerThemeWithIcon(self.departureCityField)
self.applySkyscannerThemeWithIcon(textField: self.departureCityField)
self.departureCityField.iconText = "\u{f072}" // plane icon as per https://fortawesome.github.io/Font-Awesome/cheatsheet/
self.departureCityField.placeholder = NSLocalizedString("Departure City", tableName: "SkyFloatingLabelTextField", comment: "placeholder for the departure city field")
self.departureCityField.selectedTitle = NSLocalizedString("Departure City", tableName: "SkyFloatingLabelTextField", comment: "title for the departure city field")
self.departureCityField.title = NSLocalizedString("Departure City", tableName: "SkyFloatingLabelTextField", comment: "title for the departure city field")

self.applySkyscannerThemeWithIcon(self.arrivalCityField)
self.applySkyscannerThemeWithIcon(textField: self.arrivalCityField)
if isLTRLanguage {
self.arrivalCityField.iconRotationDegrees = 90
} else { // In RTL languages the plane should point to the other side
Expand All @@ -70,9 +70,9 @@ class ShowcaseExampleViewController: UIViewController, UITextFieldDelegate {
self.emailField.selectedTitle = NSLocalizedString("Email", tableName: "SkyFloatingLabelTextField", comment: "selected title for Email field")
self.emailField.title = NSLocalizedString("Email", tableName: "SkyFloatingLabelTextField", comment: "title for Email field")

self.applySkyscannerTheme(self.titleField)
self.applySkyscannerTheme(self.nameField)
self.applySkyscannerTheme(self.emailField)
self.applySkyscannerTheme(textField: self.titleField)
self.applySkyscannerTheme(textField: self.nameField)
self.applySkyscannerTheme(textField: self.emailField)

self.arrivalCityField.delegate = self
self.departureCityField.delegate = self
Expand All @@ -84,7 +84,7 @@ class ShowcaseExampleViewController: UIViewController, UITextFieldDelegate {
// MARK: - Styling the text fields to the Skyscanner theme

func applySkyscannerThemeWithIcon(textField: SkyFloatingLabelTextFieldWithIcon) {
self.applySkyscannerTheme(textField)
self.applySkyscannerTheme(textField: textField)

textField.iconColor = lightGreyColor
textField.selectedIconColor = overcastBlueColor
Expand Down Expand Up @@ -113,36 +113,36 @@ class ShowcaseExampleViewController: UIViewController, UITextFieldDelegate {

var showingTitleInProgress = false

@IBAction func submitButtonDown(sender: AnyObject) {
@IBAction func submitButtonDown(_ sender: AnyObject) {
self.isSubmitButtonPressed = true
if !self.departureCityField.hasText() {
if !self.departureCityField.hasText {
self.showingTitleInProgress = true
self.departureCityField.setTitleVisible(true, animated: true, animationCompletion: self.showingTitleInAnimationComplete)
self.departureCityField.highlighted = true
self.departureCityField.isHighlighted = true
}
if !self.arrivalCityField.hasText() {
if !self.arrivalCityField.hasText {
self.showingTitleInProgress = true
self.arrivalCityField.setTitleVisible(true, animated: true, animationCompletion: self.showingTitleInAnimationComplete)
self.arrivalCityField.highlighted = true
self.arrivalCityField.isHighlighted = true
}
if !self.titleField.hasText() {
if !self.titleField.hasText {
self.showingTitleInProgress = true
self.titleField.setTitleVisible(true, animated: true, animationCompletion: self.showingTitleInAnimationComplete)
self.titleField.highlighted = true
self.titleField.isHighlighted = true
}
if !self.nameField.hasText() {
if !self.nameField.hasText {
self.showingTitleInProgress = true
self.nameField.setTitleVisible(true, animated: true, animationCompletion: self.showingTitleInAnimationComplete)
self.nameField.highlighted = true
self.nameField.isHighlighted = true
}
if !self.emailField.hasText() {
if !self.emailField.hasText {
self.showingTitleInProgress = true
self.emailField.setTitleVisible(true, animated: true, animationCompletion: self.showingTitleInAnimationComplete)
self.emailField.highlighted = true
self.emailField.isHighlighted = true
}
}

@IBAction func submitButtonTouchUpInside(sender: AnyObject) {
@IBAction func submitButtonTouchUpInside(_ sender: AnyObject) {
self.isSubmitButtonPressed = false
if(!self.showingTitleInProgress) {
self.hideTitleVisibleFromFields()
Expand All @@ -151,13 +151,12 @@ class ShowcaseExampleViewController: UIViewController, UITextFieldDelegate {

func showingTitleInAnimationComplete() {
// If a field is not filled out, display the highlighted title for 0.3 seco
let displayTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.3 * Double(NSEC_PER_SEC)))
dispatch_after(displayTime, dispatch_get_main_queue(), {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
self.showingTitleInProgress = false
if(!self.isSubmitButtonPressed) {
self.hideTitleVisibleFromFields()
}
})
}
}

func hideTitleVisibleFromFields() {
Expand All @@ -167,19 +166,19 @@ class ShowcaseExampleViewController: UIViewController, UITextFieldDelegate {
self.nameField.setTitleVisible(false, animated: true)
self.emailField.setTitleVisible(false, animated: true)

self.departureCityField.highlighted = false
self.arrivalCityField.highlighted = false
self.titleField.highlighted = false
self.nameField.highlighted = false
self.emailField.highlighted = false
self.departureCityField.isHighlighted = false
self.arrivalCityField.isHighlighted = false
self.titleField.isHighlighted = false
self.nameField.isHighlighted = false
self.emailField.isHighlighted = false
}

// MARK: - Delegate

func textFieldShouldReturn(textField: UITextField) -> Bool {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Validate the email field
if (textField == self.emailField) {
self.validateEmailTextFieldWithText(textField.text)
self.validateEmailTextFieldWithText(email: textField.text)
}

// When pressing return, move to the next field
Expand All @@ -192,9 +191,9 @@ class ShowcaseExampleViewController: UIViewController, UITextFieldDelegate {
return false
}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if(textField == self.emailField) {
self.validateEmailTextFieldWithText(string)
self.validateEmailTextFieldWithText(email: string)
}
return true
}
Expand All @@ -204,7 +203,7 @@ class ShowcaseExampleViewController: UIViewController, UITextFieldDelegate {
if(email.characters.count == 0) {
self.emailField.errorMessage = nil
}
else if(!isValidEmail(email)) {
else if(!isValidEmail(str: email)) {
self.emailField.errorMessage = NSLocalizedString("Email not valid", tableName: "SkyFloatingLabelTextField", comment: " ")
} else {
self.emailField.errorMessage = nil
Expand All @@ -218,6 +217,6 @@ class ShowcaseExampleViewController: UIViewController, UITextFieldDelegate {
let emailRegEx = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"

let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluateWithObject(str)
return emailTest.evaluate(with: str)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ class SettingTextsViewController: UIViewController {
@IBOutlet var addErrorButton:UIButton?

@IBAction func addError() {
if(self.addErrorButton?.titleForState(.Normal) == NSLocalizedString("Add error", tableName: "SkyFloatingLabelTextField", comment: "add error button title")) {
if(self.addErrorButton?.title(for: .normal) == NSLocalizedString("Add error", tableName: "SkyFloatingLabelTextField", comment: "add error button title")) {
self.textField?.errorMessage = NSLocalizedString("error message", tableName: "SkyFloatingLabelTextField", comment: "error message")
self.addErrorButton?.setTitle(NSLocalizedString("Clear error", tableName: "SkyFloatingLabelTextField", comment: "clear errors button title"), forState: .Normal)
self.addErrorButton?.setTitle(NSLocalizedString("Clear error", tableName: "SkyFloatingLabelTextField", comment: "clear errors button title"), for: .normal)
} else {
self.textField?.errorMessage = ""
self.addErrorButton?.setTitle(NSLocalizedString("Add error", tableName: "SkyFloatingLabelTextField", comment: "add error button title"), forState: .Normal)
self.addErrorButton?.setTitle(NSLocalizedString("Add error", tableName: "SkyFloatingLabelTextField", comment: "add error button title"), for: .normal)
}
}

@IBAction func resignTextField() {
self.textField?.resignFirstResponder()
_ = self.textField?.resignFirstResponder()
}

@IBAction func selectedTitleChanged(segmentedControl:UISegmentedControl) {
@IBAction func selectedTitleChanged(_ segmentedControl:UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0:
self.textField?.selectedTitle = nil
Expand All @@ -39,7 +39,7 @@ class SettingTextsViewController: UIViewController {
}
}

@IBAction func titleChanged(segmentedControl:UISegmentedControl) {
@IBAction func titleChanged(_ segmentedControl:UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0:
self.textField?.title = nil
Expand All @@ -50,7 +50,7 @@ class SettingTextsViewController: UIViewController {
}
}

@IBAction func placeholderChanged(segmentedControl:UISegmentedControl) {
@IBAction func placeholderChanged(_ segmentedControl:UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0:
self.textField?.placeholder = nil
Expand Down
Loading

0 comments on commit 94766c0

Please sign in to comment.