// // PayoutViewController.swift // GMERemittance // // Created by Fm-user on 2/7/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit class PayoutViewController: UIViewController { @IBOutlet weak var searchbar: UISearchBar! private var searchActive : Bool = false private var agentviewmodel = AgentViewModel() private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() private var previousIndex: Int = 0 private var agentNamesArray: [String] = [String] () public var fromReward : Int? public static var payoutConnectionTimeOutCheck = 0 @IBOutlet weak var tableViewAgent: UITableView! override func viewDidLoad() { super.viewDidLoad() setUpNavBar(id: 202, title: "GME Branches") setUpAnotherLoginListener(genericviewmodel: agentviewmodel) searchbar.delegate = self tableViewAgent.tableFooterView = UIView() hideKeyboardWhenTappedAround() tableViewAgent.delegate = self tableViewAgent.dataSource = self tableViewAgent.estimatedRowHeight = 119 tableViewAgent.rowHeight = UITableViewAutomaticDimension tableViewAgent.sectionIndexColor = UIColor(hex: 0x4a4a4a) tableViewAgent.sectionIndexBackgroundColor = UIColor(hex: 0xe7edf2) showActivityIndicator(activityIndicator: activityIndicator) disableUserInteractions() agentviewmodel.agentConnectionTimeOut.value = nil /** connection timeout */ agentviewmodel.agentConnectionTimeOut.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) if PayoutViewController.payoutConnectionTimeOutCheck == 0{ PayoutViewController.payoutConnectionTimeOutCheck = PayoutViewController.payoutConnectionTimeOutCheck+1 self.popUpMessage(value: 20) } } agentviewmodel.internetConnection.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.popUpMessage(value: 15) } agentviewmodel.fetchAgentLocation(countryId: "118", city: "") agentviewmodel.agentLocationAvailable.bind { [unowned self] in guard $0 != nil else { return } self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.enableUserInteractions() guard $0! else { return } if self.agentviewmodel.getAgentCount() > 0 { for i in 0...self.agentviewmodel.getAgentCount()-1 { self.agentNamesArray.append(self.agentviewmodel.getAgentName(index: i)) } self.tableViewAgent.reloadData() } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } extension PayoutViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return agentviewmodel.getAgentCount() } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "payoutTableViewCell", for: indexPath) as! PayoutTableViewCell cell.labelPayoutName.text = agentviewmodel.getAgentName(index: indexPath.row) cell.labelPayoutAddress.text = agentviewmodel.getAgentAddress(index: indexPath.row) cell.buttonPhone.setTitle(agentviewmodel.getAgentPhone(index: indexPath.row), for: .normal) cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor cell.layer.borderWidth = 5 cell.layer.cornerRadius = 10 cell.clipsToBounds = true return cell } func sectionIndexTitles(for tableView: UITableView) -> [String]? { return ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"] } func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int { let tappedIndex = self.agentNamesArray.index(where: {$0.prefix(1).uppercased() == title}) if tappedIndex != nil { self.previousIndex = tappedIndex! tableView.scrollToRow(at: IndexPath(row: tappedIndex!, section: 0), at: UITableViewScrollPosition.top, animated: true) } return previousIndex } } extension PayoutViewController: UISearchBarDelegate { func createSearchBar() { searchbar.returnKeyType = UIReturnKeyType.done searchbar.becomeFirstResponder() } func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { searchActive = true // searchbar.showsCancelButton = true } func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { searchActive = false } func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { searchActive = false searchbar.showsCancelButton = false searchBar.resignFirstResponder() } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { searchActive = false searchBar.resignFirstResponder() } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { if !(searchText.isEmpty){ // print("after every text gets changed") } else { } } }