Skip to content

Commit

Permalink
Merge pull request #4 from BadhanGanesh/develop
Browse files Browse the repository at this point in the history
Bug Fixes
  • Loading branch information
BadhanGanesh authored Apr 17, 2020
2 parents bdd2840 + f16e485 commit 16c3f78
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
43 changes: 28 additions & 15 deletions Sources/BJOTPViewController/BJOTPViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,15 @@ open class BJOTPViewController: UIViewController {

public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
///This fixes an issue where when used in macOS apps, the user cannot paste any text on to any text field at the very beginning. Can be pasted once a textfield has received any text though. But anyway a text has to be inserted in the beginning to avoid the issue.
for tf in allTextFields { tf.insertText("") }
self.checkClipboardAndPromptUserToPasteContent()
}

@objc func authenticateButtonTapped(_ sender: UIButton) {
var otpString = ""

let numberOfEmptyTextFields: Int = allTextFields.reduce(0, { emptyTextsCount, textField in
otpString += textField.text!
return (textField.text ?? "") == "" ? emptyTextsCount + 1 : emptyTextsCount
})

if numberOfEmptyTextFields > 0 { return }
self.view.endEditing(true)
self.delegate?.authenticate(otpString, from: self)
Expand Down Expand Up @@ -322,7 +318,12 @@ open class BJOTPViewController: UIViewController {
}
self.view.endEditing(true)
}


deinit {
self.removeListeners()
self.allTextFields.removeAll()
self.textFieldsIndexes.removeAll()
}
}


Expand All @@ -347,9 +348,9 @@ extension BJOTPViewController: UITextFieldDelegate {

///But, auto-fill from SMS - before sending in the characters one by one - will
///send two empty strings ("") in succession very fast, unlike the speed a human may enter passcode.

///
///We need to check for it and have to decide/assume that what we have received is indeed auto-filled code from SMS.

///
///This has to be done since we use a new textfield for each character instead of a single text field with all characters.

if string == "" {
Expand Down Expand Up @@ -413,6 +414,8 @@ extension BJOTPViewController: UITextFieldDelegate {
return false
}

///Normal text entry

if range.length == 0 {
textField.text = string
setNextResponder(textFieldsIndexes[textField as! BJOTPTextField], direction: .right)
Expand All @@ -433,10 +436,8 @@ extension BJOTPViewController: UITextFieldDelegate {
textField.setBorder(amount: 1.8, borderColor: UIColor.lightGray.withAlphaComponent(0.3), duration: 0.09)
}

private func setNextResponder(_ index:Int?, direction:Direction) {

private func setNextResponder(_ index: Int?, direction: Direction) {
guard let index = index else { return }

if direction == .left {
index == 0 ?
(self.resignFirstResponder(textField: allTextFields.first)) :
Expand All @@ -446,7 +447,6 @@ extension BJOTPViewController: UITextFieldDelegate {
(self.resignFirstResponder(textField: allTextFields.last)) :
(_ = allTextFields[(index + 1)].becomeFirstResponder())
}

}

private func resignFirstResponder(textField: BJOTPTextField?) {
Expand Down Expand Up @@ -524,6 +524,8 @@ extension BJOTPViewController {
view.backgroundColor = .white
}

/// 8. This fixes an issue where when used in macOS apps, the user cannot paste any text on to any text field at the very beginning. Can be pasted once a textfield has received any text though. But anyway a text has to be inserted in the beginning to avoid the issue. Not sure why this happens, weird.
for tf in allTextFields { tf.insertText("") }
}

fileprivate func layoutBottomCloseButton() {
Expand Down Expand Up @@ -799,9 +801,9 @@ extension BJOTPViewController {
}

/**
* Need this delay for the UI to finish being laid out
* to check if the keyboard is obscuring the button or not initially.
*/
* Need this delay for the UI to finish being laid out
* to check if the keyboard is obscuring the button or not initially.
*/
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
self.offsetForKeyboardPosition(notification as NSNotification)
}
Expand Down Expand Up @@ -856,7 +858,6 @@ extension BJOTPViewController {
#endif

if isKeyBoardOn {

self.isKeyBoardOn = false
let window = UIApplication.shared.windows.first
let userInfo = (notification as NSNotification).userInfo!
Expand Down Expand Up @@ -929,4 +930,16 @@ extension BJOTPViewController {
}
}
}

private func removeListeners() {
#if swift(>=5.0)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
#elseif swift(<5.0)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
#endif
}
}
4 changes: 4 additions & 0 deletions Sources/BJOTPViewController/Custom Views/BJOTPTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ final class BJOTPTextField: UITextField {
override func caretRect(for position: UITextPosition) -> CGRect {
return .init(origin: .init(x: self.bounds.midX, y: self.bounds.origin.y), size: .init(width: 0.1, height: 0.1))
}

override func closestPosition(to point: CGPoint) -> UITextPosition? {
return self.endOfDocument
}
}

0 comments on commit 16c3f78

Please sign in to comment.