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.

214 lines
6.9 KiB

6 years ago
  1. //
  2. // InviteViewController.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 InviteViewController: UIViewController {
  10. public var bonusStatus: Bool?
  11. public static var inviteStatus: Bool?
  12. public static var notificationCode = ""
  13. private var inviteviewmodel = InviteViewModel()
  14. @IBOutlet weak var labelNumberRefer: UILabel!
  15. @IBOutlet weak var labelEarnedAmount: UILabel!
  16. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  17. public static var inviteConnectionTimeOutCheck = 0
  18. public var fromNotification: Int?
  19. override func viewWillAppear(_ animated: Bool) {
  20. self.tabBarController?.navigationController?.navigationBar.barTintColor = UIColor(hex:0xEC1C24)
  21. }
  22. /**
  23. Request once again. After submitting a form view, redirect back to this view and provide updated number of count related to invitation.
  24. */
  25. override func viewDidAppear(_ animated: Bool) {
  26. setUpAnotherLoginListener(genericviewmodel: inviteviewmodel)
  27. setUpNetworkListener()
  28. if (!(UserDefaults.standard.object(forKey: "com.gmeremit.isVerified") as! Bool)) {
  29. popUpMessage(value: 14)
  30. enableUserInteractions()
  31. }
  32. if (InviteViewController.inviteStatus != nil) && InviteViewController.inviteStatus == true {
  33. disableUserInteractions()
  34. showActivityIndicator(activityIndicator: activityIndicator)
  35. inviteviewmodel.fetchInvitee()
  36. }
  37. }
  38. /**
  39. Check internet connection
  40. */
  41. func setUpNetworkListener() {
  42. inviteviewmodel.internetConnection.value = nil
  43. inviteviewmodel.internetConnection.bind { [unowned self] in
  44. guard $0 != nil else {
  45. return
  46. }
  47. self.stopLoading()
  48. self.popUpMessage(value: 15)
  49. }
  50. }
  51. override func viewDidLoad() {
  52. super.viewDidLoad()
  53. /**
  54. Nav Bar as per notification type i.e. push and internal notification
  55. */
  56. inviteviewmodel.inviteeConnectionTimeOut.value = nil
  57. /**
  58. connection timeout
  59. */
  60. inviteviewmodel.inviteeConnectionTimeOut.bind { [unowned self] in
  61. guard $0 != nil else {
  62. return
  63. }
  64. self.stopLoading()
  65. if self.fromNotification != 0{
  66. if InviteViewController.inviteConnectionTimeOutCheck == 0{
  67. InviteViewController.inviteConnectionTimeOutCheck = InviteViewController.inviteConnectionTimeOutCheck+1
  68. self.popUpMessage(value: 36)
  69. }
  70. }
  71. }
  72. if InviteViewController.notificationCode == "500" {
  73. setUpNavBar(id: 201, title: "Invite")
  74. } else if InviteViewController.notificationCode == "p500"{
  75. setUpNavBar(id: 207, title: "Invite")
  76. }else {
  77. setUpNavBar(id: 202,title: "")
  78. }
  79. if !Reachability.isConnectedToNetwork() {
  80. self.stopLoading()
  81. self.popUpMessage(value: 15)
  82. } else {
  83. /**
  84. Request Invitee Api to fetch number of invitation and total earned values
  85. */
  86. self.startLoading()
  87. inviteviewmodel.fetchInvitee()
  88. inviteviewmodel.inviteeFetch.bind{ [unowned self] in
  89. guard $0 != nil else {
  90. return
  91. }
  92. guard $0! else {
  93. self.stopLoading()
  94. self.popUpMessageError(value: 10, message: self.inviteviewmodel.getErrorMessage())
  95. return
  96. }
  97. self.stopLoading()
  98. /**
  99. Update UI only when api respond with data
  100. */
  101. if (self.inviteviewmodel.getInviteeArray().count > 0){
  102. self.viewUpdate()
  103. }else{
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. Provide number of invitations and total earned values.
  110. Count Invitation and Total Earned values as per the status.
  111. */
  112. func viewUpdate(){
  113. var inviteePendingCount: Int = 0
  114. var inviteeActiveCount: Int = 0
  115. for i in inviteviewmodel.getInviteeArray()[0..<inviteviewmodel.getCount()]{
  116. if i.status == "pending" {
  117. inviteePendingCount = inviteePendingCount + 1
  118. } else if i.status == "active" {
  119. inviteeActiveCount = inviteeActiveCount + 1
  120. } else {
  121. }
  122. }
  123. self.labelNumberRefer.text = String(inviteePendingCount)
  124. self.labelEarnedAmount.text = "+" + String(inviteeActiveCount * 7000)
  125. }
  126. /**
  127. Disable user interaction while fetching data from api
  128. */
  129. func startLoading(){
  130. disableUserInteractions()
  131. showActivityIndicator(activityIndicator: activityIndicator)
  132. }
  133. /**
  134. Enable user interaction after fetching data from api
  135. */
  136. func stopLoading(){
  137. self.enableUserInteractions()
  138. if self.activityIndicator.isAnimating {
  139. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  140. }
  141. }
  142. /**
  143. Redirect to view name ReferringViewController. A user can refer app to others through.
  144. */
  145. @IBAction func buttonInviteTap(_ sender: Any) {
  146. performSegue(withIdentifier: "refering", sender: nil)
  147. }
  148. /**
  149. Redirect to view name InviteViewController.
  150. A user can view the list of referred people who still haven't used the app.
  151. */
  152. @IBAction func pendingTap(_ sender: UITapGestureRecognizer) {
  153. InviteViewController.inviteStatus = false
  154. bonusStatus = true
  155. performSegue(withIdentifier: "earned", sender: nil)
  156. }
  157. /**
  158. Redirect to view name InviteViewController.
  159. A user can view the list of referred people who still haven't used the app.
  160. */
  161. @IBAction func earnedTap(_ sender: UITapGestureRecognizer) {
  162. InviteViewController.inviteStatus = false
  163. bonusStatus = false
  164. performSegue(withIdentifier: "earned", sender: nil)
  165. }
  166. override func didReceiveMemoryWarning() {
  167. super.didReceiveMemoryWarning()
  168. // Dispose of any resources that can be recreated.
  169. }
  170. /**
  171. Redirect to EarnedViewController. If var bonusStatus is true then redirect to Pending segment else redirect to Earned segment.
  172. */
  173. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  174. if (segue.identifier == "earned") {
  175. let vc = segue.destination as! EarnedViewController
  176. vc.bonusStatus = bonusStatus
  177. vc.inviteeArray = inviteviewmodel.getInviteeArray().reversed()
  178. }
  179. }
  180. }