Skip to content

Commit

Permalink
Swift 3 (#27)
Browse files Browse the repository at this point in the history
* Initial Swift 3 migration

* Migrate all the things to Swift 3
  • Loading branch information
3lvis authored Sep 15, 2016
1 parent ea9dc22 commit ba93817
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 194 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
4 changes: 2 additions & 2 deletions Custom/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ 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 {
CustomStyle.apply()

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = Controller()
self.window!.makeKeyAndVisible()

Expand Down
24 changes: 12 additions & 12 deletions Custom/Controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class Controller: UIViewController {
validation.maximumLength = "1234 5678 1234 5678".characters.count
validation.minimumLength = "1234 5678 1234 5678".characters.count
validation.required = true
let characterSet = NSMutableCharacterSet.decimalDigitCharacterSet()
characterSet.addCharactersInString(" ")
validation.characterSet = characterSet
let characterSet = NSMutableCharacterSet.decimalDigit()
characterSet.addCharacters(in: " ")
validation.characterSet = characterSet as CharacterSet
let inputValidator = InputValidator(validation: validation)
textField.inputValidator = inputValidator

Expand Down Expand Up @@ -76,7 +76,7 @@ class Controller: UIViewController {
var validation = Validation()
validation.maximumLength = "CVC".characters.count
validation.minimumLength = "CVC".characters.count
validation.characterSet = NSCharacterSet.decimalDigitCharacterSet()
validation.characterSet = NSCharacterSet.decimalDigits
let inputValidator = InputValidator(validation: validation)
textField.inputValidator = inputValidator

Expand All @@ -90,20 +90,20 @@ class Controller: UIViewController {
let button = UIButton(frame: previousFrame)
button.backgroundColor = UIColor(hex: "27C787")
button.titleLabel?.font = UIFont(name: "AvenirNext-Regular", size: 24)
button.setTitle("Pay", forState: .Normal)
button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
button.setTitle("Pay", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.layer.cornerRadius = 30.0
button.layer.shadowColor = UIColor(hex: "21B177").CGColor
button.layer.shadowColor = UIColor(hex: "21B177").cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 3)
button.layer.shadowRadius = 0
button.layer.shadowOpacity = 1
button.addTarget(self, action: #selector(Controller.payAction), forControlEvents: .TouchUpInside)
button.addTarget(self, action: #selector(Controller.payAction), for: .touchUpInside)

return button
}()

override func loadView() {
let view = UIView(frame: UIScreen.mainScreen().bounds)
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = UIColor(hex: "D4F3FF")
self.view = view
}
Expand All @@ -124,10 +124,10 @@ class Controller: UIViewController {
let validCardExpirationDate = self.cardExpirationDateField.validate()
let validCVC = self.cvcField.validate()
if validEmail && validCardNumber && validCardExpirationDate && validCVC {
let alertController = UIAlertController(title: "Valid!", message: "The payment details are valid", preferredStyle: .Alert)
let dismissAction = UIAlertAction(title: "Dismiss", style: .Default, handler: nil)
let alertController = UIAlertController(title: "Valid!", message: "The payment details are valid", preferredStyle: .alert)
let dismissAction = UIAlertAction(title: "Dismiss", style: .default, handler: nil)
alertController.addAction(dismissAction)
self.presentViewController(alertController, animated: true, completion: nil)
self.present(alertController, animated: true, completion: nil)
}
}
}
4 changes: 2 additions & 2 deletions Custom/CustomStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import FormTextField

struct CustomStyle {
static func apply() {
let enabledBackgroundColor = UIColor.whiteColor()
let enabledBackgroundColor = UIColor.white
let enabledBorderColor = UIColor(hex: "DFDFDF")
let enabledTextColor = UIColor(hex: "455C73")
let activeBorderColor = UIColor(hex: "70D7FF")
Expand Down Expand Up @@ -32,7 +32,7 @@ struct CustomStyle {

FormTextField.appearance().disabledBackgroundColor = UIColor(hex: "DFDFDF")
FormTextField.appearance().disabledBorderColor = UIColor(hex: "DFDFDF")
FormTextField.appearance().disabledTextColor = UIColor.whiteColor()
FormTextField.appearance().disabledTextColor = UIColor.white

FormTextField.appearance().invalidBackgroundColor = UIColor(hex: "FFC9C8")
FormTextField.appearance().invalidBorderColor = UIColor(hex: "FF4B47")
Expand Down
30 changes: 28 additions & 2 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
1469B03F1C4511670027138C /* Field.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1469B03E1C4511670027138C /* Field.swift */; };
14A139B41AEFC72B00AD732F /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14A139B31AEFC72B00AD732F /* Tests.swift */; };
36F1A36D4E0DCD4D0DA76D39 /* Pods_CocoaPods_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9C1C0345741E7AB2B034F042 /* Pods_CocoaPods_Tests.framework */; };
4446790B1D8A75FB0002A3BD /* FormTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 444679081D8A75FB0002A3BD /* FormTextField.swift */; };
4446790C1D8A75FB0002A3BD /* FormTextFieldClearButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 444679091D8A75FB0002A3BD /* FormTextFieldClearButton.swift */; };
4446790D1D8A75FB0002A3BD /* FormTextFieldTypeManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4446790A1D8A75FB0002A3BD /* FormTextFieldTypeManager.swift */; };
5F264C2EF17055B9FB84C229 /* Pods_CocoaPods_Custom.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 724AF726159E5D86D051996B /* Pods_CocoaPods_Custom.framework */; };
F09E820F0CA26D5E8C5034DB /* Pods_CocoaPods_Native.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B8FB22143BCFF71F535D2CF /* Pods_CocoaPods_Native.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -53,6 +56,9 @@
14C0AF831BD6D4230009ECBE /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
2EACAD72BB230F6D77A113AE /* Pods-CocoaPods-Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CocoaPods-Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-CocoaPods-Tests/Pods-CocoaPods-Tests.release.xcconfig"; sourceTree = "<group>"; };
3B515DB15C3B1AF0B4A4845F /* Pods-CocoaPods-Custom.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CocoaPods-Custom.debug.xcconfig"; path = "Pods/Target Support Files/Pods-CocoaPods-Custom/Pods-CocoaPods-Custom.debug.xcconfig"; sourceTree = "<group>"; };
444679081D8A75FB0002A3BD /* FormTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormTextField.swift; sourceTree = "<group>"; };
444679091D8A75FB0002A3BD /* FormTextFieldClearButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormTextFieldClearButton.swift; sourceTree = "<group>"; };
4446790A1D8A75FB0002A3BD /* FormTextFieldTypeManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormTextFieldTypeManager.swift; sourceTree = "<group>"; };
724AF726159E5D86D051996B /* Pods_CocoaPods_Custom.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_CocoaPods_Custom.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7B8FB22143BCFF71F535D2CF /* Pods_CocoaPods_Native.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_CocoaPods_Native.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9C1C0345741E7AB2B034F042 /* Pods_CocoaPods_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_CocoaPods_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -120,6 +126,7 @@
146D728A1AB782920058798C = {
isa = PBXGroup;
children = (
444679071D8A75FB0002A3BD /* Source */,
14C136501AB7849300B7B07A /* Metadata */,
14647AC71C4506360046399A /* Native */,
14647AD81C45063C0046399A /* Custom */,
Expand Down Expand Up @@ -171,6 +178,16 @@
name = Metadata;
sourceTree = "<group>";
};
444679071D8A75FB0002A3BD /* Source */ = {
isa = PBXGroup;
children = (
444679081D8A75FB0002A3BD /* FormTextField.swift */,
444679091D8A75FB0002A3BD /* FormTextFieldClearButton.swift */,
4446790A1D8A75FB0002A3BD /* FormTextFieldTypeManager.swift */,
);
path = Source;
sourceTree = "<group>";
};
9062B6463673E6F588458C9A /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -271,6 +288,7 @@
14647AC51C4506360046399A = {
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = C6K65RDJHL;
LastSwiftMigration = 0800;
};
14647AD61C45063C0046399A = {
CreatedOnToolsVersion = 7.2;
Expand All @@ -280,6 +298,7 @@
146D72AB1AB782920058798C = {
CreatedOnToolsVersion = 6.2;
DevelopmentTeam = C6K65RDJHL;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -497,6 +516,9 @@
buildActionMask = 2147483647;
files = (
14A139B41AEFC72B00AD732F /* Tests.swift in Sources */,
4446790B1D8A75FB0002A3BD /* FormTextField.swift in Sources */,
4446790C1D8A75FB0002A3BD /* FormTextFieldClearButton.swift in Sources */,
4446790D1D8A75FB0002A3BD /* FormTextFieldTypeManager.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -537,6 +559,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.demo.Native;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -554,6 +577,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.demo.Native;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -572,7 +596,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.demo.Custom;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -590,7 +614,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.demo.Custom;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down Expand Up @@ -695,6 +719,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.sample.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -710,6 +735,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.sample.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
6 changes: 3 additions & 3 deletions FormTextField.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "FormTextField"
s.summary = "A UITextField that supports formatters and input validators such as maximum length and regex"
s.version = "0.13.0"
s.version = "1.0.0"
s.homepage = "https://github.com/3lvis/FormTextField"
s.license = 'MIT'
s.author = { "Elvis Nuñez" => "[email protected]" }
Expand All @@ -11,6 +11,6 @@ Pod::Spec.new do |s|
s.requires_arc = true
s.source_files = 'Source/**/*'
s.frameworks = 'UIKit'
s.dependency 'Formatter', '~> 0.3.3'
s.dependency 'InputValidator', '~> 0.9.1'
s.dependency 'Formatter', '~> 1.0.0'
s.dependency 'InputValidator', '~> 1.0.0'
end
4 changes: 2 additions & 2 deletions Native/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)

let controller = Controller()
controller.title = "Payment Details"
Expand Down
52 changes: 26 additions & 26 deletions Native/Controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ class Controller: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 239/255, green: 239/255, blue: 244/255, alpha: 1)
self.tableView.registerClass(FormTextFieldCell.self, forCellReuseIdentifier: FormTextFieldCell.Identifier)
self.tableView.registerClass(HeaderCell.self, forCellReuseIdentifier: HeaderCell.Identifier)
self.tableView.register(FormTextFieldCell.self, forCellReuseIdentifier: FormTextFieldCell.Identifier)
self.tableView.register(HeaderCell.self, forCellReuseIdentifier: HeaderCell.Identifier)
self.tableView.tableFooterView = UIView()

let doneButton = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(Controller.done))
doneButton.enabled = false
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(Controller.done))
doneButton.isEnabled = false
self.navigationItem.rightBarButtonItem = doneButton
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.fields.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let field = self.fields[indexPath.row]
if field.type == .Header {
let cell = tableView.dequeueReusableCellWithIdentifier(HeaderCell.Identifier, forIndexPath: indexPath) as! HeaderCell
cell.textLabel?.text = field.title.uppercaseString
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let field = self.fields[(indexPath as NSIndexPath).row]
if field.type == .header {
let cell = tableView.dequeueReusableCell(withIdentifier: HeaderCell.Identifier, for: indexPath) as! HeaderCell
cell.textLabel?.text = field.title.uppercased()
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(FormTextFieldCell.Identifier, forIndexPath: indexPath) as! FormTextFieldCell
let cell = tableView.dequeueReusableCell(withIdentifier: FormTextFieldCell.Identifier, for: indexPath) as! FormTextFieldCell
cell.textField.textFieldDelegate = self
cell.textLabel?.text = field.title
cell.textField.placeholder = field.placeholder ?? field.title
Expand All @@ -43,27 +43,27 @@ class Controller: UITableViewController {
}
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let field = self.fields[indexPath.row]
if field.type == .Header {
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let field = self.fields[(indexPath as NSIndexPath).row]
if field.type == .header {
return 60
} else {
return 45
}
}

func done() {
let alertController = UIAlertController(title: "The payment details are valid", message: nil, preferredStyle: .Alert)
let dismissAction = UIAlertAction(title: "Dismiss", style: .Default, handler: nil)
let alertController = UIAlertController(title: "The payment details are valid", message: nil, preferredStyle: .alert)
let dismissAction = UIAlertAction(title: "Dismiss", style: .default, handler: nil)
alertController.addAction(dismissAction)
self.presentViewController(alertController, animated: true, completion: nil)
self.present(alertController, animated: true, completion: nil)
}

func validate() -> Bool {
var valid = true
for (index, field) in self.fields.enumerate() {
if field.type == .Field {
let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) as! FormTextFieldCell
for (index, field) in self.fields.enumerated() {
if field.type == .field {
let cell = self.tableView.cellForRow(at: IndexPath(row: index, section: 0)) as! FormTextFieldCell
let validField = cell.textField.validate()
if validField == false {
valid = validField
Expand All @@ -73,27 +73,27 @@ class Controller: UITableViewController {
return valid
}

func showCheckAccessory(textField: FormTextField) {
func showCheckAccessory(_ textField: FormTextField) {
let valid = textField.validate()
if valid {
let imageView = UIImageView(image: UIImage(named: "check-icon")!)
imageView.contentMode = .Center
imageView.contentMode = .center
imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 20)
textField.accessoryView = imageView
textField.accessoryViewMode = .Always
textField.accessoryViewMode = .always
} else {
textField.accessoryView = nil
textField.accessoryViewMode = .Never
textField.accessoryViewMode = .never
}
}
}

extension Controller: FormTextFieldDelegate {
func formTextField(textField: FormTextField, didUpdateWithText text: String?) {
func formTextField(_ textField: FormTextField, didUpdateWithText text: String?) {
self.showCheckAccessory(textField)
let valid = self.validate()
if let button = self.navigationItem.rightBarButtonItem {
button.enabled = valid
button.isEnabled = valid
}
}
}
Loading

0 comments on commit ba93817

Please sign in to comment.