Skip to content

Commit

Permalink
Merged in verification-module (pull request #2)
Browse files Browse the repository at this point in the history
Resolves issue #2
  • Loading branch information
pavankotesh committed Jan 16, 2017
2 parents 4fa43bf + 0edf6be commit 4f0f298
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 211 deletions.
3 changes: 0 additions & 3 deletions Example/KWVerificationCodeView/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}
Expand All @@ -32,7 +31,5 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func applicationWillTerminate(_ application: UIApplication) {
}


}

99 changes: 37 additions & 62 deletions Example/KWVerificationCodeView/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -35,4 +45,4 @@
"version" : 1,
"author" : "xcode"
}
}
}
77 changes: 13 additions & 64 deletions Example/KWVerificationCodeView/VerificationCodeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,82 +12,31 @@ import KWVerificationCodeView
class VerificationCodeViewController: UIViewController {

// MARK: - IBOutlets
@IBOutlet weak var verificationCodeView1: KWVerificationCodeView!
@IBOutlet weak var verificationCodeView2: KWVerificationCodeView!
@IBOutlet weak var verificationCodeView3: KWVerificationCodeView!
@IBOutlet weak var verificationCodeView4: KWVerificationCodeView!

// MARK: - Variables
var mobile: String!
var selectedVerificationCodeViewIndex = 0

lazy var verificationCodeViews: [KWVerificationCodeView] = {
[unowned self] in

return [self.verificationCodeView1, self.verificationCodeView2, self.verificationCodeView3, self.verificationCodeView4]
}()
@IBOutlet weak var verificationCodeView: KWVerificationCodeView!
@IBOutlet weak var submitButton: UIButton!

// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setupVerificationCodeViews()

submitButton.isEnabled = false
verificationCodeView.delegate = self
}

