@@ -8,8 +8,44 @@
9import UIKit
9import UIKit
11−class MessagesViewController: UITableViewController {
11+protocol SelectableDialog: class {
12+ func openDialog(with dialog: Model.Dialog)
15+class MessagesViewController: UITableViewController, UISearchControllerDelegate, UISearchResultsUpdating, SelectableDialog {
17+ func openDialog(with dialog: Model.Dialog) {
18+ onDialogDisplay?((dialog, users))
21+ func updateSearchResults(for searchController: UISearchController) {
23+ let filteredData = currentActiveDialogs
24+ let inputText = searchController.searchBar.text ?? ""
26+ let filteredResults = filteredData.filter {
29+ case .groupChat(let gropuChat):
30+ return gropuChat.group.name.contains(inputText)
32+ case .userChat(let userChat):
33+ let user = users[userChat.user]
34+ guard let u = user else { return false }
35+ return u.fullName().contains(inputText)
41+ // Apply the filtered results to the search results table.
42+ if let resultsController = searchController.searchResultsController as? SuggestedDialogsTableViewController {
43+ resultsController.users = users
44+ resultsController.currentDialogs = filteredResults
45+ resultsController.tableView.reloadData()
13 // curreent Active which can be displayed
49 // curreent Active which can be displayed
14 var currentActiveDialogs: [Model.Dialog] = [] {
50 var currentActiveDialogs: [Model.Dialog] = [] {
@@ -17,6 +53,17 @@ class MessagesViewController: UITableViewController {
56+ func setUpSearchContr(){
58+ searchController?.delegate = self
59+ searchController?.obscuresBackgroundDuringPresentation = true
60+ navigationItem.searchController = searchController
61+ definesPresentationContext = true
62+ searchController?.searchResultsUpdater = self
63+ searchController?.searchBar.placeholder = "Search dialogs"
20 // curreent users
67 // curreent users
21 var users: [Int: Model.Users] = [:]
68 var users: [Int: Model.Users] = [:]
@@ -24,17 +71,22 @@ class MessagesViewController: UITableViewController {
25 var onDialogDisplay: (((dialog: Model.Dialog, users: [Int:Model.Users]))->())?
72 var onDialogDisplay: (((dialog: Model.Dialog, users: [Int:Model.Users]))->())?
27− let searchC = UISearchController(searchResultsController: nil)
74+ var searchController: UISearchController?
29 override func viewDidLoad() {
76 override func viewDidLoad() {
30 super.viewDidLoad()
77 super.viewDidLoad()
79+ let updater = SuggestedDialogsTableViewController()
81+ updater.delegate = self
82+ searchController = UISearchController(searchResultsController: updater)
32 // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
84 // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
33 self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Write", style: .plain, target: self, action: #selector(choosePerson))
85 self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Write", style: .plain, target: self, action: #selector(choosePerson))
35 self.navigationItem.title = "Messages"
87 self.navigationItem.title = "Messages"
36 // self.navigationItem.largeTitleDisplayMode = .always
88 // self.navigationItem.largeTitleDisplayMode = .always
37− self.navigationItem.searchController = searchC
89+ setUpSearchContr()
38 navigationController?.navigationBar.prefersLargeTitles = true
90 navigationController?.navigationBar.prefersLargeTitles = true
39 self.navigationItem.hidesSearchBarWhenScrolling = false
91 self.navigationItem.hidesSearchBarWhenScrolling = false
@@ -81,3 +133,49 @@ class MessagesViewController: UITableViewController {
81 onDialogDisplay?(tuple)
133 onDialogDisplay?(tuple)
138+class SuggestedDialogsTableViewController: UITableViewController {
142+ var users: [Int: Model.Users] = [:]
144+ weak var delegate: SelectableDialog?
146+ var currentDialogs: [Model.Dialog] = []
148+ override func viewDidLoad() {
150+ tableView.register(UITableViewCell.self, forCellReuseIdentifier: idCell)
151+ tableView.reloadData()
154+ override func numberOfSections(in tableView: UITableView) -> Int {
158+ override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
159+ return currentDialogs.count
162+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
164+ let cell = tableView.dequeueReusableCell(withIdentifier: idCell, for: indexPath)
166+ switch currentDialogs[indexPath.row] {
167+ case .groupChat(let groupChat):
168+ cell.textLabel?.text = groupChat.group.name
169+ case .userChat(let userChat):
170+ cell.textLabel?.text = users[userChat.user]?.fullName()
173+ cell.accessoryType = .disclosureIndicator
177+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
178+ delegate?.openDialog(with: currentDialogs[indexPath.row])
179+ // self.dismiss(animated: true)