session-ios/Signal/src/ViewControllers/ConversationView/ConversationHeaderView.swift

133 lines
3.8 KiB
Swift
Raw Normal View History

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
@objc
public protocol ConversationHeaderViewDelegate {
func didTapConversationHeaderView(_ conversationHeaderView: ConversationHeaderView)
}
@objc
public class ConversationHeaderView: UIStackView {
2018-05-25 23:28:36 +02:00
@objc
public weak var delegate: ConversationHeaderViewDelegate?
2018-05-25 23:28:36 +02:00
@objc
public var attributedTitle: NSAttributedString? {
get {
return self.titleLabel.attributedText
}
set {
self.titleLabel.attributedText = newValue
}
}
2018-05-25 23:28:36 +02:00
@objc
public var attributedSubtitle: NSAttributedString? {
get {
return self.subtitleLabel.attributedText
}
set {
self.subtitleLabel.attributedText = newValue
}
}
public var avatarImage: UIImage? {
get {
return self.avatarView.image
}
set {
self.avatarView.image = newValue
}
}
2018-05-25 23:28:36 +02:00
@objc
public let titlePrimaryFont: UIFont = UIFont.ows_boldFont(withSize: 17)
2018-05-25 23:28:36 +02:00
@objc
public let titleSecondaryFont: UIFont = UIFont.ows_regularFont(withSize: 9)
2018-05-25 23:28:36 +02:00
@objc
public let subtitleFont: UIFont = UIFont.ows_regularFont(withSize: 12)
2018-05-25 23:28:36 +02:00
private let titleLabel: UILabel
private let subtitleLabel: UILabel
2018-06-28 19:28:14 +02:00
private let avatarView: ConversationAvatarImageView
2018-05-25 23:28:36 +02:00
@objc
public required init(thread: TSThread, contactsManager: OWSContactsManager) {
let avatarView = ConversationAvatarImageView(thread: thread, diameter: 36, contactsManager: contactsManager)
self.avatarView = avatarView
// remove default border on avatarView
avatarView.layer.borderWidth = 0
titleLabel = UILabel()
2018-07-13 15:50:49 +02:00
titleLabel.textColor = Theme.navbarTitleColor
titleLabel.lineBreakMode = .byTruncatingTail
titleLabel.font = titlePrimaryFont
2018-04-26 16:12:21 +02:00
titleLabel.setContentHuggingHigh()
subtitleLabel = UILabel()
2018-07-13 15:50:49 +02:00
subtitleLabel.textColor = Theme.navbarTitleColor
subtitleLabel.lineBreakMode = .byTruncatingTail
subtitleLabel.font = subtitleFont
2018-04-26 16:12:21 +02:00
subtitleLabel.setContentHuggingHigh()
let textRows = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
textRows.axis = .vertical
textRows.alignment = .leading
textRows.distribution = .fillProportionally
2018-04-26 16:12:21 +02:00
textRows.spacing = 0
textRows.layoutMargins = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
textRows.isLayoutMarginsRelativeArrangement = true
// low content hugging so that the text rows push container to the right bar button item(s)
textRows.setContentHuggingLow()
super.init(frame: .zero)
self.layoutMargins = UIEdgeInsets(top: 4, left: 2, bottom: 4, right: 2)
self.isLayoutMarginsRelativeArrangement = true
self.axis = .horizontal
self.alignment = .center
self.spacing = 0
self.addArrangedSubview(avatarView)
self.addArrangedSubview(textRows)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapView))
self.addGestureRecognizer(tapGesture)
}
required public init(coder: NSCoder) {
2018-08-27 16:21:03 +02:00
notImplemented()
}
required public override init(frame: CGRect) {
2018-08-27 16:21:03 +02:00
notImplemented()
}
public override var intrinsicContentSize: CGSize {
2018-04-26 16:12:21 +02:00
// Grow to fill as much of the navbar as possible.
return UILayoutFittingExpandedSize
}
2018-06-28 19:28:14 +02:00
@objc
public func updateAvatar() {
self.avatarView.updateImage()
}
// MARK: Delegate Methods
2018-05-25 18:54:25 +02:00
@objc func didTapView(tapGesture: UITapGestureRecognizer) {
guard tapGesture.state == .recognized else {
return
}
self.delegate?.didTapConversationHeaderView(self)
}
}