Răsfoiți Sursa

allow deletion of group-avatars

B. Petersen 5 ani în urmă
părinte
comite
6db549e841

+ 24 - 5
deltachat-ios/Controller/EditGroupViewController.swift

@@ -4,7 +4,9 @@ import DcCore
 class EditGroupViewController: UITableViewController, MediaPickerDelegate {
     private let dcContext: DcContext
     private let chat: DcChat
-    private var groupImage: UIImage?
+
+    private var changeGroupImage: UIImage?
+    private var deleteGroupImage: Bool = false
 
     private let rowGroupName = 0
     private let rowAvatar = 1
@@ -74,8 +76,10 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
     
     @objc func saveContactButtonPressed() {
         let newName = groupNameCell.getText()
-        if let groupImage = groupImage {
-            AvatarHelper.saveChatAvatar(dcContext: dcContext, image: groupImage, for: Int(chat.id))
+        if let groupImage = changeGroupImage {
+            AvatarHelper.saveChatAvatar(dcContext: dcContext, image: groupImage, for: chat.id)
+        } else if deleteGroupImage {
+            AvatarHelper.saveChatAvatar(dcContext: dcContext, image: nil, for: chat.id)
         }
         _ = dcContext.setChatName(chatId: chat.id, name: newName ?? "")
         navigationController?.popViewController(animated: true)
@@ -93,8 +97,12 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
         let alert = UIAlertController(title: String.localized("group_avatar"), message: nil, preferredStyle: .safeActionSheet)
             let cameraAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
             let galleryAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
+            let deleteAction = UIAlertAction(title: String.localized("delete"), style: .destructive, handler: deleteGroupAvatarPressed(_:))
             alert.addAction(cameraAction)
             alert.addAction(galleryAction)
+            if avatarSelectionCell.isAvatarSet() {
+                alert.addAction(deleteAction)
+            }
             alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
         self.present(alert, animated: true, completion: nil)
     }
@@ -107,11 +115,22 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
         mediaPicker?.showCamera(allowCropping: true, supportedMediaTypes: .photo)
     }
 
+    private func deleteGroupAvatarPressed(_ action: UIAlertAction) {
+        changeGroupImage = nil
+        deleteGroupImage = true
+        doneButton.isEnabled = true
+        updateAvatarRow(image: nil)
+    }
+
     func onImageSelected(image: UIImage) {
-        groupImage = image
+        changeGroupImage = image
+        deleteGroupImage = false
         doneButton.isEnabled = true
+        updateAvatarRow(image: changeGroupImage)
+    }
 
-        avatarSelectionCell = AvatarSelectionCell(image: groupImage)
+    func updateAvatarRow(image: UIImage?) {
+        avatarSelectionCell = AvatarSelectionCell(image: image)
         avatarSelectionCell.hintLabel.text = String.localized("group_avatar")
         avatarSelectionCell.onAvatarTapped = onAvatarTapped
 

+ 24 - 5
deltachat-ios/Controller/NewGroupController.swift

@@ -8,7 +8,10 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     var doneButton: UIBarButtonItem!
     var contactIdsForGroup: Set<Int> // TODO: check if array is sufficient
     var groupContactIds: [Int]
-    var groupImage: UIImage?
+
+    private var changeGroupImage: UIImage?
+    private var deleteGroupImage: Bool = false
+
     let isVerifiedGroup: Bool
     let dcContext: DcContext
     private var contactAddedObserver: NSObjectProtocol?
@@ -128,8 +131,10 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
         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 let groupImage = changeGroupImage {
+                AvatarHelper.saveChatAvatar(dcContext: dcContext, image: groupImage, for: groupChatId)
+            } else if deleteGroupImage {
+                AvatarHelper.saveChatAvatar(dcContext: dcContext, image: nil, for: groupChatId)
             }
 
             if success {
@@ -284,8 +289,12 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
         let alert = UIAlertController(title: String.localized("group_avatar"), message: nil, preferredStyle: .safeActionSheet)
             let cameraAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
             let galleryAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
+            let deleteAction = UIAlertAction(title: String.localized("delete"), style: .destructive, handler: deleteGroupAvatarPressed(_:))
             alert.addAction(cameraAction)
             alert.addAction(galleryAction)
+            if avatarSelectionCell.isAvatarSet() {
+                alert.addAction(deleteAction)
+            }
             alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
         self.present(alert, animated: true, completion: nil)
     }
@@ -298,10 +307,20 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
         showCamera(delegate: self)
     }
 
+    private func deleteGroupAvatarPressed(_ action: UIAlertAction) {
+        changeGroupImage = nil
+        deleteGroupImage = true
+        updateAvatarRow(image: nil)
+    }
+
     func onImageSelected(image: UIImage) {
-        groupImage = image
+        changeGroupImage = image
+        deleteGroupImage = false
+        updateAvatarRow(image: changeGroupImage)
+    }
 
-        avatarSelectionCell = AvatarSelectionCell(image: groupImage)
+    func updateAvatarRow(image: UIImage?) {
+        avatarSelectionCell = AvatarSelectionCell(image: image)
         avatarSelectionCell.hintLabel.text = String.localized("group_avatar")
         avatarSelectionCell.onAvatarTapped = onAvatarTapped
 

+ 7 - 0
deltachat-ios/View/AvatarSelectionCell.swift

@@ -3,6 +3,7 @@ import DcCore
 
 class AvatarSelectionCell: UITableViewCell {
     let badgeSize: CGFloat = 72
+    private var avatarSet = false
 
     var onAvatarTapped: (() -> Void)?
 
@@ -79,9 +80,15 @@ class AvatarSelectionCell: UITableViewCell {
     func setAvatar(image: UIImage?) {
         if let image = image {
             badge.setImage(image)
+            avatarSet = true
         } else {
             badge = InitialsBadge(image: defaultImage, size: badgeSize)
             badge.backgroundColor = DcColors.grayTextColor
+            avatarSet = false
         }
     }
+
+    func isAvatarSet() -> Bool {
+        return avatarSet
+    }
 }