ChangeNamePassswordViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // ChangeNamePassswordViewController.swift
  3. // NexilisLite
  4. //
  5. // Created by Qindi on 22/03/22.
  6. //
  7. import UIKit
  8. import NotificationBannerSwift
  9. import nuSDKService
  10. public class ChangeNamePassswordViewController: UIViewController {
  11. @IBOutlet weak var usernameField: UITextField!
  12. @IBOutlet weak var passwordField: UITextField!
  13. @IBOutlet weak var showPasswordButton: UIButton!
  14. public var fromSetting = false
  15. public var isSuccess: (() -> ())?
  16. public override func viewWillDisappear(_ animated: Bool) {
  17. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refreshView"), object: nil, userInfo: nil)
  18. }
  19. public override func viewDidLoad() {
  20. super.viewDidLoad()
  21. let randomInt = 1
  22. let image = UIImage(named: "pb_lbackground_\(randomInt)")
  23. if image != nil {
  24. self.view.backgroundColor = UIColor.init(patternImage: image!)
  25. }
  26. self.title = "Change Profile".localized()
  27. if !fromSetting {
  28. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16.0), NSAttributedString.Key.foregroundColor: UIColor.white]
  29. self.navigationController?.navigationBar.titleTextAttributes = attributes
  30. navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(didTapExit(sender:)))
  31. }
  32. navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(didTapSave(sender:)))
  33. passwordField.addPadding(.right(40))
  34. passwordField.isSecureTextEntry = true
  35. showPasswordButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  36. showPasswordButton.addTarget(self, action: #selector(showPassword), for: .touchUpInside)
  37. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
  38. tapGesture.cancelsTouchesInView = false
  39. view.addGestureRecognizer(tapGesture)
  40. }
  41. @objc func dismissKeyboard() {
  42. //Causes the view (or one of its embedded text fields) to resign the first responder status.
  43. view.endEditing(true)
  44. }
  45. @objc func didTapExit(sender: Any) {
  46. if fromSetting {
  47. self.navigationController?.popViewController(animated: true)
  48. self.isSuccess?()
  49. } else {
  50. self.dismiss(animated: true, completion: nil)
  51. }
  52. }
  53. @objc func didTapSave(sender: Any) {
  54. guard let name = usernameField.text, !name.isEmpty else {
  55. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  56. imageView.tintColor = .white
  57. let banner = FloatingNotificationBanner(title: "Username 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)
  58. banner.show()
  59. return
  60. }
  61. if !name.matches("^[a-zA-Z ]*$") {
  62. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  63. imageView.tintColor = .white
  64. let banner = FloatingNotificationBanner(title: "Contains prohibited characters. Only alphabetic characters are allowed.".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)
  65. banner.show()
  66. return
  67. }
  68. guard let password = passwordField.text, !password.isEmpty else {
  69. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  70. imageView.tintColor = .white
  71. let banner = FloatingNotificationBanner(title: "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)
  72. banner.show()
  73. return
  74. }
  75. if password.count < 6 {
  76. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  77. imageView.tintColor = .white
  78. let banner = FloatingNotificationBanner(title: "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)
  79. banner.show()
  80. return
  81. }
  82. let a = name.split(separator: " ", maxSplits: 1)
  83. let first = String(a[0])
  84. let last = a.count == 2 ? String(a[1]) : ""
  85. if first.count > 24 {
  86. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  87. imageView.tintColor = .white
  88. let banner = FloatingNotificationBanner(title: "First name is too long".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)
  89. banner.show()
  90. return
  91. }
  92. if last.count > 24 {
  93. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  94. imageView.tintColor = .white
  95. let banner = FloatingNotificationBanner(title: "Last name is too long".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)
  96. banner.show()
  97. return
  98. }
  99. let idMe = UserDefaults.standard.string(forKey: "me")!
  100. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  101. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  102. imageView.tintColor = .white
  103. let banner = FloatingNotificationBanner(title: "Check your connection".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)
  104. banner.show()
  105. return
  106. }
  107. DispatchQueue.global().async {
  108. if let resp = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSendOTPChangeProfile(name: first + " " + last, type: "2")) {
  109. if resp.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "1a" {
  110. DispatchQueue.main.async {
  111. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  112. imageView.tintColor = .white
  113. let banner = FloatingNotificationBanner(title: "Username has already been registered. Please use another username".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)
  114. banner.show()
  115. }
  116. } else if resp.isOk() {
  117. let md5Hex = Utils.getMD5(string: password).map { String(format: "%02hhx", $0) }.joined()
  118. let tMessage = CoreMessage_TMessageBank.getChangePersonInfo_New(p_f_pin: idMe)
  119. tMessage.mBodies[CoreMessage_TMessageKey.FIRST_NAME] = first
  120. tMessage.mBodies[CoreMessage_TMessageKey.LAST_NAME] = last
  121. tMessage.mBodies[CoreMessage_TMessageKey.PASSWORD] = md5Hex
  122. tMessage.mBodies[CoreMessage_TMessageKey.PASSWORD_OLD] = ""
  123. if let resp2 = Nexilis.writeAndWait(message: tMessage){
  124. if resp2.isOk() {
  125. Database.shared.database?.inTransaction({ fmdb, rollback in
  126. _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: ["first_name": first , "last_name": last], _where: "f_pin = '\(idMe)'")
  127. })
  128. UserDefaults.standard.set(password, forKey: "pwd")
  129. UserDefaults.standard.set(true, forKey: "is_change_profile")
  130. UserDefaults.standard.synchronize()
  131. // NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateFifthTab"), object: nil, userInfo: nil)
  132. DispatchQueue.main.async {
  133. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  134. imageView.tintColor = .white
  135. let banner = FloatingNotificationBanner(title: "Successfully changed".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)
  136. banner.show()
  137. self.didTapExit(sender: "exit")
  138. }
  139. }
  140. } else {
  141. DispatchQueue.main.async {
  142. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  143. imageView.tintColor = .white
  144. 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)
  145. banner.show()
  146. }
  147. }
  148. }
  149. } else {
  150. DispatchQueue.main.async {
  151. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  152. imageView.tintColor = .white
  153. 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)
  154. banner.show()
  155. }
  156. }
  157. }
  158. }
  159. @objc func showPassword() {
  160. if passwordField.isSecureTextEntry {
  161. passwordField.isSecureTextEntry = false
  162. showPasswordButton.setImage(UIImage(systemName: "eye.fill"), for: .normal)
  163. } else {
  164. passwordField.isSecureTextEntry = true
  165. showPasswordButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  166. }
  167. }
  168. }
  169. public class PasswordTextField: UITextField {
  170. public override var isSecureTextEntry: Bool {
  171. didSet {
  172. if isFirstResponder {
  173. _ = becomeFirstResponder()
  174. //MARK:- Do something what you want
  175. }
  176. }
  177. }
  178. public override func becomeFirstResponder() -> Bool {
  179. let success = super.becomeFirstResponder()
  180. if isSecureTextEntry, let text = self.text {
  181. self.text?.removeAll()
  182. insertText(text)
  183. }
  184. return success
  185. }
  186. }