ChatListController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import Foundation
  2. import UIKit
  3. import DcCore
  4. protocol ChatListDelegate: class {
  5. func onChatSelected(chatId: Int)
  6. }
  7. class ChatListController: UITableViewController {
  8. let dcContext: DcContext
  9. let viewModel: ChatListViewModel
  10. let contactCellReuseIdentifier = "contactCellReuseIdentifier"
  11. weak var chatListDelegate: ChatListDelegate?
  12. /// MARK - search
  13. private lazy var searchController: UISearchController = {
  14. let searchController = UISearchController(searchResultsController: nil)
  15. searchController.searchResultsUpdater = viewModel
  16. searchController.obscuresBackgroundDuringPresentation = false
  17. searchController.searchBar.placeholder = String.localized("search")
  18. searchController.dimsBackgroundDuringPresentation = false
  19. searchController.hidesNavigationBarDuringPresentation = true
  20. searchController.searchBar.delegate = self
  21. return searchController
  22. }()
  23. init(dcContext: DcContext, chatListDelegate: ChatListDelegate) {
  24. self.dcContext = dcContext
  25. self.chatListDelegate = chatListDelegate
  26. viewModel = ChatListViewModel(dcContext: dcContext)
  27. super.init(style: .grouped)
  28. viewModel.onChatListUpdate = handleChatListUpdate
  29. }
  30. required init?(coder: NSCoder) {
  31. fatalError("init(coder:) has not been implemented")
  32. }
  33. override func viewWillAppear(_ animated: Bool) {
  34. preferredContentSize = UIScreen.main.bounds.size
  35. navigationItem.hidesSearchBarWhenScrolling = false
  36. }
  37. override func viewDidAppear(_ animated: Bool) {
  38. navigationItem.hidesSearchBarWhenScrolling = true
  39. }
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. //chatList = dcContext.getChatlist(flags: DC_GCL_ADD_ALLDONE_HINT | DC_GCL_FOR_FORWARDING | DC_GCL_NO_SPECIALS, queryString: nil, queryId: 0)
  43. navigationItem.searchController = searchController
  44. tableView.register(ChatListCell.self, forCellReuseIdentifier: contactCellReuseIdentifier)
  45. tableView.rowHeight = 64
  46. tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double.leastNormalMagnitude))
  47. tableView.tableFooterView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double.leastNormalMagnitude))
  48. }
  49. override func numberOfSections(in tableView: UITableView) -> Int {
  50. return viewModel.numberOfSections
  51. }
  52. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  53. return viewModel.numberOfRowsIn(section: section)
  54. }
  55. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  56. guard let cell = tableView.dequeueReusableCell(withIdentifier: contactCellReuseIdentifier, for: indexPath) as? ChatListCell else {
  57. fatalError("could not deque TableViewCell")
  58. }
  59. if let chatId = viewModel.getChatId(section: indexPath.section, row: indexPath.row) {
  60. cell.updateCell(chatId: chatId)
  61. }
  62. return cell
  63. }
  64. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  65. if let chatId = viewModel.getChatId(section: indexPath.section, row: indexPath.row) {
  66. chatListDelegate?.onChatSelected(chatId: chatId)
  67. }
  68. }
  69. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  70. return viewModel.titleForHeaderIn(section: section)
  71. }
  72. func handleChatListUpdate() {
  73. tableView.reloadData()
  74. }
  75. }
  76. // MARK: - uisearchbardelegate
  77. extension ChatListController: UISearchBarDelegate {
  78. func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
  79. viewModel.beginSearch()
  80. return true
  81. }
  82. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  83. // searchBar will be set to "" by system
  84. viewModel.endSearch()
  85. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  86. self.tableView.scrollToTop()
  87. }
  88. }
  89. func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
  90. tableView.scrollToTop()
  91. return true
  92. }
  93. }