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.

218 lines
8.2 KiB

6 years ago
6 years ago
  1. //
  2. // TrackTransactionViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 2/6/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class TrackTransactionViewController: UIViewController {
  10. var recipient: Recipient?
  11. var colorCodeRed: CGFloat?
  12. var colorCodeGreen: CGFloat?
  13. var colorCodeBlue: CGFloat?
  14. @IBOutlet weak var labelRecipientName: UILabel!
  15. @IBOutlet weak var labelRecipientPhone: UILabel!
  16. @IBOutlet weak var labelRecipientInitial: UILabel!
  17. @IBOutlet weak var tableViewTransactionDetails: UITableView!
  18. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  19. private var tracktransactionviewmodel = TrackTransactionViewModel()
  20. private var selectedIndex: Int!
  21. public static var checkTransactionListConnectionTimeOut = 0
  22. override func viewDidAppear(_ animated: Bool) {
  23. setUpAnotherLoginListener(genericviewmodel: tracktransactionviewmodel)
  24. }
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. setUpNavBar(id: 201,title: "Track Your Transfer")
  28. tableViewTransactionDetails.tableFooterView = UIView()
  29. guard recipient != nil else {
  30. self.popUpMessageInfo(value: 16, title: "No data received", message: "Try again")
  31. return
  32. }
  33. setRecipientDetailData()
  34. tableViewTransactionDetails.delegate = self
  35. tableViewTransactionDetails.dataSource = self
  36. tableViewTransactionDetails.isHidden = true
  37. tableViewTransactionDetails.estimatedRowHeight = 80
  38. tableViewTransactionDetails.rowHeight = UITableViewAutomaticDimension
  39. tracktransactionviewmodel.transactionListConnectionTimeOut.value = nil
  40. /**
  41. connection timeout
  42. */
  43. tracktransactionviewmodel.transactionListConnectionTimeOut.bind { [unowned self] in
  44. guard $0 != nil else {
  45. return
  46. }
  47. self.stopLoading()
  48. if TrackTransactionViewController.checkTransactionListConnectionTimeOut == 0{
  49. TrackTransactionViewController.checkTransactionListConnectionTimeOut = TrackTransactionViewController.checkTransactionListConnectionTimeOut + 1
  50. self.popUpMessage(value: 46)
  51. }
  52. }
  53. /**
  54. Check internet connection
  55. */
  56. tracktransactionviewmodel.internetConnection.value = nil
  57. tracktransactionviewmodel.internetConnection.bind { [unowned self] in
  58. guard $0 != nil else {
  59. return
  60. }
  61. self.stopLoading()
  62. self.popUpMessage(value: 15)
  63. }
  64. /**
  65. Request an api for list of transaction commit with the recipient
  66. */
  67. self.startLoading()
  68. tracktransactionviewmodel.fetchTransactionListForTrackAndWallet(recipientId: recipient?.recipientId ?? "", recipientName: "", transactionType: "trackTransfer", startDate: nil, endDate: nil, txPage: nil, txSize: nil)
  69. tracktransactionviewmodel.transactionListObtained.bind{ [unowned self] in
  70. guard $0 != nil else {
  71. return
  72. }
  73. self.stopLoading()
  74. guard $0! else {
  75. if TrackTransactionViewController.checkTransactionListConnectionTimeOut == 0{
  76. TrackTransactionViewController.checkTransactionListConnectionTimeOut = TrackTransactionViewController.checkTransactionListConnectionTimeOut + 1
  77. self.popUpMessageError(value: 10, message: self.tracktransactionviewmodel.getErrorMessage())
  78. }
  79. return
  80. }
  81. self.stopLoading()
  82. self.updateTransactionsDetailTableView()
  83. }
  84. }
  85. /**
  86. Update table view as per list of transaction commit with the recipient
  87. */
  88. func updateTransactionsDetailTableView(){
  89. if self.tracktransactionviewmodel.getCount() == 0 {
  90. self.popUpMessageError(value: 10, message: "It seems you have made no transactions yet with this person")
  91. } else {
  92. self.tableViewTransactionDetails.isHidden = false
  93. self.tableViewTransactionDetails.reloadData()
  94. }
  95. }
  96. /**
  97. Display UI with recipient detail data
  98. */
  99. func setRecipientDetailData(){
  100. if (colorCodeRed == nil || colorCodeBlue == nil || colorCodeGreen == nil) {
  101. labelRecipientInitial.layer.backgroundColor = UIColor.gray.cgColor
  102. } else {
  103. labelRecipientInitial.layer.backgroundColor = UIColor(red: colorCodeRed!, green: colorCodeGreen!, blue: colorCodeBlue!, alpha: 1.0).cgColor
  104. }
  105. if let fullName = recipient?.getFullName(){
  106. labelRecipientName.text = fullName
  107. }
  108. if let mobileNumber = recipient?.mobileNumber{
  109. labelRecipientPhone.text = mobileNumber
  110. }
  111. if let firstName = recipient?.firstName{
  112. labelRecipientInitial.text = firstName.prefix(1).uppercased()
  113. }
  114. labelRecipientInitial.layer.cornerRadius = labelRecipientInitial.frame.height / 2
  115. }
  116. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  117. if segue.identifier == "trackStage" {
  118. let trackPhaseViewController = segue.destination as! TrackPhaseViewController
  119. trackPhaseViewController.recipient = self.recipient
  120. trackPhaseViewController.transaction = self.tracktransactionviewmodel.getTransaction(index: selectedIndex)
  121. }
  122. }
  123. /**
  124. Disable user interaction while fetching data from api
  125. */
  126. func startLoading(){
  127. disableUserInteractions()
  128. showActivityIndicator(activityIndicator: activityIndicator)
  129. }
  130. /**
  131. Enable user interaction after fetching data from api
  132. */
  133. func stopLoading(){
  134. self.enableUserInteractions()
  135. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  136. }
  137. }
  138. extension TrackTransactionViewController: UITableViewDelegate, UITableViewDataSource {
  139. func numberOfSections(in tableView: UITableView) -> Int {
  140. return 1
  141. }
  142. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  143. return tracktransactionviewmodel.getCount()
  144. }
  145. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  146. let cell = tableView.dequeueReusableCell(withIdentifier: "trackTransaction", for: indexPath) as! TrackTransactionTableViewCell
  147. let date = self.tracktransactionviewmodel.getDate(index: indexPath.row)
  148. if date != "N/A"{
  149. cell.labelTransactionDate.text = unixTimeStampToDate(unixTimeStamp: date)
  150. }
  151. let recipientName = self.tracktransactionviewmodel.getRecipient(index: indexPath.row)
  152. if recipientName != "N/A"{
  153. cell.labelTransactionRecipient.text = "To: " + firstWord(text: recipientName)
  154. }
  155. let amount = self.tracktransactionviewmodel.getAmount(index: indexPath.row)
  156. if amount != "N/A"{
  157. cell.labelTransactionAmount.text = self.getCommaAddedAmountString(amountString: amount)
  158. }
  159. let transactionMode = self.tracktransactionviewmodel.getPaymentMethodUpdated(index: indexPath.row)
  160. if transactionMode != "N/A"{
  161. cell.labelTransactionMode.text = transactionMode
  162. }
  163. let transactionId = self.tracktransactionviewmodel.getTransactionId(index: indexPath.row)
  164. if transactionId != "N/A"{
  165. cell.labelTransactionID.text = "Transaction No. " + self.tracktransactionviewmodel.getTransactionId(index: indexPath.row)
  166. }
  167. cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor
  168. cell.layer.borderWidth = 5
  169. cell.layer.cornerRadius = 15
  170. cell.clipsToBounds = true
  171. return cell
  172. }
  173. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  174. self.selectedIndex = indexPath.row
  175. performSegue(withIdentifier: "trackStage", sender: nil)
  176. }
  177. }