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.

247 lines
7.3 KiB

6 years ago
  1. //
  2. // ReferringViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by FMI-12 on 2/28/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class ReferringViewController: UIViewController {
  10. @IBOutlet weak var textFieldFullName: UITextField!
  11. @IBOutlet weak var textFieldMobileNo: UITextField!
  12. @IBOutlet weak var textFieldEmail: UITextField!
  13. @IBOutlet weak var viewContent: UIView!
  14. private var movement: CGFloat = 0.0
  15. private var activeTextField: UITextField?
  16. private var isScrolled: Bool = false
  17. public let inviteviewmodel = InviteViewModel()
  18. private var invitee: InviteeModel?
  19. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  20. override func viewDidAppear(_ animated: Bool) {
  21. setUpAnotherLoginListener(genericviewmodel: inviteviewmodel)
  22. }
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. setUpNavBar(id: 201,title: "Invite")
  26. registerTapListener()
  27. textFieldFullName.autocorrectionType = .no
  28. textFieldMobileNo.autocorrectionType = .no
  29. textFieldEmail.autocorrectionType = .no
  30. textFieldFullName.delegate = self
  31. textFieldMobileNo.delegate = self
  32. textFieldEmail.delegate = self
  33. inviteviewmodel.inviteeConnectionTimeOut.value = nil
  34. /**
  35. connection timeout
  36. */
  37. inviteviewmodel.inviteeConnectionTimeOut.bind { [unowned self] in
  38. guard $0 != nil else {
  39. return
  40. }
  41. self.stopLoading()
  42. self.popUpMessage(value: 20)
  43. }
  44. /**
  45. Check internet connection
  46. */
  47. inviteviewmodel.internetConnection.value = nil
  48. inviteviewmodel.internetConnection.bind { [unowned self] in
  49. guard $0 != nil else {
  50. return
  51. }
  52. self.stopLoading()
  53. self.popUpMessage(value: 15)
  54. }
  55. setUpListener()
  56. }
  57. /**
  58. Disable user interaction while fetching data from api
  59. */
  60. func startLoading(){
  61. disableUserInteractions()
  62. showActivityIndicator(activityIndicator: activityIndicator)
  63. }
  64. /**
  65. Enable user interaction after fetching data from api
  66. */
  67. func stopLoading(){
  68. self.enableUserInteractions()
  69. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  70. }
  71. /**
  72. UI invite button action
  73. */
  74. @IBAction func buttomInviteTap(_ sender: Any) {
  75. InviteViewController.inviteStatus = true
  76. activeTextField?.resignFirstResponder()
  77. textFieldValidation()
  78. }
  79. /**
  80. Check empty textfield validation
  81. */
  82. func textFieldValidation(){
  83. let textFieldArray = [textFieldFullName.text, textFieldMobileNo.text, textFieldEmail.text]
  84. guard inviteviewmodel.allFieldsFilled(arrayofInfo: textFieldArray as! [String]) else {
  85. self.popUpMessageError(value: 11, message: "Please fill all the fields")
  86. return
  87. }
  88. self.startLoading()
  89. inviteviewmodel.createInvitee(name: textFieldFullName.text!, mobileNumber:
  90. textFieldMobileNo.text!, email: textFieldEmail.text!)
  91. }
  92. /**
  93. Submit an invitation
  94. */
  95. func setUpListener() {
  96. inviteviewmodel.inviteeCreated.bind { [unowned self] in
  97. guard $0 != nil else {
  98. return
  99. }
  100. guard $0! else {
  101. self.stopLoading()
  102. self.popUpMessageError(value: 10, message: self.inviteviewmodel.getErrorMessage())
  103. return
  104. }
  105. self.stopLoading()
  106. self.invitee = self.inviteviewmodel.getCreatedInvitee()
  107. self.popUpMessageInvite(value: 12)
  108. self.textFieldFullName.text = ""
  109. self.textFieldMobileNo.text = ""
  110. self.textFieldEmail.text = ""
  111. }
  112. }
  113. /**
  114. Dialog with a submission message.
  115. */
  116. func popUpMessageInvite(value: Int){
  117. guard let navController = self.navigationController else { return }
  118. let popUpViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popUpInfo") as! PopUpGeneralInfo
  119. popUpViewController.indexValue = value
  120. popUpViewController.inviteeName = textFieldFullName.text
  121. navController.addChildViewController(popUpViewController)
  122. popUpViewController.view.frame = navController.view.bounds
  123. navController.view.addSubview(popUpViewController.view)
  124. popUpViewController.didMove(toParentViewController: navController)
  125. }
  126. override func didReceiveMemoryWarning() {
  127. super.didReceiveMemoryWarning()
  128. // Dispose of any resources that can be recreated.
  129. }
  130. }
  131. extension ReferringViewController: UITextFieldDelegate {
  132. func textFieldDidBeginEditing(_ textField: UITextField) {
  133. activeTextField = textField
  134. checkScroll()
  135. }
  136. func textFieldDidEndEditing(_ textField: UITextField) {
  137. checkScroll()
  138. }
  139. /**
  140. The text field calls this method whenever the user taps the return button in keypad.
  141. */
  142. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  143. if textField == textFieldFullName {
  144. textFieldMobileNo.becomeFirstResponder()
  145. } else if textField == textFieldMobileNo {
  146. textFieldEmail.becomeFirstResponder()
  147. } else {
  148. textFieldEmail.resignFirstResponder()
  149. }
  150. return true
  151. }
  152. }
  153. extension ReferringViewController: ScrollableProtocol {
  154. /**
  155. - Returns offset value to animate
  156. */
  157. var offset: CGFloat {
  158. get {
  159. return 50
  160. }
  161. }
  162. /**
  163. - Returns if var isScrolled is false then scrolling is inactive and vice-versa.
  164. */
  165. var viewScrolled: Bool {
  166. get {
  167. return isScrolled
  168. }
  169. }
  170. /**
  171. Check if textfield is active and required scroll
  172. */
  173. func checkScroll() {
  174. if !viewScrolled {
  175. performScroll(direction: 0)
  176. isScrolled = !isScrolled
  177. } else {
  178. performScroll(direction: 1)
  179. isScrolled = !isScrolled
  180. }
  181. }
  182. /**
  183. Register tap listener
  184. */
  185. func registerTapListener() {
  186. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(clearKeyboard))
  187. view.addGestureRecognizer(tap)
  188. }
  189. /**
  190. Hide keypad when ever user tap on screen condition when keypad is active
  191. */
  192. @objc func clearKeyboard() {
  193. activeTextField?.resignFirstResponder()
  194. if viewScrolled {
  195. performScroll(direction: 1)
  196. isScrolled = !isScrolled
  197. }
  198. }
  199. /**
  200. Animate scroll UI in case when textfield is active and also if inactive.
  201. */
  202. func performScroll(direction: Int) {
  203. if direction == 0 {
  204. UIView.animate(withDuration: 0.3, animations: {
  205. self.view.frame = self.view.frame.offsetBy(dx: 0, dy: self.offset * -1)
  206. })
  207. } else if direction == 1 {
  208. UIView.animate(withDuration: 0.3, animations: {
  209. self.view.frame = self.view.frame.offsetBy(dx: 0, dy: self.offset)
  210. })
  211. }
  212. }
  213. }