@@ -12,6 +12,7 @@ import Cartography
1212
1313protocol UpdateableWithChannel: class {
1414 var channel: Model.Channels? { get set }
15+ func updateChannel(on channel: Model.Channels)
1516}
1617
1718protocol UpdateableWithUser: class {
@@ -19,8 +20,14 @@ protocol UpdateableWithUser: class {
1920}
2021// MARK:- Controller with posts and channels availiable.
2122// Search is availiable within every table (posts and channels). Has button-functionality for boths post and chnnels
22−class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWithChannel
23+class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWithChannel, UISearchResultsUpdating
2324{
25+ func updateChannel(on channel: Model.Channels) {
26+ self.channel = channel
27+ news.currChannel = channel
28+ decideWhatChannelDisplay()
29+ }
30+
2431 var changedChannelName: ((String)->())?
2532
2633 @IBOutlet weak var tableView: UITableView!
@@ -31,7 +38,7 @@ class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWi
3138 // MARK: - Output -
3239 var onSelectChannel: (() -> Void)?
3340
34− var searchController = UISearchController(searchResultsController: nil)
41+ var searchController: UISearchController?
3542
3643 var news = NewsVC()
3744 var type: HeaderType? = .NEWS
@@ -41,6 +48,11 @@ class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWi
4148 override func viewDidLoad()
4249 {
4350 super.viewDidLoad()
51+
52+ let updater = TagsSuggestionController()
53+ updater.delegate = self
54+ searchController = UISearchController(searchResultsController: updater)
55+
4456 navigationItem.title = "Loading ..."
4557 tableView.refreshControl = refreshContr
4658 // Configure Refresh Control
@@ -49,8 +61,9 @@ class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWi
4961 tableView.register(PostViewCell.self, forCellReuseIdentifier: postCellId)
5062 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
5163
52− // setUpSearchContr()
64+ setUpSearchContr()
5365
66+ // navigationItem.
5467 news.viewController = self
5568 news.type = type == .NEWS ? .NEWS : type!
5669 news.currChannel = channel
@@ -59,11 +72,12 @@ class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWi
5972 }
6073
6174 func setUpSearchContr(){
62− searchController.delegate = self
63− searchController.obscuresBackgroundDuringPresentation = false
75+ searchController?.delegate = self
76+ searchController?.obscuresBackgroundDuringPresentation = false
6477 navigationItem.searchController = searchController
6578 definesPresentationContext = true
66− searchController.searchBar.placeholder = "Search anything"
79+ searchController?.searchResultsUpdater = self
80+ searchController?.searchBar.placeholder = "Search tags"
6781 }
6882
6983 @objc func refreshPostsData( _ ff: UIRefreshControl){
@@ -76,7 +90,7 @@ class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWi
7690
7791 override func viewWillAppear(_ animated: Bool) {
7892 super.viewWillAppear(animated)
79− searchController.isActive = false
93+ searchController?.isActive = false
8094 // TODO:- display something if no posts are availiable
8195
8296 decideWhatChannelDisplay()
@@ -85,16 +99,18 @@ class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWi
8599 }
86100
87101 func decideWhatChannelDisplay(){
102+
88103 switch type! {
89104 case .NEWS, .NONE:
90105 if let channel = channel, let id = channel.id, id != -1 {
91106
92− Model.getChannel(with: id) { [weak self] in
107+ Model.getAnonymousChannel(by: channel) { [weak self] in
93108 self?.news.dataSourse = $0.posts
94109 self?.news.dictionary = $0.users
95110 self?.refreshContr.endRefreshing()
96111 self?.changedChannelName?(channel.name)
97112 }
113+
98114 } else {
99115 Model.getLast { [weak self] in
100116 self?.news.dataSourse = $0.posts
@@ -110,6 +126,7 @@ class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWi
110126 changedChannelName?("Anonymous")
111127 }
112128 }
129+
113130 }
114131
115132 func setUpNavigationItemsforPosts(){
@@ -117,4 +134,27 @@ class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWi
117134 tableView.dataSource = news
118135 tableView.reloadData()
119136 }
137+
138+ func updateSearchResults(for searchController: UISearchController)
139+ {
140+ let filteredData = CompletionTree.getCompletion(tree: Model.hashTagTree!, word: searchController.searchBar.text?.lowercased() ?? "")
141+
142+ // Apply the filtered results to the search results table.
143+ if let resultsController = searchController.searchResultsController as? TagsSuggestionController {
144+ resultsController.suggestions = filteredData
145+ resultsController.tableView.reloadData()
146+ }
147+ }
148+
149+ func willPresentSearchController(_ searchController: UISearchController) {
150+ searchController.searchResultsController?.view.isHidden = false;
151+ }
152+
153+ func didPresentSearchController(_ searchController: UISearchController) {
154+ searchController.searchResultsController?.view.isHidden = false;
155+ }
156+
157+ func didDismissSearchController(_ searchController: UISearchController) {
158+ searchController.searchBar.text = ""
159+ }
120160}