@@ -16,6 +16,28 @@ struct ChannelData{
1616// TODO: make search controller availiable
1717class ChannelListController: UITableViewController, DataDelegate {
1818
19+ // MARK:- filter search controller
20+
21+ var filteredDataSource = [Channel]()
22+ var myProtocol: DataDelegate?
23+
24+ var isFiltering: Bool {
25+ return searchController.isActive && !searchBarIsEmpty()
26+ }
27+
28+ func searchBarIsEmpty() -> Bool {
29+ return searchController.searchBar.text?.isEmpty ?? true
30+ }
31+
32+ func filterContentForSearchText(_ searchText: String, scope: String = "All")
33+ {
34+ filteredDataSource = ChannelListController.dataSource.filter({(keys : Channel) -> Bool in
35+ return keys.title.lowercased().contains(searchText.lowercased())
36+ })
37+
38+ tableView.reloadData()
39+ }
40+
1941 func passData(for row: Int, channel: Channel) {
2042 ChannelListController.dataSource[row].hashtags = channel.hashtags
2143 ChannelListController.dataSource[row].people = channel.people
@@ -29,8 +51,8 @@ class ChannelListController: UITableViewController, DataDelegate {
2951 super.viewDidLoad()
3052 setUpNavigationBar()
3153 navigationItem.title = "Channels"
32−
33− searchController.searchBar.placeholder = "Search anything"
54+ searchController.obscuresBackgroundDuringPresentation = false
55+ searchController.searchBar.placeholder = "Search channel"
3456 navigationItem.searchController = searchController
3557 }
3658
@@ -48,7 +70,7 @@ class ChannelListController: UITableViewController, DataDelegate {
4870 {
4971 // insertion
5072 tableView.beginUpdates()
51− ChannelListController.dataSource.insert(Channel(title: "Untitled", subtitle: "No", hashtags: [], people: []), at: 0)
73+ ChannelListController.dataSource.insert(Channel(title: "Untitled", subtitle: "No", hashtags: [], people: [], posts: []), at: 0)
5274 tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .none)
5375 tableView.endUpdates()
5476
@@ -61,22 +83,37 @@ class ChannelListController: UITableViewController, DataDelegate {
6183 }
6284
6385 static var dataSource : [Channel] = [
64− Channel(title: "Title", subtitle: "subtitle", hashtags: ["# sad", "# happy"], people: ["Seva", "Andrey"]),
65− Channel(title: "Title2", subtitle: "subtitle2", hashtags: ["# studyhard", "# university"], people: ["Pasha", "Olya", "Andrey", "Ilya"]),
66− Channel(title: "Title3", subtitle: "subtitle3", hashtags: ["# lol", "# meme", "# hehe"], people: ["Superman"])
86+ Channel(title: "Title", subtitle: "subtitle", hashtags: ["# sad", "# happy"], people: ["Seva", "Andrey"], posts: [Post(dataArray: [.text("Le Lorem Ipsum est simplement du faux texte employé dans la composition et la mise en page avant impression. Le Lorem Ipsum est le faux texte standard.")], from: User(name: "vbogomazova", id: 2, fullName: "Богомазова Вероника Львовна"), date: "14.02.19 в 12:05")]),
87+ Channel(title: "Title2", subtitle: "subtitle2", hashtags: ["# studyhard", "# university"], people: ["Pasha", "Olya", "Andrey", "Ilya"], posts:
88+ [
89+ Post(dataArray: [.text("L'avantage du Lorem Ipsum sur un texte générique comme 'Du texte. Du texte. Du texte.' est qu'il possède une distribution de lettres plus ou moins normale.")], from: User(name: "vbogomazova", id: 2, fullName: "Богомазова Вероника Львовна"), date: "14.02.19 в 12:05"),
90+ Post(dataArray: [.text("par accident, souvent intentionnellement (histoire d'y rajouter de petits clins d'oeil, voire des phrases embarassantes).")], from: User(name: "vbogomazova", id: 2, fullName: "Богомазова Вероника Львовна"), date: "14.02.19 в 12:05")
91+ ]),
92+ Channel(title: "Title3", subtitle: "subtitle3", hashtags: ["# lol", "# meme", "# hehe"], people: ["Superman"], posts: [Post(dataArray: [.text("Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen de polices de texte. Il n'a pas fait que survivre cinq siècles, mais s'est aussi adapté à la bureautique informatique, sans que son contenu n'en soit modifié.")], from: User(name: "vbogomazova", id: 2, fullName: "Богомазова Вероника Львовна"), date: "14.02.19 в 12:05")])
6793 ]
6894
6995 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
70− return ChannelListController.dataSource.count
96+ if isFiltering {
97+ return filteredDataSource.count
98+ } else {
99+ return ChannelListController.dataSource.count
100+ }
71101 }
72102
73103 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
74104 {
105+
75106 let cell = tableView.dequeueReusableCell(withIdentifier: channelCellId, for: indexPath)
76− cell.textLabel?.text = ChannelListController.dataSource[indexPath.row].title
77− cell.detailTextLabel?.text = ChannelListController.dataSource[indexPath.row].subtitle
78107
79108
109+ if isFiltering{
110+ cell.textLabel?.text = filteredDataSource[indexPath.row].title
111+ cell.detailTextLabel?.text = filteredDataSource[indexPath.row].subtitle
112+ } else {
113+ cell.textLabel?.text = ChannelListController.dataSource[indexPath.row].title
114+ cell.detailTextLabel?.text = ChannelListController.dataSource[indexPath.row].subtitle
115+ }
116+
80117 return cell
81118 }
82119
@@ -109,4 +146,22 @@ class ChannelListController: UITableViewController, DataDelegate {
109146
110147 return [editButton, deleteButton]
111148 }
149+
150+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
151+ {
152+ if isFiltering {
153+ navigationController?.popViewController(animated: true)
154+ myProtocol?.passData(for: 0, channel: filteredDataSource[indexPath.row])
155+ } else {
156+ navigationController?.popViewController(animated: true)
157+ myProtocol?.passData(for: 0, channel: ChannelListController.dataSource[indexPath.row])
158+ }
159+ }
160+}
161+
162+
163+extension ChannelListController : UISearchResultsUpdating {
164+ func updateSearchResults(for searchController: UISearchController) {
165+ filterContentForSearchText(searchController.searchBar.text!)
166+ }
112167}