Przeglądaj źródła

Merge pull request #409 from deltachat/update_avatars_in_lists

Update avatars in lists
björn petersen 5 lat temu
rodzic
commit
4378a6a66c

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

@@ -111,6 +111,13 @@ class ContactDetailViewController: UITableViewController {
         present(alert, animated: true, completion: nil)
     }
 
+    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
+        if section == 0 {
+            return ContactDetailHeader.cellHeight
+        }
+        return 0
+    }
+    
     override func tableView(_: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         if section == 0 {
             let header = ContactDetailHeader()

+ 1 - 1
deltachat-ios/Controller/EditGroupViewController.swift

@@ -69,7 +69,7 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
         if indexPath.row == rowAvatar {
             return AvatarSelectionCell.cellSize
         }
-        return Constants.stdCellHeight
+        return Constants.defaultCellHeight
     }
     
     @objc func saveContactButtonPressed() {

+ 1 - 1
deltachat-ios/Controller/EditSettingsController.swift

@@ -107,7 +107,7 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
         if indexPath.section == section1 && indexPath.row == section1Avatar {
             return AvatarSelectionCell.cellSize
         } else {
-            return Constants.stdCellHeight
+            return Constants.defaultCellHeight
         }
     }
 

+ 33 - 3
deltachat-ios/Controller/GroupChatDetailViewController.swift

@@ -99,6 +99,13 @@ class GroupChatDetailViewController: UIViewController {
 
 extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSource {
 
+    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
+        if section == sectionMembers {
+            return ContactDetailHeader.cellHeight
+        }
+        return 0
+    }
+    
     func tableView(_: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         if section == sectionMembers {
             let header = ContactDetailHeader()
@@ -134,6 +141,26 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
         }
     }
 
+    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
+        let section = indexPath.section
+        let row = indexPath.row
+        switch section {
+        case sectionMembers:
+            switch row {
+            case sectionMembersRowAddMember:
+                return Constants.defaultCellHeight
+            case sectionMembersRowJoinQR:
+                return Constants.defaultCellHeight
+            default:
+                return ContactCell.cellHeight
+            }
+        case sectionLeaveGroup:
+            return Constants.defaultCellHeight
+        default:
+            return 0
+        }
+    }
+
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let section = indexPath.section
         let row = indexPath.row
@@ -161,9 +188,12 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
                     let displayName = contact.displayName
                     contactCell.nameLabel.text = displayName
                     contactCell.emailLabel.text = contact.email
-                    contactCell.initialsLabel.text = Utils.getInitials(inputName: displayName)
-                    contactCell.setColor(contact.color)
-                    contactCell.setVerified(isVerified: chat.isVerified)
+                    contactCell.avatar.setName(displayName)
+                    contactCell.avatar.setColor(contact.color)
+                    if let profileImage = contact.profileImage {
+                        contactCell.avatar.setImage(profileImage)
+                    }
+                    contactCell.setVerified(isVerified: contact.isVerified)
                 }
                 return cell
             }

+ 21 - 2
deltachat-ios/Controller/GroupMembersViewController.swift

@@ -123,6 +123,17 @@ class AddGroupMembersViewController: GroupMembersViewController {
         }
     }
 
+    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
+        switch indexPath.section {
+        case sectionNewContact:
+            return Constants.defaultCellHeight
+        case sectionMemberList:
+            return ContactCell.cellHeight
+        default:
+            return Constants.defaultCellHeight
+        }
+    }
+    
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         switch indexPath.section {
         case sectionNewContact:
@@ -305,6 +316,9 @@ class GroupMembersViewController: UITableViewController, UISearchResultsUpdating
         return getNumberOfRowsForContactList()
     }
 
+    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
+        return ContactCell.cellHeight
+    }
 
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         return getContactCell(cellForRowAt: indexPath)
@@ -375,8 +389,13 @@ class GroupMembersViewController: UITableViewController, UISearchResultsUpdating
         let emailLabelFontSize = cell.emailLabel.font.pointSize
         let nameLabelFontSize = cell.nameLabel.font.pointSize
 
-        cell.initialsLabel.text = Utils.getInitials(inputName: displayName)
-        cell.setColor(contact.color)
+        cell.nameLabel.text = displayName
+        cell.emailLabel.text = contact.email
+        cell.avatar.setName(displayName)
+        cell.avatar.setColor(contact.color)
+        if let profileImage = contact.profileImage {
+            cell.avatar.setImage(profileImage)
+        }
         cell.setVerified(isVerified: contact.isVerified)
 
         if let emailHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({ $0.contactDetail == .EMAIL }).first {

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

@@ -94,8 +94,12 @@ class GroupNameController: UITableViewController, MediaPickerDelegate {
                 let displayName = contact.displayName
                 contactCell.nameLabel.text = displayName
                 contactCell.emailLabel.text = contact.email
-                contactCell.initialsLabel.text = Utils.getInitials(inputName: displayName)
-                contactCell.setColor(contact.color)
+                contactCell.avatar.setName(displayName)
+                contactCell.avatar.setColor(contact.color)
+                if let profileImage = contact.profileImage {
+                    contactCell.avatar.setImage(profileImage)
+                }
+                contactCell.setVerified(isVerified: contact.isVerified)
             }
             return cell
         }
