session-ios/Signal/src/Loki/Components/ProfilePictureView.swift

98 lines
4.2 KiB
Swift
Raw Normal View History

2019-11-28 06:42:07 +01:00
2019-12-10 04:27:38 +01:00
@objc(LKProfilePictureView)
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-12-10 04:27:38 +01:00
@objc var size: CGFloat = 0 // Not an implicitly unwrapped optional due to Obj-C limitations
@objc var isRSSFeed = false
2019-12-10 04:27:38 +01:00
@objc var hexEncodedPublicKey: String!
@objc var additionalHexEncodedPublicKey: String?
2019-11-28 06:42:07 +01:00
// MARK: Components
private lazy var imageView = getImageView()
private lazy var additionalImageView = getImageView()
private lazy var rssLabel: UILabel = {
let result = UILabel()
result.textColor = Colors.text
result.font = .systemFont(ofSize: Values.smallFontSize)
result.textAlignment = .center
result.text = "RSS"
return result
}()
2019-11-28 06:42:07 +01:00
// 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
// Set up RSS label
addSubview(rssLabel)
rssLabel.pin(.leading, to: .leading, of: self)
rssLabel.pin(.top, to: .top, of: self)
rssLabel.autoPinWidth(toWidthOf: imageView)
rssLabel.autoPinHeight(toHeightOf: imageView)
2019-11-28 06:42:07 +01:00
}
// MARK: Updating
2019-12-10 04:27:38 +01:00
@objc func update() {
2019-11-28 06:42:07 +01:00
if let imageViewWidthConstraint = imageViewWidthConstraint, let imageViewHeightConstraint = imageViewHeightConstraint {
imageView.removeConstraint(imageViewWidthConstraint)
imageView.removeConstraint(imageViewHeightConstraint)
}
func getProfilePicture(of size: CGFloat, for hexEncodedPublicKey: String) -> UIImage? {
guard !hexEncodedPublicKey.isEmpty else { return nil }
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, !isRSSFeed {
2019-11-28 06:42:07 +01:00
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
}
imageView.image = isRSSFeed ? nil : getProfilePicture(of: size, for: hexEncodedPublicKey)
imageView.backgroundColor = isRSSFeed ? UIColor(hex: 0x353535) : Colors.unimportant
2019-11-28 06:42:07 +01:00
imageView.layer.cornerRadius = size / 2
rssLabel.isHidden = !isRSSFeed
rssLabel.font = size == (Values.largeProfilePictureSize) ? .systemFont(ofSize: Values.largeFontSize) : .systemFont(ofSize: Values.smallFontSize)
2019-11-28 06:42:07 +01:00
}
// 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
}
}