Explorar el Código

implement changes after PR review: move avatar selection and contact/group name to separate cells

cyberta hace 5 años
padre
commit
dc46410fbc

+ 4 - 4
deltachat-ios.xcodeproj/project.pbxproj

@@ -115,7 +115,7 @@
 		8B6D425BC604F7C43B65D436 /* Pods_deltachat_ios.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6241BE1534A653E79AD5D01D /* Pods_deltachat_ios.framework */; };
 		AE0D26FD1FB1FE88002FAFCE /* ChatListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */; };
 		AE18F294228C602A0007B1BE /* SecuritySettingsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE18F293228C602A0007B1BE /* SecuritySettingsController.swift */; };
-		AE25F09022807AD800CDEA66 /* AvatarEditTextCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE25F08F22807AD800CDEA66 /* AvatarEditTextCell.swift */; };
+		AE25F09022807AD800CDEA66 /* AvatarSelectionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE25F08F22807AD800CDEA66 /* AvatarSelectionCell.swift */; };
 		AE38B31822672DFC00EC37A1 /* ActionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE38B31722672DFC00EC37A1 /* ActionCell.swift */; };
 		AE38B31A2267328200EC37A1 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE38B3192267328200EC37A1 /* Colors.swift */; };
 		AE4AEE3522B1030D000AA495 /* PreviewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE4AEE3422B1030D000AA495 /* PreviewController.swift */; };
@@ -331,7 +331,7 @@
 		A8615D4600859851E53CAA9C /* Pods-deltachat-ios.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-deltachat-ios.release.xcconfig"; path = "Pods/Target Support Files/Pods-deltachat-ios/Pods-deltachat-ios.release.xcconfig"; sourceTree = "<group>"; };
 		AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = ChatListController.swift; sourceTree = "<group>"; tabWidth = 4; };
 		AE18F293228C602A0007B1BE /* SecuritySettingsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecuritySettingsController.swift; sourceTree = "<group>"; };
-		AE25F08F22807AD800CDEA66 /* AvatarEditTextCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarEditTextCell.swift; sourceTree = "<group>"; };
+		AE25F08F22807AD800CDEA66 /* AvatarSelectionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarSelectionCell.swift; sourceTree = "<group>"; };
 		AE38B31722672DFC00EC37A1 /* ActionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionCell.swift; sourceTree = "<group>"; };
 		AE38B3192267328200EC37A1 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = "<group>"; };
 		AE4AEE3422B1030D000AA495 /* PreviewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewController.swift; sourceTree = "<group>"; };
@@ -712,7 +712,7 @@
 				789E879C21D6DF86003ED1C5 /* ProgressHud.swift */,
 				AE38B31722672DFC00EC37A1 /* ActionCell.swift */,
 				AE851ACD227CA54300ED86F0 /* InitialsBadge.swift */,
-				AE25F08F22807AD800CDEA66 /* AvatarEditTextCell.swift */,
+				AE25F08F22807AD800CDEA66 /* AvatarSelectionCell.swift */,
 				AE728F14229D5C390047565B /* PhotoPickerAlertAction.swift */,
 				AE52EA18229EB53C00C586C9 /* ContactDetailHeader.swift */,
 				AE9DAF0E22C278C6004C9591 /* ChatTitleView.swift */,
@@ -1060,7 +1060,7 @@
 				305961E52346125100C80F33 /* LabelAlignment.swift in Sources */,
 				305961E82346125100C80F33 /* Sender.swift in Sources */,
 				305961EE2346125100C80F33 /* AvatarPosition.swift in Sources */,
-				AE25F09022807AD800CDEA66 /* AvatarEditTextCell.swift in Sources */,
+				AE25F09022807AD800CDEA66 /* AvatarSelectionCell.swift in Sources */,
 				305961E62346125100C80F33 /* LocationMessageSnapshotOptions.swift in Sources */,
 				AEE6EC3F2282C59C00EDC689 /* GroupMembersViewController.swift in Sources */,
 				B26B3BC7236DC3DC008ED35A /* SwitchCell.swift in Sources */,

+ 26 - 11
deltachat-ios/Controller/EditGroupViewController.swift

