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.

202 lines
7.0 KiB

6 years ago
  1. //
  2. // AgentSearchViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by FMI-12 on 2/6/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import MapKit
  10. class AgentSearchViewController: UIViewController {
  11. @IBOutlet weak var searchbar: UISearchBar!
  12. var searchActive : Bool = false
  13. var selectedCell:Int?
  14. var countryId: String?
  15. var timer: Timer?
  16. @IBOutlet weak var searchTableView: UITableView!
  17. var agentViewModel = AgentViewModel()
  18. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. searchbar.tintColor = UIColor.init(hex: 0xED1C24)
  22. searchbar.delegate = self
  23. setUpNavBar(id: 201, title: "Agent")
  24. setUpAnotherLoginListener(genericviewmodel: agentViewModel)
  25. agentViewModel.agentConnectionTimeOut.value = nil
  26. /**
  27. connection timeout
  28. */
  29. agentViewModel.agentConnectionTimeOut.bind { [unowned self] in
  30. guard $0 != nil else {
  31. return
  32. }
  33. self.stopLoading()
  34. self.popUpMessage(value: 20)
  35. }
  36. agentViewModel.internetConnection.value = nil
  37. /**
  38. Internet check
  39. */
  40. agentViewModel.internetConnection.bind { [unowned self] in
  41. guard $0 != nil else {
  42. return
  43. }
  44. self.enableUserInteractions()
  45. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  46. self.popUpMessage(value: 15)
  47. }
  48. /**
  49. Update Agent Location
  50. */
  51. agentViewModel.agentLocationAvailable.bind { [unowned self] in
  52. guard $0 != nil else {
  53. return
  54. }
  55. guard $0! else {
  56. self.stopLoading()
  57. return
  58. }
  59. self.stopLoading()
  60. self.searchTableView.isHidden = false
  61. self.searchbar.resignFirstResponder()
  62. self.searchTableView.reloadData()
  63. }
  64. searchbar.becomeFirstResponder()
  65. hideKeyboardWhenTappedAround()
  66. self.searchTableView.separatorStyle = UITableViewCellSeparatorStyle.none
  67. }
  68. /**
  69. Dispaly loading indicator
  70. */
  71. func startLoading(){
  72. self.showActivityIndicator(activityIndicator: self.activityIndicator)
  73. self.enableUserInteractions()
  74. }
  75. /**
  76. Hide loading indicator
  77. */
  78. func stopLoading(){
  79. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  80. self.enableUserInteractions()
  81. }
  82. override func didReceiveMemoryWarning() {
  83. super.didReceiveMemoryWarning()
  84. // Dispose of any resources that can be recreated.
  85. }
  86. /*
  87. // MARK: - Navigation
  88. // In a storyboard-based application, you will often want to do a little preparation before navigation
  89. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  90. // Get the new view controller using segue.destinationViewController.
  91. // Pass the selected object to the new view controller.
  92. }
  93. */
  94. }
  95. extension AgentSearchViewController: UITableViewDelegate, UITableViewDataSource, AgentSearchTableViewCellDelegate {
  96. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  97. return agentViewModel.getAgentCount()
  98. }
  99. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  100. let cell = tableView.dequeueReusableCell(withIdentifier: "agentSearchCell", for: indexPath) as! AgentSearchTableViewCell
  101. cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor
  102. cell.layer.borderWidth = 2.5
  103. cell.layer.cornerRadius = 10
  104. cell.clipsToBounds = true
  105. cell.labelBranchName.text = agentViewModel.getAgentName(index: indexPath.row)
  106. cell.labelBranchAddress.text = agentViewModel.getAgentAddress(index: indexPath.row)
  107. if agentViewModel.getAgentPhone(index: indexPath.row) != "N/A"{
  108. cell.buttonPhoneNumber.setTitle(agentViewModel.getAgentPhone(index: indexPath.row), for: UIControlState.normal)
  109. }else{
  110. cell.buttonPhoneNumber.setTitle(" ", for: UIControlState.normal)
  111. }
  112. cell.buttonMap?.addTarget(self, action:#selector(agentSearchTableViewCellDidTapMap(_:)), for:.touchUpInside)
  113. return cell
  114. }
  115. @objc func agentSearchTableViewCellDidTapMap(_ sender: AgentSearchTableViewCell) {
  116. guard let clickedCell = sender.superview?.superview?.superview as? AgentSearchTableViewCell else {
  117. return
  118. }
  119. let clickedCellIndexPath = searchTableView.indexPath(for: clickedCell)
  120. selectedCell = clickedCellIndexPath?.row
  121. let latitude = agentViewModel.getAgentLatitude(index: selectedCell!)
  122. let longitude = agentViewModel.getAgentLongitude(index: selectedCell!)
  123. let storyboard = UIStoryboard.init(name: "RecipientListViewController", bundle: Bundle.main)
  124. if let agentLocationViewController = storyboard.instantiateViewController(withIdentifier: "agentLocation") as? AgentLocation {
  125. if latitude != "0" && longitude != "0"{
  126. agentLocationViewController.latitude = CLLocationDegrees(latitude)!
  127. agentLocationViewController.longitude = CLLocationDegrees(longitude)!
  128. self.navigationController!.pushViewController(agentLocationViewController, animated: true)
  129. }else{
  130. self.popUpMessageInfo(value: 16, title: "Location", message: "Map view is not available.")
  131. }
  132. }
  133. }
  134. }
  135. extension AgentSearchViewController: UISearchBarDelegate {
  136. func createSearchBar() {
  137. searchbar.returnKeyType = UIReturnKeyType.done
  138. searchbar.becomeFirstResponder()
  139. }
  140. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  141. }
  142. func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
  143. }
  144. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  145. self.navigationController?.popViewController(animated: true)
  146. }
  147. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  148. }
  149. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  150. self.timer?.invalidate()
  151. if searchText != "" && !searchText.isEmpty{
  152. self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(AgentSearchViewController.requestSearchApi), userInfo: searchText, repeats: false)
  153. }else{
  154. self.searchTableView.isHidden = true
  155. searchbar.resignFirstResponder()
  156. }
  157. }
  158. @objc func requestSearchApi(){
  159. startLoading()
  160. agentViewModel.fetchAgentLocation(countryId:self.countryId!, city: timer?.userInfo as! String)
  161. timer?.invalidate()
  162. }
  163. }