ChangeDeviceViewController.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
  22. let navBarAppearance = UINavigationBarAppearance()
  23. navBarAppearance.configureWithOpaqueBackground()
  24. navBarAppearance.backgroundColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : UIColor.mainColor
  25. navBarAppearance.titleTextAttributes = attributes
  26. navigationController?.navigationBar.standardAppearance = navBarAppearance
  27. navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
  28. navigationController?.navigationBar.tintColor = .white
  29. self.title = "Sign-In".localized()
  30. descLogin.text = "Please enter your registered nickname or email address to Sign-In".localized()
  31. navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Submit".localized(), style: .plain, target: self, action: #selector(didTapSubmit(sender:)))
  32. passwordField.addPadding(.right(40))
  33. passwordField.isSecureTextEntry = true
  34. showPasswordButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  35. usernameField.placeholder = "Your Nickname".localized() + "/" + "Email".localized()
  36. passwordField.placeholder = "Password".localized()
  37. usernameField.addTarget(self, action: #selector(checkUsername(_:)), for: .editingChanged)
  38. showPasswordButton.addTarget(self, action: #selector(showPassword), for: .touchUpInside)
  39. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
  40. tapGesture.cancelsTouchesInView = false
  41. view.addGestureRecognizer(tapGesture)
  42. }
  43. public override func viewWillAppear(_ animated: Bool) {
  44. navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
  45. navigationController?.navigationBar.tintColor = .white
  46. navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)
  47. }
  48. @objc func checkUsername(_ textField: UITextField) {
  49. let text : String! = usernameField.text
  50. if isValidEmail(text) {
  51. passwordField.isHidden = true
  52. showPasswordButton.isHidden = true
  53. } else if passwordField.isHidden {
  54. passwordField.isHidden = false
  55. showPasswordButton.isHidden = false
  56. }
  57. }
  58. func isValidEmail(_ email: String) -> Bool {
  59. let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
  60. let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
  61. return emailPred.evaluate(with: email)
  62. }
  63. @objc func dismissKeyboard() {
  64. //Causes the view (or one of its embedded text fields) to resign the first responder status.
  65. view.endEditing(true)
  66. }
  67. @objc func showPassword() {
  68. if passwordField.isSecureTextEntry {
  69. passwordField.isSecureTextEntry = false
  70. showPasswordButton.setImage(UIImage(systemName: "eye.fill"), for: .normal)
  71. } else {
  72. passwordField.isSecureTextEntry = true
  73. showPasswordButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  74. }
  75. }
  76. func checkEmail(email: String) {
  77. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  78. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  79. imageView.tintColor = .white
  80. 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)
  81. banner.show()
  82. return
  83. }
  84. Nexilis.showLoader()
  85. DispatchQueue.global().async {
  86. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSendOTPLogin(p_email: email), timeout: 30 * 1000) {
  87. if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") != "00" {
  88. DispatchQueue.main.async {
  89. Nexilis.hideLoader(completion: {
  90. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  91. imageView.tintColor = .white
  92. 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)
  93. banner.show()
  94. })
  95. }
  96. } else {
  97. DispatchQueue.main.async {
  98. Nexilis.hideLoader(completion: {
  99. self.showPageOTP(email: email)
  100. })
  101. }
  102. }
  103. } else {
  104. DispatchQueue.main.async {
  105. Nexilis.hideLoader(completion: {
  106. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  107. imageView.tintColor = .white
  108. 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)
  109. banner.show()
  110. })
  111. }
  112. }
  113. }
  114. }
  115. func showPageOTP(email: String, errCode:String = "") {
  116. let showOTPVC = VerifyEmail()
  117. showOTPVC.email = email
  118. showOTPVC.showWrongOTP = errCode
  119. showOTPVC.isDismiss = { code in
  120. Nexilis.showLoader()
  121. DispatchQueue.global().async {
  122. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSendVerifyChangeDevice(p_email: email, p_vercode: code), timeout: 30 * 1000) {
  123. if !response.isOk() {
  124. DispatchQueue.main.async {
  125. Nexilis.hideLoader(completion: {
  126. self.showPageOTP(email: email, errCode: response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99"))
  127. })
  128. }
  129. } else {
  130. self.deleteAllRecordDatabase()
  131. let id = response.getBody(key: CoreMessage_TMessageKey.F_PIN, default_value: "")
  132. let thumb = response.getBody(key: CoreMessage_TMessageKey.THUMB_ID, default_value: "")
  133. if(!id.isEmpty) {
  134. // Nexilis.changeUser(f_pin: id)
  135. Utils.setProfile(value: true)
  136. // pos registration
  137. _ = Nexilis.write(message: CoreMessage_TMessageBank.getPostRegistration(p_pin: id))
  138. DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
  139. Nexilis.hideLoader(completion: {
  140. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  141. imageView.tintColor = .white
  142. 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)
  143. banner.show()
  144. if Nexilis.showFB {
  145. Nexilis.floatingButton.removeFromSuperview()
  146. Nexilis.floatingButton = FloatingButton()
  147. let viewController = (UIApplication.shared.windows.first?.rootViewController)!
  148. Nexilis.addFB(viewController: viewController, fromMAB: true)
  149. }
  150. if self.fromChangeNamePass{
  151. var vc = self.navigationController?.presentingViewController
  152. while vc?.presentingViewController != nil {
  153. vc = vc?.presentingViewController
  154. }
  155. vc?.dismiss(animated: true, completion: nil)
  156. }
  157. else if !self.forceLogin {
  158. self.navigationController?.popViewController(animated: true)
  159. } else {
  160. self.navigationController?.dismiss(animated: true)
  161. }
  162. self.isDismiss?(thumb)
  163. })
  164. })
  165. }
  166. }
  167. } else {
  168. DispatchQueue.main.async {
  169. Nexilis.hideLoader(completion: {
  170. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  171. imageView.tintColor = .white
  172. 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)
  173. banner.show()
  174. })
  175. }
  176. }
  177. }
  178. }
  179. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
  180. self.navigationController?.present(showOTPVC, animated: true, completion: nil)
  181. })
  182. }
  183. @objc func didTapSubmit(sender: Any) {
  184. guard let name = usernameField.text, !name.isEmpty else {
  185. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  186. imageView.tintColor = .white
  187. 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)
  188. banner.show()
  189. return
  190. }
  191. if isValidEmail(name) {
  192. checkEmail(email: name)
  193. return
  194. }
  195. if !name.matches("^[a-zA-Z0-9 ]*$") {
  196. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  197. imageView.tintColor = .white
  198. 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)
  199. banner.show()
  200. return
  201. }
  202. guard let password = passwordField.text, !password.isEmpty else {
  203. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  204. imageView.tintColor = .white
  205. 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)
  206. banner.show()
  207. return
  208. }
  209. if password.count < 6 {
  210. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  211. imageView.tintColor = .white
  212. 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)
  213. banner.show()
  214. return
  215. }
  216. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  217. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  218. imageView.tintColor = .white
  219. 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)
  220. banner.show()
  221. return
  222. }
  223. if Database.shared.openDatabase() == 0 {
  224. return
  225. }
  226. Nexilis.showLoader()
  227. DispatchQueue.global().async {
  228. let md5Hex = password
  229. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSignIn(p_name: name, p_password: md5Hex), timeout: 30 * 1000) {
  230. if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "11" {
  231. DispatchQueue.main.async {
  232. Nexilis.hideLoader(completion: {
  233. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  234. imageView.tintColor = .white
  235. 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)
  236. banner.show()
  237. })
  238. }
  239. } else if !response.isOk() {
  240. DispatchQueue.main.async {
  241. Nexilis.hideLoader(completion: {
  242. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  243. imageView.tintColor = .white
  244. 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)
  245. banner.show()
  246. })
  247. }
  248. } else {
  249. self.deleteAllRecordDatabase()
  250. let id = response.getBody(key: CoreMessage_TMessageKey.F_PIN, default_value: "")
  251. let f_pin = response.getBody(key: CoreMessage_TMessageKey.F_PIN_REAL, default_value: "")
  252. let thumb = response.getBody(key: CoreMessage_TMessageKey.THUMB_ID, default_value: "")
  253. let device_id = response.getBody(key: CoreMessage_TMessageKey.IMEI, default_value: id)
  254. let last_sign = response.getBody(key: CoreMessage_TMessageKey.LAST_SIGN, default_value: "0")
  255. //print("last sign: \(last_sign)")
  256. if last_sign != "0" {
  257. Utils.setLoginMultipleFPin(value: f_pin)
  258. DispatchQueue.main.async {
  259. let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
  260. imageView.tintColor = .white
  261. let banner = FloatingNotificationBanner(title: "Multiple Login Detected...".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .info, colors: nil, iconPosition: .center)
  262. banner.show()
  263. }
  264. DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
  265. Nexilis.hideLoader(completion: {
  266. if Nexilis.showFB {
  267. Nexilis.floatingButton.removeFromSuperview()
  268. Nexilis.floatingButton = FloatingButton()
  269. let viewController = (UIApplication.shared.windows.first?.rootViewController)!
  270. Nexilis.addFB(viewController: viewController, fromMAB: true)
  271. }
  272. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "onRefreshWebView"), object: nil, userInfo: nil)
  273. if self.fromChangeNamePass{
  274. var vc = self.navigationController?.presentingViewController
  275. while vc?.presentingViewController != nil {
  276. vc = vc?.presentingViewController
  277. }
  278. vc?.dismiss(animated: true, completion: nil)
  279. }
  280. else if !self.forceLogin {
  281. self.navigationController?.popViewController(animated: true)
  282. } else {
  283. self.navigationController?.dismiss(animated: true)
  284. }
  285. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
  286. let dialog = DialogUnableAccess()
  287. dialog.modalTransitionStyle = .crossDissolve
  288. dialog.modalPresentationStyle = .overCurrentContext
  289. UIApplication.shared.visibleViewController?.present(dialog, animated: true)
  290. })
  291. })
  292. })
  293. return
  294. }
  295. self.deleteAllRecordDatabase()
  296. if(!id.isEmpty) {
  297. // Nexilis.changeUser(f_pin: device_id)
  298. SecureUserDefaults.shared.set(device_id, forKey: "device_id")
  299. Utils.setProfile(value: true)
  300. // pos registration
  301. _ = Nexilis.write(message: CoreMessage_TMessageBank.getPostRegistration(p_pin: id))
  302. DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
  303. Nexilis.hideLoader(completion: {
  304. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  305. imageView.tintColor = .white
  306. 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)
  307. banner.show()
  308. if Nexilis.showFB {
  309. Nexilis.floatingButton.removeFromSuperview()
  310. Nexilis.floatingButton = FloatingButton()
  311. let viewController = (UIApplication.shared.windows.first?.rootViewController)!
  312. Nexilis.addFB(viewController: viewController, fromMAB: true)
  313. }
  314. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "onRefreshWebView"), object: nil, userInfo: nil)
  315. if self.fromChangeNamePass{
  316. var vc = self.navigationController?.presentingViewController
  317. while vc?.presentingViewController != nil {
  318. vc = vc?.presentingViewController
  319. }
  320. vc?.dismiss(animated: true, completion: nil)
  321. }
  322. else if !self.forceLogin {
  323. self.navigationController?.popViewController(animated: true)
  324. } else {
  325. self.navigationController?.dismiss(animated: true)
  326. }
  327. self.isDismiss?(thumb)
  328. })
  329. })
  330. }
  331. }
  332. } else {
  333. DispatchQueue.main.async {
  334. Nexilis.hideLoader(completion: {
  335. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  336. imageView.tintColor = .white
  337. 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)
  338. banner.show()
  339. })
  340. }
  341. }
  342. }
  343. }
  344. }