You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.3 KiB

  1. //
  2. // SRPINKeyboardView.swift
  3. // SRPinView
  4. //
  5. // Created by saroj raut on 1/23/20.
  6. // Copyright © 2020 saroj raut. All rights reserved.
  7. //
  8. import UIKit
  9. import LocalAuthentication
  10. public enum Constants {
  11. static let nibName = "PINKeyboardView"
  12. static let duration = 0.3
  13. static let maxPinLength = 6
  14. enum Button: Int {
  15. case delete = 1000
  16. case ok = 1001
  17. }
  18. }
  19. public struct Options {
  20. public var title: String?
  21. public var subtitle: String?
  22. public var image: UIImage?
  23. public var color: UIColor?
  24. public var pinColor: UIColor?
  25. public init() {}
  26. }
  27. public class PINKeyboardView: UIViewController {
  28. // MARK: - Top view
  29. @IBOutlet weak var photoImageView: UIImageView!
  30. @IBOutlet weak var messageLabel: UILabel!
  31. @IBOutlet var pinIndicators: [Indicator]!
  32. @IBOutlet weak var cancelBttn: UIButton!
  33. var pinColor:UIColor = .red
  34. var pinEntered: ((String,Bool)->())?
  35. private let context = LAContext()
  36. private var pin = ""
  37. public override func viewDidLoad() {
  38. super.viewDidLoad()
  39. modalPresentationStyle = .fullScreen
  40. configureLanguage()
  41. // modalPresentationStyle = .overFullScreen
  42. // modalTransitionStyle = .crossDissolve
  43. }
  44. func configureLanguage() {
  45. messageLabel.text = "enter_your_transaction_pin_text".localized()
  46. cancelBttn.setTitle("cancel_text".localized(), for: .normal)
  47. }
  48. public override func viewDidAppear(_ animated: Bool) {
  49. /* if (BioMetricAuthenticator.canAuthenticate() && UserDefaultManager().getTranBioAuth){
  50. switch KeychainService().secureValet?.string(forKey: KeychainService.tranPass,withPrompt: "Fingerprint required to read PIN") {
  51. case let .success(password):
  52. let pin = password
  53. self.dismiss(animated: true) {
  54. self.pinEntered?(pin,true)
  55. }
  56. case .userCancelled:
  57. break
  58. case .itemNotFound:
  59. UserDefaultManager().saveTranBioAuth(Used: false)
  60. self.showAlert(title: "You are not allowed to use fingerprint due to fingerprint update , please re-enable your fingerprint")
  61. case .none:
  62. break
  63. }
  64. } */
  65. }
  66. private func drawing(isNeedClear: Bool, tag: Int? = nil) {
  67. let results = pinIndicators.filter { $0.isNeedClear == isNeedClear }
  68. let pinView = isNeedClear ? results.last : results.first
  69. pinView?.isNeedClear = !isNeedClear
  70. UIView.animate(withDuration: Constants.duration, animations: {
  71. pinView?.backgroundColor = isNeedClear ? .clear : self.pinColor
  72. }) { _ in
  73. isNeedClear ? self.pin = String(self.pin.dropLast()) : self.pincodeChecker(tag ?? 0)
  74. self.pinEntered?(self.pin, false)
  75. }
  76. }
  77. private func pincodeChecker(_ pinNumber: Int) {
  78. if pin.count < Constants.maxPinLength {
  79. pin.append("\(pinNumber)")
  80. guard pin.count == Constants.maxPinLength else {
  81. pinEntered?(pin, false)
  82. return
  83. }
  84. dismiss(animated: true) {
  85. self.pinEntered?(self.pin,true)
  86. }
  87. }
  88. }
  89. private func incorrectPinAnimation() {
  90. pinIndicators.forEach { view in
  91. view.backgroundColor = .clear
  92. }
  93. }
  94. fileprivate func clearView() {
  95. pin = ""
  96. pinIndicators.forEach { view in
  97. view.isNeedClear = false
  98. UIView.animate(withDuration: Constants.duration, animations: {
  99. view.backgroundColor = .clear
  100. })
  101. }
  102. }
  103. // MARK: - Keyboard
  104. @IBAction func keyboardPressed(_ sender: UIButton) {
  105. switch sender.tag {
  106. case Constants.Button.delete.rawValue:
  107. drawing(isNeedClear: true)
  108. case Constants.Button.ok.rawValue:
  109. dismiss(animated: true) {
  110. self.pinEntered?(self.pin,true)
  111. }
  112. default:
  113. drawing(isNeedClear: false, tag: sender.tag)
  114. }
  115. }
  116. }
  117. // MARK: - Present
  118. public extension PINKeyboardView {
  119. // Present PINKeyboardView
  120. class func present(config: Options? = nil, over viewController: UIViewController? = nil,pinEntered:((String, Bool)->())?,presented:(()->())?) {
  121. let vc = viewController ?? UIApplication.shared.keyWindow?.rootViewController
  122. guard let root = vc,
  123. let PINView = Bundle(for: self.classForCoder()).loadNibNamed(Constants.nibName, owner: self, options: nil)?.first as? PINKeyboardView else {
  124. return
  125. }
  126. PINView.pinEntered = pinEntered
  127. PINView.messageLabel.text = config?.title ?? ""
  128. PINView.view.backgroundColor = config?.color ?? .white
  129. PINView.pinColor = config?.pinColor ?? .red
  130. if let image = config?.image {
  131. PINView.photoImageView.image = image
  132. } else {
  133. PINView.photoImageView.isHidden = false
  134. }
  135. root.present(PINView, animated: true, completion: {
  136. presented?()
  137. })
  138. }
  139. }