Files
Woka_Native_iOS/WOKA/Authentication/ViewModel/OTPVM.swift
2024-05-02 13:20:40 +05:30

105 lines
3.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// OTPVM.swift
// WOKA
//
// Created by MacBook Pro on 29/04/24.
//
import UIKit
class OTPVM{
weak var vc : OTPVC!
func initView(){
let color1 = #colorLiteral(red: 0.144693464, green: 0.1426281333, blue: 0.6686832905, alpha: 1)
let color2 = #colorLiteral(red: 0.6901960784, green: 0.2745098039, blue: 0.7568627451, alpha: 1)
vc.nextBtn.applyGradient(colors: [color1, color2], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0.8, y: 0))
vc.nextBtn.roundCorner()
self.vc.wokaLogoTopConstriant.constant = CheckPhoneHomeBtnOrNotch.shareInstance.topConstriant()
tfMod(textField: [vc.tf1, vc.tf2, vc.tf3, vc.tf4])
if AuthFunc.shareInstance.userType == .adult{
vc.codeSentLabel.text = "Please Get the OTP Sent to Your Email".localized(loc: K.GVar.localized)
vc.enterCodeLabel.text = "PLEASE ENTER THE OTP".localized(loc: K.GVar.localized)
vc.requestThemLabel.text = "Dont forget to check your JUNK/SPAM folder".localized(loc: K.GVar.localized)
}else{
vc.codeSentLabel.text = "Please Get the Code Sent to Your Parents Email".localized(loc: K.GVar.localized)
vc.enterCodeLabel.text = "PLEASE ENTER THE CODE".localized(loc: K.GVar.localized)
vc.requestThemLabel.text = "Request them for the verfication code to activate your account".localized(loc: K.GVar.localized)
}
}
private func tfMod(textField : [UITextField]){
for i in textField{
i.roundCorner(radius: i.frame.height / 2)
i.delegate = vc.self
i.textContentType = .oneTimeCode
i.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
}
}
// MARK: - TextField OTp handling
func textFieldDidChange(_ textField: UITextField, otpCode: String) {
if textField.textContentType == UITextContentType.oneTimeCode{
//here split the text to your four text fields
if otpCode.count == 4, Int(otpCode) != nil {
vc.tf1.text = String(otpCode[otpCode.index(otpCode.startIndex, offsetBy: 0)])
vc.tf2.text = String(otpCode[otpCode.index(otpCode.startIndex, offsetBy: 1)])
vc.tf3.text = String(otpCode[otpCode.index(otpCode.startIndex, offsetBy: 2)])
vc.tf4.text = String(otpCode[otpCode.index(otpCode.startIndex, offsetBy: 3)])
// let textFields = [vc.tf1,vc.tf2,vc.tf3,vc.tf4]
// for i in 0..<textFields.count{
// textFields[i].layer.borderColor = UIColor.GREEN_COLOR.cgColor
// }
} else {
textField.text = ""
}
textField.resignFirstResponder()
}
}
func emptyTF(tf : [UITextField]){
DispatchQueue.main.async {
for i in tf{
i.text = ""
}
}
}
@objc func textFieldDidChange(textField: UITextField){
let text = textField.text
if text?.count == 1 {
switch textField{
case vc.tf1:
vc.tf2.becomeFirstResponder()
case vc.tf2:
vc.tf3.becomeFirstResponder()
case vc.tf3:
vc.tf4.becomeFirstResponder()
case vc.tf4:
vc.tf4.resignFirstResponder()
default:
break
}
}
if text?.count == 0 {
switch textField{
case vc.tf1:
vc.tf1.becomeFirstResponder()
case vc.tf2:
vc.tf1.becomeFirstResponder()
case vc.tf3:
vc.tf2.becomeFirstResponder()
case vc.tf4:
vc.tf3.becomeFirstResponder()
default:
break
}
}
}
}