ContactDetailViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import UIKit
  2. import DcCore
  3. // this is also used as ChatDetail for SingleChats
  4. class ContactDetailViewController: UITableViewController {
  5. private let viewModel: ContactDetailViewModel
  6. private lazy var headerCell: ContactDetailHeader = {
  7. let cell = ContactDetailHeader()
  8. cell.updateDetails(title: viewModel.contact.displayName, subtitle: viewModel.contact.email)
  9. if let img = viewModel.contact.profileImage {
  10. cell.setImage(img)
  11. } else {
  12. cell.setBackupImage(name: viewModel.contact.displayName, color: viewModel.contact.color)
  13. }
  14. cell.setVerified(isVerified: viewModel.contact.isVerified)
  15. return cell
  16. }()
  17. private lazy var startChatCell: ActionCell = {
  18. let cell = ActionCell()
  19. cell.actionColor = SystemColor.blue.uiColor
  20. cell.actionTitle = String.localized("send_message")
  21. cell.selectionStyle = .none
  22. return cell
  23. }()
  24. private lazy var ephemeralMessagesCell: UITableViewCell = {
  25. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  26. cell.textLabel?.text = String.localized("pref_ephemeral_messages")
  27. cell.selectionStyle = .none
  28. cell.accessoryType = .disclosureIndicator
  29. return cell
  30. }()
  31. private lazy var blockContactCell: ActionCell = {
  32. let cell = ActionCell()
  33. cell.actionTitle = viewModel.contact.isBlocked ? String.localized("menu_unblock_contact") : String.localized("menu_block_contact")
  34. cell.actionColor = viewModel.contact.isBlocked ? SystemColor.blue.uiColor : UIColor.red
  35. cell.selectionStyle = .none
  36. return cell
  37. }()
  38. private lazy var muteChatCell: ActionCell = {
  39. let cell = ActionCell()
  40. cell.actionTitle = viewModel.chatIsMuted ? String.localized("menu_unmute") : String.localized("menu_mute")
  41. cell.actionColor = SystemColor.blue.uiColor
  42. cell.selectionStyle = .none
  43. return cell
  44. }()
  45. private lazy var archiveChatCell: ActionCell = {
  46. let cell = ActionCell()
  47. cell.actionTitle = viewModel.chatIsArchived ? String.localized("menu_unarchive_chat") : String.localized("menu_archive_chat")
  48. cell.actionColor = SystemColor.blue.uiColor
  49. cell.selectionStyle = .none
  50. return cell
  51. }()
  52. private lazy var deleteChatCell: ActionCell = {
  53. let cell = ActionCell()
  54. cell.actionTitle = String.localized("menu_delete_chat")
  55. cell.actionColor = UIColor.red
  56. cell.selectionStyle = .none
  57. return cell
  58. }()
  59. private lazy var galleryCell: UITableViewCell = {
  60. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  61. cell.textLabel?.text = String.localized("gallery")
  62. cell.accessoryType = .disclosureIndicator
  63. if viewModel.chatId == 0 {
  64. cell.isUserInteractionEnabled = false
  65. cell.textLabel?.isEnabled = false
  66. }
  67. return cell
  68. }()
  69. private lazy var documentsCell: UITableViewCell = {
  70. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  71. cell.textLabel?.text = String.localized("documents")
  72. cell.accessoryType = .disclosureIndicator
  73. if viewModel.chatId == 0 {
  74. cell.isUserInteractionEnabled = false
  75. cell.textLabel?.isEnabled = false
  76. }
  77. return cell
  78. }()
  79. init(dcContext: DcContext, contactId: Int) {
  80. self.viewModel = ContactDetailViewModel(dcContext: dcContext, contactId: contactId)
  81. super.init(style: .grouped)
  82. }
  83. required init?(coder _: NSCoder) {
  84. fatalError("init(coder:) has not been implemented")
  85. }
  86. // MARK: - lifecycle
  87. override func viewDidLoad() {
  88. super.viewDidLoad()
  89. configureTableView()
  90. navigationItem.rightBarButtonItem = UIBarButtonItem(
  91. title: String.localized("global_menu_edit_desktop"),
  92. style: .plain, target: self, action: #selector(editButtonPressed))
  93. self.title = String.localized("tab_contact")
  94. }
  95. override func viewWillAppear(_ animated: Bool) {
  96. super.viewWillAppear(animated)
  97. updateHeader() // maybe contact name has been edited
  98. tableView.reloadData()
  99. }
  100. // MARK: - setup and configuration
  101. private func configureTableView() {
  102. tableView.register(ActionCell.self, forCellReuseIdentifier: ActionCell.reuseIdentifier)
  103. tableView.register(ContactCell.self, forCellReuseIdentifier: ContactCell.reuseIdentifier)
  104. headerCell.frame = CGRect(0, 0, tableView.frame.width, ContactCell.cellHeight)
  105. tableView.tableHeaderView = headerCell
  106. }
  107. // MARK: - UITableViewDatasource, UITableViewDelegate
  108. override func numberOfSections(in tableView: UITableView) -> Int {
  109. return viewModel.numberOfSections
  110. }
  111. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  112. return viewModel.numberOfRowsInSection(section)
  113. }
  114. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  115. let row = indexPath.row
  116. let cellType = viewModel.typeFor(section: indexPath.section)
  117. switch cellType {
  118. case .attachments:
  119. switch viewModel.attachmentActionFor(row: row) {
  120. case .documents:
  121. return documentsCell
  122. case .gallery:
  123. return galleryCell
  124. }
  125. case .chatActions:
  126. switch viewModel.chatActionFor(row: row) {
  127. case .ephemeralMessages:
  128. return ephemeralMessagesCell
  129. case .muteChat:
  130. return muteChatCell
  131. case .archiveChat:
  132. return archiveChatCell
  133. case .blockContact:
  134. return blockContactCell
  135. case .deleteChat:
  136. return deleteChatCell
  137. }
  138. case .startChat:
  139. return startChatCell
  140. case .sharedChats:
  141. if let cell = tableView.dequeueReusableCell(withIdentifier: ContactCell.reuseIdentifier, for: indexPath) as? ContactCell {
  142. viewModel.update(sharedChatCell: cell, row: row)
  143. return cell
  144. }
  145. }
  146. return UITableViewCell() // should never get here
  147. }
  148. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  149. let type = viewModel.typeFor(section: indexPath.section)
  150. switch type {
  151. case .attachments:
  152. handleAttachmentAction(for: indexPath.row)
  153. case .chatActions:
  154. handleCellAction(for: indexPath.row)
  155. case .startChat:
  156. let contactId = viewModel.contactId
  157. chatWith(contactId: contactId)
  158. case .sharedChats:
  159. let chatId = viewModel.getSharedChatIdAt(indexPath: indexPath)
  160. showChat(chatId: chatId)
  161. }
  162. }
  163. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  164. let type = viewModel.typeFor(section: indexPath.section)
  165. switch type {
  166. case .chatActions, .startChat, .attachments:
  167. return Constants.defaultCellHeight
  168. case .sharedChats:
  169. return ContactCell.cellHeight
  170. }
  171. }
  172. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  173. return viewModel.titleFor(section: section)
  174. }
  175. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  176. return Constants.defaultHeaderHeight
  177. }
  178. // MARK: - updates
  179. private func updateHeader() {
  180. headerCell.updateDetails(title: viewModel.contact.displayName, subtitle: viewModel.contact.email)
  181. if let img = viewModel.contact.profileImage {
  182. headerCell.setImage(img)
  183. } else {
  184. headerCell.setBackupImage(name: viewModel.contact.displayName, color: viewModel.contact.color)
  185. }
  186. headerCell.setVerified(isVerified: viewModel.contact.isVerified)
  187. }
  188. // MARK: - actions
  189. private func handleCellAction(for index: Int) {
  190. let action = viewModel.chatActionFor(row: index)
  191. switch action {
  192. case .ephemeralMessages:
  193. showEphemeralMessagesController()
  194. case .muteChat:
  195. if viewModel.chatIsMuted {
  196. self.viewModel.context.setChatMuteDuration(chatId: self.viewModel.chatId, duration: 0)
  197. muteChatCell.actionTitle = String.localized("menu_mute")
  198. } else {
  199. showMuteAlert()
  200. }
  201. case .archiveChat:
  202. toggleArchiveChat()
  203. case .blockContact:
  204. toggleBlockContact()
  205. case .deleteChat:
  206. showDeleteChatConfirmationAlert()
  207. }
  208. }
  209. private func handleAttachmentAction(for index: Int) {
  210. let action = viewModel.attachmentActionFor(row: index)
  211. switch action {
  212. case .documents:
  213. showDocuments()
  214. case .gallery:
  215. showGallery()
  216. }
  217. }
  218. private func toggleArchiveChat() {
  219. let archived = viewModel.toggleArchiveChat()
  220. if archived {
  221. self.navigationController?.popToRootViewController(animated: false)
  222. } else {
  223. archiveChatCell.actionTitle = String.localized("menu_archive_chat")
  224. }
  225. }
  226. private func updateBlockContactCell() {
  227. blockContactCell.actionTitle = viewModel.contact.isBlocked ? String.localized("menu_unblock_contact") : String.localized("menu_block_contact")
  228. blockContactCell.actionColor = viewModel.contact.isBlocked ? SystemColor.blue.uiColor : UIColor.red
  229. }
  230. @objc private func editButtonPressed() {
  231. showEditContact(contactId: viewModel.contactId)
  232. }
  233. // MARK: alerts
  234. private func showDeleteChatConfirmationAlert() {
  235. let alert = UIAlertController(
  236. title: nil,
  237. message: String.localized("ask_delete_chat_desktop"),
  238. preferredStyle: .safeActionSheet
  239. )
  240. alert.addAction(UIAlertAction(title: String.localized("menu_delete_chat"), style: .destructive, handler: { _ in
  241. self.deleteChat()
  242. }))
  243. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  244. self.present(alert, animated: true, completion: nil)
  245. }
  246. private func showEphemeralMessagesController() {
  247. let ephemeralMessagesController = SettingsEphemeralMessageController(dcContext: viewModel.context, chatId: viewModel.chatId)
  248. navigationController?.pushViewController(ephemeralMessagesController, animated: true)
  249. }
  250. private func showMuteAlert() {
  251. let alert = UIAlertController(title: String.localized("mute"), message: nil, preferredStyle: .safeActionSheet)
  252. let forever = -1
  253. addDurationSelectionAction(to: alert, key: "mute_for_one_hour", duration: Time.oneHour)
  254. addDurationSelectionAction(to: alert, key: "mute_for_two_hours", duration: Time.twoHours)
  255. addDurationSelectionAction(to: alert, key: "mute_for_one_day", duration: Time.oneDay)
  256. addDurationSelectionAction(to: alert, key: "mute_for_seven_days", duration: Time.oneWeek)
  257. addDurationSelectionAction(to: alert, key: "mute_forever", duration: forever)
  258. let cancelAction = UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil)
  259. alert.addAction(cancelAction)
  260. present(alert, animated: true, completion: nil)
  261. }
  262. private func addDurationSelectionAction(to alert: UIAlertController, key: String, duration: Int) {
  263. let action = UIAlertAction(title: String.localized(key), style: .default, handler: { _ in
  264. self.viewModel.context.setChatMuteDuration(chatId: self.viewModel.chatId, duration: duration)
  265. self.muteChatCell.actionTitle = String.localized("menu_unmute")
  266. })
  267. alert.addAction(action)
  268. }
  269. private func toggleBlockContact() {
  270. if viewModel.contact.isBlocked {
  271. let alert = UIAlertController(title: String.localized("ask_unblock_contact"), message: nil, preferredStyle: .safeActionSheet)
  272. alert.addAction(UIAlertAction(title: String.localized("menu_unblock_contact"), style: .default, handler: { _ in
  273. self.viewModel.contact.unblock()
  274. self.updateBlockContactCell()
  275. }))
  276. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  277. present(alert, animated: true, completion: nil)
  278. } else {
  279. let alert = UIAlertController(title: String.localized("ask_block_contact"), message: nil, preferredStyle: .safeActionSheet)
  280. alert.addAction(UIAlertAction(title: String.localized("menu_block_contact"), style: .destructive, handler: { _ in
  281. self.viewModel.contact.block()
  282. self.updateBlockContactCell()
  283. }))
  284. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  285. present(alert, animated: true, completion: nil)
  286. }
  287. }
  288. private func chatWith(contactId: Int) {
  289. let chatId = self.viewModel.context.createChatByContactId(contactId: contactId)
  290. self.showChat(chatId: chatId)
  291. }
  292. // MARK: - coordinator
  293. private func showChat(chatId: Int) {
  294. if let chatlistViewController = navigationController?.viewControllers[0] {
  295. let chatViewController = ChatViewController(dcContext: viewModel.context, chatId: chatId)
  296. navigationController?.setViewControllers([chatlistViewController, chatViewController], animated: true)
  297. }
  298. }
  299. private func showEditContact(contactId: Int) {
  300. let editContactController = EditContactController(dcContext: viewModel.context, contactIdForUpdate: contactId)
  301. navigationController?.pushViewController(editContactController, animated: true)
  302. }
  303. private func showDocuments() {
  304. let messageIds = viewModel.context.getChatMedia(
  305. chatId: viewModel.chatId,
  306. messageType: DC_MSG_FILE,
  307. messageType2: DC_MSG_AUDIO,
  308. messageType3: 0
  309. )
  310. let fileGalleryController = DocumentGalleryController(fileMessageIds: messageIds)
  311. navigationController?.pushViewController(fileGalleryController, animated: true)
  312. }
  313. private func showGallery() {
  314. let messageIds = viewModel.context.getChatMedia(
  315. chatId: viewModel.chatId,
  316. messageType: DC_MSG_IMAGE,
  317. messageType2: DC_MSG_GIF,
  318. messageType3: DC_MSG_VIDEO
  319. )
  320. let galleryController = GalleryViewController(mediaMessageIds: messageIds)
  321. navigationController?.pushViewController(galleryController, animated: true)
  322. }
  323. private func deleteChat() {
  324. if viewModel.chatId == 0 {
  325. return
  326. }
  327. viewModel.context.deleteChat(chatId: viewModel.chatId)
  328. // just pop to viewControllers - we've in chatlist or archive then
  329. // (no not use `navigationController?` here: popping self will make the reference becoming nil)
  330. if let navigationController = navigationController {
  331. navigationController.popViewController(animated: false)
  332. navigationController.popViewController(animated: true)
  333. }
  334. }
  335. }