Skip to content

Commit

Permalink
Minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
arturdev committed Feb 28, 2020
1 parent b439cad commit 3d01b05
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion SwiftyCodeView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Pod::Spec.new do |s|
s.swift_version = '5.0'
s.name = 'SwiftyCodeView'
s.version = '0.3.4'
s.version = '0.3.5'
s.summary = 'An UI Component for verification codes written in swift'

s.description = <<-DESC
Expand Down
12 changes: 9 additions & 3 deletions SwiftyCodeView/Classes/SwiftyCodeView/SwiftyCodeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import UIKit

@objc
public protocol SwiftyCodeViewDelegate: class {
func codeView(sender: SwiftyCodeView, didFinishInput code: String)
func codeView(sender: SwiftyCodeView, didFinishInput code: String) -> Bool
}

@IBDesignable
Expand Down Expand Up @@ -42,6 +42,9 @@ open class SwiftyCodeView: UIControl {
let item = stackView.arrangedSubviews[i] as! SwiftyCodeItemView
item.textField.text = i < array.count ? array[i] : ""
}
if !stackView.arrangedSubviews.compactMap({$0 as? UITextField}).filter({$0.isFirstResponder}).isEmpty {
self.becomeFirstResponder()
}
}
}

Expand Down Expand Up @@ -81,12 +84,14 @@ open class SwiftyCodeView: UIControl {
.last as! SwiftyCodeItemView
}

@discardableResult
override open func becomeFirstResponder() -> Bool {
let items = stackView.arrangedSubviews
.map({$0 as! SwiftyCodeItemView})
return (items.filter({($0.textField.text ?? "").isEmpty}).first ?? items.last)!.becomeFirstResponder()
}

@discardableResult
override open func resignFirstResponder() -> Bool {
stackView.arrangedSubviews.forEach({$0.resignFirstResponder()})
return true
Expand All @@ -112,8 +117,9 @@ extension SwiftyCodeView: UITextFieldDelegate, SwiftyCodeTextFieldDelegate {
item.textField.text = string
sendActions(for: .valueChanged)
if index == length - 1 { //is last textfield
delegate?.codeView(sender: self, didFinishInput: self.code)
textField.resignFirstResponder()
if (delegate?.codeView(sender: self, didFinishInput: self.code) ?? false) {
textField.resignFirstResponder()
}
return false
}

Expand Down

0 comments on commit 3d01b05

Please sign in to comment.