SignUpSignIn.swift 23 KB

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