// // SRPINKeyboardView.swift // SRPinView // // Created by saroj raut on 1/23/20. // Copyright © 2020 saroj raut. All rights reserved. // import UIKit import LocalAuthentication public enum Constants { static let nibName = "PINKeyboardView" static let duration = 0.3 static let maxPinLength = 6 enum Button: Int { case delete = 1000 case ok = 1001 } } public struct Options { public var title: String? public var subtitle: String? public var image: UIImage? public var color: UIColor? public var pinColor: UIColor? public init() {} } public class PINKeyboardView: UIViewController { // MARK: - Top view @IBOutlet weak var photoImageView: UIImageView! @IBOutlet weak var messageLabel: UILabel! @IBOutlet var pinIndicators: [Indicator]! @IBOutlet weak var cancelBttn: UIButton! var pinColor:UIColor = .red var pinEntered: ((String,Bool)->())? private let context = LAContext() private var pin = "" public override func viewDidLoad() { super.viewDidLoad() modalPresentationStyle = .fullScreen configureLanguage() // modalPresentationStyle = .overFullScreen // modalTransitionStyle = .crossDissolve } func configureLanguage() { messageLabel.text = "enter_your_transaction_pin_text".localized() cancelBttn.setTitle("cancel_text".localized(), for: .normal) } public override func viewDidAppear(_ animated: Bool) { /* if (BioMetricAuthenticator.canAuthenticate() && UserDefaultManager().getTranBioAuth){ switch KeychainService().secureValet?.string(forKey: KeychainService.tranPass,withPrompt: "Fingerprint required to read PIN") { case let .success(password): let pin = password self.dismiss(animated: true) { self.pinEntered?(pin,true) } case .userCancelled: break case .itemNotFound: UserDefaultManager().saveTranBioAuth(Used: false) self.showAlert(title: "You are not allowed to use fingerprint due to fingerprint update , please re-enable your fingerprint") case .none: break } } */ } private func drawing(isNeedClear: Bool, tag: Int? = nil) { let results = pinIndicators.filter { $0.isNeedClear == isNeedClear } let pinView = isNeedClear ? results.last : results.first pinView?.isNeedClear = !isNeedClear UIView.animate(withDuration: Constants.duration, animations: { pinView?.backgroundColor = isNeedClear ? .clear : self.pinColor }) { _ in isNeedClear ? self.pin = String(self.pin.dropLast()) : self.pincodeChecker(tag ?? 0) self.pinEntered?(self.pin, false) } } private func pincodeChecker(_ pinNumber: Int) { if pin.count < Constants.maxPinLength { pin.append("\(pinNumber)") guard pin.count == Constants.maxPinLength else { pinEntered?(pin, false) return } dismiss(animated: true) { self.pinEntered?(self.pin,true) } } } private func incorrectPinAnimation() { pinIndicators.forEach { view in view.backgroundColor = .clear } } fileprivate func clearView() { pin = "" pinIndicators.forEach { view in view.isNeedClear = false UIView.animate(withDuration: Constants.duration, animations: { view.backgroundColor = .clear }) } } // MARK: - Keyboard @IBAction func keyboardPressed(_ sender: UIButton) { switch sender.tag { case Constants.Button.delete.rawValue: drawing(isNeedClear: true) case Constants.Button.ok.rawValue: dismiss(animated: true) { self.pinEntered?(self.pin,true) } default: drawing(isNeedClear: false, tag: sender.tag) } } } // MARK: - Present public extension PINKeyboardView { // Present PINKeyboardView class func present(config: Options? = nil, over viewController: UIViewController? = nil,pinEntered:((String, Bool)->())?,presented:(()->())?) { let vc = viewController ?? UIApplication.shared.keyWindow?.rootViewController guard let root = vc, let PINView = Bundle(for: self.classForCoder()).loadNibNamed(Constants.nibName, owner: self, options: nil)?.first as? PINKeyboardView else { return } PINView.pinEntered = pinEntered PINView.messageLabel.text = config?.title ?? "" PINView.view.backgroundColor = config?.color ?? .white PINView.pinColor = config?.pinColor ?? .red if let image = config?.image { PINView.photoImageView.image = image } else { PINView.photoImageView.isHidden = false } root.present(PINView, animated: true, completion: { presented?() }) } }