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.

173 lines
5.4 KiB

  1. //
  2. // RegisterInteractor.swift
  3. // GMERemittance
  4. //
  5. // Created by gme_2 on 10/09/2018.
  6. //Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import FirebaseInstanceID
  10. class RegisterInteractor {
  11. // MARK: Properties
  12. weak var output: RegisterInteractorOutput?
  13. private let service: RegisterServiceType
  14. // MARK: Initialization
  15. init(service: RegisterServiceType) {
  16. self.service = service
  17. }
  18. private func getAppVersion () -> String? {
  19. let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
  20. return version
  21. }
  22. private func getOsVersion() -> String {
  23. let version = UIDevice.current.systemVersion
  24. return version
  25. }
  26. private func getPhoneBrand() -> String {
  27. let phoneName = UIDevice.current.model
  28. return phoneName
  29. }
  30. private func getPhoneOs() -> String {
  31. let os = UIDevice.current.systemName
  32. return os
  33. }
  34. private func getUUid() -> String? {
  35. let uuid = UIDevice.current.identifierForVendor?.uuidString
  36. return uuid
  37. }
  38. private func getFcmToken() -> String? {
  39. let token = InstanceID.instanceID().token()
  40. return token
  41. }
  42. private func getCliendId() -> String {
  43. return "172017F9EC11222E8107142733"
  44. }
  45. // func hasOnlyAlphanumeric() -> (String) -> Bool {
  46. // let alphaCharacterSet = CharacterSet.alphanumerics
  47. // return {text in
  48. // text.rangeOfCharacter(from: alphaCharacterSet) != nil
  49. // }
  50. // }
  51. //
  52. // func hasOnlyLettersWithSpaces(_ text: String) -> Bool {
  53. // let alphaCharacterSet = CharacterSet.letters
  54. // let spaceCharacterSet = CharacterSet.init(charactersIn: " ")
  55. // let alphaWithSpacesCharacterSet = alphaCharacterSet.union(spaceCharacterSet)
  56. //
  57. // return text.rangeOfCharacter(from: alphaWithSpacesCharacterSet) != nil
  58. // }
  59. private func isValidPasword(password: String, confirmPassword: String) -> (isValid: Bool, error: String) {
  60. var error = ""
  61. var isValid = true
  62. // >= 9, 1 special character, 1 number
  63. func isValidLength() -> Bool {
  64. return password.count >= 9
  65. }
  66. func hasNumber() -> Bool {
  67. let characterSet = CharacterSet.init(charactersIn: "1234567890")
  68. return password.rangeOfCharacter(from: characterSet) != nil
  69. }
  70. func hasLetter() -> Bool {
  71. let characterSet = CharacterSet.letters
  72. return password.rangeOfCharacter(from: characterSet) != nil
  73. }
  74. func hasSpecialCharacter() -> Bool {
  75. let characterSet = CharacterSet.alphanumerics
  76. return password.rangeOfCharacter(from: characterSet.inverted) != nil
  77. }
  78. if password.isEmpty {
  79. error = error + "\n Password field is required."
  80. isValid = false
  81. }else {
  82. if !isValidLength() {
  83. error = error + "\n Password should be greator than 8 character."
  84. isValid = false
  85. }
  86. let validPassword = hasNumber() && hasLetter() && hasSpecialCharacter()
  87. if !validPassword {
  88. isValid = false
  89. error = error + "\n Passwords should contain at least 1 number, 1 letter and 1 special character"
  90. }
  91. if password != confirmPassword {
  92. error = error + "\n Passwords do not match."
  93. }
  94. }
  95. return (isValid, error)
  96. }
  97. // MARK: Converting entities
  98. private func isValid(userName: String, password: String, confirmPassword: String, dob: String) -> (isValid: Bool, error: Error){
  99. var error = ""
  100. var isValid = true
  101. // user name
  102. if userName.isEmpty { error = error + "Username field is required."; isValid = false }
  103. let result = self.isValidPasword(password: password, confirmPassword: confirmPassword)
  104. if !result.isValid {
  105. error = error + result.error
  106. isValid = false
  107. }
  108. if dob.isEmpty { error = error + "\n Date Of Birth field is required."; isValid = false }
  109. let _error = NSError.init(domain: "LoginInteractor", code: 0, userInfo: [NSLocalizedDescriptionKey : error])
  110. return (isValid, _error)
  111. }
  112. }
  113. extension RegisterInteractor: RegisterInteractorInput {
  114. func register(model: RegisterRequestModel) {
  115. let validationResult = self.isValid(userName: model.username ?? "", password: model.password ?? "", confirmPassword: model.confirmPassword ?? "", dob: model.dob ?? "")
  116. if !validationResult.isValid {
  117. self.output?.show(error: validationResult.error)
  118. return
  119. }
  120. model.uuid = self.getUUid()
  121. model.appVersion = self.getAppVersion()
  122. model.phoneBrand = self.getPhoneBrand()
  123. model.phoneOs = self.getPhoneOs()
  124. model.osVersion = self.getOsVersion()
  125. model.fcmId = self.getFcmToken()
  126. model.clientId = self.getCliendId()
  127. self.service.register(params: model.serialize(), success: { (message) in
  128. self.output?.success(message: message ?? "")
  129. }) { (error) in
  130. self.output?.show(error: error)
  131. }
  132. }
  133. }