@@ -8,44 +8,8 @@
9import UIKit
9import UIKit
11−protocol SelectableDialog: class {
11+class MessagesViewController: UITableViewController {
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()
49 // curreent Active which can be displayed
13 // curreent Active which can be displayed
50 var currentActiveDialogs: [Model.Dialog] = [] {
14 var currentActiveDialogs: [Model.Dialog] = [] {
@@ -53,17 +17,6 @@ class MessagesViewController: UITableViewController, UISearchControllerDelegate,
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"
67 // curreent users
20 // curreent users
68 var users: [Int: Model.Users] = [:]
21 var users: [Int: Model.Users] = [:]
@@ -71,22 +24,17 @@ class MessagesViewController: UITableViewController, UISearchControllerDelegate,
72 var onDialogDisplay: (((dialog: Model.Dialog, users: [Int:Model.Users]))->())?
25 var onDialogDisplay: (((dialog: Model.Dialog, users: [Int:Model.Users]))->())?
74− var searchController: UISearchController?
27+ let searchC = UISearchController(searchResultsController: nil)
76 override func viewDidLoad() {
29 override func viewDidLoad() {
77 super.viewDidLoad()
30 super.viewDidLoad()
79− let updater = SuggestedDialogsTableViewController()
81− updater.delegate = self
82− searchController = UISearchController(searchResultsController: updater)
84 // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
32 // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
85 self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Write", style: .plain, target: self, action: #selector(choosePerson))
33 self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Write", style: .plain, target: self, action: #selector(choosePerson))
87 self.navigationItem.title = "Messages"
35 self.navigationItem.title = "Messages"
88 // self.navigationItem.largeTitleDisplayMode = .always
36 // self.navigationItem.largeTitleDisplayMode = .always
89− setUpSearchContr()
37+ self.navigationItem.searchController = searchC
90 navigationController?.navigationBar.prefersLargeTitles = true
38 navigationController?.navigationBar.prefersLargeTitles = true
91 self.navigationItem.hidesSearchBarWhenScrolling = false
39 self.navigationItem.hidesSearchBarWhenScrolling = false
@@ -133,49 +81,3 @@ class MessagesViewController: UITableViewController, UISearchControllerDelegate,
133 onDialogDisplay?(tuple)
81 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)