Selaa lähdekoodia

add clear-chat action to profiles

B. Petersen 2 vuotta sitten
vanhempi
commit
c752a40d9a

+ 29 - 0
deltachat-ios/Controller/ContactDetailViewController.swift

@@ -62,6 +62,13 @@ class ContactDetailViewController: UITableViewController {
         return cell
     }()
 
+    private lazy var clearChatCell: ActionCell = {
+        let cell = ActionCell()
+        cell.actionTitle = String.localized("clear_chat")
+        cell.actionColor = UIColor.red
+        return cell
+    }()
+
     private lazy var deleteChatCell: ActionCell = {
         let cell = ActionCell()
         cell.actionTitle = String.localized("menu_delete_chat")
@@ -182,6 +189,8 @@ class ContactDetailViewController: UITableViewController {
                 return copyToClipboardCell
             case .blockContact:
                 return blockContactCell
+            case .clearChat:
+                return clearChatCell
             case .deleteChat:
                 return deleteChatCell
             }
@@ -318,6 +327,9 @@ class ContactDetailViewController: UITableViewController {
         case .blockContact:
             tableView.deselectRow(at: indexPath, animated: false)
             toggleBlockContact()
+        case .clearChat:
+            tableView.deselectRow(at: indexPath, animated: false)
+            showClearChatConfirmationAlert()
         case .deleteChat:
             tableView.deselectRow(at: indexPath, animated: false)
             showDeleteChatConfirmationAlert()
@@ -369,6 +381,23 @@ class ContactDetailViewController: UITableViewController {
 
     // MARK: alerts
 
+    private func showClearChatConfirmationAlert() {
+        let msgIds = viewModel.context.getChatMsgs(chatId: viewModel.chatId)
+        if !msgIds.isEmpty {
+            let alert = UIAlertController(
+                title: nil,
+                message: String.localized(stringID: "ask_delete_messages_simple", count: msgIds.count),
+                preferredStyle: .safeActionSheet
+            )
+            alert.addAction(UIAlertAction(title: String.localized("clear_chat"), style: .destructive, handler: { _ in
+                self.viewModel.context.deleteMessages(msgIds: msgIds)
+                self.navigationController?.popViewController(animated: true)
+            }))
+            alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
+            self.present(alert, animated: true, completion: nil)
+        }
+    }
+
     private func showDeleteChatConfirmationAlert() {
         let alert = UIAlertController(
             title: nil,

+ 34 - 5
deltachat-ios/Controller/GroupChatDetailViewController.swift

@@ -19,6 +19,7 @@ class GroupChatDetailViewController: UIViewController {
     enum ChatAction {
         case archiveChat
         case leaveGroup
+        case clearChat
         case deleteChat
         case copyToClipboard
     }
@@ -109,6 +110,13 @@ class GroupChatDetailViewController: UIViewController {
         return cell
     }()
 
+    private lazy var clearChatCell: ActionCell = {
+        let cell = ActionCell()
+        cell.actionTitle = String.localized("clear_chat")
+        cell.actionColor = UIColor.red
+        return cell
+    }()
+
     private lazy var deleteChatCell: ActionCell = {
         let cell = ActionCell()
         cell.actionTitle = String.localized("menu_delete_chat")
@@ -266,22 +274,22 @@ class GroupChatDetailViewController: UIViewController {
         if chat.isMailinglist {
             self.chatOptions = [.allMedia]
             self.memberManagementRows = 0
-            self.chatActions = [.archiveChat, .copyToClipboard, .deleteChat]
+            self.chatActions = [.archiveChat, .copyToClipboard, .clearChat, .deleteChat]
             self.groupHeader.showMuteButton(show: true)
         } else if chat.isBroadcast {
             self.chatOptions = [.allMedia]
             self.memberManagementRows = 1
-            self.chatActions = [.archiveChat, .deleteChat]
+            self.chatActions = [.archiveChat, .clearChat, .deleteChat]
             self.groupHeader.showMuteButton(show: false)
         } else if chat.canSend {
             self.chatOptions = [.allMedia, .ephemeralMessages]
             self.memberManagementRows = 2
-            self.chatActions = [.archiveChat, .leaveGroup, .deleteChat]
+            self.chatActions = [.archiveChat, .leaveGroup, .clearChat, .deleteChat]
             self.groupHeader.showMuteButton(show: true)
         } else {
             self.chatOptions = [.allMedia]
             self.memberManagementRows = 0
-            self.chatActions = [.archiveChat, .deleteChat]
+            self.chatActions = [.archiveChat, .clearChat, .deleteChat]
             self.groupHeader.showMuteButton(show: true)
         }
     }
@@ -413,7 +421,6 @@ class GroupChatDetailViewController: UIViewController {
         dcContext.deleteChat(chatId: chatId)
         NotificationManager.removeNotificationsForChat(dcContext: dcContext, chatId: chatId)
         INInteraction.delete(with: ["\(dcContext.id).\(chatId)"])
-
         navigationController?.popViewControllers(viewsToPop: 2, animated: true)
     }
 
@@ -500,6 +507,8 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
                 return archiveChatCell
             case .leaveGroup:
                 return leaveGroupCell
+            case .clearChat:
+                return clearChatCell
             case .deleteChat:
                 return deleteChatCell
             case .copyToClipboard:
@@ -545,6 +554,9 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
             case .leaveGroup:
                 tableView.deselectRow(at: indexPath, animated: false)
                 showLeaveGroupConfirmationAlert()
+            case .clearChat:
+                tableView.deselectRow(at: indexPath, animated: false)
+                showClearChatConfirmationAlert()
             case .deleteChat:
                 tableView.deselectRow(at: indexPath, animated: false)
                 showDeleteChatConfirmationAlert()
@@ -652,6 +664,23 @@ extension GroupChatDetailViewController {
         alert.addAction(action)
     }
 
+    private func showClearChatConfirmationAlert() {
+        let msgIds = dcContext.getChatMsgs(chatId: chatId)
+        if !msgIds.isEmpty {
+            let alert = UIAlertController(
+                title: nil,
+                message: String.localized(stringID: "ask_delete_messages_simple", count: msgIds.count),
+                preferredStyle: .safeActionSheet
+            )
+            alert.addAction(UIAlertAction(title: String.localized("clear_chat"), style: .destructive, handler: { _ in
+                self.dcContext.deleteMessages(msgIds: msgIds)
+                self.navigationController?.popViewController(animated: true)
+            }))
+            alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
+            self.present(alert, animated: true, completion: nil)
+        }
+    }
+
     private func showDeleteChatConfirmationAlert() {
         let alert = UIAlertController(
             title: nil,

+ 2 - 0
deltachat-ios/ViewModel/ContactDetailViewModel.swift

@@ -23,6 +23,7 @@ class ContactDetailViewModel {
         case showEncrInfo
         case copyToClipboard
         case blockContact
+        case clearChat
         case deleteChat
     }
 
@@ -85,6 +86,7 @@ class ContactDetailViewModel {
                 chatActions.append(.copyToClipboard)
                 chatActions.append(.blockContact)
             }
+            chatActions.append(.clearChat)
             chatActions.append(.deleteChat)
         } else {
             chatOptions = [.allMedia, .startChat]