ContactDetailViewController.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import UIKit
  2. // this is also used as ChatDetail for SingleChats
  3. class ContactDetailViewController: UITableViewController {
  4. weak var coordinator: ContactDetailCoordinatorProtocol?
  5. var showChatCell: Bool = false // if this is set to true it will show a "goToChat-cell"
  6. private enum CellIdentifiers: String {
  7. case notification = "notificationCell"
  8. case block = "blockContactCell"
  9. case chat = "chatCell"
  10. }
  11. private let contactId: Int
  12. private var contact: DCContact {
  13. return DCContact(id: contactId)
  14. }
  15. private var notificationsCell: UITableViewCell = {
  16. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  17. cell.textLabel?.text = String.localized("pref_notifications")
  18. cell.accessibilityIdentifier = CellIdentifiers.notification.rawValue
  19. cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
  20. cell.selectionStyle = .none
  21. // TODO: add current notification status
  22. return cell
  23. }()
  24. private lazy var chatCell: ActionCell = {
  25. let cell = ActionCell()
  26. cell.accessibilityIdentifier = CellIdentifiers.chat.rawValue
  27. cell.actionColor = SystemColor.blue.uiColor
  28. cell.actionTitle = String.localizedStringWithFormat(String.localized("ask_start_chat_with"), contact.name)
  29. cell.selectionStyle = .none
  30. return cell
  31. }()
  32. private lazy var blockContactCell: ActionCell = {
  33. let cell = ActionCell()
  34. cell.accessibilityIdentifier = CellIdentifiers.block.rawValue
  35. cell.actionTitle = contact.isBlocked ? String.localized("menu_unblock_contact") : String.localized("menu_block_contact")
  36. cell.actionColor = contact.isBlocked ? SystemColor.blue.uiColor : UIColor.red
  37. cell.selectionStyle = .none
  38. return cell
  39. }()
  40. init(contactId: Int) {
  41. self.contactId = contactId
  42. super.init(style: .grouped)
  43. }
  44. required init?(coder _: NSCoder) {
  45. fatalError("init(coder:) has not been implemented")
  46. }
  47. override func viewDidLoad() {
  48. super.viewDidLoad()
  49. navigationItem.rightBarButtonItem = UIBarButtonItem(
  50. title: String.localized("global_menu_edit_desktop"),
  51. style: .plain, target: self, action: #selector(editButtonPressed))
  52. self.title = String.localized("contact_detail_title_desktop")
  53. }
  54. override func viewWillAppear(_ animated: Bool) {
  55. super.viewWillAppear(animated)
  56. tableView.reloadData()
  57. }
  58. override func numberOfSections(in tableView: UITableView) -> Int {
  59. return 2
  60. }
  61. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  62. if section == 0 {
  63. return showChatCell ? 2 : 1
  64. } else if section == 1 {
  65. return 1
  66. }
  67. return 0
  68. }
  69. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  70. let section = indexPath.section
  71. let row = indexPath.row
  72. if section == 0 {
  73. if row == 0 {
  74. return notificationsCell
  75. } else {
  76. return chatCell
  77. }
  78. } else {
  79. return blockContactCell
  80. }
  81. }
  82. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  83. guard let cell = tableView.cellForRow(at: indexPath) else {
  84. return
  85. }
  86. if let identifier = CellIdentifiers(rawValue: cell.accessibilityIdentifier ?? "") {
  87. switch identifier {
  88. case .block:
  89. toggleBlockContact()
  90. case .chat:
  91. let chatId = Int(dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId)))
  92. coordinator?.showChat(chatId: chatId)
  93. case .notification:
  94. showNotificationSetup()
  95. }
  96. }
  97. }
  98. override func tableView(_: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  99. if section == 0 {
  100. let header = ContactDetailHeader()
  101. header.updateDetails(title: contact.name, subtitle: contact.email)
  102. if let img = contact.profileImage {
  103. header.setImage(img)
  104. } else {
  105. header.setBackupImage(name: contact.name, color: contact.color)
  106. }
  107. header.setVerified(isVerified: contact.isVerified)
  108. return header
  109. }
  110. return nil
  111. }
  112. private func toggleBlockContact() {
  113. contact.isBlocked ? contact.unblock() : contact.block()
  114. updateBlockContactCell()
  115. }
  116. private func updateBlockContactCell() {
  117. blockContactCell.actionTitle = contact.isBlocked ? String.localized("menu_unblock_contact") : String.localized("menu_block_contact")
  118. blockContactCell.actionColor = contact.isBlocked ? SystemColor.blue.uiColor : UIColor.red
  119. }
  120. private func showNotificationSetup() {
  121. let notificationSetupAlert = UIAlertController(title: "Notifications Setup is not implemented yet",
  122. message: "But you get an idea where this is going",
  123. preferredStyle: .actionSheet)
  124. let cancelAction = UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil)
  125. notificationSetupAlert.addAction(cancelAction)
  126. present(notificationSetupAlert, animated: true, completion: nil)
  127. }
  128. @objc private func editButtonPressed() {
  129. coordinator?.showEditContact(contactId: contactId)
  130. }
  131. }