ChangePasswordViewController.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 oldPassword = oldPassField.text, !oldPassword.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 oldPassword.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. // let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  52. // imageView.tintColor = .white
  53. // 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)
  54. // banner.show()
  55. // return
  56. // }
  57. guard let newPassword = newPassField.text, !newPassword.isEmpty else {
  58. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  59. imageView.tintColor = .white
  60. 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)
  61. banner.show()
  62. return
  63. }
  64. if newPassword.count < 6 {
  65. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  66. imageView.tintColor = .white
  67. 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)
  68. banner.show()
  69. return
  70. }
  71. let idMe = User.getMyPin()!
  72. DispatchQueue.global().async {
  73. let tMessage = CoreMessage_TMessageBank.getChangePersonInfo_New(p_f_pin: idMe)
  74. let md5HexOld = oldPassword
  75. let md5HexNew = newPassword
  76. tMessage.mBodies[CoreMessage_TMessageKey.PSWD] = md5HexNew
  77. tMessage.mBodies[CoreMessage_TMessageKey.PSWD_OLD] = md5HexOld
  78. if let resp = Nexilis.writeAndWait(message: tMessage){
  79. if resp.isOk() {
  80. DispatchQueue.main.async {
  81. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  82. imageView.tintColor = .white
  83. 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)
  84. banner.show()
  85. self.navigationController?.popViewController(animated: true)
  86. }
  87. } else {
  88. DispatchQueue.main.async {
  89. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  90. imageView.tintColor = .white
  91. 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)
  92. banner.show()
  93. }
  94. }
  95. } else {
  96. DispatchQueue.main.async {
  97. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  98. imageView.tintColor = .white
  99. 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)
  100. banner.show()
  101. }
  102. }
  103. }
  104. }
  105. @objc func showOldPassword() {
  106. if oldPassField.isSecureTextEntry {
  107. oldPassField.isSecureTextEntry = false
  108. showOldPassButton.setImage(UIImage(systemName: "eye.fill"), for: .normal)
  109. } else {
  110. oldPassField.isSecureTextEntry = true
  111. showOldPassButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  112. }
  113. }
  114. @objc func showNewPassword() {
  115. if newPassField.isSecureTextEntry {
  116. newPassField.isSecureTextEntry = false
  117. showNewPassButton.setImage(UIImage(systemName: "eye.fill"), for: .normal)
  118. } else {
  119. newPassField.isSecureTextEntry = true
  120. showNewPassButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  121. }
  122. }
  123. }