// // AgentSearchViewController.swift // GMERemittance // // Created by FMI-12 on 2/6/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import MapKit class AgentSearchViewController: UIViewController { @IBOutlet weak var searchbar: UISearchBar! var searchActive : Bool = false var selectedCell:Int? var countryId: String? var timer: Timer? @IBOutlet weak var searchTableView: UITableView! var agentViewModel = AgentViewModel() private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() override func viewDidLoad() { super.viewDidLoad() searchbar.tintColor = UIColor.init(hex: 0xED1C24) searchbar.delegate = self setUpNavBar(id: 201, title: "Agent") setUpAnotherLoginListener(genericviewmodel: agentViewModel) agentViewModel.agentConnectionTimeOut.value = nil /** connection timeout */ agentViewModel.agentConnectionTimeOut.bind { [unowned self] in guard $0 != nil else { return } self.stopLoading() self.popUpMessage(value: 20) } agentViewModel.internetConnection.value = nil /** Internet check */ agentViewModel.internetConnection.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.popUpMessage(value: 15) } /** Update Agent Location */ agentViewModel.agentLocationAvailable.bind { [unowned self] in guard $0 != nil else { return } guard $0! else { self.stopLoading() return } self.stopLoading() self.searchTableView.isHidden = false self.searchbar.resignFirstResponder() self.searchTableView.reloadData() } searchbar.becomeFirstResponder() hideKeyboardWhenTappedAround() self.searchTableView.separatorStyle = UITableViewCellSeparatorStyle.none } /** Dispaly loading indicator */ func startLoading(){ self.showActivityIndicator(activityIndicator: self.activityIndicator) self.enableUserInteractions() } /** Hide loading indicator */ func stopLoading(){ self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.enableUserInteractions() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } extension AgentSearchViewController: UITableViewDelegate, UITableViewDataSource, AgentSearchTableViewCellDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return agentViewModel.getAgentCount() } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "agentSearchCell", for: indexPath) as! AgentSearchTableViewCell cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor cell.layer.borderWidth = 2.5 cell.layer.cornerRadius = 10 cell.clipsToBounds = true cell.labelBranchName.text = agentViewModel.getAgentName(index: indexPath.row) cell.labelBranchAddress.text = agentViewModel.getAgentAddress(index: indexPath.row) if agentViewModel.getAgentPhone(index: indexPath.row) != "N/A"{ cell.buttonPhoneNumber.setTitle(agentViewModel.getAgentPhone(index: indexPath.row), for: UIControlState.normal) }else{ cell.buttonPhoneNumber.setTitle(" ", for: UIControlState.normal) } cell.buttonMap?.addTarget(self, action:#selector(agentSearchTableViewCellDidTapMap(_:)), for:.touchUpInside) return cell } @objc func agentSearchTableViewCellDidTapMap(_ sender: AgentSearchTableViewCell) { guard let clickedCell = sender.superview?.superview?.superview as? AgentSearchTableViewCell else { return } let clickedCellIndexPath = searchTableView.indexPath(for: clickedCell) selectedCell = clickedCellIndexPath?.row let latitude = agentViewModel.getAgentLatitude(index: selectedCell!) let longitude = agentViewModel.getAgentLongitude(index: selectedCell!) let storyboard = UIStoryboard.init(name: "RecipientListViewController", bundle: Bundle.main) if let agentLocationViewController = storyboard.instantiateViewController(withIdentifier: "agentLocation") as? AgentLocation { if latitude != "0" && longitude != "0"{ agentLocationViewController.latitude = CLLocationDegrees(latitude)! agentLocationViewController.longitude = CLLocationDegrees(longitude)! self.navigationController!.pushViewController(agentLocationViewController, animated: true) }else{ self.popUpMessageInfo(value: 16, title: "Location", message: "Map view is not available.") } } } } extension AgentSearchViewController: UISearchBarDelegate { func createSearchBar() { searchbar.returnKeyType = UIReturnKeyType.done searchbar.becomeFirstResponder() } func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { } func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { } func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { self.navigationController?.popViewController(animated: true) } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { self.timer?.invalidate() if searchText != "" && !searchText.isEmpty{ self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(AgentSearchViewController.requestSearchApi), userInfo: searchText, repeats: false) }else{ self.searchTableView.isHidden = true searchbar.resignFirstResponder() } } @objc func requestSearchApi(){ startLoading() agentViewModel.fetchAgentLocation(countryId:self.countryId!, city: timer?.userInfo as! String) timer?.invalidate() } }