SearchLabelVC.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // SearchLabelVC.swift
  3. // Sirani
  4. //
  5. // Created by Hendi on 20/06/23.
  6. // Copyright © 2023 PT. Mitra Sinerji Teknoindo. All rights reserved.
  7. //
  8. import UIKit
  9. protocol SearchLabelDelegate {
  10. func onSearch(search: String, status: String, jenisLabel: String)
  11. }
  12. class SearchLabelVC: UIViewController {
  13. // Outlets / Views
  14. @IBOutlet weak var inputSearch: RoundTextField!
  15. @IBOutlet weak var optionsLayoutTV: UITableView!
  16. @IBOutlet weak var optionsLayout: UIView!
  17. @IBOutlet weak var optionsBg: UIView!
  18. @IBOutlet weak var btnStatusOptions: UIButton!
  19. // Initialize
  20. var delegate: SearchLabelDelegate?
  21. // constants vallue data
  22. private let jenisSertifikatOptionItems = [
  23. OptionItem(label: "Semua", value: "label_all"),
  24. OptionItem(label: "Sudah Upload", value: "label_sudah_upload"),
  25. OptionItem(label: "Belum Upload", value: "label_belum_upload")
  26. ]
  27. private let OPTION_JENIS_SERTIFIKAT_ID = 1
  28. // Others
  29. private var selectedOptions = 0
  30. private var selectedOptionItems = [OptionItem]()
  31. private var optionsShown = false
  32. // Result Values
  33. private var search = ""
  34. private var status = ""
  35. private var jenisLabel = "Semua"
  36. // View Lifecycles
  37. override var preferredStatusBarStyle: UIStatusBarStyle {
  38. return .lightContent
  39. }
  40. override func viewWillAppear(_ animated: Bool) {
  41. btnStatusOptions.layer.masksToBounds = false
  42. btnStatusOptions.layer.shadowColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
  43. btnStatusOptions.layer.shadowOpacity = 0.5
  44. btnStatusOptions.layer.shadowOffset = CGSize(width: 1, height: 1)
  45. btnStatusOptions.layer.shadowRadius = 1
  46. status = jenisSertifikatOptionItems[0].value as! String
  47. jenisLabel = jenisSertifikatOptionItems[0].label
  48. btnStatusOptions.setTitle(jenisLabel, for: .normal)
  49. hideOptions()
  50. optionsLayoutTV.bringSubviewToFront(optionsBg)
  51. let onOptionsBgTapped = UITapGestureRecognizer(target: self, action: #selector(hideOptions))
  52. optionsBg.addGestureRecognizer(onOptionsBgTapped)
  53. }
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. optionsLayoutTV.delegate = self
  57. optionsLayoutTV.dataSource = self
  58. optionsLayoutTV.tableFooterView = UIView()
  59. }
  60. @IBAction func onInputSearchKeyTap(_ sender: Any) {
  61. view.endEditing(true)
  62. }
  63. @IBAction func unwindToLabel(_ sender: Any) {
  64. if optionsShown {
  65. hideOptions()
  66. } else {
  67. performSegue(withIdentifier: "unwindSearchToLabelSegueId", sender: self)
  68. }
  69. }
  70. @IBAction func onJenisSertifikatOptionsTapped(_ sender: Any) {
  71. selectedOptions = OPTION_JENIS_SERTIFIKAT_ID
  72. selectedOptionItems.removeAll()
  73. selectedOptionItems += jenisSertifikatOptionItems
  74. optionsLayoutTV.reloadData()
  75. showOptions()
  76. }
  77. @IBAction func onResetTapped(_ sender: Any) {
  78. search = ""
  79. status = ""
  80. jenisLabel = "Semua"
  81. inputSearch.text = ""
  82. btnStatusOptions.setTitle(jenisLabel, for: .normal)
  83. }
  84. @IBAction func onSearchTapped(_ sender: Any) {
  85. if let searchText = inputSearch.getText() {
  86. search = searchText
  87. }
  88. self.delegate?.onSearch(search: search, status: status, jenisLabel: jenisLabel)
  89. performSegue(withIdentifier: "unwindSearchToLabelSegueId", sender: self)
  90. }
  91. private func showOptions() {
  92. optionsLayout.show()
  93. optionsShown = true
  94. }
  95. @objc private func hideOptions() {
  96. optionsLayout.hide()
  97. optionsShown = false
  98. }
  99. }
  100. extension SearchLabelVC: UITableViewDelegate, UITableViewDataSource {
  101. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  102. return selectedOptionItems.count
  103. }
  104. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  105. if let cell = tableView.dequeueReusableCell(withIdentifier: "searchLabelCell", for: indexPath) as? OptionsCell {
  106. let optionItem = selectedOptionItems[indexPath.row]
  107. cell.updateViews(item: optionItem)
  108. return cell
  109. } else {
  110. return OptionsCell()
  111. }
  112. }
  113. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  114. if selectedOptions == OPTION_JENIS_SERTIFIKAT_ID {
  115. status = selectedOptionItems[indexPath.row].value as! String
  116. jenisLabel = selectedOptionItems[indexPath.row].label
  117. btnStatusOptions.setTitle(jenisLabel, for: .normal)
  118. }
  119. hideOptions()
  120. }
  121. }