@@ -15,35 +15,42 @@ enum DataSourse{
15class AddToChannelVC: UITableViewController {
15class AddToChannelVC: UITableViewController {
17 var channel: Model.Channels?
17 var channel: Model.Channels?
18− var dataSourse: DataSourse = .people
19 weak var update: UpdatableChannel?
18 weak var update: UpdatableChannel?
21 // data sources for people and hashtags
20 // data sources for people and hashtags
22 var dataSourcePeople: [Model.Users] = Model.Channels.fullPeople
21 var dataSourcePeople: [Model.Users] = Model.Channels.fullPeople
23− var fullTags: [String] = CompletionTree.getCompletion(tree: Model.hashTagTree!, word: "")
26 var reloadtable: Bool = false {
23 var reloadtable: Bool = false {
28 tableView.reloadData()
25 tableView.reloadData()
29+ var filteredDataSource = [Model.Users]()
31+ func filterContentForSearchText(_ searchText: String, scope: String = "All")
33+ filteredDataSource = dataSourcePeople.filter { $0.fullName().lowercased().contains(searchText.lowercased()) }
34+ tableView.reloadData()
32 let searchC = UISearchController(searchResultsController: nil)
37 let searchC = UISearchController(searchResultsController: nil)
34 override func viewDidLoad() {
39 override func viewDidLoad() {
35 super.viewDidLoad()
40 super.viewDidLoad()
42+ navigationItem.title = "People"
37 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
43 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
38 self.navigationItem.searchController = searchC
44 self.navigationItem.searchController = searchC
39 navigationItem.largeTitleDisplayMode = .never
45 navigationItem.largeTitleDisplayMode = .never
40 self.navigationItem.hidesSearchBarWhenScrolling = false
46 self.navigationItem.hidesSearchBarWhenScrolling = false
41−
47+ definesPresentationContext = true
48+ searchC.searchResultsUpdater = self
49+ searchC.obscuresBackgroundDuringPresentation = false
44 override func viewWillDisappear(_ animated: Bool) {
52 override func viewWillDisappear(_ animated: Bool) {
45 super.viewWillDisappear(animated)
53 super.viewWillDisappear(animated)
47 update?.updateChannel(with: channel!)
54 update?.updateChannel(with: channel!)
49 // MARK: - Table view data source
56 // MARK: - Table view data source
@@ -52,12 +59,19 @@ class AddToChannelVC: UITableViewController {
62+ var isFiltering: Bool {
63+ return searchC.isActive && !searchBarIsEmpty()
66+ func searchBarIsEmpty() -> Bool {
67+ return searchC.searchBar.text?.isEmpty ?? true
55 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
70 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
56− switch dataSourse {
71+ if isFiltering {
57− case .people:
72+ return filteredDataSource.count
58 return dataSourcePeople.count
74 return dataSourcePeople.count
60− return fullTags.count
@@ -65,17 +79,16 @@ class AddToChannelVC: UITableViewController {
66 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
80 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
68− switch dataSourse {
82+ if isFiltering{
70− cell.textLabel?.text = dataSourcePeople[indexPath.row].fullName()
83+ cell.textLabel?.text = filteredDataSource[indexPath.row].fullName()
71− if channel!.people.contains(dataSourcePeople[indexPath.row].id) {
84+ if channel!.people.contains(filteredDataSource[indexPath.row].id) {
72 cell.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
85 cell.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
74 cell.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
87 cell.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
77− cell.textLabel?.text = "# \(fullTags[indexPath.row])"
90+ cell.textLabel?.text = dataSourcePeople[indexPath.row].fullName()
78− if channel!.tags.contains(fullTags[indexPath.row]) {
91+ if channel!.people.contains(dataSourcePeople[indexPath.row].id) {
79 cell.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
92 cell.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
81 cell.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
94 cell.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
@@ -89,29 +102,31 @@ class AddToChannelVC: UITableViewController {
90 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
103 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
91 if let cell = tableView.cellForRow(at: indexPath) {
104 if let cell = tableView.cellForRow(at: indexPath) {
92− switch dataSourse {
105+ if isFiltering{
94 if cell.backgroundColor == #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1) {
106 if cell.backgroundColor == #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1) {
95− let filtered = channel!.people.filter{ $0 != dataSourcePeople[indexPath.row].id }
107+ let filtered = channel!.people.filter{ $0 != filteredDataSource[indexPath.row].id }
96 channel?.people = filtered
108 channel?.people = filtered
97 cell.backgroundColor = #colorLiteral(red: 0.9999960065, green: 1, blue: 1, alpha: 1)
109 cell.backgroundColor = #colorLiteral(red: 0.9999960065, green: 1, blue: 1, alpha: 1)
99− channel?.people.append(dataSourcePeople[indexPath.row].id)
111+ channel?.people.append(filteredDataSource[indexPath.row].id)
100 cell.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
112 cell.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
102− default: //tags
114+ } else {
103 if cell.backgroundColor == #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1) {
115 if cell.backgroundColor == #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1) {
104− let filtered = channel!.tags.filter{
116+ let filtered = channel!.people.filter{ $0 != dataSourcePeople[indexPath.row].id }
105− !fullTags[indexPath.row].contains($0)
107− channel?.tags = filtered
117+ channel?.people = filtered
108 cell.backgroundColor = #colorLiteral(red: 0.9999960065, green: 1, blue: 1, alpha: 1)
118 cell.backgroundColor = #colorLiteral(red: 0.9999960065, green: 1, blue: 1, alpha: 1)
110− channel?.tags.append(fullTags[indexPath.row])
120+ channel?.people.append(dataSourcePeople[indexPath.row].id)
111 cell.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
121 cell.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
128+extension AddToChannelVC : UISearchResultsUpdating {
129+ func updateSearchResults(for searchController: UISearchController) {
130+ filterContentForSearchText(searchController.searchBar.text!)