session-ios/SignalMessaging/Loki/ProfilePictureView.swift

98 lines
4.5 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?
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()
2020-01-20 03:20:27 +01:00
result.textColor = UIColor(rgbHex: 0xFFFFFF) // Colors.text
result.font = .systemFont(ofSize: 13) // Values.smallFontSize
result.textAlignment = .center
result.text = "RSS"
return result
}()
2019-11-28 06:42:07 +01:00
// 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)
// Set up additional image view
addSubview(additionalImageView)
additionalImageView.pin(.trailing, to: .trailing, of: self)
additionalImageView.pin(.bottom, to: .bottom, of: self)
2020-01-20 03:20:27 +01:00
let additionalImageViewSize = CGFloat(35) // 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
// 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
2020-01-20 03:20:27 +01:00
@objc public 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 {
2020-01-20 03:20:27 +01:00
size = 35 // Values.smallProfilePictureSize
2019-11-28 06:42:07 +01:00
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)
2020-01-20 03:20:27 +01:00
imageView.backgroundColor = isRSSFeed ? UIColor(rgbHex: 0x353535) : UIColor(rgbHex: 0xD8D8D8) // UIColor(rgbHex: 0xD8D8D8) = Colors.unimportant
2019-11-28 06:42:07 +01:00
imageView.layer.cornerRadius = size / 2
rssLabel.isHidden = !isRSSFeed
2020-01-20 03:20:27 +01:00
rssLabel.font = size == (75) ? .systemFont(ofSize: 20) : .systemFont(ofSize: 13) // Values.largeProfilePictureSize / Values.largeFontSize / Values.smallFontSize
2019-11-28 06:42:07 +01:00
}
// MARK: Convenience
private func getImageView() -> UIImageView {
let result = UIImageView()
result.layer.masksToBounds = true
2020-01-20 03:20:27 +01:00
result.backgroundColor = UIColor(rgbHex: 0xD8D8D8) // Colors.unimportant
result.layer.borderColor = UIColor(rgbHex: 0x979797).cgColor // Colors.border
result.layer.borderWidth = 1 // Values.borderThickness
2019-11-29 06:30:01 +01:00
result.contentMode = .scaleAspectFit
2019-11-28 06:42:07 +01:00
return result
}
}