SeminarListViewController.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // SeminarListViewController.swift
  3. // NexilisLite
  4. //
  5. // Created by Maronakins on 13/06/23.
  6. //
  7. import UIKit
  8. import nuSDKService
  9. class SeminarListViewController: UIViewController {
  10. @IBOutlet weak var tableView: UITableView!
  11. var searchController: UISearchController!
  12. var data: [SeminarViewer] = []
  13. var fillteredData: [SeminarViewer] = []
  14. var isSearchBarEmpty: Bool {
  15. return searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
  16. }
  17. var isFilltering: Bool {
  18. return searchController.isActive && !isSearchBarEmpty
  19. }
  20. var makeSpeaker: ((SeminarViewer) -> ())?
  21. var removeSpeaker: ((SeminarViewer) -> ())?
  22. var timerSearch: Timer?
  23. func filterContentForSearchText(_ searchText: String) {
  24. fillteredData = data.filter{ $0.name.lowercased().contains(searchText.lowercased()) }
  25. tableView.reloadData()
  26. }
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. title = "Seminar".localized()
  30. let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
  31. let navBarAppearance = UINavigationBarAppearance()
  32. navBarAppearance.configureWithOpaqueBackground()
  33. navBarAppearance.backgroundColor = UIColor.mainColor
  34. navBarAppearance.titleTextAttributes = attributes
  35. navigationController?.navigationBar.standardAppearance = navBarAppearance
  36. navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
  37. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel".localized(), style: .plain, target: self, action: #selector(cancel(sender:)))
  38. searchController = UISearchController(searchResultsController: nil)
  39. searchController.delegate = self
  40. searchController.searchResultsUpdater = self
  41. searchController.searchBar.autocapitalizationType = .none
  42. searchController.searchBar.delegate = self
  43. searchController.searchBar.barTintColor = .secondaryColor
  44. searchController.searchBar.searchTextField.backgroundColor = .secondaryColor
  45. searchController.obscuresBackgroundDuringPresentation = false
  46. searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "Search".localized(), attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray])
  47. searchController.searchBar.setMagnifyingGlassColorTo(color: .mainColor)
  48. searchController.searchBar.tintColor = .mainColor
  49. definesPresentationContext = true
  50. navigationItem.searchController = searchController
  51. navigationItem.hidesSearchBarWhenScrolling = false
  52. }
  53. @objc func cancel(sender: Any) {
  54. navigationController?.dismiss(animated: true, completion: nil)
  55. }
  56. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  57. searchBar.showsCancelButton = true
  58. let cBtn = searchBar.value(forKey: "cancelButton") as! UIButton
  59. cBtn.setTitle("Cancel".localized(), for: .normal)
  60. }
  61. func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
  62. searchBar.showsCancelButton = false
  63. }
  64. func showChooserButtonTapped(viewer: SeminarViewer) {
  65. // Create an instance of the alert controller with the action sheet style
  66. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  67. // Add action buttons to the alert controller
  68. var actionTitle = "Make Speaker"
  69. if(viewer.isSpeak){
  70. actionTitle = "Remove Speaker"
  71. }
  72. let action = UIAlertAction(title: actionTitle, style: .default) { (_) in
  73. self.handleSpeaker(viewer: viewer)
  74. }
  75. alertController.addAction(action)
  76. let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
  77. alertController.addAction(cancelAction)
  78. present(alertController, animated: true, completion: nil)
  79. }
  80. func handleSpeaker(viewer: SeminarViewer) {
  81. navigationController?.dismiss(animated: true)
  82. if (viewer.isSpeak){
  83. removeSpeaker!(viewer)
  84. }
  85. else {
  86. makeSpeaker!(viewer)
  87. }
  88. }
  89. }
  90. class SeminarListCell: UITableViewCell {
  91. @IBOutlet weak var imagePerson: UIImageView!
  92. @IBOutlet weak var speakerButton: UIButton!
  93. @IBOutlet weak var namePerson: UILabel!
  94. }
  95. // MARK: - Extension
  96. extension SeminarListViewController: UITableViewDelegate {
  97. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  98. let viewer: SeminarViewer
  99. if isFilltering {
  100. viewer = fillteredData[indexPath.row]
  101. } else {
  102. viewer = data[indexPath.row]
  103. }
  104. showChooserButtonTapped(viewer: viewer)
  105. }
  106. }
  107. extension SeminarListViewController: UITableViewDataSource {
  108. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  109. return 60
  110. }
  111. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  112. if isFilltering {
  113. return fillteredData.count
  114. }
  115. return data.count
  116. }
  117. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  118. let cell = tableView.dequeueReusableCell(withIdentifier: "seminarListCell", for: indexPath) as! SeminarListCell
  119. cell.imagePerson.layer.masksToBounds = false
  120. cell.imagePerson.circle()
  121. cell.imagePerson.clipsToBounds = true
  122. cell.imagePerson.image = UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  123. cell.imagePerson.contentMode = .scaleAspectFit
  124. let user: SeminarViewer
  125. if isFilltering {
  126. user = fillteredData[indexPath.row]
  127. } else {
  128. user = data[indexPath.row]
  129. }
  130. let pictureImage = user.thumb
  131. if (pictureImage != "") {
  132. cell.imagePerson.setImage(name: pictureImage)
  133. cell.imagePerson.contentMode = .scaleAspectFill
  134. }
  135. cell.namePerson.text = user.name
  136. if (user.isSpeak){
  137. cell.speakerButton.isHidden = false
  138. cell.speakerButton.setImage(UIImage(named: "pb_seminar_speaking", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), for: .normal)
  139. }
  140. else if (user.isRaise){
  141. cell.speakerButton.isHidden = false
  142. cell.speakerButton.setImage(UIImage(named: "pb_raise_hand", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), for: .normal)
  143. }
  144. else {
  145. cell.speakerButton.isHidden = true
  146. }
  147. return cell
  148. }
  149. }
  150. extension SeminarListViewController: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
  151. func updateSearchResults(for searchController: UISearchController) {
  152. timerSearch?.invalidate()
  153. let currentText = searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines)
  154. if currentText.count >= 2 || currentText.isEmpty {
  155. timerSearch = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: {_ in
  156. self.filterContentForSearchText(currentText)
  157. })
  158. }
  159. }
  160. }