WorkingAreaPicker.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // WorkingAreaPicker.swift
  3. // NexilisLite
  4. //
  5. // Created by Qindi on 26/07/22.
  6. //
  7. import UIKit
  8. class WorkingAreaPicker: UIViewController, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {
  9. let subContainerView = UIView()
  10. lazy var searchBar:UISearchBar = UISearchBar()
  11. var dataWorkingArea: [WorkingArea] = []
  12. let tableView = UITableView()
  13. public var selectedData: ((WorkingArea) -> ())?
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. // self.view.backgroundColor = .black.withAlphaComponent(0.3)
  17. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
  18. tapGesture.cancelsTouchesInView = false
  19. view.addGestureRecognizer(tapGesture)
  20. let containerView = UIView()
  21. view.addSubview(containerView)
  22. containerView.anchor(centerX: view.centerXAnchor, centerY: view.centerYAnchor, width: view.bounds.width - 40, height: view.bounds.height - 100)
  23. containerView.backgroundColor = .white.withAlphaComponent(0.9)
  24. subContainerView.backgroundColor = .clear
  25. containerView.addSubview(subContainerView)
  26. subContainerView.anchor(top: containerView.topAnchor, left: containerView.leftAnchor, bottom: containerView.bottomAnchor, right: containerView.rightAnchor, paddingTop: 20.0, paddingLeft: 10.0, paddingBottom: 20.0, paddingRight: 10.0)
  27. let buttonClose = UIButton(type: .close)
  28. buttonClose.frame.size = CGSize(width: 30, height: 30)
  29. buttonClose.layer.cornerRadius = 15.0
  30. buttonClose.clipsToBounds = true
  31. buttonClose.backgroundColor = .secondaryColor.withAlphaComponent(0.5)
  32. buttonClose.addTarget(self, action: #selector(close), for: .touchUpInside)
  33. containerView.addSubview(buttonClose)
  34. buttonClose.anchor(top: containerView.topAnchor, right: containerView.rightAnchor, width: 30, height: 30)
  35. let titleWA = UILabel()
  36. titleWA.font = .systemFont(ofSize: 18, weight: .bold)
  37. titleWA.text = "Working Area".localized()
  38. titleWA.textAlignment = .center
  39. subContainerView.addSubview(titleWA)
  40. titleWA.anchor(top: subContainerView.topAnchor, left: subContainerView.leftAnchor, right: subContainerView.rightAnchor)
  41. searchBar.searchBarStyle = UISearchBar.Style.default
  42. searchBar.placeholder = " Search..."
  43. searchBar.sizeToFit()
  44. searchBar.isTranslucent = true
  45. searchBar.updateHeight(height: 36, radius: 18)
  46. searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
  47. searchBar.delegate = self
  48. subContainerView.addSubview(searchBar)
  49. searchBar.anchor(top: titleWA.bottomAnchor, left: subContainerView.leftAnchor, right: subContainerView.rightAnchor, paddingTop: 10.0)
  50. tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellWorkingArea")
  51. tableView.dataSource = self
  52. tableView.delegate = self
  53. tableView.backgroundColor = .clear
  54. self.view.addSubview(tableView)
  55. tableView.anchor(top: searchBar.bottomAnchor, left: subContainerView.leftAnchor, bottom: subContainerView.bottomAnchor, right: subContainerView.rightAnchor, paddingBottom: 10.0)
  56. dataWorkingArea = WorkingArea.getData(name: "")
  57. }
  58. @objc func dismissKeyboard() {
  59. //Causes the view (or one of its embedded text fields) to resign the first responder status.
  60. view.endEditing(true)
  61. }
  62. @objc func close() {
  63. self.dismiss(animated: true)
  64. }
  65. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  66. DispatchQueue.main.async {
  67. self.dataWorkingArea = WorkingArea.getData(name: searchText)
  68. self.tableView.reloadData()
  69. }
  70. }
  71. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  72. self.selectedData?(self.dataWorkingArea[indexPath.row])
  73. self.dismiss(animated: true)
  74. }
  75. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  76. return dataWorkingArea.count
  77. }
  78. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  79. let cell = tableView.dequeueReusableCell(withIdentifier: "cellWorkingArea", for: indexPath as IndexPath)
  80. cell.textLabel!.text = dataWorkingArea[indexPath.row].name
  81. cell.backgroundColor = .clear
  82. return cell
  83. }
  84. }