// // TrackTransferViewController.swift // GMERemittance // // Created by Fm-user on 2/5/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit class TrackRecipientViewController: UIViewController { @IBOutlet weak var textFieldReceiverName: UITextField! @IBOutlet weak var viewDetails: UIView! @IBOutlet weak var tableViewRecipientDetails: UITableView! @IBOutlet weak var viewMain: UIView! @IBOutlet weak var mainStackView: UIStackView! private var selectedIndex: Int! private var colorCodeRed: CGFloat? private var colorCodeGreen: CGFloat? private var colorCodeBlue: CGFloat? private var timer: Timer? private var trackrecipientviewmodel = TrackRecipientViewModel() private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() override func viewDidAppear(_ animated: Bool) { setUpAnotherLoginListener(genericviewmodel: trackrecipientviewmodel) trackrecipientviewmodel.recipientListObtained.value = nil } override func viewDidLoad() { super.viewDidLoad() setUpNavBar(id: 201, title: "Track Your Transfer") hideKeyboardWhenTappedAround() self.mainStackView.spacing = 1 tableViewRecipientDetails.delegate = self tableViewRecipientDetails.dataSource = self textFieldReceiverName.delegate = self tableViewRecipientDetails.isHidden = true tableViewRecipientDetails.estimatedRowHeight = 80 tableViewRecipientDetails.rowHeight = UITableViewAutomaticDimension trackrecipientviewmodel.trackRecipientTimeOut.value = nil /** connection timeout */ trackrecipientviewmodel.trackRecipientTimeOut.bind { [unowned self] in guard $0 != nil else { return } self.stopLoading() self.popUpMessage(value: 20) } /** Check internet connection */ trackrecipientviewmodel.internetConnection.value = nil trackrecipientviewmodel.internetConnection.bind { [unowned self] in guard $0 != nil else { return } self.popUpMessage(value: 15) self.textFieldReceiverName.resignFirstResponder() self.stopLoading() } /** Get recipient list as per search keyword */ trackrecipientviewmodel.recipientListObtained.bind{ [unowned self] in guard $0 != nil else { return } guard $0! else { return } self.stopLoading() if self.trackrecipientviewmodel.getRecipientCount() == 0 { self.popUpMessageInfo(value: 16, title: "No recipient found", message: "Recipient \(self.textFieldReceiverName.text!) does not exist") } else { self.updateRecipientListUI() } } } /** Disable user interaction while fetching data from api */ func startLoading(){ disableUserInteractions() showActivityIndicator(activityIndicator: activityIndicator) } /** Enable user interaction after fetching data from api */ func stopLoading(){ self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) } /** Refresh table view when collection of data is updated */ func updateRecipientListUI(){ self.tableViewRecipientDetails.isHidden = false self.tableViewRecipientDetails.reloadData() self.tableViewRecipientDetails.tableFooterView = UIView() } /** Redirect list of values to next view */ override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showThreeTransactions" { let trackTransactionViewController = segue.destination as! TrackTransactionViewController trackTransactionViewController.recipient = self.trackrecipientviewmodel.getFilteredRecipientList(index: selectedIndex) trackTransactionViewController.colorCodeRed = colorCodeRed trackTransactionViewController.colorCodeGreen = colorCodeGreen trackTransactionViewController.colorCodeBlue = colorCodeBlue } } /** After user stops typing send an api request for recipient list. */ @objc func recipientSearchApi(){ if let inputText = textFieldReceiverName.text{ trackrecipientviewmodel.clearArray() tableViewRecipientDetails.reloadData() self.startLoading() self.trackrecipientviewmodel.fetchRecipientList(queryString: inputText) } } } extension TrackRecipientViewController: UITextFieldDelegate { func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let inputText = (textField.text! as NSString).replacingCharacters(in: range, with: string) self.timer?.invalidate() if inputText == "" { trackrecipientviewmodel.clearArray() self.tableViewRecipientDetails.isHidden = true } else { self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(TrackRecipientViewController.recipientSearchApi), userInfo: nil, repeats: false) } return true } func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } } extension TrackRecipientViewController: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.trackrecipientviewmodel.getRecipientCount() } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "trackTransferRecipient", for: indexPath) as! TrackRecipientTableViewCell let name = trackrecipientviewmodel.getRecipientName(index: indexPath.row) if name != "nil"{ cell.labelRecipientName.text = name cell.labelRecipientInitial.text = name.prefix(1).uppercased() colorCodeRed = CGFloat(name[name.index(name.startIndex, offsetBy: 0)].asciiValue!)/100.0 * 0.2 colorCodeGreen = CGFloat(name[name.index(name.startIndex, offsetBy: 1)].asciiValue!)/100.0 * 0.8 colorCodeBlue = CGFloat(name[name.index(name.startIndex, offsetBy: 2)].asciiValue!)/100.0 * 0.5 } let phoneNumber = trackrecipientviewmodel.getRecipientPhone(index: indexPath.row) if phoneNumber != "nil"{ cell.labelRecipientPhone.text = phoneNumber } cell.labelRecipientInitial.layer.backgroundColor = UIColor(red: colorCodeRed!, green: colorCodeGreen!, blue: colorCodeBlue!, alpha: 1.0).cgColor cell.labelRecipientInitial.layer.cornerRadius = cell.labelRecipientInitial.frame.height / 2 cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor cell.layer.borderWidth = 5 cell.layer.cornerRadius = 15 cell.clipsToBounds = true return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.selectedIndex = indexPath.row performSegue(withIdentifier: "showThreeTransactions", sender: nil) } }