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.

205 lines
6.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // TablePickerViewController.swift
  3. //
  4. //
  5. // Created by shishir sapkota
  6. //
  7. import UIKit
  8. enum PickerTitle: String {
  9. case country = "Country"
  10. case state = "Provience"
  11. case district = "District"
  12. case relation = "Relation"
  13. case transferReasons = "Transfer Reason"
  14. case bank = "Bank"
  15. case branch = "Branch"
  16. case currency = "Currency"
  17. case occupation = "Occupation"
  18. case verificationIdTypes = "Idi Type"
  19. case sourceOfFund = "Source Of Fund"
  20. }
  21. class CountryPickerViewController: UIViewController {
  22. struct Constants {
  23. static let seperatorColor = "#DFDFDF"
  24. static let tintColor = UIColor.red
  25. static let gradientviewLeftColor = UIColor.init(hex: "#FF0000")
  26. static let gradientviewRightColor = UIColor.init(hex: "#9C272D")
  27. }
  28. @IBOutlet weak var titleView: UIView!
  29. @IBOutlet weak var tableVIew: UITableView!
  30. @IBOutlet weak var searchTextField: UITextField!
  31. @IBOutlet weak var titleLabel: UILabel!
  32. @IBOutlet weak var noResultFoundLabel: UILabel!
  33. @IBOutlet weak var mainview: UIView!
  34. var data: [SendMoneyCountryViewModel] = []
  35. var filteredPlaces: [SendMoneyCountryViewModel] = [] {
  36. didSet {
  37. self.tableVIew.reloadData()
  38. self.setNoResultText()
  39. self.filteredPlaces.isEmpty ? (self.noResultFoundLabel.isHidden = false) : (self.noResultFoundLabel.isHidden = true)
  40. }
  41. }
  42. var allowMultipleSelection = false
  43. var allowSelection = true
  44. var doneAction: (([SendMoneyCountryViewModel]) -> ())?
  45. var defaultSelectedData: [SendMoneyCountryViewModel?] = []
  46. var searchText = ""
  47. var type: PickerTitle?
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. self.setupTableView()
  51. self.addGradientView()
  52. self.setupSearchService()
  53. self.setupPlaces()
  54. self.titleView.layer.cornerRadius = 10
  55. self.mainview.layer.cornerRadius = 10
  56. setTitle()
  57. }
  58. private func setNoResultText() {
  59. var text = ""
  60. if let type = self.type {
  61. text = "No matching \(type.rawValue) found for "
  62. } else {
  63. text = "No matching result found for "
  64. }
  65. self.noResultFoundLabel.text = text + "'\(searchText)'"
  66. }
  67. private func setTitle() {
  68. self.titleLabel.text = "Select " + (self.type?.rawValue ?? "Location").capitalized
  69. }
  70. private func setupTableView() {
  71. self.tableVIew.delegate = self
  72. self.tableVIew.dataSource = self
  73. self.tableVIew.separatorColor = UIColor.init(hex: Constants.seperatorColor)
  74. self.tableVIew.allowsMultipleSelection = self.allowMultipleSelection
  75. self.tableVIew.allowsSelection = self.allowSelection
  76. self.noResultFoundLabel.isHidden = true
  77. }
  78. private func setupPlaces() {
  79. self.filteredPlaces = self.data
  80. }
  81. private func setupSearchTextFieldUi() {
  82. let searchIconImageView = UIImageView(image: #imageLiteral(resourceName: "ic_search"))
  83. searchIconImageView.tintColor = Constants.tintColor
  84. self.searchTextField.leftView = searchIconImageView
  85. self.searchTextField.leftViewMode = .always
  86. }
  87. private func setupSearchService() {
  88. self.searchTextField.addTarget(self, action: #selector(self.search(sender:)), for: UIControlEvents.editingChanged)
  89. }
  90. private func addGradientView() {
  91. }
  92. @objc private func search(sender: UITextField) {
  93. let searchString = sender.text!
  94. self.searchText = searchString
  95. if searchString.isEmpty {
  96. self.filteredPlaces = self.data
  97. return
  98. }
  99. self.filteredPlaces = self.data.filter({
  100. return searchString.isEmpty ||
  101. ($0.name ?? "").lowercased().contains(searchString.lowercased())
  102. }).sorted(by: { (a, _) -> Bool in
  103. return (a.name?.lowercased() ?? "").hasPrefix(searchString.lowercased())
  104. })
  105. }
  106. // MARK: IBActions
  107. @IBAction func close(_ sender: Any?) {
  108. let selectedIndexPaths = self.tableVIew.indexPathsForSelectedRows ?? self.tableVIew.indexPathForSelectedRow.map({[$0]})
  109. let selectedData = selectedIndexPaths?.compactMap { indexPath -> SendMoneyCountryViewModel? in
  110. let value = filteredPlaces.elementAt(index: indexPath.row)
  111. return value
  112. }
  113. self.doneAction?(selectedData ?? [])
  114. self.dismiss(animated: true, completion: nil)
  115. }
  116. @IBAction func clear(_ sender: Any) {
  117. self.tableVIew.indexPathsForSelectedRows?.forEach({self.tableVIew.deselectRow(at: $0, animated: true)})
  118. self.tableVIew.indexPathForSelectedRow.map({self.tableVIew.deselectRow(at: $0, animated: true)})
  119. self.defaultSelectedData = []
  120. self.tableVIew.reloadData()
  121. }
  122. @IBAction func dismiss(_ sender: Any) {
  123. self.dismiss(animated: true, completion: nil)
  124. }
  125. }
  126. extension CountryPickerViewController: UITableViewDelegate {
  127. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  128. let cell = tableView.cellForRow(at: indexPath) as! CountryPickerTableViewCell
  129. cell.selectedPlace = self.filteredPlaces.elementAt(index: indexPath.row)
  130. cell.setup()
  131. if !self.allowMultipleSelection {
  132. self.close(nil)
  133. }
  134. }
  135. func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
  136. if let cell = tableView.cellForRow(at: indexPath) as? CountryPickerTableViewCell {
  137. cell.selectedPlace = nil
  138. cell.setup()
  139. } else {
  140. print("something happened here??")
  141. }
  142. }
  143. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  144. return 75
  145. }
  146. }
  147. extension CountryPickerViewController: UITableViewDataSource {
  148. func numberOfSections(in tableView: UITableView) -> Int {
  149. return 1
  150. }
  151. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  152. return filteredPlaces.count
  153. }
  154. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  155. return self.configureImageCell(tableView: tableView, indexPath: indexPath)
  156. }
  157. func configureImageCell(tableView: UITableView, indexPath: IndexPath) -> UITableViewCell {
  158. let cell = tableVIew.dequeueReusableCell(withIdentifier: "CountryPickerTableViewCell", for: indexPath) as! CountryPickerTableViewCell
  159. if let _default = self.defaultSelectedData.first {
  160. cell.selectedPlace = _default
  161. }
  162. cell.place = self.filteredPlaces.elementAt(index: indexPath.row)
  163. cell.setup()
  164. return cell
  165. }
  166. }