Ver código fonte

Merge pull request #275 from deltachat/unblock_block_contacts

Unblock blocked contacts
björn petersen 5 anos atrás
pai
commit
7d6f14dd14

+ 61 - 3
deltachat-ios/Controller/GroupMembersViewController.swift

@@ -97,8 +97,60 @@ class AddGroupMembersViewController: GroupMembersViewController {
     }
 }
 
+class BlockedContactsViewController: GroupMembersViewController, GroupMemberSelectionDelegate {
+
+    override init() {
+        super.init()
+        enableCheckmarks = false
+    }
+
+    required init?(coder _: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    override func viewDidLoad() {
+        super.viewDidLoad()
+        title = String.localized("pref_blocked_contacts")
+        contactIds = Utils.getBlockedContactIds()
+        selectedContactIds = Set(contactIds)
+        navigationItem.searchController = nil
+        groupMemberSelectionDelegate = self
+    }
+
+    override func viewWillAppear(_ animated: Bool) {
+        super.viewWillAppear(animated)
+        NavBarUtils.setSmallTitle(navigationController: navigationController)
+    }
+
+    func selected(contactId: Int, selected: Bool) {
+        if !selected {
+            let dcContact = DcContact(id: contactId)
+            let title = dcContact.displayName.isEmpty ? dcContact.email : dcContact.displayName
+            let alert = UIAlertController(title: title, message: String.localized("ask_unblock_contact"), preferredStyle: .actionSheet)
+            alert.addAction(UIAlertAction(title: String.localized("menu_unblock_contact"), style: .default, handler: { _ in
+                let contact = DcContact(id: contactId)
+                contact.unblock()
+                self.contactIds = Utils.getBlockedContactIds()
+                self.selectedContactIds = Set(self.contactIds)
+                self.tableView.reloadData()
+            }))
+            alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: { _ in
+                self.selectedContactIds = Set(self.contactIds)
+                self.tableView.reloadData()
+            }))
+           present(alert, animated: true, completion: nil)
+        }
+    }
+}
+
+protocol GroupMemberSelectionDelegate: class {
+    func selected(contactId: Int, selected: Bool)
+}
+
 class GroupMembersViewController: UITableViewController, UISearchResultsUpdating {
     let contactCellReuseIdentifier = "contactCell"
+    weak var groupMemberSelectionDelegate: GroupMemberSelectionDelegate?
+    var enableCheckmarks = true
 
     var contactIds: [Int] = [] {
         didSet {
@@ -174,7 +226,7 @@ class GroupMembersViewController: UITableViewController, UISearchResultsUpdating
         let row = indexPath.row
         let contact: ContactWithSearchResults = contactSearchResultByRow(row)
         updateContactCell(cell: cell, contactWithHighlight: contact)
-        cell.accessoryType = selectedContactIds.contains(contactIdByRow(row)) ? .checkmark : .none
+        cell.accessoryType = selectedContactIds.contains(contactIdByRow(row)) && enableCheckmarks ? .checkmark : .none
 
         return cell
     }
@@ -186,10 +238,16 @@ class GroupMembersViewController: UITableViewController, UISearchResultsUpdating
             let contactId = contactIdByRow(row)
             if selectedContactIds.contains(contactId) {
                 selectedContactIds.remove(contactId)
-                cell.accessoryType = .none
+                if enableCheckmarks {
+                    cell.accessoryType = .none
+                }
+                groupMemberSelectionDelegate?.selected(contactId: contactId, selected: false)
             } else {
                 selectedContactIds.insert(contactId)
-                cell.accessoryType = .checkmark
+                if enableCheckmarks {
+                    cell.accessoryType = .checkmark
+                }
+                groupMemberSelectionDelegate?.selected(contactId: contactId, selected: true)
             }
         }
     }

+ 9 - 0
deltachat-ios/Controller/SettingsController.swift

@@ -28,6 +28,8 @@ internal final class SettingsViewController: QuickTableViewController {
     override func viewDidLoad() {
         super.viewDidLoad()
         title = String.localized("menu_settings")
+        let backButton = UIBarButtonItem(title: String.localized("menu_settings"), style: .plain, target: nil, action: nil)
+        navigationItem.backBarButtonItem = backButton
         documentInteractionController.delegate = self as? UIDocumentInteractionControllerDelegate
     }
 
@@ -109,6 +111,9 @@ internal final class SettingsViewController: QuickTableViewController {
             Section(
                 title: String.localized("pref_communication"),
                 rows: [
+                    NavigationRow(text: String.localized("pref_blocked_contacts"),
+                              detailText: .none,
+                              action: { [weak self] in self?.showBlockedContacts($0) }),
                     SwitchRow(text: String.localized("pref_read_receipts"),
                               switchValue: DcConfig.mdnsEnabled,
                               action: { row in
@@ -206,6 +211,10 @@ internal final class SettingsViewController: QuickTableViewController {
         present(alert, animated: true, completion: nil)
     }
 
+    private func showBlockedContacts(_: Row) {
+        coordinator?.showBlockedContacts()
+    }
+
     private func sendAsm(_: Row) {
         let askAlert = UIAlertController(title: String.localized("autocrypt_send_asm_explain_before"), message: nil, preferredStyle: .actionSheet)
         askAlert.addAction(UIAlertAction(title: String.localized("autocrypt_send_asm_title"), style: .default, handler: { _ in

+ 5 - 0
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -206,6 +206,11 @@ class SettingsCoordinator: Coordinator {
         let accountSetupNavigationController = DcNavigationController(rootViewController: accountSetupVC)
         navigationController.present(accountSetupNavigationController, animated: true, completion: nil)
     }
+
+    func showBlockedContacts() {
+        let blockedContactsController = BlockedContactsViewController()
+        navigationController.pushViewController(blockedContactsController, animated: true)
+    }
 }
 
 class AccountSetupCoordinator: Coordinator {

+ 5 - 0
deltachat-ios/Helper/Utils.swift

@@ -8,6 +8,11 @@ struct Utils {
         return Utils.copyAndFreeArray(inputArray: cContacts)
     }
 
+    static func getBlockedContactIds() -> [Int] {
+        let cBlockedContacts = dc_get_blocked_contacts(mailboxPointer)
+        return Utils.copyAndFreeArray(inputArray: cBlockedContacts)
+    }
+
     static func getInitials(inputName: String) -> String {
         var nameParts = inputName.split(separator: " ")
         // this limits initials to max 2, otherwise just takes first letter to avoid messy badges