|
@@ -2,35 +2,89 @@ import UIKit
|
|
|
|
|
|
class GroupChatDetailViewController: UIViewController {
|
|
class GroupChatDetailViewController: UIViewController {
|
|
|
|
|
|
- private let sectionMembers = 0
|
|
|
|
- private let sectionMembersRowAddMember = 0
|
|
|
|
- private let sectionMembersRowJoinQR = 1
|
|
|
|
- private let sectionMembersStaticRowCount = 2 // followed by one row per member
|
|
|
|
|
|
+ enum ProfileSections {
|
|
|
|
+ case memberManagement // add member, qr invideCode
|
|
|
|
+ case members // contactCells
|
|
|
|
+ case chatActions // archive, leave, delete
|
|
|
|
+ }
|
|
|
|
|
|
- private let sectionLeaveGroup = 1
|
|
|
|
- private let sectionLeaveGroupRowCount = 1
|
|
|
|
|
|
+ private let context: DcContext
|
|
|
|
+ weak var coordinator: GroupChatDetailCoordinator?
|
|
|
|
|
|
- private let sectionCount = 2
|
|
|
|
|
|
+ private let sections: [ProfileSections] = [.memberManagement, .members, .chatActions]
|
|
|
|
|
|
private var currentUser: DcContact? {
|
|
private var currentUser: DcContact? {
|
|
- return groupMembers.filter { $0.email == DcConfig.addr }.first
|
|
|
|
|
|
+ let myId = groupMemberIds.filter { DcContact(id: $0).email == DcConfig.addr }.first
|
|
|
|
+ guard let currentUserId = myId else {
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+ return DcContact(id: currentUserId)
|
|
}
|
|
}
|
|
|
|
|
|
- weak var coordinator: GroupChatDetailCoordinator?
|
|
|
|
-
|
|
|
|
fileprivate var chat: DcChat
|
|
fileprivate var chat: DcChat
|
|
|
|
|
|
- var chatDetailTable: UITableView = {
|
|
|
|
|
|
+ // stores contactIds
|
|
|
|
+ private var groupMemberIds: [Int] = []
|
|
|
|
+
|
|
|
|
+ // MARK: -subviews
|
|
|
|
+
|
|
|
|
+ private lazy var editBarButtonItem: UIBarButtonItem = {
|
|
|
|
+ UIBarButtonItem(title: String.localized("global_menu_edit_desktop"), style: .plain, target: self, action: #selector(editButtonPressed))
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ lazy var tableView: UITableView = {
|
|
let table = UITableView(frame: .zero, style: .grouped)
|
|
let table = UITableView(frame: .zero, style: .grouped)
|
|
table.bounces = false
|
|
table.bounces = false
|
|
table.register(UITableViewCell.self, forCellReuseIdentifier: "tableCell")
|
|
table.register(UITableViewCell.self, forCellReuseIdentifier: "tableCell")
|
|
table.register(ActionCell.self, forCellReuseIdentifier: "actionCell")
|
|
table.register(ActionCell.self, forCellReuseIdentifier: "actionCell")
|
|
table.register(ContactCell.self, forCellReuseIdentifier: "contactCell")
|
|
table.register(ContactCell.self, forCellReuseIdentifier: "contactCell")
|
|
-
|
|
|
|
|
|
+ table.delegate = self
|
|
|
|
+ table.dataSource = self
|
|
|
|
+ table.tableHeaderView = groupHeader
|
|
return table
|
|
return table
|
|
}()
|
|
}()
|
|
|
|
|
|
- init(chatId: Int) {
|
|
|
|
|
|
+ private lazy var groupHeader: ContactDetailHeader = {
|
|
|
|
+ let header = ContactDetailHeader()
|
|
|
|
+ header.updateDetails(
|
|
|
|
+ title: chat.name,
|
|
|
|
+ subtitle: String.localizedStringWithFormat(String.localized("n_members"), chat.contactIds.count)
|
|
|
|
+ )
|
|
|
|
+ if let img = chat.profileImage {
|
|
|
|
+ header.setImage(img)
|
|
|
|
+ } else {
|
|
|
|
+ header.setBackupImage(name: chat.name, color: chat.color)
|
|
|
|
+ }
|
|
|
|
+ header.setVerified(isVerified: chat.isVerified)
|
|
|
|
+ return header
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ private lazy var archiveChatCell: ActionCell = {
|
|
|
|
+ let cell = ActionCell()
|
|
|
|
+ cell.actionTitle = chat.isArchived ? String.localized("menu_unarchive_chat") : String.localized("menu_archive_chat")
|
|
|
|
+ cell.actionColor = SystemColor.blue.uiColor
|
|
|
|
+ cell.selectionStyle = .none
|
|
|
|
+ return cell
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ private lazy var leaveGroupCell: ActionCell = {
|
|
|
|
+ let cell = ActionCell()
|
|
|
|
+ cell.actionTitle = String.localized("menu_leave_group")
|
|
|
|
+ cell.actionColor = UIColor.red
|
|
|
|
+ return cell
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private lazy var deleteChatCell: ActionCell = {
|
|
|
|
+ let cell = ActionCell()
|
|
|
|
+ cell.actionTitle = String.localized("menu_delete_chat")
|
|
|
|
+ cell.actionColor = UIColor.red
|
|
|
|
+ cell.selectionStyle = .none
|
|
|
|
+ return cell
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ init(chatId: Int, context: DcContext) {
|
|
|
|
+ self.context = context
|
|
chat = DcChat(id: chatId)
|
|
chat = DcChat(id: chatId)
|
|
super.init(nibName: nil, bundle: nil)
|
|
super.init(nibName: nil, bundle: nil)
|
|
setupSubviews()
|
|
setupSubviews()
|
|
@@ -41,209 +95,187 @@ class GroupChatDetailViewController: UIViewController {
|
|
}
|
|
}
|
|
|
|
|
|
private func setupSubviews() {
|
|
private func setupSubviews() {
|
|
- view.addSubview(chatDetailTable)
|
|
|
|
- chatDetailTable.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
+ view.addSubview(tableView)
|
|
|
|
+ tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
- chatDetailTable.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
|
|
|
|
- chatDetailTable.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
|
|
|
|
- chatDetailTable.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
|
|
|
|
- chatDetailTable.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
|
|
|
|
|
|
+ tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
|
|
|
|
+ tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
|
|
|
|
+ tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
|
|
|
|
+ tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
|
|
}
|
|
}
|
|
|
|
|
|
- private lazy var editBarButtonItem: UIBarButtonItem = {
|
|
|
|
- UIBarButtonItem(title: String.localized("global_menu_edit_desktop"), style: .plain, target: self, action: #selector(editButtonPressed))
|
|
|
|
- }()
|
|
|
|
-
|
|
|
|
- private var groupMembers: [DcContact] = []
|
|
|
|
-
|
|
|
|
|
|
+ // MARK: - lifecycle
|
|
override func viewDidLoad() {
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
super.viewDidLoad()
|
|
title = String.localized("tab_group")
|
|
title = String.localized("tab_group")
|
|
- chatDetailTable.delegate = self
|
|
|
|
- chatDetailTable.dataSource = self
|
|
|
|
navigationItem.rightBarButtonItem = editBarButtonItem
|
|
navigationItem.rightBarButtonItem = editBarButtonItem
|
|
|
|
+ groupHeader.frame = CGRect(0, 0, tableView.frame.width, ContactCell.cellHeight)
|
|
}
|
|
}
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
super.viewWillAppear(animated)
|
|
updateGroupMembers()
|
|
updateGroupMembers()
|
|
- chatDetailTable.reloadData() // to display updates
|
|
|
|
|
|
+ tableView.reloadData() // to display updates
|
|
editBarButtonItem.isEnabled = currentUser != nil
|
|
editBarButtonItem.isEnabled = currentUser != nil
|
|
|
|
+ updateHeader()
|
|
//update chat object, maybe chat name was edited
|
|
//update chat object, maybe chat name was edited
|
|
chat = DcChat(id: chat.id)
|
|
chat = DcChat(id: chat.id)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // MARK: - update
|
|
private func updateGroupMembers() {
|
|
private func updateGroupMembers() {
|
|
- let ids = chat.contactIds
|
|
|
|
- groupMembers = ids.map { DcContact(id: $0) }
|
|
|
|
- chatDetailTable.reloadData()
|
|
|
|
|
|
+ groupMemberIds = chat.contactIds
|
|
|
|
+ tableView.reloadData()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private func updateHeader() {
|
|
|
|
+ groupHeader.updateDetails(
|
|
|
|
+ title: chat.name,
|
|
|
|
+ subtitle: String.localizedStringWithFormat(String.localized("n_members"), chat.contactIds.count)
|
|
|
|
+ )
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // MARK: - actions
|
|
@objc func editButtonPressed() {
|
|
@objc func editButtonPressed() {
|
|
coordinator?.showGroupChatEdit(chat: chat)
|
|
coordinator?.showGroupChatEdit(chat: chat)
|
|
}
|
|
}
|
|
|
|
|
|
- private func leaveGroup() {
|
|
|
|
- if let userId = currentUser?.id {
|
|
|
|
- let alert = UIAlertController(title: String.localized("ask_leave_group"), message: nil, preferredStyle: .safeActionSheet)
|
|
|
|
- alert.addAction(UIAlertAction(title: String.localized("menu_leave_group"), style: .destructive, handler: { _ in
|
|
|
|
- dc_remove_contact_from_chat(mailboxPointer, UInt32(self.chat.id), UInt32(userId))
|
|
|
|
- self.editBarButtonItem.isEnabled = false
|
|
|
|
- self.updateGroupMembers()
|
|
|
|
- }))
|
|
|
|
- alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
|
|
|
|
- present(alert, animated: true, completion: nil)
|
|
|
|
|
|
+ private func toggleArchiveChat() {
|
|
|
|
+ let archivedBefore = chat.isArchived
|
|
|
|
+ context.archiveChat(chatId: chat.id, archive: !archivedBefore)
|
|
|
|
+ if archivedBefore {
|
|
|
|
+ archiveChatCell.actionTitle = String.localized("menu_archive_chat")
|
|
|
|
+ } else {
|
|
|
|
+ self.navigationController?.popToRootViewController(animated: false)
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ self.chat = DcChat(id: chat.id)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// MARK: - UITableViewDelegate, UITableViewDataSource
|
|
extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSource {
|
|
extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSource {
|
|
|
|
|
|
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
|
|
- if section == sectionMembers {
|
|
|
|
- return ContactDetailHeader.cellHeight
|
|
|
|
- }
|
|
|
|
- return 0
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- func tableView(_: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
|
|
|
- if section == sectionMembers {
|
|
|
|
- let header = ContactDetailHeader()
|
|
|
|
- header.updateDetails(title: chat.name,
|
|
|
|
- subtitle: String.localizedStringWithFormat(String.localized("n_members"), chat.contactIds.count))
|
|
|
|
- if let img = chat.profileImage {
|
|
|
|
- header.setImage(img)
|
|
|
|
- } else {
|
|
|
|
- header.setBackupImage(name: chat.name, color: chat.color)
|
|
|
|
- }
|
|
|
|
- header.setVerified(isVerified: chat.isVerified)
|
|
|
|
- return header
|
|
|
|
- } else {
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
func numberOfSections(in _: UITableView) -> Int {
|
|
func numberOfSections(in _: UITableView) -> Int {
|
|
- if currentUser == nil {
|
|
|
|
- return sectionCount-1 // leave out last section (leaveSection)
|
|
|
|
- }
|
|
|
|
- return sectionCount
|
|
|
|
|
|
+ return sections.count
|
|
}
|
|
}
|
|
|
|
|
|
func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
- switch section {
|
|
|
|
- case sectionMembers:
|
|
|
|
- return sectionMembersStaticRowCount + groupMembers.count
|
|
|
|
- case sectionLeaveGroup:
|
|
|
|
- return sectionLeaveGroupRowCount
|
|
|
|
- default:
|
|
|
|
- return 0
|
|
|
|
|
|
+ let sectionType = sections[section]
|
|
|
|
+ switch sectionType {
|
|
|
|
+ case .memberManagement:
|
|
|
|
+ return 2
|
|
|
|
+ case .members:
|
|
|
|
+ return groupMemberIds.count
|
|
|
|
+ case .chatActions:
|
|
|
|
+ return 3
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
- let section = indexPath.section
|
|
|
|
- let row = indexPath.row
|
|
|
|
- switch section {
|
|
|
|
- case sectionMembers:
|
|
|
|
- switch row {
|
|
|
|
- case sectionMembersRowAddMember:
|
|
|
|
- return Constants.defaultCellHeight
|
|
|
|
- case sectionMembersRowJoinQR:
|
|
|
|
- return Constants.defaultCellHeight
|
|
|
|
- default:
|
|
|
|
- return ContactCell.cellHeight
|
|
|
|
- }
|
|
|
|
- case sectionLeaveGroup:
|
|
|
|
|
|
+ let sectionType = sections[indexPath.section]
|
|
|
|
+ switch sectionType {
|
|
|
|
+ case .memberManagement:
|
|
|
|
+ return Constants.defaultCellHeight
|
|
|
|
+ case .members:
|
|
|
|
+ return ContactCell.cellHeight
|
|
|
|
+ case .chatActions:
|
|
return Constants.defaultCellHeight
|
|
return Constants.defaultCellHeight
|
|
- default:
|
|
|
|
- return 0
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
- let section = indexPath.section
|
|
|
|
let row = indexPath.row
|
|
let row = indexPath.row
|
|
- switch section {
|
|
|
|
- case sectionMembers:
|
|
|
|
- switch row {
|
|
|
|
- case sectionMembersRowAddMember:
|
|
|
|
- let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath)
|
|
|
|
- if let actionCell = cell as? ActionCell {
|
|
|
|
- actionCell.actionTitle = String.localized("group_add_members")
|
|
|
|
- actionCell.actionColor = UIColor.systemBlue
|
|
|
|
- }
|
|
|
|
- return cell
|
|
|
|
- case sectionMembersRowJoinQR:
|
|
|
|
- let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath)
|
|
|
|
- if let actionCell = cell as? ActionCell {
|
|
|
|
- actionCell.actionTitle = String.localized("qrshow_join_group_title")
|
|
|
|
- actionCell.actionColor = UIColor.systemBlue
|
|
|
|
- }
|
|
|
|
- return cell
|
|
|
|
- default:
|
|
|
|
- let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
|
|
|
|
- if let contactCell = cell as? ContactCell {
|
|
|
|
- let contact = groupMembers[row - sectionMembersStaticRowCount]
|
|
|
|
- let displayName = contact.displayName
|
|
|
|
- contactCell.titleLabel.text = displayName
|
|
|
|
- contactCell.subtitleLabel.text = contact.email
|
|
|
|
- contactCell.avatar.setName(displayName)
|
|
|
|
- contactCell.avatar.setColor(contact.color)
|
|
|
|
- if let profileImage = contact.profileImage {
|
|
|
|
- contactCell.avatar.setImage(profileImage)
|
|
|
|
- }
|
|
|
|
- contactCell.setVerified(isVerified: contact.isVerified)
|
|
|
|
- }
|
|
|
|
- return cell
|
|
|
|
|
|
+ let sectionType = sections[indexPath.section]
|
|
|
|
+ switch sectionType {
|
|
|
|
+ case .memberManagement:
|
|
|
|
+ guard let actionCell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath) as? ActionCell else {
|
|
|
|
+ safe_fatalError("could not dequeu action cell")
|
|
|
|
+ break
|
|
}
|
|
}
|
|
- case sectionLeaveGroup:
|
|
|
|
- let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath)
|
|
|
|
- if let actionCell = cell as? ActionCell {
|
|
|
|
- actionCell.actionTitle = String.localized("menu_leave_group")
|
|
|
|
- actionCell.actionColor = UIColor.red
|
|
|
|
|
|
+ if row == 0 {
|
|
|
|
+ actionCell.actionTitle = String.localized("group_add_members")
|
|
|
|
+ actionCell.actionColor = UIColor.systemBlue
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ actionCell.actionTitle = String.localized("qrshow_join_group_title")
|
|
|
|
+ actionCell.actionColor = UIColor.systemBlue
|
|
|
|
+ }
|
|
|
|
+ return actionCell
|
|
|
|
+ case .members:
|
|
|
|
+ guard let contactCell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as? ContactCell else {
|
|
|
|
+ safe_fatalError("could not dequeu contactCell cell")
|
|
|
|
+ break
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ let cellData = ContactCellData(contactId: groupMemberIds[row])
|
|
|
|
+ let cellViewModel = ContactCellViewModel(contactData: cellData)
|
|
|
|
+ contactCell.updateCell(cellViewModel: cellViewModel)
|
|
|
|
+ return contactCell
|
|
|
|
+ case .chatActions:
|
|
|
|
+ if row == 0 {
|
|
|
|
+ return archiveChatCell
|
|
|
|
+ } else if row == 1 {
|
|
|
|
+ return leaveGroupCell
|
|
|
|
+ } else if row == 2 {
|
|
|
|
+ return deleteChatCell
|
|
}
|
|
}
|
|
- return cell
|
|
|
|
- default:
|
|
|
|
- return UITableViewCell(frame: .zero)
|
|
|
|
}
|
|
}
|
|
|
|
+ // should never get here
|
|
|
|
+ return UITableViewCell(frame: .zero)
|
|
}
|
|
}
|
|
|
|
|
|
func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
- let section = indexPath.section
|
|
|
|
|
|
+ let sectionType = sections[indexPath.section]
|
|
let row = indexPath.row
|
|
let row = indexPath.row
|
|
- if section == sectionMembers {
|
|
|
|
- if row == sectionMembersRowAddMember {
|
|
|
|
|
|
+
|
|
|
|
+ switch sectionType {
|
|
|
|
+ case .memberManagement:
|
|
|
|
+ if row == 0 {
|
|
coordinator?.showAddGroupMember(chatId: chat.id)
|
|
coordinator?.showAddGroupMember(chatId: chat.id)
|
|
- } else if row == sectionMembersRowJoinQR {
|
|
|
|
|
|
+ } else if row == 1 {
|
|
coordinator?.showQrCodeInvite(chatId: chat.id)
|
|
coordinator?.showQrCodeInvite(chatId: chat.id)
|
|
- } else {
|
|
|
|
- let contact = getGroupMember(at: row)
|
|
|
|
- coordinator?.showContactDetail(of: contact.id)
|
|
|
|
}
|
|
}
|
|
- } else if section == sectionLeaveGroup {
|
|
|
|
- leaveGroup()
|
|
|
|
|
|
+ case .members:
|
|
|
|
+ let member = getGroupMember(at: row)
|
|
|
|
+ coordinator?.showContactDetail(of: member.id)
|
|
|
|
+ case .chatActions:
|
|
|
|
+ if row == 0 {
|
|
|
|
+ toggleArchiveChat()
|
|
|
|
+ } else if row == 1 {
|
|
|
|
+ showLeaveGroupConfirmationAlert()
|
|
|
|
+ } else if row == 2 {
|
|
|
|
+ showDeleteChatConfirmationAlert()
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
|
|
+ if sections[section] == .members {
|
|
|
|
+ return String.localized("tab_members")
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+
|
|
func tableView(_: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
|
func tableView(_: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
|
- let section = indexPath.section
|
|
|
|
|
|
+ guard let currentUser = self.currentUser else {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
let row = indexPath.row
|
|
let row = indexPath.row
|
|
-
|
|
|
|
- if let currentUser = currentUser {
|
|
|
|
- if section == sectionMembers, row >= sectionMembersStaticRowCount, groupMembers[row - sectionMembersStaticRowCount].id != currentUser.id {
|
|
|
|
- return true
|
|
|
|
- }
|
|
|
|
|
|
+ let sectionType = sections[indexPath.section]
|
|
|
|
+ if sectionType == .members && groupMemberIds[row] != currentUser.id {
|
|
|
|
+ return true
|
|
}
|
|
}
|
|
return false
|
|
return false
|
|
}
|
|
}
|
|
|
|
|
|
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
|
|
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
|
|
- let section = indexPath.section
|
|
|
|
|
|
+ guard let currentUser = self.currentUser else {
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
let row = indexPath.row
|
|
let row = indexPath.row
|
|
-
|
|
|
|
- // assigning swipe by delete to members (except for current user)
|
|
|
|
- if section == sectionMembers, row >= sectionMembersStaticRowCount, groupMembers[row - sectionMembersStaticRowCount].id != currentUser?.id {
|
|
|
|
|
|
+ let sectionType = sections[indexPath.section]
|
|
|
|
+ if sectionType == .members && groupMemberIds[row] != currentUser.id {
|
|
|
|
+ // action set for members except for current user
|
|
let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [unowned self] _, indexPath in
|
|
let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [unowned self] _, indexPath in
|
|
|
|
|
|
let contact = self.getGroupMember(at: row)
|
|
let contact = self.getGroupMember(at: row)
|
|
@@ -252,24 +284,55 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
|
|
alert.addAction(UIAlertAction(title: String.localized("remove_desktop"), style: .destructive, handler: { _ in
|
|
alert.addAction(UIAlertAction(title: String.localized("remove_desktop"), style: .destructive, handler: { _ in
|
|
let success = dc_remove_contact_from_chat(mailboxPointer, UInt32(self.chat.id), UInt32(contact.id))
|
|
let success = dc_remove_contact_from_chat(mailboxPointer, UInt32(self.chat.id), UInt32(contact.id))
|
|
if success == 1 {
|
|
if success == 1 {
|
|
- self.groupMembers.remove(at: row - self.sectionMembersStaticRowCount)
|
|
|
|
- tableView.deleteRows(at: [indexPath], with: .fade)
|
|
|
|
- tableView.reloadData()
|
|
|
|
|
|
+ self.removeGroupeMemberFromTableAt(indexPath)
|
|
}
|
|
}
|
|
}))
|
|
}))
|
|
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
|
|
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
|
|
self.present(alert, animated: true, completion: nil)
|
|
self.present(alert, animated: true, completion: nil)
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ }
|
|
delete.backgroundColor = UIColor.red
|
|
delete.backgroundColor = UIColor.red
|
|
return [delete]
|
|
return [delete]
|
|
- } else {
|
|
|
|
- return nil
|
|
|
|
}
|
|
}
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private func getGroupMember(at row: Int) -> DcContact {
|
|
|
|
+ return DcContact(id: groupMemberIds[row])
|
|
}
|
|
}
|
|
|
|
|
|
- func getGroupMember(at row: Int) -> DcContact {
|
|
|
|
- let memberId = self.groupMembers[row - self.sectionMembersStaticRowCount].id
|
|
|
|
- return DcContact(id: memberId)
|
|
|
|
|
|
+ private func removeGroupeMemberFromTableAt(_ indexPath: IndexPath) {
|
|
|
|
+ self.groupMemberIds.remove(at: indexPath.row)
|
|
|
|
+ self.tableView.deleteRows(at: [indexPath], with: .automatic)
|
|
|
|
+ updateHeader() // to display correct group size
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// MARK: -alerts
|
|
|
|
+extension GroupChatDetailViewController {
|
|
|
|
+ private func showDeleteChatConfirmationAlert() {
|
|
|
|
+ let alert = UIAlertController(
|
|
|
|
+ title: nil,
|
|
|
|
+ message: String.localized("ask_delete_chat_desktop"),
|
|
|
|
+ preferredStyle: .safeActionSheet
|
|
|
|
+ )
|
|
|
|
+ alert.addAction(UIAlertAction(title: String.localized("menu_delete_chat"), style: .destructive, handler: { _ in
|
|
|
|
+ self.coordinator?.deleteChat()
|
|
|
|
+ }))
|
|
|
|
+ alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
|
|
|
|
+ self.present(alert, animated: true, completion: nil)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private func showLeaveGroupConfirmationAlert() {
|
|
|
|
+ if let userId = currentUser?.id {
|
|
|
|
+ let alert = UIAlertController(title: String.localized("ask_leave_group"), message: nil, preferredStyle: .safeActionSheet)
|
|
|
|
+ alert.addAction(UIAlertAction(title: String.localized("menu_leave_group"), style: .destructive, handler: { _ in
|
|
|
|
+ dc_remove_contact_from_chat(mailboxPointer, UInt32(self.chat.id), UInt32(userId))
|
|
|
|
+ self.editBarButtonItem.isEnabled = false
|
|
|
|
+ self.updateGroupMembers()
|
|
|
|
+ }))
|
|
|
|
+ alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
|
|
|
|
+ present(alert, animated: true, completion: nil)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|