// MARK: - IBAction
@IBAction func submitButtonTapped(_ sender: UIButton) {
if validateTextFields() {
let alertController = UIAlertController(title: "Success", message: "Code is \(getVerificationCode())", preferredStyle: .alert)
if verificationCodeView.hasValidCode() {
let alertController = UIAlertController(title: "Success", message: "Code is \(verificationCodeView.getVerificationCode())", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}

// MARK: - FilePrivate Methods
fileprivate func setupVerificationCodeViews() {
for verificationCodeView in verificationCodeViews {
verificationCodeView.delegate = self
}

verificationCodeViews.first?.activate()
}

fileprivate func validateTextFields() -> Bool {
for verificationCodeView in verificationCodeViews {
if Int(verificationCodeView.numberTextField.text!) == nil {
return false
}
}

return true
}

fileprivate func getVerificationCode() -> String {
var verificationCode = ""
for verificationCodeView in verificationCodeViews {
verificationCode += verificationCodeView.numberTextField.text!
present(alertController, animated: true, completion: nil)
}

return verificationCode
}

}

// MARK: - KWVerificationCodeDelegate
extension VerificationCodeViewController: KWVerificationCodeDelegate {
func moveToNext(_ verificationCodeView: KWVerificationCodeView) {
let validIndex = verificationCodeViews.index(of: verificationCodeView) == verificationCodeViews.count - 1 ? verificationCodeViews.index(of: verificationCodeView)! : verificationCodeViews.index(of: verificationCodeView)! + 1
verificationCodeViews[validIndex].activate()
}

func moveToPrevious(_ verificationCodeView: KWVerificationCodeView, oldCode: String) {
if verificationCodeViews.last == verificationCodeView && oldCode != " " {
return
}

let validIndex = verificationCodeViews.index(of: verificationCodeView)! == 0 ? 0 : verificationCodeViews.index(of: verificationCodeView)! - 1
verificationCodeViews[validIndex].activate()
verificationCodeViews[validIndex].reset()
// MARK: - KWVerificationCodeViewDelegate
extension VerificationCodeViewController: KWVerificationCodeViewDelegate {
func didChangeVerificationCode() {
submitButton.isEnabled = verificationCodeView.hasValidCode()
}
}

16 changes: 12 additions & 4 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions KWVerificationCodeView.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = 'KWVerificationCodeView'
s.version = '0.1.0'
s.summary = 'A subclass on UIView that displays a verification code field.'
s.description = 'This CocoaPod provides the ability to use a UIView that can display a verification field.'
s.homepage = 'https://bitbucket.org/keepworks/kwverificationcodeview'
s.summary = 'A verification code view with validation.'
s.description = 'A customisable verification code view with built in validation. Can be used for one time passwords (OTPs), email verification codes etc.'
s.homepage = 'https://github.com/keepworks/kwverificationcodeview'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'KeepWorks' => '[email protected]' }
s.source = { :git => 'https://bitbucket.org/keepworks/kwverificationcodeview.git', :tag => s.version.to_s }
s.source = { :git => 'https://github.com/keepworks/kwverificationcodeview.git', :tag => s.version.to_s }

s.ios.deployment_target = '8.0'
s.source_files = 'KWVerificationCodeView/Classes/**/*'
Expand Down
104 changes: 104 additions & 0 deletions KWVerificationCodeView/Classes/KWTextFieldView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// KWTextFieldView.swift
// Pods
//
// Created by KeepWorks on 13/01/17.
// Copyright © 2017 KeepWorks Technologies Pvt Ltd. All rights reserved.
//

import UIKit

protocol KWTextFieldDelegate: class {
func moveToNext(_ textFieldView: KWTextFieldView)
func moveToPrevious(_ textFieldView: KWTextFieldView, oldCode: String)
func didChangeCharacters()
}

@IBDesignable class KWTextFieldView: UIView {

// MARK: - Constants
static let maxCharactersLength = 1

// MARK: - IBInspectables
@IBInspectable var underlineColor: UIColor = UIColor.darkGray {
didSet {
underlineView.backgroundColor = underlineColor
}
}
@IBInspectable var underlineSelectedColor: UIColor = UIColor.blue

// MARK: - IBOutlets
@IBOutlet weak var numberTextField: UITextField!
@IBOutlet weak private var underlineView: UIView!

// MARK: - Variables
weak var delegate: KWTextFieldDelegate?

// MARK: - Lifecycle
override init(frame: CGRect) {
super.init(frame: frame)

loadViewFromNib()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

loadViewFromNib()
numberTextField.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChange(_:)), name: NSNotification.Name.UITextFieldTextDidChange, object: numberTextField)
}

deinit {
NotificationCenter.default.removeObserver(self)
}

// MARK: - Public Methods
public func activate() {
numberTextField.becomeFirstResponder()
if numberTextField.text?.characters.count == 0 {
numberTextField.text = " "
}
}

public func deactivate() {
numberTextField.resignFirstResponder()
}

public func reset() {
numberTextField.text = " "
updateUnderline()
}

// MARK: - FilePrivate Methods
dynamic fileprivate func textFieldDidChange(_ notification: Foundation.Notification) {
if numberTextField.text?.characters.count == 0 {
numberTextField.text = " "
}
}

fileprivate func updateUnderline() {
underlineView.backgroundColor = numberTextField.text?.trim() != "" ? underlineSelectedColor : underlineColor
}
}

// MARK: - UITextFieldDelegate
extension KWTextFieldView: UITextFieldDelegate {
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentString = numberTextField.text!
let newString = currentString.replacingCharacters(in: textField.text!.range(from: range)!, with: string)

if newString.characters.count > type(of: self).maxCharactersLength {
delegate?.moveToNext(self)
textField.text = string
} else if newString.characters.count == 0 {
delegate?.moveToPrevious(self, oldCode: textField.text!)
numberTextField.text = " "
}

delegate?.didChangeCharacters()
updateUnderline()

return newString.characters.count <= type(of: self).maxCharactersLength
}
}
Loading

0 comments on commit 4f0f298

Please sign in to comment.