ChangePasswordViewController.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // ChangePasswordViewController.swift
  3. // NexilisLite
  4. //
  5. // Created by Qindi on 23/03/22.
  6. //
  7. import UIKit
  8. import NotificationBannerSwift
  9. class ChangePasswordViewController: UIViewController {
  10. @IBOutlet weak var oldPassField: PasswordTextField!
  11. @IBOutlet weak var newPassField: PasswordTextField!
  12. @IBOutlet weak var showOldPassButton: UIButton!
  13. @IBOutlet weak var showNewPassButton: UIButton!
  14. @IBOutlet weak var labelDesc: UILabel!
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. self.title = "Change Password".localized()
  18. navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save".localized(), style: .plain, target: self, action: #selector(didTapNext(sender:)))
  19. oldPassField.addPadding(.right(40))
  20. oldPassField.isSecureTextEntry = false
  21. newPassField.addPadding(.right(40))
  22. newPassField.isSecureTextEntry = false
  23. oldPassField.placeholder = "Old Password".localized()
  24. newPassField.placeholder = "New Password".localized()
  25. showOldPassButton.addTarget(self, action: #selector(showOldPassword), for: .touchUpInside)
  26. showNewPassButton.addTarget(self, action: #selector(showNewPassword), for: .touchUpInside)
  27. labelDesc.text = "Change password to keep your account secure".localized()
  28. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
  29. tapGesture.cancelsTouchesInView = false
  30. view.addGestureRecognizer(tapGesture)
  31. }
  32. @objc func dismissKeyboard() {
  33. //Causes the view (or one of its embedded text fields) to resign the first responder status.
  34. view.endEditing(true)
  35. }
  36. @objc func didTapNext(sender: Any) {
  37. guard let odlPassword = oldPassField.text, !odlPassword.isEmpty else {
  38. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  39. imageView.tintColor = .white
  40. let banner = FloatingNotificationBanner(title: "Old password can't be empty".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  41. banner.show()
  42. return
  43. }
  44. if odlPassword.count < 6 {
  45. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  46. imageView.tintColor = .white
  47. let banner = FloatingNotificationBanner(title: "Old password min 6 character".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  48. banner.show()
  49. return
  50. }
  51. // if odlPassword != UserDefaults.standard.string(forKey: "pwd"){
  52. // let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  53. // imageView.tintColor = .white
  54. // let banner = FloatingNotificationBanner(title: "Incorrect old password".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  55. // banner.show()
  56. // return
  57. // }
  58. guard let newPassword = newPassField.text, !newPassword.isEmpty else {
  59. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  60. imageView.tintColor = .white
  61. let banner = FloatingNotificationBanner(title: "New password can't be empty".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  62. banner.show()
  63. return
  64. }
  65. if newPassword.count < 6 {
  66. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  67. imageView.tintColor = .white
  68. let banner = FloatingNotificationBanner(title: "New password min 6 character".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  69. banner.show()
  70. return
  71. }
  72. let idMe = UserDefaults.standard.string(forKey: "me")!
  73. DispatchQueue.global().async {
  74. let tMessage = CoreMessage_TMessageBank.getChangePersonInfo_New(p_f_pin: idMe)
  75. let md5HexOld = Utils.getMD5(string: odlPassword).map { String(format: "%02hhx", $0) }.joined()
  76. let md5HexNew = Utils.getMD5(string: newPassword).map { String(format: "%02hhx", $0) }.joined()
  77. tMessage.mBodies[CoreMessage_TMessageKey.PASSWORD] = md5HexNew
  78. tMessage.mBodies[CoreMessage_TMessageKey.PASSWORD_OLD] = md5HexOld
  79. if let resp = Nexilis.writeAndWait(message: tMessage){
  80. if resp.isOk() {
  81. DispatchQueue.main.async {
  82. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  83. imageView.tintColor = .white
  84. let banner = FloatingNotificationBanner(title: "Successfully changed password".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .success, colors: nil, iconPosition: .center)
  85. banner.show()
  86. self.navigationController?.popViewController(animated: true)
  87. }
  88. } else {
  89. DispatchQueue.main.async {
  90. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  91. imageView.tintColor = .white
  92. let banner = FloatingNotificationBanner(title: resp.getBody(key: CoreMessage_TMessageKey.MESSAGE_TEXT), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  93. banner.show()
  94. }
  95. }
  96. } else {
  97. DispatchQueue.main.async {
  98. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  99. imageView.tintColor = .white
  100. let banner = FloatingNotificationBanner(title: "Unable to access servers. Try again later".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  101. banner.show()
  102. }
  103. }
  104. }
  105. }
  106. @objc func showOldPassword() {
  107. if oldPassField.isSecureTextEntry {
  108. oldPassField.isSecureTextEntry = false
  109. showOldPassButton.setImage(UIImage(systemName: "eye.fill"), for: .normal)
  110. } else {
  111. oldPassField.isSecureTextEntry = true
  112. showOldPassButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  113. }
  114. }
  115. @objc func showNewPassword() {
  116. if newPassField.isSecureTextEntry {
  117. newPassField.isSecureTextEntry = false
  118. showNewPassButton.setImage(UIImage(systemName: "eye.fill"), for: .normal)
  119. } else {
  120. newPassField.isSecureTextEntry = true
  121. showNewPassButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  122. }
  123. }
  124. }