SeminarListViewController.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : 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.obscuresBackgroundDuringPresentation = false
  44. // searchController.searchBar.updateHeight(height: 36, radius: 18)
  45. searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "Search".localized(), attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)])
  46. // searchController.searchBar.setMagnifyingGlassColorTo(color: .white)
  47. searchController.searchBar.setImage(UIImage(), for: .search, state: .normal)
  48. searchController.searchBar.setPositionAdjustment(UIOffset(horizontal: 10, vertical: 0), for: .search)
  49. searchController.searchBar.setCustomBackgroundImage(image: UIImage(named: self.traitCollection.userInterfaceStyle == .dark ? "nx_search_bar_dark" : "nx_search_bar", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!)
  50. searchController.searchBar.tintColor = .mainColor
  51. tableView.delegate = self
  52. tableView.dataSource = self
  53. definesPresentationContext = true
  54. navigationItem.searchController = searchController
  55. navigationItem.hidesSearchBarWhenScrolling = false
  56. }
  57. override func viewWillAppear(_ animated: Bool) {
  58. tableView.reloadData()
  59. }
  60. override func viewDidAppear(_ animated: Bool) {
  61. tableView.reloadData()
  62. }
  63. @objc func cancel(sender: Any) {
  64. navigationController?.dismiss(animated: true, completion: nil)
  65. }
  66. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  67. searchBar.showsCancelButton = true
  68. let cBtn = searchBar.value(forKey: "cancelButton") as! UIButton
  69. cBtn.setTitle("Cancel".localized(), for: .normal)
  70. }
  71. func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
  72. searchBar.showsCancelButton = false
  73. }
  74. func showChooserButtonTapped(viewer: SeminarViewer) {
  75. // Create an instance of the alert controller with the action sheet style
  76. let alertController = LibAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  77. // Add action buttons to the alert controller
  78. var actionTitle = "Make Speaker"
  79. if(viewer.isSpeak){
  80. actionTitle = "Remove Speaker"
  81. }
  82. let action = UIAlertAction(title: actionTitle, style: .default) { (_) in
  83. self.handleSpeaker(viewer: viewer)
  84. }
  85. alertController.addAction(action)
  86. let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
  87. alertController.addAction(cancelAction)
  88. present(alertController, animated: true, completion: nil)
  89. }
  90. func handleSpeaker(viewer: SeminarViewer) {
  91. navigationController?.dismiss(animated: true)
  92. if (viewer.isSpeak){
  93. removeSpeaker!(viewer)
  94. }
  95. else {
  96. makeSpeaker!(viewer)
  97. }
  98. }
  99. }
  100. class SeminarListCell: UITableViewCell {
  101. @IBOutlet weak var imagePerson: UIImageView!
  102. @IBOutlet weak var speakerButton: UIButton!
  103. @IBOutlet weak var namePerson: UILabel!
  104. }
  105. // MARK: - Extension
  106. extension SeminarListViewController: UITableViewDelegate {
  107. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  108. let viewer: SeminarViewer
  109. if isFilltering {
  110. viewer = fillteredData[indexPath.row]
  111. } else {
  112. viewer = data[indexPath.row]
  113. }
  114. showChooserButtonTapped(viewer: viewer)
  115. }
  116. }
  117. extension SeminarListViewController: UITableViewDataSource {
  118. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  119. return 60
  120. }
  121. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  122. if isFilltering {
  123. return fillteredData.count
  124. }
  125. return data.count
  126. }
  127. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  128. let cell = tableView.dequeueReusableCell(withIdentifier: "seminarListCell", for: indexPath) as! SeminarListCell
  129. cell.imagePerson.layer.masksToBounds = false
  130. cell.imagePerson.circle()
  131. cell.imagePerson.clipsToBounds = true
  132. cell.imagePerson.image = UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  133. cell.imagePerson.contentMode = .scaleAspectFit
  134. let user: SeminarViewer
  135. if isFilltering {
  136. user = fillteredData[indexPath.row]
  137. } else {
  138. user = data[indexPath.row]
  139. }
  140. let pictureImage = user.thumb
  141. if (pictureImage != "") {
  142. cell.imagePerson.setImage(name: pictureImage)
  143. cell.imagePerson.contentMode = .scaleAspectFill
  144. }
  145. cell.namePerson.text = user.name
  146. if (user.isSpeak){
  147. cell.speakerButton.isHidden = false
  148. cell.speakerButton.setImage(UIImage(named: "pb_seminar_speaking", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), for: .normal)
  149. }
  150. else if (user.isRaise){
  151. cell.speakerButton.isHidden = false
  152. cell.speakerButton.setImage(UIImage(named: "pb_raise_hand", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), for: .normal)
  153. }
  154. else {
  155. cell.speakerButton.isHidden = true
  156. }
  157. return cell
  158. }
  159. }
  160. extension SeminarListViewController: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
  161. func updateSearchResults(for searchController: UISearchController) {
  162. timerSearch?.invalidate()
  163. let currentText = searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines)
  164. if currentText.count >= 2 || currentText.isEmpty {
  165. timerSearch = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: {_ in
  166. self.filterContentForSearchText(currentText)
  167. })
  168. }
  169. }
  170. }