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.

248 lines
7.3 KiB

6 years ago
  1. //
  2. // SignUpViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Kushal on 11/30/17.
  6. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class SignUpViewController: UIViewController {
  10. var isScrolled: Bool = false
  11. private var activeTextField: UITextField?
  12. private var signupviewmodel = SignUpViewModel()
  13. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  14. @IBOutlet weak var textFieldUsername: UITextField!
  15. @IBOutlet weak var textFieldPassword: UITextField!
  16. @IBOutlet weak var textFieldPasswordConfirm: UITextField!
  17. @IBOutlet weak var viewContent: UIView!
  18. @IBAction func initialSignUp(_ sender: Any) {
  19. activeTextField?.resignFirstResponder()
  20. if viewScrolled {
  21. performScroll(direction: 1)
  22. isScrolled = !isScrolled
  23. }
  24. signupviewmodel.signUpConnectionTimeOut.value = nil
  25. /**
  26. connection timeout
  27. */
  28. signupviewmodel.signUpConnectionTimeOut.bind { [unowned self] in
  29. guard $0 != nil else {
  30. return
  31. }
  32. self.enableUserInteractions()
  33. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  34. self.popUpMessage(value: 20)
  35. }
  36. disableUserInteractions()
  37. showActivityIndicator(activityIndicator: activityIndicator)
  38. signupviewmodel.setUserCredentials(username: textFieldUsername.text!, password: textFieldPassword.text!, confirmpassword: textFieldPasswordConfirm.text!)
  39. switch signupviewmodel.validateUser() {
  40. case .Valid:
  41. signupviewmodel.register()
  42. case .InValid(let error):
  43. enableUserInteractions()
  44. dismissActivityIndicator(activityIndicator: activityIndicator)
  45. self.popUpMessageError(value: 11, message: error)
  46. }
  47. }
  48. @objc func appMovedToForeground() {
  49. activeTextField?.resignFirstResponder()
  50. if isScrolled {
  51. isScrolled = !isScrolled
  52. }
  53. }
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. setUpNavBar(id: 100, title: "")
  57. NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
  58. registerTapListener()
  59. self.view.clipsToBounds = true
  60. textFieldUsername.autocorrectionType = .no
  61. textFieldPassword.autocorrectionType = .no
  62. textFieldPasswordConfirm.autocorrectionType = .no
  63. textFieldUsername.delegate = self
  64. textFieldPassword.delegate = self
  65. textFieldPasswordConfirm.delegate = self
  66. textFieldUsername.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor
  67. textFieldUsername.layer.borderWidth = 1.0
  68. textFieldUsername.layer.cornerRadius = 5
  69. textFieldPassword.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor
  70. textFieldPassword.layer.borderWidth = 1.0
  71. textFieldPassword.layer.cornerRadius = 5
  72. textFieldPasswordConfirm.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor
  73. textFieldPasswordConfirm.layer.borderWidth = 1.0
  74. textFieldPasswordConfirm.layer.cornerRadius = 5
  75. /**
  76. Internet Check
  77. */
  78. signupviewmodel.internetConnection.bind { [unowned self] in
  79. guard $0 != nil else {
  80. return
  81. }
  82. self.enableUserInteractions()
  83. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  84. self.popUpMessage(value: 15)
  85. }
  86. /**
  87. Update user registration info
  88. */
  89. signupviewmodel.registered.bind { [unowned self] in
  90. guard $0 != nil else {
  91. return
  92. }
  93. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  94. self.enableUserInteractions()
  95. guard $0! else {
  96. self.popUpMessageError(value: 10, message: self.signupviewmodel.getErrorMessage())
  97. return
  98. }
  99. self.performSegue(withIdentifier: "fillOTP", sender: nil)
  100. }
  101. }
  102. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  103. if segue.identifier == "fillOTP" {
  104. let fillcodeViewController = segue.destination as! FillSignUpCodeController
  105. fillcodeViewController.deviceORpassword = 1
  106. }
  107. }
  108. override func didReceiveMemoryWarning() {
  109. super.didReceiveMemoryWarning()
  110. }
  111. }
  112. // UITextFieldDelegate and Scroll Protocol goes here
  113. extension SignUpViewController: UITextFieldDelegate {
  114. func textFieldDidBeginEditing(_ textField: UITextField) {
  115. activeTextField = textField
  116. checkScroll()
  117. }
  118. func textFieldDidEndEditing(_ textField: UITextField) {
  119. checkScroll()
  120. }
  121. /**
  122. Curser move to next textfield when return button click in keyboad
  123. */
  124. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  125. if textField == textFieldUsername {
  126. textFieldPassword.becomeFirstResponder()
  127. } else if textField == textFieldPassword {
  128. textFieldPasswordConfirm.becomeFirstResponder()
  129. } else {
  130. textFieldPasswordConfirm.resignFirstResponder()
  131. }
  132. return true
  133. }
  134. }
  135. extension SignUpViewController: ScrollableProtocol {
  136. /**
  137. - Returns offset value to animate
  138. */
  139. var offset: CGFloat {
  140. get {
  141. return viewContent.frame.origin.y - 5
  142. }
  143. }
  144. /**
  145. - Returns Bool value to scroll
  146. */
  147. var viewScrolled: Bool {
  148. get {
  149. return isScrolled
  150. }
  151. }
  152. /**
  153. Check if textfield is active and required scroll
  154. */
  155. func checkScroll() {
  156. if !viewScrolled {
  157. performScroll(direction: 0)
  158. isScrolled = !isScrolled
  159. } else {
  160. performScroll(direction: 1)
  161. isScrolled = !isScrolled
  162. }
  163. }
  164. /**
  165. Dissmiss keyboard when tap outside to textfield
  166. */
  167. func registerTapListener() {
  168. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(clearKeyboard))
  169. view.addGestureRecognizer(tap)
  170. }
  171. /**
  172. Dissmiss keyboard
  173. */
  174. @objc func clearKeyboard() {
  175. activeTextField?.resignFirstResponder()
  176. if viewScrolled {
  177. performScroll(direction: 1)
  178. isScrolled = !isScrolled
  179. }
  180. }
  181. /**
  182. Animate scroll UI in case when textfield is active and also if inactive.
  183. */
  184. func performScroll(direction: Int) {
  185. if direction == 0 {
  186. UIView.animate(withDuration: 0.3, animations: {
  187. self.view.frame = self.view.frame.offsetBy(dx: 0, dy: self.offset * -1)
  188. })
  189. } else if direction == 1 {
  190. UIView.animate(withDuration: 0.3, animations: {
  191. self.view.frame = self.view.frame.offsetBy(dx: 0, dy: self.offset)
  192. })
  193. }
  194. }
  195. }