ChangeDeviceViewController.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // ChangeDeviceViewController.swift
  3. // NexilisLite
  4. //
  5. // Created by Qindi on 23/03/22.
  6. //
  7. import UIKit
  8. import NotificationBannerSwift
  9. import nuSDKService
  10. public class ChangeDeviceViewController: UIViewController {
  11. @IBOutlet weak var usernameField: UITextField!
  12. @IBOutlet weak var passwordField: PasswordTextField!
  13. @IBOutlet weak var showPasswordButton: UIButton!
  14. @IBOutlet weak var descLogin: UILabel!
  15. public var isDismiss: ((String) -> ())?
  16. public var forceLogin = false
  17. public var fromChangeNamePass = false
  18. public override func viewDidLoad() {
  19. super.viewDidLoad()
  20. self.view.backgroundColor = .white
  21. self.title = "Sign-In".localized()
  22. descLogin.text = "Please enter your registered nickname or email address to Sign-In".localized()
  23. navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Submit".localized(), style: .plain, target: self, action: #selector(didTapSubmit(sender:)))
  24. passwordField.addPadding(.right(40))
  25. passwordField.isSecureTextEntry = true
  26. showPasswordButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  27. usernameField.placeholder = "Your Nickname".localized() + "/" + "Email".localized()
  28. passwordField.placeholder = "Password".localized()
  29. usernameField.addTarget(self, action: #selector(checkUsername(_:)), for: .editingChanged)
  30. showPasswordButton.addTarget(self, action: #selector(showPassword), for: .touchUpInside)
  31. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
  32. tapGesture.cancelsTouchesInView = false
  33. view.addGestureRecognizer(tapGesture)
  34. }
  35. @objc func checkUsername(_ textField: UITextField) {
  36. let text : String! = usernameField.text
  37. if isValidEmail(text) {
  38. passwordField.isHidden = true
  39. showPasswordButton.isHidden = true
  40. } else if passwordField.isHidden {
  41. passwordField.isHidden = false
  42. showPasswordButton.isHidden = false
  43. }
  44. }
  45. func isValidEmail(_ email: String) -> Bool {
  46. let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
  47. let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
  48. return emailPred.evaluate(with: email)
  49. }
  50. @objc func dismissKeyboard() {
  51. //Causes the view (or one of its embedded text fields) to resign the first responder status.
  52. view.endEditing(true)
  53. }
  54. @objc func showPassword() {
  55. if passwordField.isSecureTextEntry {
  56. passwordField.isSecureTextEntry = false
  57. showPasswordButton.setImage(UIImage(systemName: "eye.fill"), for: .normal)
  58. } else {
  59. passwordField.isSecureTextEntry = true
  60. showPasswordButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  61. }
  62. }
  63. func checkEmail(email: String) {
  64. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  65. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  66. imageView.tintColor = .white
  67. 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)
  68. banner.show()
  69. return
  70. }
  71. Nexilis.showLoader()
  72. DispatchQueue.global().async {
  73. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSendOTPLogin(p_email: email), timeout: 30 * 1000) {
  74. if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") != "00" {
  75. DispatchQueue.main.async {
  76. Nexilis.hideLoader(completion: {
  77. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  78. imageView.tintColor = .white
  79. let banner = FloatingNotificationBanner(title: "Unregistered email account".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)
  80. banner.show()
  81. })
  82. }
  83. } else {
  84. DispatchQueue.main.async {
  85. Nexilis.hideLoader(completion: {
  86. self.showPageOTP(email: email)
  87. })
  88. }
  89. }
  90. } else {
  91. DispatchQueue.main.async {
  92. Nexilis.hideLoader(completion: {
  93. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  94. imageView.tintColor = .white
  95. 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)
  96. banner.show()
  97. })
  98. }
  99. }
  100. }
  101. }
  102. func showPageOTP(email: String, errCode:String = "") {
  103. let showOTPVC = VerifyEmail()
  104. showOTPVC.email = email
  105. showOTPVC.showWrongOTP = errCode
  106. showOTPVC.isDismiss = { code in
  107. Nexilis.showLoader()
  108. DispatchQueue.global().async {
  109. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSendVerifyChangeDevice(p_email: email, p_vercode: code), timeout: 30 * 1000) {
  110. if !response.isOk() {
  111. DispatchQueue.main.async {
  112. Nexilis.hideLoader(completion: {
  113. self.showPageOTP(email: email, errCode: response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99"))
  114. })
  115. }
  116. } else {
  117. self.deleteAllRecordDatabase()
  118. let id = response.getBody(key: CoreMessage_TMessageKey.F_PIN, default_value: "")
  119. let thumb = response.getBody(key: CoreMessage_TMessageKey.THUMB_ID, default_value: "")
  120. if(!id.isEmpty) {
  121. Nexilis.changeUser(f_pin: id)
  122. Utils.setProfile(value: true)
  123. UserDefaults.standard.synchronize()
  124. // pos registration
  125. _ = Nexilis.write(message: CoreMessage_TMessageBank.getPostRegistration(p_pin: id))
  126. DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
  127. Nexilis.hideLoader(completion: {
  128. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  129. imageView.tintColor = .white
  130. let banner = FloatingNotificationBanner(title: "Successfully Sign-In".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)
  131. banner.show()
  132. if self.fromChangeNamePass{
  133. var vc = self.navigationController?.presentingViewController
  134. while vc?.presentingViewController != nil {
  135. vc = vc?.presentingViewController
  136. }
  137. vc?.dismiss(animated: true, completion: nil)
  138. }
  139. else if !self.forceLogin {
  140. self.navigationController?.popViewController(animated: true)
  141. } else {
  142. self.navigationController?.dismiss(animated: true)
  143. }
  144. self.isDismiss?(thumb)
  145. })
  146. })
  147. }
  148. }
  149. } else {
  150. DispatchQueue.main.async {
  151. Nexilis.hideLoader(completion: {
  152. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  153. imageView.tintColor = .white
  154. 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)
  155. banner.show()
  156. })
  157. }
  158. }
  159. }
  160. }
  161. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
  162. self.navigationController?.present(showOTPVC, animated: true, completion: nil)
  163. })
  164. }
  165. @objc func didTapSubmit(sender: Any) {
  166. guard let name = usernameField.text, !name.isEmpty else {
  167. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  168. imageView.tintColor = .white
  169. 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)
  170. banner.show()
  171. return
  172. }
  173. if isValidEmail(name) {
  174. checkEmail(email: name)
  175. return
  176. }
  177. if !name.matches("^[a-zA-Z ]*$") {
  178. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  179. imageView.tintColor = .white
  180. 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)
  181. banner.show()
  182. return
  183. }
  184. guard let password = passwordField.text, !password.isEmpty else {
  185. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  186. imageView.tintColor = .white
  187. 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)
  188. banner.show()
  189. return
  190. }
  191. if password.count < 6 {
  192. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  193. imageView.tintColor = .white
  194. 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)
  195. banner.show()
  196. return
  197. }
  198. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  199. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  200. imageView.tintColor = .white
  201. 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)
  202. banner.show()
  203. return
  204. }
  205. Nexilis.showLoader()
  206. DispatchQueue.global().async {
  207. let md5Hex = Utils.getMD5(string: password).map { String(format: "%02hhx", $0) }.joined()
  208. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSignIn(p_name: name, p_password: md5Hex), timeout: 30 * 1000) {
  209. if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "11" {
  210. DispatchQueue.main.async {
  211. Nexilis.hideLoader(completion: {
  212. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  213. imageView.tintColor = .white
  214. let banner = FloatingNotificationBanner(title: "Invalid user / Username and password does not match".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)
  215. banner.show()
  216. })
  217. }
  218. } else if !response.isOk() {
  219. DispatchQueue.main.async {
  220. Nexilis.hideLoader(completion: {
  221. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  222. imageView.tintColor = .white
  223. 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)
  224. banner.show()
  225. })
  226. }
  227. } else {
  228. self.deleteAllRecordDatabase()
  229. let id = response.getBody(key: CoreMessage_TMessageKey.F_PIN, default_value: "")
  230. let thumb = response.getBody(key: CoreMessage_TMessageKey.THUMB_ID, default_value: "")
  231. if(!id.isEmpty) {
  232. Nexilis.changeUser(f_pin: id)
  233. Utils.setProfile(value: true)
  234. UserDefaults.standard.synchronize()
  235. // pos registration
  236. _ = Nexilis.write(message: CoreMessage_TMessageBank.getPostRegistration(p_pin: id))
  237. DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
  238. Nexilis.hideLoader(completion: {
  239. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  240. imageView.tintColor = .white
  241. let banner = FloatingNotificationBanner(title: "Successfully Sign-In".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)
  242. banner.show()
  243. if self.fromChangeNamePass{
  244. var vc = self.navigationController?.presentingViewController
  245. while vc?.presentingViewController != nil {
  246. vc = vc?.presentingViewController
  247. }
  248. vc?.dismiss(animated: true, completion: nil)
  249. }
  250. else if !self.forceLogin {
  251. self.navigationController?.popViewController(animated: true)
  252. } else {
  253. self.navigationController?.dismiss(animated: true)
  254. }
  255. self.isDismiss?(thumb)
  256. })
  257. })
  258. }
  259. }
  260. } else {
  261. DispatchQueue.main.async {
  262. Nexilis.hideLoader(completion: {
  263. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  264. imageView.tintColor = .white
  265. 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)
  266. banner.show()
  267. })
  268. }
  269. }
  270. }
  271. }
  272. }