session-ios/SignalMessaging/Loki/Redesign/Components/ProfilePictureView.swift

87 lines
4.0 KiB
Swift
Raw Normal View History

2019-11-28 06:42:07 +01:00
2019-12-10 04:27:38 +01:00
@objc(LKProfilePictureView)
2020-01-20 03:20:27 +01:00
public final class ProfilePictureView : UIView {
2019-11-28 06:42:07 +01:00
private var imageViewWidthConstraint: NSLayoutConstraint!
private var imageViewHeightConstraint: NSLayoutConstraint!
2020-01-20 03:20:27 +01:00
@objc public var size: CGFloat = 0 // Not an implicitly unwrapped optional due to Obj-C limitations
@objc public var isRSSFeed = false
@objc public var hexEncodedPublicKey: String!
@objc public var additionalHexEncodedPublicKey: String?
2020-06-15 05:36:45 +02:00
@objc public var openGroupProfilePicture: UIImage?
2019-11-28 06:42:07 +01:00
// MARK: Components
private lazy var imageView = getImageView()
private lazy var additionalImageView = getImageView()
// MARK: Lifecycle
2020-01-20 03:20:27 +01:00
public override init(frame: CGRect) {
2019-11-28 06:42:07 +01:00
super.init(frame: frame)
2019-12-02 01:58:15 +01:00
setUpViewHierarchy()
2019-11-28 06:42:07 +01:00
}
2020-01-20 03:20:27 +01:00
public required init?(coder: NSCoder) {
2019-11-28 06:42:07 +01:00
super.init(coder: coder)
2019-12-02 01:58:15 +01:00
setUpViewHierarchy()
2019-11-28 06:42:07 +01:00
}
2019-12-02 01:58:15 +01:00
private func setUpViewHierarchy() {
2019-11-28 06:42:07 +01:00
// Set up image view
addSubview(imageView)
imageView.pin(.leading, to: .leading, of: self)
imageView.pin(.top, to: .top, of: self)
2020-03-17 06:18:53 +01:00
let imageViewSize = CGFloat(Values.mediumProfilePictureSize)
2020-01-22 03:34:49 +01:00
imageViewWidthConstraint = imageView.set(.width, to: imageViewSize)
imageViewHeightConstraint = imageView.set(.height, to: imageViewSize)
2019-11-28 06:42:07 +01:00
// Set up additional image view
addSubview(additionalImageView)
additionalImageView.pin(.trailing, to: .trailing, of: self)
additionalImageView.pin(.bottom, to: .bottom, of: self)
2020-03-17 06:18:53 +01:00
let additionalImageViewSize = CGFloat(Values.smallProfilePictureSize)
2019-11-28 06:42:07 +01:00
additionalImageView.set(.width, to: additionalImageViewSize)
additionalImageView.set(.height, to: additionalImageViewSize)
additionalImageView.layer.cornerRadius = additionalImageViewSize / 2
}
// MARK: Updating
2020-01-20 03:20:27 +01:00
@objc public func update() {
2020-06-15 05:50:56 +02:00
AssertIsOnMainThread()
func getProfilePicture(of size: CGFloat, for hexEncodedPublicKey: String) -> UIImage? {
guard !hexEncodedPublicKey.isEmpty else { return nil }
2020-09-08 05:36:52 +02:00
return OWSProfileManager.shared().profileAvatar(forRecipientId: hexEncodedPublicKey) ?? Identicon.generatePlaceholderIcon(seed: hexEncodedPublicKey, text: OWSProfileManager.shared().profileNameForRecipient(withID: hexEncodedPublicKey) ?? hexEncodedPublicKey, size: size)
2019-11-29 06:30:01 +01:00
}
2019-11-28 06:42:07 +01:00
let size: CGFloat
2020-08-05 08:55:23 +02:00
if let additionalHexEncodedPublicKey = additionalHexEncodedPublicKey, !isRSSFeed, openGroupProfilePicture == nil {
2020-03-17 06:18:53 +01:00
size = Values.smallProfilePictureSize
2020-01-22 03:34:49 +01:00
imageViewWidthConstraint.constant = size
imageViewHeightConstraint.constant = size
2019-11-28 06:42:07 +01:00
additionalImageView.isHidden = false
2019-11-29 06:30:01 +01:00
additionalImageView.image = getProfilePicture(of: size, for: additionalHexEncodedPublicKey)
2019-11-28 06:42:07 +01:00
} else {
size = self.size
2020-01-22 03:34:49 +01:00
imageViewWidthConstraint.constant = size
imageViewHeightConstraint.constant = size
2019-11-28 06:42:07 +01:00
additionalImageView.isHidden = true
additionalImageView.image = nil
}
2020-06-15 05:36:45 +02:00
guard hexEncodedPublicKey != nil || openGroupProfilePicture != nil else { return }
imageView.image = isRSSFeed ? nil : (openGroupProfilePicture ?? getProfilePicture(of: size, for: hexEncodedPublicKey))
2020-06-19 01:20:03 +02:00
imageView.backgroundColor = isRSSFeed ? UIColor(rgbHex: 0x353535) : Colors.unimportant
2019-11-28 06:42:07 +01:00
imageView.layer.cornerRadius = size / 2
imageView.contentMode = isRSSFeed ? .center : .scaleAspectFit
if isRSSFeed {
2020-01-23 05:01:31 +01:00
imageView.image = (size == 45) ? #imageLiteral(resourceName: "SessionWhite24") : #imageLiteral(resourceName: "SessionWhite40")
}
2019-11-28 06:42:07 +01:00
}
// MARK: Convenience
private func getImageView() -> UIImageView {
let result = UIImageView()
result.layer.masksToBounds = true
2020-03-17 06:18:53 +01:00
result.backgroundColor = Colors.unimportant
2020-09-09 03:42:13 +02:00
result.layer.borderColor = Colors.text.withAlphaComponent(0.35).cgColor
2020-03-17 06:18:53 +01:00
result.layer.borderWidth = Values.borderThickness
2019-11-29 06:30:01 +01:00
result.contentMode = .scaleAspectFit
2019-11-28 06:42:07 +01:00
return result
}
}