ChangeNamePassswordViewController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. @IBOutlet weak var descField: UILabel!
  15. @IBOutlet weak var loginQuest: UILabel!
  16. @IBOutlet weak var loginButton: UIButton!
  17. public var fromSetting = false
  18. public var isSuccess: (() -> ())?
  19. public override func viewWillDisappear(_ animated: Bool) {
  20. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refreshView"), object: nil, userInfo: nil)
  21. }
  22. public override func viewDidLoad() {
  23. super.viewDidLoad()
  24. self.view.backgroundColor = .white
  25. self.title = "Change Profile".localized()
  26. if !fromSetting {
  27. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16.0), NSAttributedString.Key.foregroundColor: UIColor.white]
  28. self.navigationController?.navigationBar.titleTextAttributes = attributes
  29. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel".localized(), style: .plain, target: self, action: #selector(didTapExit(sender:)))
  30. }
  31. navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save".localized(), style: .plain, target: self, action: #selector(didTapSave(sender:)))
  32. passwordField.addPadding(.right(40))
  33. passwordField.isSecureTextEntry = true
  34. showPasswordButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  35. showPasswordButton.addTarget(self, action: #selector(showPassword), for: .touchUpInside)
  36. usernameField.placeholder = "Username".localized()
  37. passwordField.placeholder = "Password".localized()
  38. descField.text = "Please enter your desired Username and Password".localized()
  39. loginQuest.text = "Or do you have an account".localized()
  40. loginButton.setTitle(("Sign-In".localized()).uppercased(), for: .normal)
  41. loginButton.addTarget(self, action: #selector(showLogin), for: .touchUpInside)
  42. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
  43. tapGesture.cancelsTouchesInView = false
  44. view.addGestureRecognizer(tapGesture)
  45. }
  46. @objc func showLogin() {
  47. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "changeDevice") as! ChangeDeviceViewController
  48. controller.fromChangeNamePass = true
  49. navigationController?.show(controller, sender: nil)
  50. }
  51. @objc func dismissKeyboard() {
  52. //Causes the view (or one of its embedded text fields) to resign the first responder status.
  53. view.endEditing(true)
  54. }
  55. @objc func didTapExit(sender: Any) {
  56. if fromSetting {
  57. self.navigationController?.popViewController(animated: true)
  58. self.isSuccess?()
  59. } else {
  60. let vc = self.navigationController?.presentingViewController
  61. vc?.dismiss(animated: true, completion: nil)
  62. }
  63. }
  64. @objc func didTapSave(sender: Any) {
  65. guard let name = usernameField.text, !name.isEmpty else {
  66. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  67. imageView.tintColor = .white
  68. 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)
  69. banner.show()
  70. return
  71. }
  72. if !name.matches("^[a-zA-Z ]*$") {
  73. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  74. imageView.tintColor = .white
  75. 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)
  76. banner.show()
  77. return
  78. }
  79. guard let password = passwordField.text, !password.isEmpty else {
  80. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  81. imageView.tintColor = .white
  82. 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)
  83. banner.show()
  84. return
  85. }
  86. if password.count < 6 {
  87. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  88. imageView.tintColor = .white
  89. 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)
  90. banner.show()
  91. return
  92. }
  93. let a = name.split(separator: " ", maxSplits: 1)
  94. let first = String(a[0])
  95. let last = a.count == 2 ? String(a[1]) : ""
  96. if first.count > 24 {
  97. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  98. imageView.tintColor = .white
  99. 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)
  100. banner.show()
  101. return
  102. }
  103. if last.count > 24 {
  104. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  105. imageView.tintColor = .white
  106. 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)
  107. banner.show()
  108. return
  109. }
  110. let idMe = UserDefaults.standard.string(forKey: "me")!
  111. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  112. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  113. imageView.tintColor = .white
  114. 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)
  115. banner.show()
  116. return
  117. }
  118. Nexilis.showLoader()
  119. DispatchQueue.global().async {
  120. if let resp = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSendOTPChangeProfile(name: first + " " + last, type: "2")) {
  121. if resp.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "1a" {
  122. DispatchQueue.main.async {
  123. Nexilis.hideLoader(completion: {
  124. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  125. imageView.tintColor = .white
  126. 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)
  127. banner.show()
  128. })
  129. }
  130. } else if resp.isOk() {
  131. let md5Hex = password
  132. let tMessage = CoreMessage_TMessageBank.getChangePersonInfo_New(p_f_pin: idMe)
  133. tMessage.mBodies[CoreMessage_TMessageKey.FIRST_NAME] = first
  134. tMessage.mBodies[CoreMessage_TMessageKey.LAST_NAME] = last
  135. tMessage.mBodies[CoreMessage_TMessageKey.PASSWORD] = md5Hex
  136. tMessage.mBodies[CoreMessage_TMessageKey.PASSWORD_OLD] = ""
  137. if let resp2 = Nexilis.writeAndWait(message: tMessage){
  138. if resp2.isOk() {
  139. Database.shared.database?.inTransaction({ fmdb, rollback in
  140. _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: ["first_name": first , "last_name": last], _where: "f_pin = '\(idMe)'")
  141. })
  142. Utils.setProfile(value: true)
  143. UserDefaults.standard.synchronize()
  144. // NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateFifthTab"), object: nil, userInfo: nil)
  145. DispatchQueue.main.async {
  146. Nexilis.hideLoader(completion: {
  147. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  148. imageView.tintColor = .white
  149. 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)
  150. banner.show()
  151. self.didTapExit(sender: "exit")
  152. })
  153. }
  154. }
  155. } else {
  156. DispatchQueue.main.async {
  157. Nexilis.hideLoader(completion: {
  158. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  159. imageView.tintColor = .white
  160. 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)
  161. banner.show()
  162. })
  163. }
  164. }
  165. }
  166. } else {
  167. DispatchQueue.main.async {
  168. Nexilis.hideLoader(completion: {
  169. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  170. imageView.tintColor = .white
  171. 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)
  172. banner.show()
  173. })
  174. }
  175. }
  176. }
  177. }
  178. @objc func showPassword() {
  179. if passwordField.isSecureTextEntry {
  180. passwordField.isSecureTextEntry = false
  181. showPasswordButton.setImage(UIImage(systemName: "eye.fill"), for: .normal)
  182. } else {
  183. passwordField.isSecureTextEntry = true
  184. showPasswordButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  185. }
  186. }
  187. }
  188. public class PasswordTextField: UITextField {
  189. public override var isSecureTextEntry: Bool {
  190. didSet {
  191. if isFirstResponder {
  192. _ = becomeFirstResponder()
  193. //MARK:- Do something what you want
  194. }
  195. }
  196. }
  197. public override func becomeFirstResponder() -> Bool {
  198. let success = super.becomeFirstResponder()
  199. if isSecureTextEntry, let text = self.text {
  200. self.text?.removeAll()
  201. insertText(text)
  202. }
  203. return success
  204. }
  205. }