Răsfoiți Sursa

Merge pull request #378 from deltachat/notification-switch

notification switch
björn petersen 5 ani în urmă
părinte
comite
2adbfcf737

+ 19 - 44
deltachat-ios/Controller/ContactDetailViewController.swift

@@ -3,17 +3,19 @@ import UIKit
 // this is also used as ChatDetail for SingleChats
 class ContactDetailViewController: UITableViewController {
     weak var coordinator: ContactDetailCoordinatorProtocol?
+
     let sectionOptions = 0
+    let sectionOptionsRowStartChat = 1
+    let sectionOptionsRowCount = 1
+
     let sectionBlockContact = 1
-    let sectionOptionsRowNotifications = 0
+    let sectionBlockContactRowCount = 1
+
+    let sectionCount = 2
+
     var showStartChat = true
-    var optionCells: [UITableViewCell] = []
 
-    private enum CellIdentifiers: String {
-        case notification = "notificationCell"
-        case block = "blockContactCell"
-        case chat = "chatCell"
-    }
+    var optionCells: [UITableViewCell] = []
 
     private let contactId: Int
 
@@ -21,19 +23,8 @@ class ContactDetailViewController: UITableViewController {
         return DcContact(id: contactId)
     }
 
-    private var notificationsCell: UITableViewCell = {
-        let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
-        cell.textLabel?.text = String.localized("pref_notifications")
-        cell.accessibilityIdentifier = CellIdentifiers.notification.rawValue
-        cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
-        cell.selectionStyle = .none
-        // TODO: add current notification status
-        return cell
-    }()
-
-    private lazy var chatCell: ActionCell = {
+    private lazy var startChatCell: ActionCell = {
         let cell = ActionCell()
-        cell.accessibilityIdentifier = CellIdentifiers.chat.rawValue
         cell.actionColor = SystemColor.blue.uiColor
         cell.actionTitle = String.localized("menu_new_chat")
         cell.selectionStyle = .none
@@ -42,7 +33,6 @@ class ContactDetailViewController: UITableViewController {
 
     private lazy var blockContactCell: ActionCell = {
         let cell = ActionCell()
-        cell.accessibilityIdentifier = CellIdentifiers.block.rawValue
         cell.actionTitle = contact.isBlocked ? String.localized("menu_unblock_contact") : String.localized("menu_block_contact")
         cell.actionColor = contact.isBlocked ? SystemColor.blue.uiColor : UIColor.red
         cell.selectionStyle = .none
@@ -64,9 +54,8 @@ class ContactDetailViewController: UITableViewController {
             title: String.localized("global_menu_edit_desktop"),
             style: .plain, target: self, action: #selector(editButtonPressed))
         self.title = String.localized("tab_contact")
-        optionCells.insert(notificationsCell, at: 0)
         if showStartChat {
-            optionCells.insert(chatCell, at: 1)
+            optionCells.append(startChatCell)
         }
     }
 
@@ -76,47 +65,33 @@ class ContactDetailViewController: UITableViewController {
     }
 
     override func numberOfSections(in tableView: UITableView) -> Int {
-        return 2
+        return sectionCount
     }
 
     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         if section == sectionOptions {
             return optionCells.count
         } else if section == sectionBlockContact {
-            return 1
+            return sectionBlockContactRowCount
         }
         return 0
     }
 
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let section = indexPath.section
-        let row = indexPath.row
-
         if section == sectionOptions {
-            if row == sectionOptionsRowNotifications {
-                return notificationsCell
-            } else {
-                return chatCell
-            }
+            return startChatCell
         } else {
             return blockContactCell
         }
     }
 
     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-        guard let cell = tableView.cellForRow(at: indexPath) else {
-            return
-        }
-
-        if let identifier = CellIdentifiers(rawValue: cell.accessibilityIdentifier ?? "") {
-            switch identifier {
-            case .block:
-                toggleBlockContact()
-            case .chat:
-                askToChatWith(contactId: contactId)
-            case .notification:
-                showNotificationSetup()
-            }
+        let section = indexPath.section
+        if section == sectionOptions {
+            askToChatWith(contactId: contactId)
+        } else {
+            toggleBlockContact()
         }
     }
 

+ 17 - 47
deltachat-ios/Controller/GroupChatDetailViewController.swift

@@ -2,12 +2,15 @@ import UIKit
 
 class GroupChatDetailViewController: UIViewController {
 
-    private let sectionConfig = 0
-    private let sectionMembers = 1
-    private let sectionLeaveGroup = 2
+    private let sectionMembers = 0
     private let sectionMembersRowAddMember = 0
     private let sectionMembersRowJoinQR = 1
+    private let sectionMembersStaticRowCount = 2 // followed by one row per member
 
+    private let sectionLeaveGroup = 1
+    private let sectionLeaveGroupRowCount = 1
+
+    private let sectionCount = 2
 
     private var currentUser: DcContact? {
         return groupMembers.filter { $0.email == DcConfig.addr }.first
@@ -47,23 +50,12 @@ class GroupChatDetailViewController: UIViewController {
         chatDetailTable.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
     }
 
-    private func showNotificationSetup() {
-        let notificationSetupAlert = UIAlertController(title: "Notifications Setup is not implemented yet",
-                                                       message: "But you get an idea where this is going",
-                                                       preferredStyle: .actionSheet)
-        let cancelAction = UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil)
-        notificationSetupAlert.addAction(cancelAction)
-        present(notificationSetupAlert, animated: true, completion: nil)
-    }
-
     private lazy var editBarButtonItem: UIBarButtonItem = {
         UIBarButtonItem(title: String.localized("global_menu_edit_desktop"), style: .plain, target: self, action: #selector(editButtonPressed))
     }()
 
     private var groupMembers: [DcContact] = []
 
-    private let staticCellCountMemberSection = 2
-
     override func viewDidLoad() {
         super.viewDidLoad()
         title = String.localized("tab_group")
@@ -106,15 +98,9 @@ class GroupChatDetailViewController: UIViewController {
 }
 
 extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSource {
-    func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
-        if section == 1 {
-            return String.localized("tab_members")
-        }
-        return nil
-    }
 
     func tableView(_: UITableView, viewForHeaderInSection section: Int) -> UIView? {
-        if section == sectionConfig {
+        if section == sectionMembers {
             let header = ContactDetailHeader()
             header.updateDetails(title: chat.name,
                                  subtitle: String.localizedStringWithFormat(String.localized("n_members"), chat.contactIds.count))
@@ -131,26 +117,18 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
     }
 
     func numberOfSections(in _: UITableView) -> Int {
-        /*
-         section 0: config
-         section 1: members
-         section 2: leave group (optional - if user already left group this option will be hidden)
-         */
-
         if currentUser == nil {
-            return 2
+            return sectionCount-1 // leave out last section (leaveSection)
         }
-        return 3
+        return sectionCount
     }
 
     func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
         switch section {
-        case sectionConfig:
-            return 1
         case sectionMembers:
-            return groupMembers.count + staticCellCountMemberSection
+            return sectionMembersStaticRowCount + groupMembers.count
         case sectionLeaveGroup:
-            return 1
+            return sectionLeaveGroupRowCount
         default:
             return 0
         }
@@ -160,11 +138,6 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
         let section = indexPath.section
         let row = indexPath.row
         switch section {
-        case sectionConfig:
-            let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
-            cell.textLabel?.text = String.localized("pref_notifications")
-            cell.selectionStyle = .none
-            return cell
         case sectionMembers:
             switch row {
             case sectionMembersRowAddMember:
@@ -184,7 +157,7 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
             default:
                 let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
                 if let contactCell = cell as? ContactCell {
-                    let contact = groupMembers[row - staticCellCountMemberSection]
+                    let contact = groupMembers[row - sectionMembersStaticRowCount]
                     let displayName = contact.displayName
                     contactCell.nameLabel.text = displayName
                     contactCell.emailLabel.text = contact.email
@@ -209,9 +182,7 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
     func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
         let section = indexPath.section
         let row = indexPath.row
-        if section == sectionConfig {
-            showNotificationSetup()
-        } else if section == sectionMembers {
+        if section == sectionMembers {
             if row == sectionMembersRowAddMember {
                 coordinator?.showAddGroupMember(chatId: chat.id)
             } else if row == sectionMembersRowJoinQR {
@@ -220,7 +191,6 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
                 let contact = getGroupMember(at: row)
                 coordinator?.showContactDetail(of: contact.id)
             }
-            // ignore for now - in Telegram tapping a contactCell leads into ContactDetail
         } else if section == sectionLeaveGroup {
             leaveGroup()
         }
@@ -231,7 +201,7 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
         let row = indexPath.row
 
         if let currentUser = currentUser {
-            if section == sectionMembers, row >= staticCellCountMemberSection, groupMembers[row - staticCellCountMemberSection].id != currentUser.id {
+            if section == sectionMembers, row >= sectionMembersStaticRowCount, groupMembers[row - sectionMembersStaticRowCount].id != currentUser.id {
                 return true
             }
         }
@@ -243,7 +213,7 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
         let row = indexPath.row
 
         // assigning swipe by delete to members (except for current user)
-        if section == sectionMembers, row >= staticCellCountMemberSection, groupMembers[row - staticCellCountMemberSection].id != currentUser?.id {
+        if section == sectionMembers, row >= sectionMembersStaticRowCount, groupMembers[row - sectionMembersStaticRowCount].id != currentUser?.id {
             let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [unowned self] _, indexPath in
 
                 let contact = self.getGroupMember(at: row)
@@ -252,7 +222,7 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
                 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))
                     if success == 1 {
-                        self.groupMembers.remove(at: row - self.staticCellCountMemberSection)
+                        self.groupMembers.remove(at: row - self.sectionMembersStaticRowCount)
                         tableView.deleteRows(at: [indexPath], with: .fade)
                         tableView.reloadData()
                     }
@@ -268,7 +238,7 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
     }
 
     func getGroupMember(at row: Int) -> DcContact {
-        let memberId = self.groupMembers[row - self.staticCellCountMemberSection].id
+        let memberId = self.groupMembers[row - self.sectionMembersStaticRowCount].id
         return DcContact(id: memberId)
     }
 

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

@@ -188,6 +188,13 @@ internal final class SettingsViewController: QuickTableViewController {
                     NavigationRow(text: String.localized("pref_blocked_contacts"),
                               detailText: .none,
                               action: { [weak self] in self?.showBlockedContacts($0) }),
+                    SwitchRow(text: String.localized("pref_notifications"),
+                              switchValue: !UserDefaults.standard.bool(forKey: "notifications_disabled"),
+                              action: { row in
+                                if let row = row as? SwitchRow {
+                                    UserDefaults.standard.set(!row.switchValue, forKey: "notifications_disabled")
+                                }
+                    }),
                     SwitchRow(text: String.localized("pref_read_receipts"),
                               switchValue: DcConfig.mdnsEnabled,
                               action: { row in

+ 14 - 12
deltachat-ios/DC/events.swift

@@ -101,18 +101,20 @@ public func callbackSwift(event: CInt, data1: CUnsignedLong, data2: CUnsignedLon
                     object: nil,
                     userInfo: userInfo)
 
-            let content = UNMutableNotificationContent()
-            let msg = DcMsg(id: Int(data2))
-            content.title = msg.fromContact.displayName
-            content.body = msg.summary(chars: 40) ?? ""
-            content.userInfo = userInfo
-            content.sound = .default
-
-            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
-
-            let request = UNNotificationRequest(identifier: Constants.notificationIdentifier, content: content, trigger: trigger)
-            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
-            logger.info("notifications: added \(content)")
+            if !UserDefaults.standard.bool(forKey: "notifications_disabled") {
+                let content = UNMutableNotificationContent()
+                let msg = DcMsg(id: Int(data2))
+                content.title = msg.fromContact.displayName
+                content.body = msg.summary(chars: 40) ?? ""
+                content.userInfo = userInfo
+                content.sound = .default
+
+                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
+
+                let request = UNNotificationRequest(identifier: Constants.notificationIdentifier, content: content, trigger: trigger)
+                UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
+                logger.info("notifications: added \(content)")
+            }
 
             let array = DcArray(arrayPointer: dc_get_fresh_msgs(mailboxPointer))
             UIApplication.shared.applicationIconBadgeNumber = array.count