123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- import UIKit
- import DcCore
- class NewGroupController: UITableViewController, MediaPickerDelegate {
- var groupName: String = ""
- var groupChatId: Int = 0
- var doneButton: UIBarButtonItem!
- var contactIdsForGroup: Set<Int> // TODO: check if array is sufficient
- var groupContactIds: [Int]
- var groupImage: UIImage?
- let isVerifiedGroup: Bool
- let dcContext: DcContext
- private var contactAddedObserver: NSObjectProtocol?
- ///TODO: remove the the line below as soon as deltachat-core 4b7b6d6cb3c26d817e3f3eeb6a20d8e8c66a4578 was released
- private var workaroundObserver: NSObjectProtocol?
- private let sectionGroupDetails = 0
- private let sectionGroupDetailsRowAvatar = 0
- private let sectionGroupDetailsRowName = 1
- private let countSectionGroupDetails = 2
- private let sectionInvite = 1
- private let sectionInviteRowAddMembers = 0
- private let sectionInviteRowShowQrCode = 1
- private lazy var countSectionInvite: Int = 2
- private let sectionGroupMembers = 2
- private lazy var mediaPicker: MediaPicker? = {
- return MediaPicker(navigationController: navigationController)
- }()
- lazy var groupNameCell: TextFieldCell = {
- let cell = TextFieldCell(description: String.localized("group_name"), placeholder: String.localized("group_name"))
- cell.onTextFieldChange = self.updateGroupName
- cell.textField.autocorrectionType = UITextAutocorrectionType.no
- return cell
- }()
- lazy var avatarSelectionCell: AvatarSelectionCell = {
- let cell = AvatarSelectionCell(context: nil)
- cell.hintLabel.text = String.localized("group_avatar")
- cell.onAvatarTapped = onAvatarTapped
- return cell
- }()
- var qrInviteCodeCell: ActionCell?
- init(dcContext: DcContext, isVerified: Bool) {
- self.contactIdsForGroup = [Int(DC_CONTACT_ID_SELF)]
- self.groupContactIds = Array(contactIdsForGroup)
- self.isVerifiedGroup = isVerified
- self.dcContext = dcContext
- super.init(style: .grouped)
- }
- required init?(coder _: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- title = isVerifiedGroup ? String.localized("menu_new_verified_group") : String.localized("menu_new_group")
- doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonPressed))
- navigationItem.rightBarButtonItem = doneButton
- doneButton.isEnabled = false
- tableView.register(ContactCell.self, forCellReuseIdentifier: "contactCell")
- tableView.register(ActionCell.self, forCellReuseIdentifier: "actionCell")
- self.hideKeyboardOnTap()
- }
- override func viewWillAppear(_ animated: Bool) {
- let nc = NotificationCenter.default
- contactAddedObserver = nc.addObserver(
- forName: dcNotificationChatModified,
- object: nil,
- queue: nil
- ) { [weak self] notification in
- guard let self = self else { return }
- if let ui = notification.userInfo {
- if let chatId = ui["chat_id"] as? Int {
- if self.groupChatId == 0 || chatId != self.groupChatId {
- return
- }
- self.updateGroupContactIdsOnQRCodeInvite()
- }
- }
- }
- ///TODO: remove the the lines below as soon as deltachat-core 4b7b6d6cb3c26d817e3f3eeb6a20d8e8c66a4578 was released
- workaroundObserver = nc.addObserver(
- forName: dcNotificationChanged,
- object: nil,
- queue: nil
- ) { [weak self] notification in
- guard let self = self else { return }
- if let ui = notification.userInfo {
- if let chatId = ui["chat_id"] as? Int {
- if self.groupChatId == 0 || chatId != self.groupChatId {
- return
- }
- self.updateGroupContactIdsOnQRCodeInvite()
- }
- }
- }
- }
- override func viewWillDisappear(_ animated: Bool) {
- if let observer = self.contactAddedObserver {
- NotificationCenter.default.removeObserver(observer)
- }
- ///TODO: remove the the lines below as soon as deltachat-core 4b7b6d6cb3c26d817e3f3eeb6a20d8e8c66a4578 was released
- if let workaroundObserver = self.workaroundObserver {
- NotificationCenter.default.removeObserver(workaroundObserver)
- }
- }
- @objc func doneButtonPressed() {
- if groupChatId == 0 {
- groupChatId = dcContext.createGroupChat(verified: isVerifiedGroup, name: groupName)
- } else {
- _ = dcContext.setChatName(chatId: groupChatId, name: groupName)
- }
- for contactId in contactIdsForGroup {
- let success = dcContext.addContactToChat(chatId: groupChatId, contactId: contactId)
- if let groupImage = groupImage {
- AvatarHelper.saveChatAvatar(dcContext: dcContext, image: groupImage, for: Int(groupChatId))
- }
- if success {
- logger.info("successfully added \(contactId) to group \(groupName)")
- } else {
- logger.error("failed to add \(contactId) to group \(groupName)")
- }
- }
- showGroupChat(chatId: Int(groupChatId))
- }
- override func numberOfSections(in _: UITableView) -> Int {
- return 3
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let section = indexPath.section
- let row = indexPath.row
- switch section {
- case sectionGroupDetails:
- if row == sectionGroupDetailsRowAvatar {
- return avatarSelectionCell
- } else {
- return groupNameCell
- }
- case sectionInvite:
- if row == sectionInviteRowAddMembers {
- 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
- actionCell.isUserInteractionEnabled = true
- }
- return cell
- } else {
- let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath)
- if let actionCell = cell as? ActionCell {
- actionCell.actionTitle = String.localized("qrshow_join_group_title")
- actionCell.actionColor = groupName.isEmpty ? DcColors.colorDisabled : UIColor.systemBlue
- actionCell.isUserInteractionEnabled = !groupName.isEmpty
- qrInviteCodeCell = actionCell
- }
- return cell
- }
- default:
- let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
- if let contactCell = cell as? ContactCell {
- let contact = DcContact(id: groupContactIds[row])
- 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
- }
- }
- override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
- if section == sectionGroupDetails && isVerifiedGroup {
- return String.localized("verified_group_explain")
- }
- return nil
- }
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- let section = indexPath.section
- switch section {
- case sectionGroupDetails, sectionInvite:
- return UITableView.automaticDimension
- default:
- return ContactCell.cellHeight
- }
- }
- override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
- switch section {
- case sectionGroupDetails:
- return countSectionGroupDetails
- case sectionInvite:
- return countSectionInvite
- default:
- return contactIdsForGroup.count
- }
- }
- override func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
- if section == sectionGroupMembers {
- return String.localized("in_this_group_desktop")
- } else {
- return nil
- }
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- let section = indexPath.section
- let row = indexPath.row
- if section == sectionInvite {
- if row == sectionInviteRowAddMembers {
- var contactsWithoutSelf = contactIdsForGroup
- contactsWithoutSelf.remove(Int(DC_CONTACT_ID_SELF))
- showAddMembers(preselectedMembers: contactsWithoutSelf, isVerified: self.isVerifiedGroup)
- } else {
- self.groupChatId = dcContext.createGroupChat(verified: isVerifiedGroup, name: groupName)
- showQrCodeInvite(chatId: Int(self.groupChatId))
- }
- }
- }
- override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
- let section = indexPath.section
- let row = indexPath.row
- //swipe by delete
- if section == sectionGroupMembers, groupContactIds[row] != DC_CONTACT_ID_SELF {
- let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [weak self] _, indexPath in
- guard let self = self else { return }
- if self.groupChatId != 0,
- self.dcContext.getChat(chatId: self.groupChatId).contactIds.contains(self.groupContactIds[row]) {
- let success = self.dcContext.removeContactFromChat(chatId: self.groupChatId, contactId: self.groupContactIds[row])
- if success {
- self.removeGroupContactFromList(at: indexPath)
- }
- } else {
- self.removeGroupContactFromList(at: indexPath)
- }
- }
- delete.backgroundColor = UIColor.red
- return [delete]
- } else {
- return nil
- }
- }
- private func updateGroupName(textView: UITextField) {
- let name = textView.text ?? ""
- groupName = name
- doneButton.isEnabled = name.containsCharacters()
- qrInviteCodeCell?.isUserInteractionEnabled = name.containsCharacters()
- qrInviteCodeCell?.actionColor = groupName.isEmpty ? DcColors.colorDisabled : UIColor.systemBlue
- }
- private func onAvatarTapped() {
- let alert = UIAlertController(title: nil, message: nil, preferredStyle: .safeActionSheet)
- let photoAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
- let videoAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
- alert.addAction(photoAction)
- alert.addAction(videoAction)
- alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
- self.present(alert, animated: true, completion: nil)
- }
- private func galleryButtonPressed(_ action: UIAlertAction) {
- showPhotoPicker(delegate: self)
- }
- private func cameraButtonPressed(_ action: UIAlertAction) {
- showCamera(delegate: self)
- }
- func onImageSelected(image: UIImage) {
- groupImage = image
- avatarSelectionCell = AvatarSelectionCell(context: nil, with: groupImage)
- avatarSelectionCell.hintLabel.text = String.localized("group_avatar")
- avatarSelectionCell.onAvatarTapped = onAvatarTapped
- self.tableView.beginUpdates()
- let indexPath = IndexPath(row: sectionGroupDetailsRowAvatar, section: sectionGroupDetails)
- self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
- self.tableView.endUpdates()
- }
- func updateGroupContactIdsOnQRCodeInvite() {
- for contactId in dcContext.getChat(chatId: groupChatId).contactIds {
- contactIdsForGroup.insert(contactId)
- }
- groupContactIds = Array(contactIdsForGroup)
- self.tableView.reloadData()
- }
- func updateGroupContactIdsOnListSelection(_ members: Set<Int>) {
- if groupChatId != 0 {
- var members = members
- for contactId in dcContext.getChat(chatId: groupChatId).contactIds {
- members.insert(contactId)
- }
- }
- contactIdsForGroup = members
- groupContactIds = Array(members)
- self.tableView.reloadData()
- }
- func removeGroupContactFromList(at indexPath: IndexPath) {
- let row = indexPath.row
- self.contactIdsForGroup.remove(self.groupContactIds[row])
- self.groupContactIds.remove(at: row)
- tableView.deleteRows(at: [indexPath], with: .fade)
- }
- // MARK: - coordinator
- private func showGroupChat(chatId: Int) {
- if let chatlistViewController = navigationController?.viewControllers[0] {
- let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId)
- navigationController?.setViewControllers([chatlistViewController, chatViewController], animated: true)
- }
- }
- private func showPhotoPicker(delegate: MediaPickerDelegate) {
- mediaPicker?.showPhotoGallery(delegate: delegate)
- }
- private func showCamera(delegate: MediaPickerDelegate) {
- mediaPicker?.showCamera(delegate: delegate)
- }
- private func showQrCodeInvite(chatId: Int) {
- var hint = ""
- let dcChat = dcContext.getChat(chatId: chatId)
- if !dcChat.name.isEmpty {
- hint = String.localizedStringWithFormat(String.localized("qrshow_join_group_hint"), dcChat.name)
- }
- let qrInviteCodeController = QrViewController(dcContext: dcContext, chatId: chatId, qrCodeHint: hint)
- qrInviteCodeController.onDismissed = { [weak self] in
- self?.updateGroupContactIdsOnQRCodeInvite()
- }
- navigationController?.pushViewController(qrInviteCodeController, animated: true)
- }
- private func showAddMembers(preselectedMembers: Set<Int>, isVerified: Bool) {
- let newGroupController = NewGroupAddMembersViewController(preselected: preselectedMembers,
- isVerified: isVerified)
- newGroupController.onMembersSelected = { [weak self] (memberIds: Set<Int>) -> Void in
- guard let self = self else { return }
- self.updateGroupContactIdsOnListSelection(memberIds)
- self.navigationController?.popViewController(animated: true)
- }
- navigationController?.pushViewController(newGroupController, animated: true)
- }
- }
|