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.

270 lines
11 KiB

6 years ago
  1. //
  2. // WalletTransactionListViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 3/2/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class WalletTransactionListViewController: UIViewController {
  10. @IBOutlet weak var tableView: UITableView!
  11. var walletCell: WalletTransactionTableViewCell?
  12. private var tracktransactionviewmodel = TrackTransactionViewModel()
  13. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  14. var walletStatus: String?
  15. private var showLoadMoreButton: Bool = false
  16. private var size: String = "10"
  17. private var page: Int = 0
  18. private var arrayIndexPath = [IndexPath] ()
  19. static var notificationCode: String = ""
  20. public static var walletListConnectionTimeOutCheck = 0
  21. /**
  22. Call transaction api
  23. */
  24. func fetchTransactions() {
  25. self.startLoading()
  26. tracktransactionviewmodel.fetchTransactionListForTrackAndWallet(recipientId: "", recipientName: "", transactionType: walletStatus!, startDate: nil, endDate: nil, txPage: String(page), txSize: size)
  27. }
  28. /**
  29. Check internet connection
  30. */
  31. func setUpNetworkListener() {
  32. tracktransactionviewmodel.internetConnection.value = nil
  33. tracktransactionviewmodel.internetConnection.bind { [unowned self] in
  34. guard $0 != nil else {
  35. return
  36. }
  37. self.enableUserInteractions()
  38. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  39. self.popUpMessage(value: 15)
  40. self.tracktransactionviewmodel.internetConnection.value = nil
  41. }
  42. }
  43. /**
  44. Api listener
  45. */
  46. func setUpTransactionListListener() {
  47. tracktransactionviewmodel.transactionListObtained.bind{ [unowned self] in
  48. guard $0 != nil else {
  49. return
  50. }
  51. self.stopLoading()
  52. guard $0! else {
  53. if WalletTransactionListViewController.walletListConnectionTimeOutCheck == 0{
  54. WalletTransactionListViewController.walletListConnectionTimeOutCheck = WalletTransactionListViewController.walletListConnectionTimeOutCheck+1
  55. self.popUpMessageError(value: 10, message: self.tracktransactionviewmodel.getErrorMessage())
  56. }
  57. return
  58. }
  59. let count = self.tracktransactionviewmodel.getCount()
  60. if count < Int(self.size)! || count == 0 {
  61. self.showLoadMoreButton = false
  62. if count == 0 {
  63. self.tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: .none)
  64. }
  65. } else {
  66. self.showLoadMoreButton = true
  67. }
  68. if count > 0 {
  69. self.arrayIndexPath.removeAll()
  70. self.tableView.beginUpdates()
  71. let totalCount = self.tracktransactionviewmodel.getTotalCount()
  72. for i in 0...count-1 {
  73. self.arrayIndexPath.append(IndexPath.init(row: totalCount - count + i, section: 0))
  74. }
  75. self.tableView.insertRows(at: self.arrayIndexPath, with: .automatic)
  76. self.tableView.endUpdates()
  77. }
  78. }
  79. }
  80. override func viewDidLoad() {
  81. super.viewDidLoad()
  82. if WalletTransactionListViewController.notificationCode == "p202" || WalletTransactionListViewController.notificationCode == "p205"{
  83. setUpNavBar(id: 208, title: "Wallet to Wallet Transfer")
  84. }else {
  85. setUpNavBar(id: 204, title: "Wallet to Wallet Transfer")
  86. }
  87. guard walletStatus != nil else {
  88. return
  89. }
  90. tableView.tableFooterView = UIView()
  91. self.tableView.delegate = self
  92. self.tableView.dataSource = self
  93. self.tableView.estimatedRowHeight = 82
  94. self.tableView.rowHeight = UITableViewAutomaticDimension
  95. setUpAnotherLoginListener(genericviewmodel: tracktransactionviewmodel)
  96. tracktransactionviewmodel.transactionListConnectionTimeOut.value = nil
  97. /**
  98. connection timeout
  99. */
  100. print("wallet status \(String(describing: walletStatus))")
  101. tracktransactionviewmodel.transactionListConnectionTimeOut.bind { [unowned self] in
  102. guard $0 != nil else {
  103. return
  104. }
  105. self.stopLoading()
  106. if self.walletStatus != "walletTransfer"{
  107. if self.walletStatus != "walletBorrow"{
  108. if WalletTransactionListViewController.walletListConnectionTimeOutCheck == 0{
  109. WalletTransactionListViewController.walletListConnectionTimeOutCheck = WalletTransactionListViewController.walletListConnectionTimeOutCheck+1
  110. self.popUpMessage(value: 32)
  111. }
  112. }
  113. }
  114. }
  115. setUpNetworkListener()
  116. setUpTransactionListListener()
  117. fetchTransactions()
  118. }
  119. /**
  120. Disable user interaction while fetching data from api
  121. */
  122. func startLoading(){
  123. self.showActivityIndicator(activityIndicator: self.activityIndicator)
  124. self.disableUserInteractions()
  125. }
  126. /**
  127. Enable user interaction while fetching data from api
  128. */
  129. func stopLoading(){
  130. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  131. self.enableUserInteractions()
  132. }
  133. }
  134. extension WalletTransactionListViewController: UITableViewDelegate, UITableViewDataSource {
  135. func numberOfSections(in tableView: UITableView) -> Int {
  136. return 2
  137. }
  138. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  139. switch section {
  140. case 0:
  141. return tracktransactionviewmodel.getTotalCount()
  142. default:
  143. return 1
  144. }
  145. }
  146. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  147. switch indexPath.section {
  148. case 0:
  149. walletCell = tableView.dequeueReusableCell(withIdentifier: "walletCell", for: indexPath) as? WalletTransactionTableViewCell
  150. walletCell?.labelAmount.text = getCommaAddedAmountString(amountString: tracktransactionviewmodel.getAmountForWallet(index: indexPath.row))
  151. if tracktransactionviewmodel.getDateForWallet(index: indexPath.row) != "N/A"{
  152. walletCell?.labelDate.text = unixTimeStampToDate(unixTimeStamp: tracktransactionviewmodel.getDateForWallet(index: indexPath.row))
  153. }
  154. if tracktransactionviewmodel.getRecipientForWallet(index: indexPath.row) != "N/A"{
  155. walletCell?.labelName.text = "To: " + tracktransactionviewmodel.getRecipientForWallet(index: indexPath.row)
  156. }
  157. if tracktransactionviewmodel.getAmountForWallet(index: indexPath.row) != "N/A"{
  158. walletCell?.labelAmount.text = getCommaAddedAmountString(amountString: tracktransactionviewmodel.getAmountForWallet(index: indexPath.row))
  159. }
  160. if tracktransactionviewmodel.getTransactionIdForWallet(index: indexPath.row) != "N/A"{
  161. walletCell?.labelTransactionNumber.text = "Transaction No. " + tracktransactionviewmodel.getTransactionIdForWallet(index: indexPath.row)
  162. }
  163. if tracktransactionviewmodel.getPaymentMethodUpdatedForWallet(index: indexPath.row) != "N/A"{
  164. walletCell?.labelPayoutMethod.text = tracktransactionviewmodel.getPaymentMethodUpdatedForWallet(index: indexPath.row)
  165. }
  166. if walletStatus == "walletBorrow" {
  167. walletCell?.labelName.text = "To: " + tracktransactionviewmodel.getSenderForWallet(index: indexPath.row)
  168. if self.tracktransactionviewmodel.getBorrowStatusForWallet(index: indexPath.row) != "N/A"{
  169. let status = self.tracktransactionviewmodel.getBorrowStatusForWallet(index: indexPath.row)
  170. if status != "N/A"{
  171. walletCell?.labelStatus.text = status.capitalizeFirstLetter()
  172. switch status.lowercased() {
  173. case "paid":
  174. walletCell?.labelAmount.textColor = UIColor(hex: 0x2e3192)
  175. walletCell?.labelStatus.textColor = UIColor.white
  176. walletCell?.labelStatus.backgroundColor = UIColor(hex: 0xec1c24)
  177. walletCell?.labelStatus.clipsToBounds = true
  178. walletCell?.labelStatus.layer.cornerRadius = 5.0
  179. case "rejected":
  180. walletCell?.labelAmount.textColor = UIColor(hex: 0xec1c24)
  181. walletCell?.labelStatus.textColor = UIColor(hex: 0xec1c24)
  182. walletCell?.labelStatus.backgroundColor = UIColor(hex: 0xd6e3ee)
  183. walletCell?.labelStatus.clipsToBounds = true
  184. walletCell?.labelStatus.layer.cornerRadius = 5.0
  185. case "pending":
  186. walletCell?.labelAmount.textColor = UIColor(hex: 0x4a4a4a)
  187. walletCell?.labelStatus.textColor = UIColor.white
  188. walletCell?.labelStatus.backgroundColor = UIColor(hex: 0x2e3192)
  189. walletCell?.labelStatus.clipsToBounds = true
  190. walletCell?.labelStatus.layer.cornerRadius = 5.0
  191. default:
  192. walletCell?.labelAmount.isHidden = true
  193. walletCell?.labelStatus.isHidden = true
  194. }
  195. }
  196. }
  197. }
  198. else {
  199. walletCell?.labelStatus.isHidden = true
  200. walletCell?.labelAmount.textColor = UIColor(hex: 0xec1c24)
  201. }
  202. return walletCell!
  203. default:
  204. let loadMoreCell = tableView.dequeueReusableCell(withIdentifier: "walletLoadMore", for: indexPath) as! WalletLoadMoreTableViewCell
  205. loadMoreCell.buttonLoadMore.addTarget(self, action: #selector(loadMore(sender:)), for: .touchUpInside)
  206. if showLoadMoreButton {
  207. loadMoreCell.buttonLoadMore.isHidden = false
  208. }
  209. return loadMoreCell
  210. }
  211. }
  212. @objc func loadMore(sender: WalletLoadMoreTableViewCell) {
  213. page = page + 1
  214. fetchTransactions()
  215. }
  216. }