You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.3 KiB

6 years ago
  1. //
  2. // SignUpViewModel.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 12/8/17.
  6. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftyJSON
  10. class SignUpViewModel: ModelExtension {
  11. private let minPasswordLength: Int = 1
  12. var registered: Box<Bool?> = Box(nil)
  13. var signUpConnectionTimeOut: Box<Bool?> = Box(nil)
  14. var username: String!
  15. var password: String!
  16. var confirmpassword: String!
  17. }
  18. extension SignUpViewModel {
  19. func setUserCredentials(username: String, password: String, confirmpassword: String) {
  20. self.username = username
  21. self.password = password
  22. self.confirmpassword = confirmpassword
  23. }
  24. /**
  25. Validate empty textfield
  26. */
  27. func validateUser() -> isValid {
  28. if (username.isBlank || password.isBlank || confirmpassword.isBlank) {
  29. return .InValid("Please fill all the fields")
  30. } else {
  31. if isNumber(username: username) {
  32. if isValidPhone(phone: username) {
  33. if isValidPasswordLength(password: password) {
  34. if passwordsMatch(password: password, confirmpassword: confirmpassword) {
  35. return .Valid
  36. }
  37. return .InValid("Passwords do not match")
  38. }
  39. return .InValid("Password length doesn't meet criteria")
  40. }
  41. return .InValid("InValid Phone")
  42. } else {
  43. if isValidEmail(email: username) {
  44. if isValidPasswordLength(password: password) {
  45. if passwordsMatch(password: password, confirmpassword: confirmpassword) {
  46. return .Valid
  47. }
  48. return .InValid("Passwords do not match")
  49. }
  50. return .InValid("Password length doesn't meet criteria")
  51. }
  52. return .InValid("InValid Email")
  53. }
  54. }
  55. }
  56. /**
  57. Validate email format
  58. */
  59. func isValidEmail(email:String) -> Bool {
  60. let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
  61. return NSPredicate(format:"SELF MATCHES %@", emailRegEx).evaluate(with: email)
  62. }
  63. /**
  64. Validate phone number
  65. */
  66. func isValidPhone(phone: String) -> Bool {
  67. return true
  68. }
  69. /**
  70. Validate empty textfield
  71. */
  72. func isBlank(text: String) -> Bool {
  73. return text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
  74. }
  75. /**
  76. Validate password length
  77. */
  78. func isValidPasswordLength(password: String) -> Bool {
  79. guard password.count < minPasswordLength else {
  80. return true
  81. }
  82. return false
  83. }
  84. /**
  85. Validate password match
  86. */
  87. func passwordsMatch(password: String, confirmpassword: String) -> Bool {
  88. guard password == confirmpassword else {
  89. return false
  90. }
  91. return true
  92. }
  93. /**
  94. Validate phone number
  95. */
  96. func isNumber(username: String) -> Bool {
  97. guard let _ = Int(username) else {
  98. return false
  99. }
  100. return true
  101. }
  102. }
  103. extension SignUpViewModel {
  104. /**
  105. Api request for register the account
  106. */
  107. func register() {
  108. if !Reachability.isConnectedToNetwork() {
  109. self.internetConnection.value = false
  110. } else {
  111. RestApiMananger.sharedInstance.registerUser(userId: self.username, password: self.password) { result in
  112. switch result {
  113. case let .success(json):
  114. print(json, "code")
  115. UserDefaults.standard.set(self.username, forKey: "com.gmeremit.username")
  116. self.registered.value = true
  117. case let .failure(errorJSON):
  118. self.setErrorMessage(message: errorJSON["message"].stringValue)
  119. self.registered.value = false
  120. case .updateAccessCode:
  121. return
  122. case .logOutUser():
  123. return
  124. case .timeOut:
  125. self.signUpConnectionTimeOut.value = false
  126. }
  127. }
  128. }
  129. }
  130. }