// // WalletTransactionListViewController.swift // GMERemittance // // Created by Sujal on 3/2/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit class WalletTransactionListViewController: UIViewController { @IBOutlet weak var tableView: UITableView! var walletCell: WalletTransactionTableViewCell? private var tracktransactionviewmodel = TrackTransactionViewModel() private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() var walletStatus: String? private var showLoadMoreButton: Bool = false private var size: String = "10" private var page: Int = 0 private var arrayIndexPath = [IndexPath] () static var notificationCode: String = "" public static var walletListConnectionTimeOutCheck = 0 /** Call transaction api */ func fetchTransactions() { self.startLoading() tracktransactionviewmodel.fetchTransactionListForTrackAndWallet(recipientId: "", recipientName: "", transactionType: walletStatus!, startDate: nil, endDate: nil, txPage: String(page), txSize: size) } /** Check internet connection */ func setUpNetworkListener() { tracktransactionviewmodel.internetConnection.value = nil tracktransactionviewmodel.internetConnection.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.popUpMessage(value: 15) self.tracktransactionviewmodel.internetConnection.value = nil } } /** Api listener */ func setUpTransactionListListener() { tracktransactionviewmodel.transactionListObtained.bind{ [unowned self] in guard $0 != nil else { return } self.stopLoading() guard $0! else { if WalletTransactionListViewController.walletListConnectionTimeOutCheck == 0{ WalletTransactionListViewController.walletListConnectionTimeOutCheck = WalletTransactionListViewController.walletListConnectionTimeOutCheck+1 self.popUpMessageError(value: 10, message: self.tracktransactionviewmodel.getErrorMessage()) } return } let count = self.tracktransactionviewmodel.getCount() if count < Int(self.size)! || count == 0 { self.showLoadMoreButton = false if count == 0 { self.tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: .none) } } else { self.showLoadMoreButton = true } if count > 0 { self.arrayIndexPath.removeAll() self.tableView.beginUpdates() let totalCount = self.tracktransactionviewmodel.getTotalCount() for i in 0...count-1 { self.arrayIndexPath.append(IndexPath.init(row: totalCount - count + i, section: 0)) } self.tableView.insertRows(at: self.arrayIndexPath, with: .automatic) self.tableView.endUpdates() } } } override func viewDidLoad() { super.viewDidLoad() if WalletTransactionListViewController.notificationCode == "p202" || WalletTransactionListViewController.notificationCode == "p205"{ setUpNavBar(id: 208, title: "Wallet to Wallet Transfer") }else { setUpNavBar(id: 204, title: "Wallet to Wallet Transfer") } guard walletStatus != nil else { return } tableView.tableFooterView = UIView() self.tableView.delegate = self self.tableView.dataSource = self self.tableView.estimatedRowHeight = 82 self.tableView.rowHeight = UITableViewAutomaticDimension setUpAnotherLoginListener(genericviewmodel: tracktransactionviewmodel) tracktransactionviewmodel.transactionListConnectionTimeOut.value = nil /** connection timeout */ print("wallet status \(String(describing: walletStatus))") tracktransactionviewmodel.transactionListConnectionTimeOut.bind { [unowned self] in guard $0 != nil else { return } self.stopLoading() if self.walletStatus != "walletTransfer"{ if self.walletStatus != "walletBorrow"{ if WalletTransactionListViewController.walletListConnectionTimeOutCheck == 0{ WalletTransactionListViewController.walletListConnectionTimeOutCheck = WalletTransactionListViewController.walletListConnectionTimeOutCheck+1 self.popUpMessage(value: 32) } } } } setUpNetworkListener() setUpTransactionListListener() fetchTransactions() } /** Disable user interaction while fetching data from api */ func startLoading(){ self.showActivityIndicator(activityIndicator: self.activityIndicator) self.disableUserInteractions() } /** Enable user interaction while fetching data from api */ func stopLoading(){ self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.enableUserInteractions() } } extension WalletTransactionListViewController: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 2 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case 0: return tracktransactionviewmodel.getTotalCount() default: return 1 } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch indexPath.section { case 0: walletCell = tableView.dequeueReusableCell(withIdentifier: "walletCell", for: indexPath) as? WalletTransactionTableViewCell walletCell?.labelAmount.text = getCommaAddedAmountString(amountString: tracktransactionviewmodel.getAmountForWallet(index: indexPath.row)) if tracktransactionviewmodel.getDateForWallet(index: indexPath.row) != "N/A"{ walletCell?.labelDate.text = unixTimeStampToDate(unixTimeStamp: tracktransactionviewmodel.getDateForWallet(index: indexPath.row)) } if tracktransactionviewmodel.getRecipientForWallet(index: indexPath.row) != "N/A"{ walletCell?.labelName.text = "To: " + tracktransactionviewmodel.getRecipientForWallet(index: indexPath.row) } if tracktransactionviewmodel.getAmountForWallet(index: indexPath.row) != "N/A"{ walletCell?.labelAmount.text = getCommaAddedAmountString(amountString: tracktransactionviewmodel.getAmountForWallet(index: indexPath.row)) } if tracktransactionviewmodel.getTransactionIdForWallet(index: indexPath.row) != "N/A"{ walletCell?.labelTransactionNumber.text = "Transaction No. " + tracktransactionviewmodel.getTransactionIdForWallet(index: indexPath.row) } if tracktransactionviewmodel.getPaymentMethodUpdatedForWallet(index: indexPath.row) != "N/A"{ walletCell?.labelPayoutMethod.text = tracktransactionviewmodel.getPaymentMethodUpdatedForWallet(index: indexPath.row) } if walletStatus == "walletBorrow" { walletCell?.labelName.text = "To: " + tracktransactionviewmodel.getSenderForWallet(index: indexPath.row) if self.tracktransactionviewmodel.getBorrowStatusForWallet(index: indexPath.row) != "N/A"{ let status = self.tracktransactionviewmodel.getBorrowStatusForWallet(index: indexPath.row) if status != "N/A"{ walletCell?.labelStatus.text = status.capitalizeFirstLetter() switch status.lowercased() { case "paid": walletCell?.labelAmount.textColor = UIColor(hex: 0x2e3192) walletCell?.labelStatus.textColor = UIColor.white walletCell?.labelStatus.backgroundColor = UIColor(hex: 0xec1c24) walletCell?.labelStatus.clipsToBounds = true walletCell?.labelStatus.layer.cornerRadius = 5.0 case "rejected": walletCell?.labelAmount.textColor = UIColor(hex: 0xec1c24) walletCell?.labelStatus.textColor = UIColor(hex: 0xec1c24) walletCell?.labelStatus.backgroundColor = UIColor(hex: 0xd6e3ee) walletCell?.labelStatus.clipsToBounds = true walletCell?.labelStatus.layer.cornerRadius = 5.0 case "pending": walletCell?.labelAmount.textColor = UIColor(hex: 0x4a4a4a) walletCell?.labelStatus.textColor = UIColor.white walletCell?.labelStatus.backgroundColor = UIColor(hex: 0x2e3192) walletCell?.labelStatus.clipsToBounds = true walletCell?.labelStatus.layer.cornerRadius = 5.0 default: walletCell?.labelAmount.isHidden = true walletCell?.labelStatus.isHidden = true } } } } else { walletCell?.labelStatus.isHidden = true walletCell?.labelAmount.textColor = UIColor(hex: 0xec1c24) } return walletCell! default: let loadMoreCell = tableView.dequeueReusableCell(withIdentifier: "walletLoadMore", for: indexPath) as! WalletLoadMoreTableViewCell loadMoreCell.buttonLoadMore.addTarget(self, action: #selector(loadMore(sender:)), for: .touchUpInside) if showLoadMoreButton { loadMoreCell.buttonLoadMore.isHidden = false } return loadMoreCell } } @objc func loadMore(sender: WalletLoadMoreTableViewCell) { page = page + 1 fetchTransactions() } }