2019-11-28 06:42:07 +01:00
|
|
|
|
2019-11-29 06:30:01 +01:00
|
|
|
final class ProfilePictureView : UIView {
|
2019-11-28 06:42:07 +01:00
|
|
|
private var imageViewWidthConstraint: NSLayoutConstraint!
|
|
|
|
private var imageViewHeightConstraint: NSLayoutConstraint!
|
2019-11-29 06:30:01 +01:00
|
|
|
var size: CGFloat!
|
|
|
|
var hexEncodedPublicKey: String!
|
|
|
|
var additionalHexEncodedPublicKey: String?
|
2019-11-28 06:42:07 +01:00
|
|
|
|
|
|
|
// MARK: Components
|
|
|
|
private lazy var imageView = getImageView()
|
|
|
|
private lazy var additionalImageView = getImageView()
|
|
|
|
|
|
|
|
// MARK: Lifecycle
|
2019-11-29 06:30:01 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-29 06:30:01 +01:00
|
|
|
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)
|
|
|
|
// Set up additional image view
|
|
|
|
addSubview(additionalImageView)
|
|
|
|
additionalImageView.pin(.trailing, to: .trailing, of: self)
|
|
|
|
additionalImageView.pin(.bottom, to: .bottom, of: self)
|
|
|
|
let additionalImageViewSize = Values.smallProfilePictureSize
|
|
|
|
additionalImageView.set(.width, to: additionalImageViewSize)
|
|
|
|
additionalImageView.set(.height, to: additionalImageViewSize)
|
|
|
|
additionalImageView.layer.cornerRadius = additionalImageViewSize / 2
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Updating
|
2019-11-29 06:30:01 +01:00
|
|
|
func update() {
|
2019-11-28 06:42:07 +01:00
|
|
|
if let imageViewWidthConstraint = imageViewWidthConstraint, let imageViewHeightConstraint = imageViewHeightConstraint {
|
|
|
|
imageView.removeConstraint(imageViewWidthConstraint)
|
|
|
|
imageView.removeConstraint(imageViewHeightConstraint)
|
|
|
|
}
|
2019-11-29 06:30:01 +01:00
|
|
|
func getProfilePicture(of size: CGFloat, for hexEncodedPublicKey: String) -> UIImage {
|
2019-12-03 04:17:08 +01:00
|
|
|
return OWSProfileManager.shared().profileAvatar(forRecipientId: hexEncodedPublicKey) ?? Identicon.generateIcon(string: hexEncodedPublicKey, size: size)
|
2019-11-29 06:30:01 +01:00
|
|
|
}
|
2019-11-28 06:42:07 +01:00
|
|
|
let size: CGFloat
|
|
|
|
if let additionalHexEncodedPublicKey = additionalHexEncodedPublicKey {
|
|
|
|
size = Values.smallProfilePictureSize
|
|
|
|
imageViewWidthConstraint = imageView.set(.width, to: size)
|
|
|
|
imageViewHeightConstraint = imageView.set(.height, to: size)
|
|
|
|
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
|
|
|
|
imageViewWidthConstraint = imageView.pin(.trailing, to: .trailing, of: self)
|
|
|
|
imageViewHeightConstraint = imageView.pin(.bottom, to: .bottom, of: self)
|
|
|
|
additionalImageView.isHidden = true
|
|
|
|
additionalImageView.image = nil
|
|
|
|
}
|
2019-11-29 06:30:01 +01:00
|
|
|
imageView.image = getProfilePicture(of: size, for: hexEncodedPublicKey)
|
2019-11-28 06:42:07 +01:00
|
|
|
imageView.layer.cornerRadius = size / 2
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Convenience
|
|
|
|
private func getImageView() -> UIImageView {
|
|
|
|
let result = UIImageView()
|
|
|
|
result.layer.masksToBounds = true
|
|
|
|
result.backgroundColor = Colors.unimportant
|
2019-12-02 01:58:15 +01:00
|
|
|
result.layer.borderColor = Colors.border.cgColor
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|