@@ -6,7 +6,17 @@ class EditGroupViewController: UITableViewController {
 
     private let chat: DcChat
 
-    var groupNameCell: AvatarEditTextCell
+    private let rowAvatar = 0
+    private let rowGroupName = 1
+
+    var avatarSelectionCell: AvatarSelectionCell
+
+    lazy var groupNameCell: TextFieldCell = {
+        let cell = TextFieldCell(description: String.localized("group_name"), placeholder: self.chat.name)
+        cell.setText(text: self.chat.name)
+        cell.onTextFieldChange = self.groupNameEdited(_:)
+        return cell
+    }()
 
     lazy var doneButton: UIBarButtonItem = {
         let button = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(saveContactButtonPressed))
@@ -21,12 +31,10 @@ class EditGroupViewController: UITableViewController {
 
     init(chat: DcChat) {
         self.chat = chat
-        self.groupNameCell = AvatarEditTextCell(chat: chat)
+        self.avatarSelectionCell = AvatarSelectionCell(chat: chat)
         super.init(style: .grouped)
-        self.groupNameCell.inputField.text = chat.name
-        self.groupNameCell.onTextChanged = groupNameEdited(_:)
-        self.groupNameCell.selectionStyle = .none
-        self.groupNameCell.hintLabel.text = String.localized("group_name")
+        self.avatarSelectionCell.selectionStyle = .none
+        self.avatarSelectionCell.hintLabel.text = String.localized("group_avatar")
     }
 
     required init?(coder aDecoder: NSCoder) {
@@ -40,7 +48,11 @@ class EditGroupViewController: UITableViewController {
     }
 
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-        return groupNameCell
+        if indexPath.row == rowAvatar {
+            return avatarSelectionCell
+        } else {
+            return groupNameCell
+        }
     }
 
     override func numberOfSections(in tableView: UITableView) -> Int {
@@ -48,13 +60,15 @@ class EditGroupViewController: UITableViewController {
     }
 
     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-        return 1
+        return 2
     }
 
     override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
-        return AvatarEditTextCell.cellSize
+        if indexPath.row == rowAvatar {
+            return AvatarSelectionCell.cellSize
+        }
+        return Constants.stdCellHeight
     }
-
     
     @objc func saveContactButtonPressed() {
         let newName = groupNameCell.getText()
@@ -66,7 +80,8 @@ class EditGroupViewController: UITableViewController {
         coordinator?.navigateBack()
     }
 
-    private func groupNameEdited(_ newName: String) {
+    private func groupNameEdited(_ textField: UITextField) {
+        avatarSelectionCell.onInitialsChanged(text: textField.text)
         doneButton.isEnabled = true
     }
 }

+ 24 - 27
deltachat-ios/Controller/EditSettingsController.swift

@@ -10,9 +10,10 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     private let groupBadgeSize: CGFloat = 72
 
     private let section1 = 0
-    private let section1PictureAndName = 0
-    private let section1Status = 1
-    private let section1RowCount = 2
+    private let section1Avatar = 0
+    private let section1Name = 1
+    private let section1Status = 2
+    private let section1RowCount = 3
 
     private let section2 = 1
     private let section2AccountSettings = 0
@@ -24,13 +25,6 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
 
     private var childCoordinators: Coordinator?
 
-    private lazy var defaultImage: UIImage = {
-        if let image = UIImage(named: "camera") {
-            return image.invert()
-        }
-        return UIImage()
-    }()
-
     private lazy var statusCell: TextFieldCell = {
         let cell = TextFieldCell(description: String.localized("pref_default_status_label"), placeholder: String.localized("pref_default_status_label"))
         cell.setText(text: DcConfig.selfstatus ?? nil)
@@ -46,10 +40,15 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     }()
 
 
-    private lazy var pictureAndNameCell: AvatarEditTextCell = {
+    private lazy var avatarSelectionCell: AvatarSelectionCell = {
         return createPictureAndNameCell()
     }()
 
+    private lazy var nameCell: TextFieldCell = {
+        let cell = TextFieldCell(description: String.localized("pref_your_name"), placeholder: String.localized("pref_your_name"))
+        cell.setText(text: DcConfig.displayname)
+        return cell
+    }()
 
     init(dcContext: DcContext) {
         self.dcContext = dcContext
@@ -64,15 +63,12 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     override func viewDidLoad() {
         super.viewDidLoad()
         title = String.localized("pref_profile_info_headline")
-
-        tableView.register(AvatarEditTextCell.self, forCellReuseIdentifier: "pictureAndNameCell")
-        tableView.register(ContactCell.self, forCellReuseIdentifier: "contactCell")
-        pictureAndNameCell.onAvatarTapped = onAvatarTapped
+        avatarSelectionCell.onAvatarTapped = onAvatarTapped
     }
 
     override func viewWillDisappear(_ animated: Bool) {
         DcConfig.selfstatus = statusCell.getText()
-        DcConfig.displayname = pictureAndNameCell.getText()
+        DcConfig.displayname = nameCell.getText()
         dc_configure(mailboxPointer)
     }
 
@@ -93,8 +89,10 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         if indexPath.section == section1 {
             switch indexPath.row {
-            case section1PictureAndName:
-                return pictureAndNameCell
+            case section1Avatar:
+                return avatarSelectionCell
+            case section1Name:
+                return nameCell
             case section1Status:
                 return statusCell
             default:
@@ -106,10 +104,10 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     }
 
     override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
-        if indexPath.section == section1 && indexPath.row == section1PictureAndName {
-            return AvatarEditTextCell.cellSize
+        if indexPath.section == section1 && indexPath.row == section1Avatar {
+            return AvatarSelectionCell.cellSize
         } else {
-            return 48
+            return Constants.stdCellHeight
         }
     }
 
@@ -158,20 +156,19 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     func onImageSelected(image: UIImage) {
         AvatarHelper.saveSelfAvatarImage(image: image)
 
-        self.pictureAndNameCell = createPictureAndNameCell()
-        self.pictureAndNameCell.onAvatarTapped = onAvatarTapped
+        self.avatarSelectionCell = createPictureAndNameCell()
+        self.avatarSelectionCell.onAvatarTapped = onAvatarTapped
 
         self.tableView.beginUpdates()
-        let indexPath = IndexPath(row: section1PictureAndName, section: section1)
+        let indexPath = IndexPath(row: section1Avatar, section: section1)
         self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
         self.tableView.endUpdates()
     }
 
     func onDismiss() { }
 
-    private func createPictureAndNameCell() -> AvatarEditTextCell {
-        let cell = AvatarEditTextCell(context: dcContext, defaultImage: defaultImage, downscale: 0.6)
-        cell.inputField.text = DcConfig.displayname
+    private func createPictureAndNameCell() -> AvatarSelectionCell {
+        let cell = AvatarSelectionCell(context: dcContext)
         cell.selectionStyle = .none
         return cell
     }

+ 34 - 13
deltachat-ios/Controller/GroupNameController.swift

@@ -9,6 +9,24 @@ class GroupNameController: UITableViewController {
     let contactIdsForGroup: Set<Int> // TODO: check if array is sufficient
     let groupContactIds: [Int]
 
+    private let sectionGroupDetails = 0
+    private let sectionGroupDetailsRowAvatar = 0
+    private let sectionGroupDetailsRowName = 1
+    private let countSectionGroupDetails = 2
+
+    lazy var groupNameCell: TextFieldCell = {
+        let cell = TextFieldCell(description: String.localized("group_name"), placeholder: String.localized("menu_edit_group_name"))
+        cell.onTextFieldChange = self.updateGroupName
+        return cell
+    }()
+
+    lazy var avatarSelectionCell: AvatarSelectionCell = {
+        let cell = AvatarSelectionCell(context: nil)
+        cell.selectionStyle = .none
+        cell.hintLabel.text = String.localized("group_avatar")
+        return cell
+    }()    
+
     init(contactIdsForGroup: Set<Int>) {
         self.contactIdsForGroup = contactIdsForGroup
         groupContactIds = Array(contactIdsForGroup)
@@ -26,9 +44,7 @@ class GroupNameController: UITableViewController {
         navigationItem.rightBarButtonItem = doneButton
         tableView.bounces = false
         doneButton.isEnabled = false
-        tableView.register(AvatarEditTextCell.self, forCellReuseIdentifier: "groupLabelCell")
         tableView.register(ContactCell.self, forCellReuseIdentifier: "contactCell")
-        // setupSubviews()
     }
 
     @objc func doneButtonPressed() {
@@ -58,13 +74,12 @@ class GroupNameController: UITableViewController {
         let section = indexPath.section
         let row = indexPath.row
 
-        if section == 0 {
-            let cell = tableView.dequeueReusableCell(withIdentifier: "groupLabelCell", for: indexPath)
-            if let groupLabelCell = cell as? AvatarEditTextCell {
-                groupLabelCell.onTextChanged = updateGroupName
-                groupLabelCell.hintLabel.text = String.localized("group_name")
+        if section == sectionGroupDetails {
+            if row == sectionGroupDetailsRowAvatar {
+                return avatarSelectionCell
+            } else {
+                return groupNameCell
             }
-            return cell
         } else {
             let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
             if let contactCell = cell as? ContactCell {
@@ -81,16 +96,21 @@ class GroupNameController: UITableViewController {
 
     override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
         let section = indexPath.section
-        if section == 0 {
-            return AvatarEditTextCell.cellSize
+        let row = indexPath.row
+        if section == sectionGroupDetails {
+            if row == sectionGroupDetailsRowAvatar {
+                return AvatarSelectionCell.cellSize
+            } else {
+                return Constants.stdCellHeight
+            }
         } else {
             return ContactCell.cellSize
         }
     }
 
     override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
-        if section == 0 {
-            return 1
+        if section == sectionGroupDetails {
+            return countSectionGroupDetails
         } else {
             return contactIdsForGroup.count
         }
@@ -104,7 +124,8 @@ class GroupNameController: UITableViewController {
         }
     }
 
-    private func updateGroupName(name: String) {
+    private func updateGroupName(textView: UITextField) {
+        let name = textView.text ?? ""
         groupName = name
         doneButton.isEnabled = name.containsCharacters()
     }

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

@@ -15,4 +15,6 @@ struct Constants {
     static let onlineShadow = UIImage(color: UIColor(hexString: "3ed67e"), size: CGSize(width: 1, height: 1))
 
     static let notificationIdentifier = "deltachat-ios-local-notifications"
+
+    static let stdCellHeight: CGFloat = 48
 }

+ 20 - 43
deltachat-ios/View/AvatarEditTextCell.swift → deltachat-ios/View/AvatarSelectionCell.swift

@@ -1,12 +1,19 @@
 import UIKit
 
-class AvatarEditTextCell: UITableViewCell {
+class AvatarSelectionCell: UITableViewCell {
     let badgeSize: CGFloat = 72
     static let cellSize: CGFloat = 98
+    let downscaleDefaultImage: CGFloat = 0.6
 
-    var onTextChanged: ((String) -> Void)? // use this callback to update editButton in navigationController
     var onAvatarTapped: (() -> Void)?
 
+    lazy var defaultImage: UIImage = {
+        if let image = UIImage(named: "camera") {
+            return image.invert()
+        }
+        return UIImage()
+    }()
+
     lazy var badge: InitialsBadge = {
         let badge = InitialsBadge(size: badgeSize)
         badge.layer.cornerRadius = badgeSize / 2
@@ -15,24 +22,11 @@ class AvatarEditTextCell: UITableViewCell {
         return badge
     }()
 
-    lazy var inputField: UITextField = {
-        let textField = UITextField()
-        textField.borderStyle = .none
-        textField.becomeFirstResponder()
-        textField.autocorrectionType = .no
-        textField.addTarget(self, action: #selector(inputFieldChanged), for: .editingChanged)
-        textField.translatesAutoresizingMaskIntoConstraints = false
-        textField.textAlignment = .right
-        return textField
-    }()
-
     lazy var hintLabel: UILabel = {
         let label = UILabel(frame: .zero)
         label.translatesAutoresizingMaskIntoConstraints = false
         label.textColor = DcColors.defaultTextColor
-        label.font = UIFont.preferredFont(forTextStyle: .footnote)
-        label.text = String.localized("pref_your_name")
-        label.textAlignment = .right
+        label.text = String.localized("pref_profile_photo")
         return label
     }()
 
@@ -42,10 +36,9 @@ class AvatarEditTextCell: UITableViewCell {
         setupSubviews()
     }
 
-
-    init(context: DcContext, defaultImage: UIImage, downscale: CGFloat? = nil) {
+    init(context: DcContext?) {
         super.init(style: .default, reuseIdentifier: nil)
-        setSelfAvatar(context: context, with: defaultImage, downscale: downscale)
+        setAvatar(image: context?.getSelfAvatarImage(), with: self.defaultImage, downscale: downscaleDefaultImage)
         setupSubviews()
     }
 
@@ -60,32 +53,24 @@ class AvatarEditTextCell: UITableViewCell {
 
     private func setupSubviews() {
         contentView.addSubview(badge)
-
-        badge.alignLeadingToAnchor(contentView.layoutMarginsGuide.leadingAnchor)
+        badge.alignTrailingToAnchor(contentView.layoutMarginsGuide.trailingAnchor)
         badge.alignTopToAnchor(contentView.layoutMarginsGuide.topAnchor)
-        contentView.addSubview(inputField)
 
-        inputField.alignLeadingToAnchor(badge.trailingAnchor, paddingLeading: 15)
-        inputField.addConstraints(heightConstant: CGFloat(20))
-        inputField.alignTrailingToAnchor(contentView.layoutMarginsGuide.trailingAnchor)
-        inputField.alignBottomToAnchor(contentView.layoutMarginsGuide.bottomAnchor, paddingBottom: 15)
         contentView.addSubview(hintLabel)
-
+        hintLabel.alignLeadingToAnchor(contentView.layoutMarginsGuide.leadingAnchor)
         hintLabel.alignTopToAnchor(contentView.layoutMarginsGuide.topAnchor)
-        hintLabel.alignTrailingToAnchor(contentView.layoutMarginsGuide.trailingAnchor)
-        hintLabel.addConstraints(heightConstant: CGFloat(20))
+        hintLabel.alignTrailingToAnchor(badge.leadingAnchor)
+        hintLabel.alignBottomToAnchor(contentView.layoutMarginsGuide.bottomAnchor)
 
         let touchListener = UILongPressGestureRecognizer(target: self, action: #selector(onBadgeTouched))
         touchListener.minimumPressDuration = 0
         badge.addGestureRecognizer(touchListener)
     }
 
-    @objc func inputFieldChanged() {
-        let groupName = inputField.text ?? ""
+    func onInitialsChanged(text: String?) {
         if badge.showsInitials() {
-            badge.setName(groupName)
+            badge.setName(text ?? "")
         }
-        onTextChanged?(groupName)
     }
 
     @objc func onBadgeTouched(gesture: UILongPressGestureRecognizer) {
@@ -102,10 +87,6 @@ class AvatarEditTextCell: UITableViewCell {
         }
     }
 
-    func getText() -> String {
-        return inputField.text ?? ""
-    }
-
     func setAvatar(for chat: DcChat) {
         if let image = chat.profileImage {
             badge = InitialsBadge(image: image, size: badgeSize)
@@ -115,12 +96,8 @@ class AvatarEditTextCell: UITableViewCell {
         badge.setVerified(chat.isVerified)
     }
 
-    func setSelfAvatar(context: DcContext?, with defaultImage: UIImage?, downscale: CGFloat? = nil) {
-        guard let context = context else {
-            return
-        }
-        
-        if let image = context.getSelfAvatarImage() {
+    func setAvatar(image: UIImage?, with defaultImage: UIImage?, downscale: CGFloat? = nil) {
+        if let image = image {
             badge = InitialsBadge(image: image, size: badgeSize)
         } else if let defaultImage = defaultImage {
             badge = InitialsBadge(image: defaultImage, size: badgeSize, downscale: downscale)