-
-
Save pardel/c28eecec05239ce5efbc5c3058ae503e to your computer and use it in GitHub Desktop.
CodeViewController - complete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class CodeViewController: UIViewController { | |
@IBOutlet weak var codeTextField: UITextField! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
title = "Enter Code" | |
navigationItem.leftBarButtonItem = | |
UIBarButtonItem(title: "", style: .plain, target: self, action: nil) | |
DispatchQueue.main.asyncAfter(deadline: .now() + 30) { [weak self] in | |
self?.showCancelButton() | |
} | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
codeTextField.text = "" | |
codeTextField.becomeFirstResponder() | |
} | |
func showCancelButton() { | |
navigationItem.leftBarButtonItem = | |
UIBarButtonItem(title: "Cancel", style: .plain, target: self, | |
action: #selector(cancel)) | |
} | |
@objc func cancel() { | |
codeTextField.resignFirstResponder() | |
SwiftSpinner.show("Cancelling your request") | |
guard let requestId = UserDefaults.standard.value(forKey: | |
Constant.UserDefault.requestId) as? String else { | |
goBack() | |
return | |
} | |
VerifyService.shared.cancel(requestId: requestId, successResponse: { [weak self] in | |
DispatchQueue.main.async { | |
SwiftSpinner.hide() | |
self?.goBack() | |
} | |
}) { [weak self] error in | |
DispatchQueue.main.async { | |
SwiftSpinner.show(duration: 1, title: "Something went wrong") | |
self?.goBack() | |
} | |
} | |
} | |
internal func goBack() { | |
UserDefaults.standard.removeObject(forKey: Constant.UserDefault.requestId) | |
navigationController?.popViewController(animated: true) | |
} | |
@IBAction func codeChanged(_ sender: Any) { | |
let textLength = codeTextField.text?.lengthOfBytes(using: .utf8) ?? 0 | |
if textLength >= 4 { | |
checkPin() | |
} | |
} | |
internal func checkPin(){ | |
codeTextField.resignFirstResponder() | |
guard let code = codeTextField.text else { | |
return | |
} | |
SwiftSpinner.show("Validating the code...") | |
guard let requestId = UserDefaults.standard.value(forKey: | |
Constant.UserDefault.requestId) as? String else { | |
DispatchQueue.main.async { | |
SwiftSpinner.show(duration: 2, | |
title: "Something went wrong.\nPlease try again...", | |
animated: false) | |
} | |
return | |
} | |
VerifyService.shared.check(requestId: requestId, code: code, | |
successResponse: { | |
DispatchQueue.main.async { | |
SwiftSpinner.show(duration: 1, | |
title: "Validated successfully!", | |
animated: false) | |
UserDefaults.standard.setValue(Date(), | |
forKey: Constant.UserDefault.lastVerifiedAt) | |
UserDefaults.standard.removeObject(forKey: | |
Constant.UserDefault.requestId) | |
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { | |
Router.navigateTo("home") | |
} | |
} | |
}) { [weak self] error in | |
DispatchQueue.main.async { | |
SwiftSpinner.show(duration: 2, | |
title: "Something went wrong.\nPlease try again...", | |
animated: false) | |
self?.codeTextField.text = "" | |
} | |
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { | |
self?.codeTextField.becomeFirstResponder() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment