Эх сурвалжийг харах

Merge pull request #879 from deltachat/tweak-ordering

tweak ordering, allow avatar-deletion
cyBerta 5 жил өмнө
parent
commit
9f7731a39e

+ 1 - 1
DcCore/DcCore/DC/Wrapper.swift

@@ -371,7 +371,7 @@ public class DcContext {
        return nil
     }
 
-    public func saveChatAvatarImage(chatId: Int, path: String) {
+    public func setChatProfileImage(chatId: Int, path: String?) {
         dc_set_chat_profile_image(contextPointer, UInt32(chatId), path)
     }
 

+ 29 - 14
deltachat-ios/Controller/EditGroupViewController.swift

@@ -4,10 +4,12 @@ import DcCore
 class EditGroupViewController: UITableViewController, MediaPickerDelegate {
     private let dcContext: DcContext
     private let chat: DcChat
-    private var groupImage: UIImage?
 
-    private let rowAvatar = 0
-    private let rowGroupName = 1
+    private var changeGroupImage: UIImage?
+    private var deleteGroupImage: Bool = false
+
+    private let rowGroupName = 0
+    private let rowAvatar = 1
 
     var avatarSelectionCell: AvatarSelectionCell
 
@@ -38,7 +40,7 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
     init(dcContext: DcContext, chat: DcChat) {
         self.dcContext = dcContext
         self.chat = chat
-        self.avatarSelectionCell = AvatarSelectionCell(chat: chat)
+        self.avatarSelectionCell = AvatarSelectionCell(image: chat.profileImage)
         super.init(style: .grouped)
         self.avatarSelectionCell.hintLabel.text = String.localized("group_avatar")
         self.avatarSelectionCell.onAvatarTapped = onAvatarTapped
@@ -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)
@@ -86,16 +90,16 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
     }
 
     private func groupNameEdited(_ textField: UITextField) {
-        avatarSelectionCell.onInitialsChanged(text: textField.text)
         doneButton.isEnabled = true
     }
 
     private func onAvatarTapped() {
-        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .safeActionSheet)
-            let photoAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
-            let videoAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
-            alert.addAction(photoAction)
-            alert.addAction(videoAction)
+        let alert = UIAlertController(title: String.localized("group_avatar"), message: nil, preferredStyle: .safeActionSheet)
+            alert.addAction(PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:)))
+            alert.addAction(PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:)))
+            if avatarSelectionCell.isAvatarSet() {
+                alert.addAction(UIAlertAction(title: String.localized("delete"), style: .destructive, handler: deleteGroupAvatarPressed(_:)))
+            }
             alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
         self.present(alert, animated: true, completion: nil)
     }
@@ -108,11 +112,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(context: nil, with: groupImage)
+    func updateAvatarRow(image: UIImage?) {
+        avatarSelectionCell = AvatarSelectionCell(image: image)
         avatarSelectionCell.hintLabel.text = String.localized("group_avatar")
         avatarSelectionCell.onAvatarTapped = onAvatarTapped
 

+ 4 - 8
deltachat-ios/Controller/EditSettingsController.swift

@@ -139,14 +139,10 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
 
     private func onAvatarTapped() {
         let alert = UIAlertController(title: String.localized("pref_profile_photo"), message: nil, preferredStyle: .safeActionSheet)
-        let photoAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
-        let videoAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
-        let deleteAction = UIAlertAction(title: String.localized("delete"), style: .destructive, handler: deleteProfileIconPressed(_:))
-
-        alert.addAction(photoAction)
-        alert.addAction(videoAction)
+        alert.addAction(PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:)))
+        alert.addAction(PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:)))
         if dcContext.getSelfAvatarImage() != nil {
-            alert.addAction(deleteAction)
+            alert.addAction(UIAlertAction(title: String.localized("delete"), style: .destructive, handler: deleteProfileIconPressed(_:)))
         }
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
 
@@ -169,7 +165,7 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     }
 
     private func createPictureAndNameCell() -> AvatarSelectionCell {
-        let cell = AvatarSelectionCell(context: dcContext)
+        let cell = AvatarSelectionCell(image: dcContext.getSelfAvatarImage())
         return cell
     }
 

+ 29 - 13
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?
@@ -16,8 +19,8 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     private var workaroundObserver: NSObjectProtocol?
 
     private let sectionGroupDetails = 0
-    private let sectionGroupDetailsRowAvatar = 0
-    private let sectionGroupDetailsRowName = 1
+    private let sectionGroupDetailsRowName = 0
+    private let sectionGroupDetailsRowAvatar = 1
     private let countSectionGroupDetails = 2
     private let sectionInvite = 1
     private let sectionInviteRowAddMembers = 0
@@ -39,7 +42,7 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     }()
 
     lazy var avatarSelectionCell: AvatarSelectionCell = {
-        let cell = AvatarSelectionCell(context: nil)
+        let cell = AvatarSelectionCell(image: nil)
         cell.hintLabel.text = String.localized("group_avatar")
         cell.onAvatarTapped = onAvatarTapped
         return cell
@@ -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 {
@@ -281,11 +286,12 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     }
 
     private func onAvatarTapped() {
-        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .safeActionSheet)
-            let photoAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
-            let videoAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
-            alert.addAction(photoAction)
-            alert.addAction(videoAction)
+        let alert = UIAlertController(title: String.localized("group_avatar"), message: nil, preferredStyle: .safeActionSheet)
+            alert.addAction(PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:)))
+            alert.addAction(PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:)))
+            if avatarSelectionCell.isAvatarSet() {
+                alert.addAction(UIAlertAction(title: String.localized("delete"), style: .destructive, handler: deleteGroupAvatarPressed(_:)))
+            }
             alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
         self.present(alert, animated: true, completion: nil)
     }
@@ -298,10 +304,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(context: nil, with: groupImage)
+    func updateAvatarRow(image: UIImage?) {
+        avatarSelectionCell = AvatarSelectionCell(image: image)
         avatarSelectionCell.hintLabel.text = String.localized("group_avatar")
         avatarSelectionCell.onAvatarTapped = onAvatarTapped
 

+ 2 - 2
deltachat-ios/Controller/ProfileInfoViewController.swift

@@ -22,7 +22,7 @@ class ProfileInfoViewController: UITableViewController {
     }()
 
     private lazy var avatarCell: AvatarSelectionCell = {
-        let cell = AvatarSelectionCell(context: self.dcContext)
+        let cell = AvatarSelectionCell(image: dcContext.getSelfAvatarImage())
         cell.onAvatarTapped = avatarTapped
         return cell
     }()
@@ -76,7 +76,7 @@ class ProfileInfoViewController: UITableViewController {
     // MARK: - updates
     private func updateAvatarCell() {
         if let avatarImage = dcContext.getSelfAvatarImage() {
-            avatarCell.updateAvatar(image: avatarImage)
+            avatarCell.setAvatar(image: avatarImage)
         }
         self.tableView.beginUpdates()
         let indexPath = IndexPath(row: 1, section: 0)

+ 8 - 4
deltachat-ios/Helper/AvatarHelper.swift

@@ -20,11 +20,15 @@ class AvatarHelper {
         }
     }
 
-    static func saveChatAvatar(dcContext: DcContext, image: UIImage, for chatId: Int) {
+    static func saveChatAvatar(dcContext: DcContext, image: UIImage?, for chatId: Int) {
         do {
-            let groupFileName = try saveAvatarImageToFile(image: image)
-            dcContext.saveChatAvatarImage(chatId: chatId, path: groupFileName.path)
-            deleteAvatarFile(groupFileName)
+            if let image = image {
+                let groupFileName = try saveAvatarImageToFile(image: image)
+                dcContext.setChatProfileImage(chatId: chatId, path: groupFileName.path)
+                deleteAvatarFile(groupFileName)
+            } else {
+                dcContext.setChatProfileImage(chatId: chatId, path: nil)
+            }
         } catch let error {
             logger.error("Error saving Image: \(error.localizedDescription)")
         }

+ 10 - 31
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)?
 
@@ -29,16 +30,9 @@ class AvatarSelectionCell: UITableViewCell {
         return label
     }()
 
-    init(chat: DcChat) {
+    init(image: UIImage?) {
         super.init(style: .default, reuseIdentifier: nil)
-        setAvatar(for: chat)
-        setupSubviews()
-    }
-
-    init(context: DcContext?, with defaultImage: UIImage? = nil) {
-        super.init(style: .default, reuseIdentifier: nil)
-        setAvatar(image: context?.getSelfAvatarImage(),
-                  with: defaultImage ?? self.defaultImage)
+        setAvatar(image: image)
         setupSubviews()
     }
 
@@ -69,12 +63,6 @@ class AvatarSelectionCell: UITableViewCell {
         selectionStyle = .none
     }
 
-    func onInitialsChanged(text: String?) {
-        if badge.showsInitials() {
-            badge.setName(text ?? "")
-        }
-    }
-
     @objc func onBadgeTouched(gesture: UILongPressGestureRecognizer) {
         switch gesture.state {
         case .began:
@@ -89,27 +77,18 @@ class AvatarSelectionCell: UITableViewCell {
         }
     }
 
-    // I think this is no good, we should rather update badge than overwriting it.
-    func setAvatar(for chat: DcChat) {
-        if let image = chat.profileImage {
-            badge = InitialsBadge(image: image, size: badgeSize)
-        } else {
-            badge = InitialsBadge(name: chat.name, color: chat.color, size: badgeSize)
-        }
-        badge.setVerified(chat.isVerified)
-    }
-
-    // I think this is no good, we should rather update badge than overwriting it.
-    func setAvatar(image: UIImage?, with defaultImage: UIImage?) {
+    func setAvatar(image: UIImage?) {
         if let image = image {
-            badge = InitialsBadge(image: image, size: badgeSize)
-        } else if let defaultImage = defaultImage {
+            badge.setImage(image)
+            avatarSet = true
+        } else {
             badge = InitialsBadge(image: defaultImage, size: badgeSize)
             badge.backgroundColor = DcColors.grayTextColor
+            avatarSet = false
         }
     }
 
-    func updateAvatar(image: UIImage) {
-        badge.setImage(image)
+    func isAvatarSet() -> Bool {
+        return avatarSet
     }
 }