ChatListController.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. var chatList: DcChatlist?
  10. let contactCellReuseIdentifier = "contactCellReuseIdentifier"
  11. weak var chatListDelegate: ChatListDelegate?
  12. init(dcContext: DcContext, chatListDelegate: ChatListDelegate) {
  13. self.dcContext = dcContext
  14. self.chatListDelegate = chatListDelegate
  15. super.init(style: .grouped)
  16. }
  17. required init?(coder: NSCoder) {
  18. fatalError("init(coder:) has not been implemented")
  19. }
  20. override func viewWillAppear(_ animated: Bool) {
  21. preferredContentSize = UIScreen.main.bounds.size
  22. }
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. chatList = dcContext.getChatlist(flags: DC_GCL_ADD_ALLDONE_HINT | DC_GCL_FOR_FORWARDING | DC_GCL_NO_SPECIALS, queryString: nil, queryId: 0)
  26. tableView.register(ChatListCell.self, forCellReuseIdentifier: contactCellReuseIdentifier)
  27. tableView.rowHeight = 64
  28. tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double.leastNormalMagnitude))
  29. tableView.tableFooterView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double.leastNormalMagnitude))
  30. }
  31. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  32. return chatList?.length ?? 0
  33. }
  34. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  35. guard let cell = tableView.dequeueReusableCell(withIdentifier: contactCellReuseIdentifier, for: indexPath) as? ChatListCell else {
  36. fatalError("could not deque TableViewCell")
  37. }
  38. if let chatList = chatList {
  39. cell.updateCell(chatId: chatList.getChatId(index: indexPath.row))
  40. }
  41. return cell
  42. }
  43. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  44. if let chatList = chatList {
  45. chatListDelegate?.onChatSelected(chatId: chatList.getChatId(index: indexPath.row))
  46. }
  47. }
  48. }