@@ -108,10 +112,10 @@ class GroupNameController: UITableViewController, MediaPickerDelegate {
             if row == sectionGroupDetailsRowAvatar {
                 return AvatarSelectionCell.cellSize
             } else {
-                return Constants.stdCellHeight
+                return Constants.defaultCellHeight
             }
         } else {
-            return ContactCell.cellSize
+            return ContactCell.cellHeight
         }
     }
 

+ 22 - 2
deltachat-ios/Controller/NewChatViewController.swift

@@ -146,6 +146,21 @@ class NewChatViewController: UITableViewController {
         }
     }
 
+    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
+        let section = indexPath.section
+        if section == sectionNew {
+           return Constants.defaultCellHeight
+        } else if section == sectionImportedContacts {
+            if deviceContactAccessGranted {
+                return ContactCell.cellHeight
+            } else {
+                return Constants.defaultCellHeight
+            }
+        } else {
+            return ContactCell.cellHeight
+        }
+    }
+
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let section = indexPath.section
         let row = indexPath.row
@@ -333,8 +348,13 @@ class NewChatViewController: UITableViewController {
         let emailLabelFontSize = cell.emailLabel.font.pointSize
         let nameLabelFontSize = cell.nameLabel.font.pointSize
 
-        cell.initialsLabel.text = Utils.getInitials(inputName: displayName)
-        cell.setColor(contact.color)
+        cell.nameLabel.text = displayName
+        cell.emailLabel.text = contact.email
+        cell.avatar.setName(displayName)
+        cell.avatar.setColor(contact.color)
+        if let profileImage = contact.profileImage {
+            cell.avatar.setImage(profileImage)
+        }
         cell.setVerified(isVerified: contact.isVerified)
 
         if let emailHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({ $0.contactDetail == .EMAIL }).first {

+ 1 - 1
deltachat-ios/Helper/Constants.swift

@@ -16,5 +16,5 @@ struct Constants {
 
     static let notificationIdentifier = "deltachat-ios-local-notifications"
 
-    static let stdCellHeight: CGFloat = 48
+    static let defaultCellHeight: CGFloat = 48
 }

+ 1 - 3
deltachat-ios/View/AvatarSelectionCell.swift

@@ -16,8 +16,6 @@ class AvatarSelectionCell: UITableViewCell {
 
     lazy var badge: InitialsBadge = {
         let badge = InitialsBadge(size: badgeSize)
-        badge.layer.cornerRadius = badgeSize / 2
-        badge.clipsToBounds = true
         badge.setColor(UIColor.lightGray)
         return badge
     }()
@@ -40,7 +38,7 @@ class AvatarSelectionCell: UITableViewCell {
         super.init(style: .default, reuseIdentifier: nil)
         setAvatar(image: context?.getSelfAvatarImage(),
                   with: defaultImage ?? self.defaultImage,
-                  downscale: (defaultImage != nil) ? 1 : downscaleDefaultImage)
+                  downscale: (defaultImage != nil) ? nil : downscaleDefaultImage)
         setupSubviews()
     }
 

+ 19 - 69
deltachat-ios/View/ContactCell.swift

@@ -1,46 +1,21 @@
 import UIKit
 
-// TODO: integrate InitialsBadge in here
-
 protocol ContactCellDelegate: class {
     func onAvatarTapped(at index: Int)
 }
 
 class ContactCell: UITableViewCell {
 
-    public static let cellSize: CGFloat = 72
+    public static let cellHeight: CGFloat = 74.5
     weak var delegate: ContactCellDelegate?
     var rowIndex = -1
-    private let initialsLabelSize: CGFloat = 54
+    private let badgeSize: CGFloat = 54
     private let imgSize: CGFloat = 20
 
-    let avatar: UIView = {
-        let avatar = UIView()
-        return avatar
-    }()
-
-    lazy var imgView: UIImageView = {
-        let imgView = UIImageView()
-        let img = UIImage(named: "verified")
-        imgView.isHidden = true
-        imgView.image = img
-        imgView.bounds = CGRect(
-            x: 0,
-            y: 0,
-            width: imgSize, height: imgSize
-        )
-        return imgView
-    }()
-
-    lazy var initialsLabel: UILabel = {
-        let initialsLabel = UILabel()
-        initialsLabel.textAlignment = NSTextAlignment.center
-        initialsLabel.textColor = UIColor.white
-        initialsLabel.font = UIFont.systemFont(ofSize: 22)
-        let initialsLabelCornerRadius = (initialsLabelSize - 6) / 2
-        initialsLabel.layer.cornerRadius = initialsLabelCornerRadius
-        initialsLabel.clipsToBounds = true
-        return initialsLabel
+    lazy var avatar: InitialsBadge = {
+        let badge = InitialsBadge(size: badgeSize)
+        badge.setColor(UIColor.lightGray)
+        return badge
     }()
 
     let nameLabel: UILabel = {
@@ -92,28 +67,15 @@ class ContactCell: UITableViewCell {
     private func setupSubviews() {
         let margin: CGFloat = 10
 
-        initialsLabel.translatesAutoresizingMaskIntoConstraints = false
         avatar.translatesAutoresizingMaskIntoConstraints = false
-        initialsLabel.widthAnchor.constraint(equalToConstant: initialsLabelSize - 6).isActive = true
-        initialsLabel.heightAnchor.constraint(equalToConstant: initialsLabelSize - 6).isActive = true
-        // avatar.backgroundColor = .red
-
-        avatar.widthAnchor.constraint(equalToConstant: initialsLabelSize).isActive = true
-        avatar.heightAnchor.constraint(equalToConstant: initialsLabelSize).isActive = true
-
-        avatar.addSubview(initialsLabel)
         contentView.addSubview(avatar)
 
-        initialsLabel.topAnchor.constraint(equalTo: avatar.topAnchor, constant: 3).isActive = true
-        initialsLabel.leadingAnchor.constraint(equalTo: avatar.leadingAnchor, constant: 3).isActive = true
-        initialsLabel.trailingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: -3).isActive = true
-
-        avatar.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: margin).isActive = true
-        avatar.center.y = contentView.center.y
-        avatar.center.x += initialsLabelSize / 2
-        avatar.topAnchor.constraint(equalTo: contentView.topAnchor, constant: margin).isActive = true
-        avatar.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -margin).isActive = true
-        initialsLabel.center = avatar.center
+        contentView.addConstraints([
+            avatar.constraintWidthTo(badgeSize),
+            avatar.constraintHeightTo(badgeSize),
+            avatar.constraintAlignLeadingTo(contentView, paddingLeading: badgeSize / 4),
+            avatar.constraintCenterYTo(contentView),
+        ])
 
         deliveryStatusIndicator.translatesAutoresizingMaskIntoConstraints = false
         deliveryStatusIndicator.heightAnchor.constraint(equalToConstant: 20).isActive = true
@@ -144,11 +106,6 @@ class ContactCell: UITableViewCell {
         myStackView.addArrangedSubview(toplineStackView)
         myStackView.addArrangedSubview(bottomLineStackView)
 
-        avatar.addSubview(imgView)
-
-        imgView.center.x = avatar.center.x + (avatar.frame.width / 2) + imgSize
-        imgView.center.y = avatar.center.y + (avatar.frame.height / 2) + imgSize
-
         if delegate != nil {
             let tap = UITapGestureRecognizer(target: self, action: #selector(onAvatarTapped))
             avatar.addGestureRecognizer(tap)
@@ -156,29 +113,22 @@ class ContactCell: UITableViewCell {
     }
 
     func setVerified(isVerified: Bool) {
-        imgView.isHidden = !isVerified
+        avatar.setVerified(isVerified)
     }
 
     func setImage(_ img: UIImage) {
-        if let resizedImg = img.scaleDownImage(toMax: initialsLabelSize - 6) {
-            let attachment = NSTextAttachment()
-            attachment.image = resizedImg
-            initialsLabel.attributedText = NSAttributedString(attachment: attachment)
-        }
+        avatar.setImage(img)
     }
 
     func resetBackupImage() {
-        initialsLabel.text = ""
-        setColor(UIColor.clear)
+        avatar.setColor(UIColor.clear)
+        avatar.setName("")
     }
 
     func setBackupImage(name: String, color: UIColor) {
         let text = Utils.getInitials(inputName: name)
-
-        initialsLabel.textAlignment = .center
-        initialsLabel.text = text
-
-        setColor(color)
+        avatar.setColor(color)
+        avatar.setName(text)
     }
 
     func setUnreadMessageCounter(_ count: Int) {
@@ -223,7 +173,7 @@ class ContactCell: UITableViewCell {
     }
 
     func setColor(_ color: UIColor) {
-        initialsLabel.backgroundColor = color
+        avatar.setColor(color)
     }
 
     @objc func onAvatarTapped() {

+ 2 - 2
deltachat-ios/View/InitialsBadge.swift

@@ -79,8 +79,8 @@ class InitialsBadge: UIView {
 
     func setName(_ name: String) {
         label.text = Utils.getInitials(inputName: name)
-        label.isHidden = false
-        imageView.isHidden = true
+        label.isHidden = name.isEmpty
+        imageView.isHidden = !name.isEmpty
     }
 
     func setImage(_ image: UIImage, downscale: CGFloat? = nil) {