GroupChatDetailViewController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. import UIKit
  2. import DcCore
  3. import QuickLook
  4. import Intents
  5. class GroupChatDetailViewController: UIViewController {
  6. enum ProfileSections {
  7. case chatOptions
  8. case members
  9. case chatActions
  10. }
  11. enum ChatOption {
  12. case gallery
  13. case documents
  14. case search
  15. case ephemeralMessages
  16. case muteChat
  17. }
  18. enum ChatAction {
  19. case archiveChat
  20. case leaveGroup
  21. case deleteChat
  22. }
  23. private var chatOptions: [ChatOption]
  24. private var chatActions: [ChatAction]
  25. private let membersRowAddMembers = 0
  26. private let membersRowQrInvite = 1
  27. private var memberManagementRows: Int
  28. private let dcContext: DcContext
  29. private let sections: [ProfileSections]
  30. private var chatId: Int
  31. private var chat: DcChat {
  32. return dcContext.getChat(chatId: chatId)
  33. }
  34. var chatIsEphemeral: Bool {
  35. return chatId != 0 && dcContext.getChatEphemeralTimer(chatId: chatId) > 0
  36. }
  37. private var galleryItemMessageIds: [Int] {
  38. return dcContext.getChatMedia(
  39. chatId: chatId,
  40. messageType: DC_MSG_IMAGE,
  41. messageType2: DC_MSG_GIF,
  42. messageType3: DC_MSG_VIDEO
  43. )
  44. }
  45. private var documentItemMessageIds: [Int] {
  46. return dcContext.getChatMedia(
  47. chatId: chatId,
  48. messageType: DC_MSG_FILE,
  49. messageType2: DC_MSG_AUDIO,
  50. messageType3: 0
  51. )
  52. }
  53. // stores contactIds
  54. private var groupMemberIds: [Int] = []
  55. private var incomingMsgsObserver: NSObjectProtocol?
  56. private var ephemeralTimerObserver: NSObjectProtocol?
  57. private var chatModifiedObserver: NSObjectProtocol?
  58. // MARK: - subviews
  59. private lazy var editBarButtonItem: UIBarButtonItem = {
  60. UIBarButtonItem(title: String.localized("global_menu_edit_desktop"), style: .plain, target: self, action: #selector(editButtonPressed))
  61. }()
  62. lazy var tableView: UITableView = {
  63. let table = UITableView(frame: .zero, style: .grouped)
  64. table.register(UITableViewCell.self, forCellReuseIdentifier: "tableCell")
  65. table.register(ActionCell.self, forCellReuseIdentifier: "actionCell")
  66. table.register(ContactCell.self, forCellReuseIdentifier: "contactCell")
  67. table.delegate = self
  68. table.dataSource = self
  69. table.tableHeaderView = groupHeader
  70. return table
  71. }()
  72. private lazy var groupHeader: ContactDetailHeader = {
  73. let header = ContactDetailHeader()
  74. header.updateDetails(
  75. title: chat.name,
  76. subtitle: String.localizedStringWithFormat(String.localized("n_members"), chat.getContactIds(dcContext).count)
  77. )
  78. if let img = chat.profileImage {
  79. header.setImage(img)
  80. } else {
  81. header.setBackupImage(name: chat.name, color: chat.color)
  82. }
  83. header.onAvatarTap = showGroupAvatarIfNeeded
  84. header.setVerified(isVerified: chat.isProtected)
  85. return header
  86. }()
  87. private lazy var ephemeralMessagesCell: UITableViewCell = {
  88. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  89. cell.textLabel?.text = String.localized("ephemeral_messages")
  90. cell.accessoryType = .disclosureIndicator
  91. return cell
  92. }()
  93. private lazy var muteChatCell: ActionCell = {
  94. let cell = ActionCell()
  95. cell.actionTitle = self.chat.isMuted ? String.localized("menu_unmute") : String.localized("menu_mute")
  96. cell.actionColor = SystemColor.blue.uiColor
  97. return cell
  98. }()
  99. private lazy var archiveChatCell: ActionCell = {
  100. let cell = ActionCell()
  101. cell.actionTitle = chat.isArchived ? String.localized("menu_unarchive_chat") : String.localized("menu_archive_chat")
  102. cell.actionColor = UIColor.systemBlue
  103. return cell
  104. }()
  105. private lazy var leaveGroupCell: ActionCell = {
  106. let cell = ActionCell()
  107. cell.actionTitle = String.localized("menu_leave_group")
  108. cell.actionColor = UIColor.red
  109. return cell
  110. }()
  111. private lazy var deleteChatCell: ActionCell = {
  112. let cell = ActionCell()
  113. cell.actionTitle = String.localized("menu_delete_chat")
  114. cell.actionColor = UIColor.red
  115. return cell
  116. }()
  117. private lazy var galleryCell: UITableViewCell = {
  118. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  119. cell.textLabel?.text = String.localized("images_and_videos")
  120. cell.accessoryType = .disclosureIndicator
  121. return cell
  122. }()
  123. private lazy var documentsCell: UITableViewCell = {
  124. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  125. cell.textLabel?.text = String.localized("files")
  126. cell.accessoryType = .disclosureIndicator
  127. return cell
  128. }()
  129. private lazy var searchCell: UITableViewCell = {
  130. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  131. cell.textLabel?.text = String.localized("search")
  132. cell.accessoryType = .disclosureIndicator
  133. if chatId == 0 {
  134. cell.isUserInteractionEnabled = false
  135. cell.textLabel?.isEnabled = false
  136. }
  137. return cell
  138. }()
  139. init(chatId: Int, dcContext: DcContext) {
  140. self.dcContext = dcContext
  141. self.chatId = chatId
  142. let chat = dcContext.getChat(chatId: chatId)
  143. self.chatActions = []
  144. self.chatOptions = []
  145. self.memberManagementRows = 0
  146. if chat.isMailinglist {
  147. self.sections = [.chatOptions, .chatActions]
  148. } else if chat.isBroadcast {
  149. self.sections = [.chatOptions, .members, .chatActions]
  150. } else {
  151. self.sections = [.chatOptions, .members, .chatActions]
  152. }
  153. super.init(nibName: nil, bundle: nil)
  154. setupSubviews()
  155. }
  156. required init?(coder _: NSCoder) {
  157. fatalError("init(coder:) has not been implemented")
  158. }
  159. private func setupSubviews() {
  160. view.addSubview(tableView)
  161. tableView.translatesAutoresizingMaskIntoConstraints = false
  162. tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
  163. tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
  164. tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
  165. tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  166. }
  167. // MARK: - lifecycle
  168. override func viewDidLoad() {
  169. super.viewDidLoad()
  170. if chat.isMailinglist {
  171. title = String.localized("mailing_list")
  172. } else if chat.isBroadcast {
  173. title = String.localized("broadcast_list")
  174. } else {
  175. title = String.localized("tab_group")
  176. }
  177. navigationItem.rightBarButtonItem = editBarButtonItem
  178. groupHeader.frame = CGRect(0, 0, tableView.frame.width, ContactCell.cellHeight)
  179. }
  180. override func viewWillAppear(_ animated: Bool) {
  181. super.viewWillAppear(animated)
  182. updateGroupMembers()
  183. updateOptions()
  184. tableView.reloadData()
  185. setupObservers()
  186. updateHeader()
  187. updateMediaCellValues()
  188. updateEphemeralTimerCellValue()
  189. }
  190. override func viewWillDisappear(_ animated: Bool) {
  191. removeObservers()
  192. }
  193. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  194. if previousTraitCollection?.preferredContentSizeCategory !=
  195. traitCollection.preferredContentSizeCategory {
  196. groupHeader.frame = CGRect(0, 0, tableView.frame.width, ContactCell.cellHeight)
  197. }
  198. }
  199. // MARK: - observers
  200. private func setupObservers() {
  201. let nc = NotificationCenter.default
  202. incomingMsgsObserver = nc.addObserver(
  203. forName: dcNotificationIncoming,
  204. object: nil,
  205. queue: OperationQueue.main) { [weak self] notification in
  206. guard let self = self else { return }
  207. if let ui = notification.userInfo,
  208. self.chatId == ui["chat_id"] as? Int {
  209. self.updateMediaCellValues()
  210. }
  211. }
  212. ephemeralTimerObserver = nc.addObserver(
  213. forName: dcEphemeralTimerModified,
  214. object: nil,
  215. queue: OperationQueue.main) { [weak self] notification in
  216. guard let self = self else { return }
  217. if let ui = notification.userInfo,
  218. self.chatId == ui["chat_id"] as? Int {
  219. self.updateEphemeralTimerCellValue()
  220. }
  221. }
  222. chatModifiedObserver = nc.addObserver(
  223. forName: dcNotificationChatModified,
  224. object: nil,
  225. queue: OperationQueue.main) { [weak self] notification in
  226. guard let self = self else { return }
  227. if let ui = notification.userInfo,
  228. self.chatId == ui["chat_id"] as? Int {
  229. self.updateHeader()
  230. self.updateGroupMembers()
  231. self.updateOptions()
  232. self.tableView.reloadData()
  233. }
  234. }
  235. }
  236. private func removeObservers() {
  237. let nc = NotificationCenter.default
  238. if let msgChangedObserver = self.incomingMsgsObserver {
  239. nc.removeObserver(msgChangedObserver)
  240. }
  241. if let ephemeralTimerObserver = self.ephemeralTimerObserver {
  242. nc.removeObserver(ephemeralTimerObserver)
  243. }
  244. if let chatModifiedObserver = self.chatModifiedObserver {
  245. nc.removeObserver(chatModifiedObserver)
  246. }
  247. }
  248. // MARK: - update
  249. private func updateGroupMembers() {
  250. groupMemberIds = chat.getContactIds(dcContext)
  251. }
  252. private func updateOptions() {
  253. self.editBarButtonItem.isEnabled = chat.isMailinglist || chat.canSend
  254. if chat.isMailinglist {
  255. self.chatOptions = [.gallery, .documents, .search, .muteChat]
  256. self.memberManagementRows = 0
  257. self.chatActions = [.archiveChat, .deleteChat]
  258. } else if chat.isBroadcast {
  259. self.chatOptions = [.gallery, .documents, .search]
  260. self.memberManagementRows = 1
  261. self.chatActions = [.archiveChat, .deleteChat]
  262. } else if chat.canSend {
  263. self.chatOptions = [.gallery, .documents, .search, .ephemeralMessages, .muteChat]
  264. self.memberManagementRows = 2
  265. self.chatActions = [.archiveChat, .leaveGroup, .deleteChat]
  266. } else {
  267. self.chatOptions = [.gallery, .documents, .search, .muteChat]
  268. self.memberManagementRows = 0
  269. self.chatActions = [.archiveChat, .deleteChat]
  270. }
  271. }
  272. private func updateHeader() {
  273. groupHeader.updateDetails(title: chat.name, subtitle: nil)
  274. if let img = chat.profileImage {
  275. groupHeader.setImage(img)
  276. } else {
  277. groupHeader.setBackupImage(name: chat.name, color: chat.color)
  278. }
  279. groupHeader.setVerified(isVerified: chat.isProtected)
  280. }
  281. private func updateEphemeralTimerCellValue() {
  282. ephemeralMessagesCell.detailTextLabel?.text = String.localized(chatIsEphemeral ? "on" : "off")
  283. }
  284. private func updateMediaCellValues() {
  285. galleryCell.detailTextLabel?.text = String.numberOrNone(galleryItemMessageIds.count)
  286. documentsCell.detailTextLabel?.text = String.numberOrNone(documentItemMessageIds.count)
  287. }
  288. // MARK: - actions
  289. @objc func editButtonPressed() {
  290. showGroupChatEdit(chat: chat)
  291. }
  292. private func toggleArchiveChat() {
  293. let archivedBefore = chat.isArchived
  294. if !archivedBefore {
  295. NotificationManager.removeNotificationsForChat(dcContext: dcContext, chatId: chatId)
  296. }
  297. dcContext.archiveChat(chatId: chat.id, archive: !archivedBefore)
  298. if archivedBefore {
  299. archiveChatCell.actionTitle = String.localized("menu_archive_chat")
  300. } else {
  301. self.navigationController?.popToRootViewController(animated: false)
  302. }
  303. }
  304. private func getGroupMemberIdFor(_ row: Int) -> Int {
  305. let index = row - memberManagementRows
  306. if index >= 0 && index < groupMemberIds.count {
  307. return groupMemberIds[index]
  308. } else {
  309. return 0
  310. }
  311. }
  312. private func isMemberManagementRow(row: Int) -> Bool {
  313. return row < memberManagementRows
  314. }
  315. // MARK: - coordinator
  316. private func showSingleChatEdit(contactId: Int) {
  317. let editContactController = EditContactController(dcContext: dcContext, contactIdForUpdate: contactId)
  318. navigationController?.pushViewController(editContactController, animated: true)
  319. }
  320. private func showAddGroupMember(chatId: Int) {
  321. let groupMemberViewController = AddGroupMembersViewController(dcContext: dcContext, chatId: chatId)
  322. groupMemberViewController.onMembersSelected = { [weak self] (memberIds: Set<Int>) -> Void in
  323. guard let self = self else { return }
  324. let chat = self.dcContext.getChat(chatId: chatId)
  325. var chatMembersToRemove = chat.getContactIds(self.dcContext)
  326. chatMembersToRemove.removeAll(where: { memberIds.contains($0)})
  327. for contactId in chatMembersToRemove {
  328. _ = self.dcContext.removeContactFromChat(chatId: chatId, contactId: contactId)
  329. }
  330. for contactId in memberIds {
  331. _ = self.dcContext.addContactToChat(chatId: chatId, contactId: contactId)
  332. }
  333. }
  334. navigationController?.pushViewController(groupMemberViewController, animated: true)
  335. }
  336. private func showQrCodeInvite(chatId: Int) {
  337. var hint = ""
  338. let dcChat = dcContext.getChat(chatId: chatId)
  339. if !dcChat.name.isEmpty {
  340. hint = String.localizedStringWithFormat(String.localized("qrshow_join_group_hint"), dcChat.name)
  341. }
  342. let qrInviteCodeController = QrViewController(dcContext: dcContext, chatId: chatId, qrCodeHint: hint)
  343. navigationController?.pushViewController(qrInviteCodeController, animated: true)
  344. }
  345. private func showGroupChatEdit(chat: DcChat) {
  346. let editGroupViewController = EditGroupViewController(dcContext: dcContext, chat: chat)
  347. navigationController?.pushViewController(editGroupViewController, animated: true)
  348. }
  349. private func showContactDetail(of contactId: Int) {
  350. let contactDetailController = ContactDetailViewController(dcContext: dcContext, contactId: contactId)
  351. navigationController?.pushViewController(contactDetailController, animated: true)
  352. }
  353. private func showDocuments() {
  354. let messageIds: [Int] = documentItemMessageIds.reversed()
  355. let fileGalleryController = DocumentGalleryController(context: dcContext, chatId: chatId, fileMessageIds: messageIds)
  356. navigationController?.pushViewController(fileGalleryController, animated: true) }
  357. private func showGallery() {
  358. let messageIds: [Int] = galleryItemMessageIds.reversed()
  359. let galleryController = GalleryViewController(context: dcContext, chatId: chatId, mediaMessageIds: messageIds)
  360. navigationController?.pushViewController(galleryController, animated: true)
  361. }
  362. private func showSearch() {
  363. if let chatViewController = navigationController?.viewControllers.last(where: {
  364. $0 is ChatViewController
  365. }) as? ChatViewController {
  366. chatViewController.activateSearchOnAppear()
  367. navigationController?.popViewController(animated: true)
  368. }
  369. }
  370. private func deleteChat() {
  371. dcContext.deleteChat(chatId: chatId)
  372. NotificationManager.removeNotificationsForChat(dcContext: dcContext, chatId: chatId)
  373. INInteraction.delete(with: ["\(dcContext.id).\(chatId)"])
  374. navigationController?.popViewControllers(viewsToPop: 2, animated: true)
  375. }
  376. private func showGroupAvatarIfNeeded() {
  377. if let url = chat.profileImageURL {
  378. let previewController = PreviewController(dcContext: dcContext, type: .single(url))
  379. previewController.customTitle = self.title
  380. present(previewController, animated: true, completion: nil)
  381. }
  382. }
  383. }
  384. // MARK: - UITableViewDelegate, UITableViewDataSource
  385. extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSource {
  386. func numberOfSections(in _: UITableView) -> Int {
  387. return sections.count
  388. }
  389. func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
  390. let sectionType = sections[section]
  391. switch sectionType {
  392. case .chatOptions:
  393. return chatOptions.count
  394. case .members:
  395. return groupMemberIds.count + memberManagementRows
  396. case .chatActions:
  397. return chatActions.count
  398. }
  399. }
  400. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  401. let sectionType = sections[indexPath.section]
  402. let row = indexPath.row
  403. if sectionType == .members && !isMemberManagementRow(row: row) {
  404. return ContactCell.cellHeight
  405. } else {
  406. return UITableView.automaticDimension
  407. }
  408. }
  409. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  410. let row = indexPath.row
  411. let sectionType = sections[indexPath.section]
  412. switch sectionType {
  413. case .chatOptions:
  414. switch chatOptions[row] {
  415. case .gallery:
  416. return galleryCell
  417. case .documents:
  418. return documentsCell
  419. case .search:
  420. return searchCell
  421. case .ephemeralMessages:
  422. return ephemeralMessagesCell
  423. case .muteChat:
  424. return muteChatCell
  425. }
  426. case .members:
  427. if isMemberManagementRow(row: row) {
  428. guard let actionCell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath) as? ActionCell else {
  429. safe_fatalError("could not dequeue action cell")
  430. break
  431. }
  432. if row == membersRowAddMembers {
  433. actionCell.actionTitle = String.localized(chat.isBroadcast ? "add_recipients" : "group_add_members")
  434. actionCell.actionColor = UIColor.systemBlue
  435. } else if row == membersRowQrInvite {
  436. actionCell.actionTitle = String.localized("qrshow_join_group_title")
  437. actionCell.actionColor = UIColor.systemBlue
  438. }
  439. return actionCell
  440. }
  441. guard let contactCell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as? ContactCell else {
  442. safe_fatalError("could not dequeue contactCell cell")
  443. break
  444. }
  445. let contactId: Int = getGroupMemberIdFor(row)
  446. let cellData = ContactCellData(
  447. contactId: contactId,
  448. chatId: dcContext.getChatIdByContactIdOld(contactId)
  449. )
  450. let cellViewModel = ContactCellViewModel(dcContext: dcContext, contactData: cellData)
  451. contactCell.updateCell(cellViewModel: cellViewModel)
  452. return contactCell
  453. case .chatActions:
  454. switch chatActions[row] {
  455. case .archiveChat:
  456. return archiveChatCell
  457. case .leaveGroup:
  458. return leaveGroupCell
  459. case .deleteChat:
  460. return deleteChatCell
  461. }
  462. }
  463. // should never get here
  464. return UITableViewCell(frame: .zero)
  465. }
  466. func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
  467. let sectionType = sections[indexPath.section]
  468. let row = indexPath.row
  469. switch sectionType {
  470. case .chatOptions:
  471. switch chatOptions[row] {
  472. case .gallery:
  473. showGallery()
  474. case .documents:
  475. showDocuments()
  476. case .search:
  477. showSearch()
  478. case .ephemeralMessages:
  479. showEphemeralMessagesController()
  480. case .muteChat:
  481. tableView.deselectRow(at: indexPath, animated: false)
  482. if chat.isMuted {
  483. dcContext.setChatMuteDuration(chatId: chatId, duration: 0)
  484. muteChatCell.actionTitle = String.localized("menu_mute")
  485. navigationController?.popViewController(animated: true)
  486. } else {
  487. showMuteAlert()
  488. }
  489. }
  490. case .members:
  491. if isMemberManagementRow(row: row) {
  492. if row == membersRowAddMembers {
  493. showAddGroupMember(chatId: chat.id)
  494. } else if row == membersRowQrInvite {
  495. showQrCodeInvite(chatId: chat.id)
  496. }
  497. } else {
  498. let memberId = getGroupMemberIdFor(row)
  499. if memberId == DC_CONTACT_ID_SELF {
  500. tableView.deselectRow(at: indexPath, animated: true) // animated as no other elements pop up
  501. } else {
  502. showContactDetail(of: memberId)
  503. }
  504. }
  505. case .chatActions:
  506. switch chatActions[row] {
  507. case .archiveChat:
  508. tableView.deselectRow(at: indexPath, animated: true) // animated as no other elements pop up
  509. toggleArchiveChat()
  510. case .leaveGroup:
  511. tableView.deselectRow(at: indexPath, animated: false)
  512. showLeaveGroupConfirmationAlert()
  513. case .deleteChat:
  514. tableView.deselectRow(at: indexPath, animated: false)
  515. showDeleteChatConfirmationAlert()
  516. }
  517. }
  518. }
  519. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  520. if sections[section] == .members {
  521. return String.localizedStringWithFormat(String.localized(chat.isBroadcast ? "n_recipients" : "n_members"),
  522. chat.getContactIds(dcContext).count)
  523. }
  524. return nil
  525. }
  526. func tableView(_: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  527. if !chat.canSend {
  528. return false
  529. }
  530. let row = indexPath.row
  531. let sectionType = sections[indexPath.section]
  532. if sectionType == .members &&
  533. !isMemberManagementRow(row: row) &&
  534. getGroupMemberIdFor(row) != DC_CONTACT_ID_SELF {
  535. return true
  536. }
  537. return false
  538. }
  539. func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  540. if !chat.canSend {
  541. return nil
  542. }
  543. let row = indexPath.row
  544. let sectionType = sections[indexPath.section]
  545. if sectionType == .members &&
  546. !isMemberManagementRow(row: row) &&
  547. getGroupMemberIdFor(row) != DC_CONTACT_ID_SELF {
  548. // action set for members except for current user
  549. let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [weak self] _, indexPath in
  550. guard let self = self else { return }
  551. let contact = self.getGroupMember(at: row)
  552. let title = String.localizedStringWithFormat(String.localized(self.chat.isBroadcast ? "ask_remove_from_broadcast" : "ask_remove_members"), contact.nameNAddr)
  553. let alert = UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
  554. alert.addAction(UIAlertAction(title: String.localized("remove_desktop"), style: .destructive, handler: { _ in
  555. let success = self.dcContext.removeContactFromChat(chatId: self.chat.id, contactId: contact.id)
  556. if success {
  557. self.removeGroupMemberFromTableAt(indexPath)
  558. }
  559. }))
  560. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  561. self.present(alert, animated: true, completion: nil)
  562. }
  563. delete.backgroundColor = UIColor.red
  564. return [delete]
  565. }
  566. return nil
  567. }
  568. private func getGroupMember(at row: Int) -> DcContact {
  569. return dcContext.getContact(id: getGroupMemberIdFor(row))
  570. }
  571. private func removeGroupMemberFromTableAt(_ indexPath: IndexPath) {
  572. self.groupMemberIds.remove(at: indexPath.row - memberManagementRows)
  573. self.tableView.deleteRows(at: [indexPath], with: .automatic)
  574. updateHeader() // to display correct group size
  575. }
  576. private func showEphemeralMessagesController() {
  577. let ephemeralMessagesController = SettingsEphemeralMessageController(dcContext: dcContext, chatId: chatId)
  578. navigationController?.pushViewController(ephemeralMessagesController, animated: true)
  579. }
  580. }
  581. // MARK: - alerts
  582. extension GroupChatDetailViewController {
  583. private func showMuteAlert() {
  584. let alert = UIAlertController(title: String.localized("mute"), message: nil, preferredStyle: .safeActionSheet)
  585. let forever = -1
  586. addDurationSelectionAction(to: alert, key: "mute_for_one_hour", duration: Time.oneHour)
  587. addDurationSelectionAction(to: alert, key: "mute_for_two_hours", duration: Time.twoHours)
  588. addDurationSelectionAction(to: alert, key: "mute_for_one_day", duration: Time.oneDay)
  589. addDurationSelectionAction(to: alert, key: "mute_for_seven_days", duration: Time.oneWeek)
  590. addDurationSelectionAction(to: alert, key: "mute_forever", duration: forever)
  591. let cancelAction = UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil)
  592. alert.addAction(cancelAction)
  593. present(alert, animated: true, completion: nil)
  594. }
  595. private func addDurationSelectionAction(to alert: UIAlertController, key: String, duration: Int) {
  596. let action = UIAlertAction(title: String.localized(key), style: .default, handler: { _ in
  597. self.dcContext.setChatMuteDuration(chatId: self.chatId, duration: duration)
  598. self.muteChatCell.actionTitle = String.localized("menu_unmute")
  599. self.navigationController?.popViewController(animated: true)
  600. })
  601. alert.addAction(action)
  602. }
  603. private func showDeleteChatConfirmationAlert() {
  604. let alert = UIAlertController(
  605. title: nil,
  606. message: String.localizedStringWithFormat(String.localized("ask_delete_named_chat"), dcContext.getChat(chatId: chatId).name),
  607. preferredStyle: .safeActionSheet
  608. )
  609. alert.addAction(UIAlertAction(title: String.localized("menu_delete_chat"), style: .destructive, handler: { _ in
  610. self.deleteChat()
  611. }))
  612. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  613. self.present(alert, animated: true, completion: nil)
  614. }
  615. private func showLeaveGroupConfirmationAlert() {
  616. let alert = UIAlertController(title: String.localized("ask_leave_group"), message: nil, preferredStyle: .safeActionSheet)
  617. alert.addAction(UIAlertAction(title: String.localized("menu_leave_group"), style: .destructive, handler: { _ in
  618. _ = self.dcContext.removeContactFromChat(chatId: self.chat.id, contactId: Int(DC_CONTACT_ID_SELF))
  619. }))
  620. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  621. present(alert, animated: true, completion: nil)
  622. }
  623. }