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.

217 lines
7.7 KiB

6 years ago
  1. //
  2. // TrackTransferViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 2/5/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class TrackRecipientViewController: UIViewController {
  10. @IBOutlet weak var textFieldReceiverName: UITextField!
  11. @IBOutlet weak var viewDetails: UIView!
  12. @IBOutlet weak var tableViewRecipientDetails: UITableView!
  13. @IBOutlet weak var viewMain: UIView!
  14. @IBOutlet weak var mainStackView: UIStackView!
  15. private var selectedIndex: Int!
  16. private var colorCodeRed: CGFloat?
  17. private var colorCodeGreen: CGFloat?
  18. private var colorCodeBlue: CGFloat?
  19. private var timer: Timer?
  20. private var trackrecipientviewmodel = TrackRecipientViewModel()
  21. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  22. override func viewDidAppear(_ animated: Bool) {
  23. setUpAnotherLoginListener(genericviewmodel: trackrecipientviewmodel)
  24. trackrecipientviewmodel.recipientListObtained.value = nil
  25. }
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. setUpNavBar(id: 201, title: "Track Your Transfer")
  29. hideKeyboardWhenTappedAround()
  30. self.mainStackView.spacing = 1
  31. tableViewRecipientDetails.delegate = self
  32. tableViewRecipientDetails.dataSource = self
  33. textFieldReceiverName.delegate = self
  34. tableViewRecipientDetails.isHidden = true
  35. tableViewRecipientDetails.estimatedRowHeight = 80
  36. tableViewRecipientDetails.rowHeight = UITableViewAutomaticDimension
  37. trackrecipientviewmodel.trackRecipientTimeOut.value = nil
  38. /**
  39. connection timeout
  40. */
  41. trackrecipientviewmodel.trackRecipientTimeOut.bind { [unowned self] in
  42. guard $0 != nil else {
  43. return
  44. }
  45. self.stopLoading()
  46. self.popUpMessage(value: 20)
  47. }
  48. /**
  49. Check internet connection
  50. */
  51. trackrecipientviewmodel.internetConnection.value = nil
  52. trackrecipientviewmodel.internetConnection.bind { [unowned self] in
  53. guard $0 != nil else {
  54. return
  55. }
  56. self.popUpMessage(value: 15)
  57. self.textFieldReceiverName.resignFirstResponder()
  58. self.stopLoading()
  59. }
  60. /**
  61. Get recipient list as per search keyword
  62. */
  63. trackrecipientviewmodel.recipientListObtained.bind{ [unowned self] in
  64. guard $0 != nil else {
  65. return
  66. }
  67. guard $0! else {
  68. return
  69. }
  70. self.stopLoading()
  71. if self.trackrecipientviewmodel.getRecipientCount() == 0 {
  72. self.popUpMessageInfo(value: 16, title: "No recipient found", message: "Recipient \(self.textFieldReceiverName.text!) does not exist")
  73. } else {
  74. self.updateRecipientListUI()
  75. }
  76. }
  77. }
  78. /**
  79. Disable user interaction while fetching data from api
  80. */
  81. func startLoading(){
  82. disableUserInteractions()
  83. showActivityIndicator(activityIndicator: activityIndicator)
  84. }
  85. /**
  86. Enable user interaction after fetching data from api
  87. */
  88. func stopLoading(){
  89. self.enableUserInteractions()
  90. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  91. }
  92. /**
  93. Refresh table view when collection of data is updated
  94. */
  95. func updateRecipientListUI(){
  96. self.tableViewRecipientDetails.isHidden = false
  97. self.tableViewRecipientDetails.reloadData()
  98. self.tableViewRecipientDetails.tableFooterView = UIView()
  99. }
  100. /**
  101. Redirect list of values to next view
  102. */
  103. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  104. if segue.identifier == "showThreeTransactions" {
  105. let trackTransactionViewController
  106. = segue.destination as! TrackTransactionViewController
  107. trackTransactionViewController.recipient = self.trackrecipientviewmodel.getFilteredRecipientList(index: selectedIndex)
  108. trackTransactionViewController.colorCodeRed = colorCodeRed
  109. trackTransactionViewController.colorCodeGreen = colorCodeGreen
  110. trackTransactionViewController.colorCodeBlue = colorCodeBlue
  111. }
  112. }
  113. /**
  114. After user stops typing send an api request for recipient list.
  115. */
  116. @objc func recipientSearchApi(){
  117. if let inputText = textFieldReceiverName.text{
  118. trackrecipientviewmodel.clearArray()
  119. tableViewRecipientDetails.reloadData()
  120. self.startLoading()
  121. self.trackrecipientviewmodel.fetchRecipientList(queryString: inputText)
  122. }
  123. }
  124. }
  125. extension TrackRecipientViewController: UITextFieldDelegate {
  126. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  127. let inputText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
  128. self.timer?.invalidate()
  129. if inputText == "" {
  130. trackrecipientviewmodel.clearArray()
  131. self.tableViewRecipientDetails.isHidden = true
  132. } else {
  133. self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(TrackRecipientViewController.recipientSearchApi), userInfo: nil, repeats: false)
  134. }
  135. return true
  136. }
  137. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  138. textField.resignFirstResponder()
  139. return true
  140. }
  141. }
  142. extension TrackRecipientViewController: UITableViewDelegate, UITableViewDataSource {
  143. func numberOfSections(in tableView: UITableView) -> Int {
  144. return 1
  145. }
  146. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  147. return self.trackrecipientviewmodel.getRecipientCount()
  148. }
  149. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  150. let cell = tableView.dequeueReusableCell(withIdentifier: "trackTransferRecipient", for: indexPath) as! TrackRecipientTableViewCell
  151. let name = trackrecipientviewmodel.getRecipientName(index: indexPath.row)
  152. if name != "nil"{
  153. cell.labelRecipientName.text = name
  154. cell.labelRecipientInitial.text = name.prefix(1).uppercased()
  155. colorCodeRed = CGFloat(name[name.index(name.startIndex, offsetBy: 0)].asciiValue!)/100.0 * 0.2
  156. colorCodeGreen = CGFloat(name[name.index(name.startIndex, offsetBy: 1)].asciiValue!)/100.0 * 0.8
  157. colorCodeBlue = CGFloat(name[name.index(name.startIndex, offsetBy: 2)].asciiValue!)/100.0 * 0.5
  158. }
  159. let phoneNumber = trackrecipientviewmodel.getRecipientPhone(index: indexPath.row)
  160. if phoneNumber != "nil"{
  161. cell.labelRecipientPhone.text = phoneNumber
  162. }
  163. cell.labelRecipientInitial.layer.backgroundColor = UIColor(red: colorCodeRed!, green: colorCodeGreen!, blue: colorCodeBlue!, alpha: 1.0).cgColor
  164. cell.labelRecipientInitial.layer.cornerRadius = cell.labelRecipientInitial.frame.height / 2
  165. cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor
  166. cell.layer.borderWidth = 5
  167. cell.layer.cornerRadius = 15
  168. cell.clipsToBounds = true
  169. return cell
  170. }
  171. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  172. self.selectedIndex = indexPath.row
  173. performSegue(withIdentifier: "showThreeTransactions", sender: nil)
  174. }
  175. }