Prechádzať zdrojové kódy

implement mute chats menu item in contact detail views, refactor deprecated getChatIdByContactIdOld call

cyberta 5 rokov pred
rodič
commit
95f12e5a4f

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

@@ -33,6 +33,14 @@ class ContactDetailViewController: UITableViewController {
         return cell
     }()
 
+    private lazy var muteChatCell: ActionCell = {
+        let cell = ActionCell()
+        cell.actionTitle = viewModel.chatIsMuted ? String.localized("menu_unmute") :  String.localized("menu_mute")
+        cell.actionColor = SystemColor.blue.uiColor
+        cell.selectionStyle = .none
+        return cell
+    }()
+
     private lazy var archiveChatCell: ActionCell = {
         let cell = ActionCell()
         cell.actionTitle = viewModel.chatIsArchived ? String.localized("menu_unarchive_chat") :  String.localized("menu_archive_chat")
@@ -53,7 +61,7 @@ class ContactDetailViewController: UITableViewController {
         let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
         cell.textLabel?.text = String.localized("gallery")
         cell.accessoryType = .disclosureIndicator
-        if viewModel.chatId == nil {
+        if viewModel.chatId == 0 {
             cell.isUserInteractionEnabled = false
             cell.textLabel?.isEnabled = false
         }
@@ -64,7 +72,7 @@ class ContactDetailViewController: UITableViewController {
         let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
         cell.textLabel?.text = String.localized("documents")
         cell.accessoryType = .disclosureIndicator
-        if viewModel.chatId == nil {
+        if viewModel.chatId == 0 {
             cell.isUserInteractionEnabled = false
             cell.textLabel?.isEnabled = false
         }
@@ -128,6 +136,8 @@ class ContactDetailViewController: UITableViewController {
             }
         case .chatActions:
             switch viewModel.chatActionFor(row: row) {
+            case .muteChat:
+                return muteChatCell
             case .archiveChat:
                 return archiveChatCell
             case .blockContact:
@@ -195,6 +205,13 @@ class ContactDetailViewController: UITableViewController {
     private func handleCellAction(for index: Int) {
         let action = viewModel.chatActionFor(row: index)
         switch action {
+        case .muteChat:
+            if viewModel.chatIsMuted {
+                self.viewModel.context.setChatMuteDuration(chatId: self.viewModel.chatId, duration: 0)
+                muteChatCell.actionTitle = String.localized("menu_mute")
+            } else {
+                showMuteAlert()
+            }
         case .archiveChat:
             toggleArchiveChat()
         case .blockContact:
@@ -248,14 +265,26 @@ class ContactDetailViewController: UITableViewController {
         self.present(alert, animated: true, completion: nil)
     }
 
-    private func showNotificationSetup() {
-        let notificationSetupAlert = UIAlertController(
-            title: "Notifications Setup is not implemented yet",
-            message: "But you get an idea where this is going",
-            preferredStyle: .safeActionSheet)
+    private func showMuteAlert() {
+        let alert = UIAlertController(title: String.localized("mute"), message: nil, preferredStyle: .safeActionSheet)
+        let forever = -1
+        addDurationSelectionAction(to: alert, key: "mute_for_one_hour", duration: Time.oneHour)
+        addDurationSelectionAction(to: alert, key: "mute_for_one_hour", duration: Time.twoHours)
+        addDurationSelectionAction(to: alert, key: "mute_for_one_day", duration: Time.oneDay)
+        addDurationSelectionAction(to: alert, key: "mute_for_seven_days", duration: Time.oneWeek)
+        addDurationSelectionAction(to: alert, key: "mute_forever", duration: forever)
+
         let cancelAction = UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil)
-        notificationSetupAlert.addAction(cancelAction)
-        present(notificationSetupAlert, animated: true, completion: nil)
+        alert.addAction(cancelAction)
+        present(alert, animated: true, completion: nil)
+    }
+
+    private func addDurationSelectionAction(to alert: UIAlertController, key: String, duration: Int) {
+        let action = UIAlertAction(title: String.localized(key), style: .default, handler: { _ in
+            self.viewModel.context.setChatMuteDuration(chatId: self.viewModel.chatId, duration: duration)
+            self.muteChatCell.actionTitle = String.localized("menu_unmute")
+        })
+        alert.addAction(action)
     }
 
     private func toggleBlockContact() {
@@ -305,8 +334,10 @@ class ContactDetailViewController: UITableViewController {
     }
 
     private func presentPreview(for messageType: Int32, messageType2: Int32, messageType3: Int32) {
-        guard let chatId = viewModel.chatId else { return }
-        let messageIds = viewModel.context.getChatMedia(chatId: chatId, messageType: messageType, messageType2: messageType2, messageType3: messageType3)
+        if viewModel.chatId == 0 {
+            return
+        }
+        let messageIds = viewModel.context.getChatMedia(chatId: viewModel.chatId, messageType: messageType, messageType2: messageType2, messageType3: messageType3)
         var mediaUrls: [URL] = []
         for messageId in messageIds {
             let message = DcMsg.init(id: messageId)
@@ -319,10 +350,10 @@ class ContactDetailViewController: UITableViewController {
     }
 
     private func deleteChat() {
-        guard let chatId = viewModel.chatId else {
+        if viewModel.chatId == 0 {
             return
         }
-        viewModel.context.deleteChat(chatId: chatId)
+        viewModel.context.deleteChat(chatId: viewModel.chatId)
 
         // just pop to viewControllers - we've in chatlist or archive then
         // (no not use `navigationController?` here: popping self will make the reference becoming nil)

+ 2 - 0
deltachat-ios/Helper/Constants.swift

@@ -28,4 +28,6 @@ struct Time {
     static let oneHour = 60 * 60
     static let twoHours = 2 * 60 * 60
     static let sixHours = 6 * 60 * 60
+    static let oneDay = 24 * 60 * 60
+    static let oneWeek = 7 * 24 * 60 * 60
 }

+ 12 - 10
deltachat-ios/ViewModel/ContactDetailViewModel.swift

@@ -13,6 +13,7 @@ class ContactDetailViewModel {
     }
 
     enum ChatAction {
+        case muteChat
         case archiveChat
         case blockContact
         case deleteChat
@@ -29,16 +30,16 @@ class ContactDetailViewModel {
         return DcContact(id: contactId)
     }
 
-    let chatId: Int?
+    let chatId: Int
     private let sharedChats: DcChatlist
     private var sections: [ProfileSections] = []
-    private var chatActions: [ChatAction] = [] // chatDetail: archive, block, delete - else: block
+    private var chatActions: [ChatAction] = []
     private var attachmentActions: [AttachmentAction] = [.gallery, .documents]
 
     init(dcContext: DcContext, contactId: Int) {
         self.context = dcContext
         self.contactId = contactId
-        self.chatId = dcContext.getChatIdByContactIdOld(contactId)
+        self.chatId = dcContext.getChatIdByContactId(contactId: contactId)
         self.sharedChats = context.getChatlist(flags: 0, queryString: nil, queryId: contactId)
 
         sections.append(.attachments)
@@ -48,8 +49,8 @@ class ContactDetailViewModel {
         }
         sections.append(.chatActions)
 
-        if chatId != nil {
-            chatActions = [.archiveChat, .blockContact, .deleteChat]
+        if chatId != 0 {
+            chatActions = [.muteChat, .archiveChat, .blockContact, .deleteChat]
         } else {
             chatActions = [.blockContact]
         }
@@ -68,10 +69,11 @@ class ContactDetailViewModel {
     }
 
     var chatIsArchived: Bool {
-        guard let chatId = chatId else {
-            return false
-        }
-        return context.getChat(chatId: chatId).isArchived
+        return chatId != 0 && context.getChat(chatId: chatId).isArchived
+    }
+
+    var chatIsMuted: Bool {
+        return chatId != 0 && context.getChat(chatId: chatId).isMuted
     }
 
     var numberOfSections: Int {
@@ -111,7 +113,7 @@ class ContactDetailViewModel {
 
     // returns true if chat is archived after action
     func toggleArchiveChat() -> Bool {
-        guard let chatId = chatId else {
+        if chatId == 0 {
             safe_fatalError("there is no chatId - you are probably are calling this from ContactDetail - this should be only called from ChatDetail")
             return false
         }