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

222 lines
12 KiB
Swift
Raw Normal View History

2019-11-28 06:42:07 +01:00
final class ConversationCell : UITableViewCell {
2019-11-29 06:30:01 +01:00
var threadViewModel: ThreadViewModel! { didSet { update() } }
2019-11-28 06:42:07 +01:00
2019-11-29 06:30:01 +01:00
static let reuseIdentifier = "ConversationCell"
2019-11-28 06:42:07 +01:00
// MARK: Components
2019-11-29 06:30:01 +01:00
private lazy var unreadMessagesIndicatorView: UIView = {
2019-11-28 06:42:07 +01:00
let result = UIView()
result.backgroundColor = Colors.accent
return result
}()
private lazy var profilePictureView = ProfilePictureView()
private lazy var displayNameLabel: UILabel = {
let result = UILabel()
result.font = .boldSystemFont(ofSize: Values.mediumFontSize)
result.textColor = Colors.text
result.lineBreakMode = .byTruncatingTail
return result
}()
2019-11-29 06:30:01 +01:00
private lazy var timestampLabel: UILabel = {
let result = UILabel()
result.font = .systemFont(ofSize: Values.smallFontSize)
result.textColor = Colors.text
result.lineBreakMode = .byTruncatingTail
result.alpha = Values.conversationCellTimestampOpacity
return result
}()
2019-11-28 06:42:07 +01:00
private lazy var snippetLabel: UILabel = {
let result = UILabel()
result.font = .systemFont(ofSize: Values.smallFontSize)
result.textColor = Colors.text
result.lineBreakMode = .byTruncatingTail
return result
}()
private lazy var typingIndicatorView = TypingIndicatorView()
2019-11-29 06:30:01 +01:00
private lazy var statusIndicatorView: UIImageView = {
let result = UIImageView()
result.contentMode = .scaleAspectFit
result.layer.cornerRadius = Values.conversationCellStatusIndicatorSize / 2
result.layer.masksToBounds = true
2019-11-29 06:30:01 +01:00
return result
}()
2019-11-28 06:42:07 +01:00
// MARK: Initialization
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpViewHierarchy()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setUpViewHierarchy()
}
private func setUpViewHierarchy() {
2020-01-30 10:09:02 +01:00
let cellHeight: CGFloat = 72
2019-11-29 06:30:01 +01:00
// Set the cell background color
backgroundColor = Colors.cellBackground
2019-11-29 06:30:01 +01:00
// Set up the highlight color
let selectedBackgroundView = UIView()
selectedBackgroundView.backgroundColor = Colors.cellSelected
2019-11-29 06:30:01 +01:00
self.selectedBackgroundView = selectedBackgroundView
// Set up the unread messages indicator view
unreadMessagesIndicatorView.set(.width, to: Values.accentLineThickness)
2020-01-30 10:09:02 +01:00
unreadMessagesIndicatorView.set(.height, to: cellHeight)
2019-11-28 06:42:07 +01:00
// Set up the profile picture view
let profilePictureViewSize = Values.mediumProfilePictureSize
profilePictureView.set(.width, to: profilePictureViewSize)
profilePictureView.set(.height, to: profilePictureViewSize)
profilePictureView.size = profilePictureViewSize
// Set up the label stack view
2020-01-23 02:27:58 +01:00
let topLabelSpacer = UIView.hStretchingSpacer()
let topLabelStackView = UIStackView(arrangedSubviews: [ displayNameLabel, topLabelSpacer, timestampLabel ])
2019-11-29 06:30:01 +01:00
topLabelStackView.axis = .horizontal
2020-01-23 02:27:58 +01:00
topLabelStackView.alignment = .center
2019-11-29 06:30:01 +01:00
topLabelStackView.spacing = Values.smallSpacing / 2 // Effectively Values.smallSpacing because there'll be spacing before and after the invisible spacer
2019-11-28 06:42:07 +01:00
let snippetLabelContainer = UIView()
snippetLabelContainer.addSubview(snippetLabel)
snippetLabelContainer.addSubview(typingIndicatorView)
2020-01-23 02:27:58 +01:00
let bottomLabelSpacer = UIView.hStretchingSpacer()
let bottomLabelStackView = UIStackView(arrangedSubviews: [ snippetLabelContainer, bottomLabelSpacer, statusIndicatorView ])
2019-11-29 06:30:01 +01:00
bottomLabelStackView.axis = .horizontal
2020-01-23 02:27:58 +01:00
bottomLabelStackView.alignment = .center
2019-11-29 06:30:01 +01:00
bottomLabelStackView.spacing = Values.smallSpacing / 2 // Effectively Values.smallSpacing because there'll be spacing before and after the invisible spacer
2020-01-23 02:27:58 +01:00
let labelContainerView = UIView()
labelContainerView.addSubview(topLabelStackView)
labelContainerView.addSubview(bottomLabelStackView)
2019-11-28 06:42:07 +01:00
// Set up the main stack view
2020-01-23 02:27:58 +01:00
let stackView = UIStackView(arrangedSubviews: [ unreadMessagesIndicatorView, profilePictureView, labelContainerView ])
2019-11-28 06:42:07 +01:00
stackView.axis = .horizontal
stackView.alignment = .center
stackView.spacing = Values.mediumSpacing
contentView.addSubview(stackView)
// Set up the constraints
2020-01-23 02:27:58 +01:00
unreadMessagesIndicatorView.pin(.top, to: .top, of: contentView)
unreadMessagesIndicatorView.pin(.bottom, to: .bottom, of: contentView)
// The three lines below are part of a workaround for a weird layout bug
topLabelStackView.set(.width, to: UIScreen.main.bounds.width - Values.accentLineThickness - Values.mediumSpacing - profilePictureViewSize - Values.mediumSpacing - Values.mediumSpacing)
topLabelStackView.set(.height, to: 18)
topLabelSpacer.set(.height, to: 18)
2019-11-29 06:30:01 +01:00
timestampLabel.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
2020-01-23 02:27:58 +01:00
// The three lines below are part of a workaround for a weird layout bug
bottomLabelStackView.set(.width, to: UIScreen.main.bounds.width - Values.accentLineThickness - Values.mediumSpacing - profilePictureViewSize - Values.mediumSpacing - Values.mediumSpacing)
bottomLabelStackView.set(.height, to: 16)
bottomLabelSpacer.set(.height, to: 16)
2019-11-29 06:30:01 +01:00
statusIndicatorView.set(.width, to: Values.conversationCellStatusIndicatorSize)
statusIndicatorView.set(.height, to: Values.conversationCellStatusIndicatorSize)
2019-11-28 06:42:07 +01:00
snippetLabel.pin(to: snippetLabelContainer)
typingIndicatorView.pin(.leading, to: .leading, of: snippetLabelContainer)
typingIndicatorView.centerYAnchor.constraint(equalTo: snippetLabel.centerYAnchor).isActive = true
2020-01-23 02:27:58 +01:00
// Not using a stack view for this is part of a workaround for a weird layout bug
topLabelStackView.pin(.leading, to: .leading, of: labelContainerView)
topLabelStackView.pin(.top, to: .top, of: labelContainerView, withInset: Values.mediumSpacing)
topLabelStackView.pin(.trailing, to: .trailing, of: labelContainerView)
bottomLabelStackView.pin(.leading, to: .leading, of: labelContainerView)
bottomLabelStackView.pin(.top, to: .bottom, of: topLabelStackView, withInset: Values.smallSpacing)
labelContainerView.pin(.bottom, to: .bottom, of: bottomLabelStackView, withInset: Values.mediumSpacing)
// The two lines below are part of a workaround for a weird layout bug
labelContainerView.set(.width, to: UIScreen.main.bounds.width - Values.accentLineThickness - Values.mediumSpacing - profilePictureViewSize - Values.mediumSpacing - Values.mediumSpacing)
2020-01-30 10:09:02 +01:00
labelContainerView.set(.height, to: cellHeight)
2019-11-28 06:42:07 +01:00
stackView.pin(.leading, to: .leading, of: contentView)
stackView.pin(.top, to: .top, of: contentView)
2020-01-23 02:27:58 +01:00
// The two lines below are part of a workaround for a weird layout bug
stackView.set(.width, to: UIScreen.main.bounds.width - Values.mediumSpacing)
2020-01-30 10:09:02 +01:00
stackView.set(.height, to: cellHeight)
2019-11-28 06:42:07 +01:00
}
// MARK: Updating
private func update() {
LokiAPI.populateUserHexEncodedPublicKeyCacheIfNeeded(for: threadViewModel.threadRecord.uniqueId!) // FIXME: This is a terrible place to do this
2020-01-23 02:27:58 +01:00
unreadMessagesIndicatorView.alpha = threadViewModel.hasUnreadMessages ? 1 : 0.0001 // Setting the alpha to exactly 0 causes an issue on iOS 12
2019-11-28 06:42:07 +01:00
if threadViewModel.isGroupThread {
if threadViewModel.name == "Session Public Chat" {
profilePictureView.hexEncodedPublicKey = ""
profilePictureView.isRSSFeed = true
} else {
let users = LokiAPI.userHexEncodedPublicKeyCache[threadViewModel.threadRecord.uniqueId!] ?? []
let randomUsers = users.sorted().prefix(2) // Sort to provide a level of stability
if !randomUsers.isEmpty {
profilePictureView.hexEncodedPublicKey = randomUsers[0]
profilePictureView.additionalHexEncodedPublicKey = randomUsers.count >= 2 ? randomUsers[1] : ""
} else {
profilePictureView.hexEncodedPublicKey = ""
profilePictureView.additionalHexEncodedPublicKey = ""
}
profilePictureView.isRSSFeed = (threadViewModel.threadRecord as? TSGroupThread)?.isRSSFeed ?? false
2019-11-28 06:42:07 +01:00
}
} else {
profilePictureView.hexEncodedPublicKey = threadViewModel.contactIdentifier!
profilePictureView.additionalHexEncodedPublicKey = nil
profilePictureView.isRSSFeed = false
2019-11-28 06:42:07 +01:00
}
profilePictureView.update()
displayNameLabel.text = getDisplayName()
2019-11-29 06:30:01 +01:00
timestampLabel.text = DateUtil.formatDateShort(threadViewModel.lastMessageDate)
2019-11-28 06:42:07 +01:00
if SSKEnvironment.shared.typingIndicators.typingRecipientId(forThread: self.threadViewModel.threadRecord) != nil {
snippetLabel.text = ""
typingIndicatorView.isHidden = false
typingIndicatorView.startAnimation()
} else {
snippetLabel.attributedText = getSnippet()
typingIndicatorView.isHidden = true
typingIndicatorView.stopAnimation()
}
statusIndicatorView.backgroundColor = nil
2019-11-29 06:30:01 +01:00
let lastMessage = threadViewModel.lastMessageForInbox
if let lastMessage = lastMessage as? TSOutgoingMessage {
let image: UIImage
let status = MessageRecipientStatusUtils.recipientStatus(outgoingMessage: lastMessage)
switch status {
2019-12-01 23:19:39 +01:00
case .calculatingPoW, .uploading, .sending: image = #imageLiteral(resourceName: "CircleDotDotDot")
case .sent, .skipped, .delivered: image = #imageLiteral(resourceName: "CircleCheck")
case .read:
statusIndicatorView.backgroundColor = .white
image = #imageLiteral(resourceName: "FilledCircleCheck")
2020-02-02 09:08:46 +01:00
case .failed: image = #imageLiteral(resourceName: "message_status_failed").asTintedImage(color: Colors.text)!
2019-11-29 06:30:01 +01:00
}
statusIndicatorView.image = image
statusIndicatorView.isHidden = false
} else {
statusIndicatorView.isHidden = true
}
2019-11-28 06:42:07 +01:00
}
private func getDisplayName() -> String {
if threadViewModel.isGroupThread {
2020-01-30 10:09:02 +01:00
if threadViewModel.name.isEmpty {
2020-01-30 23:42:36 +01:00
return GroupDisplayNameUtilities.getDefaultDisplayName(for: threadViewModel.threadRecord as! TSGroupThread)
2019-11-28 06:42:07 +01:00
} else {
return threadViewModel.name
}
} else {
if threadViewModel.threadRecord.isNoteToSelf() {
return NSLocalizedString("Note to Self", comment: "")
} else {
let hexEncodedPublicKey = threadViewModel.contactIdentifier!
2020-01-30 23:42:36 +01:00
return UserDisplayNameUtilities.getPrivateChatDisplayName(for: hexEncodedPublicKey) ?? hexEncodedPublicKey
2019-11-28 06:42:07 +01:00
}
}
}
private func getSnippet() -> NSMutableAttributedString {
let result = NSMutableAttributedString()
if threadViewModel.isMuted {
2019-11-29 06:30:01 +01:00
result.append(NSAttributedString(string: "\u{e067} ", attributes: [ .font : UIFont.ows_elegantIconsFont(10), .foregroundColor : Colors.unimportant ]))
2019-11-28 06:42:07 +01:00
}
if let rawSnippet = threadViewModel.lastMessageText {
let snippet = MentionUtilities.highlightMentions(in: rawSnippet, threadID: threadViewModel.threadRecord.uniqueId!)
2019-11-29 06:30:01 +01:00
let font = threadViewModel.hasUnreadMessages ? UIFont.boldSystemFont(ofSize: Values.smallFontSize) : UIFont.systemFont(ofSize: Values.smallFontSize)
result.append(NSAttributedString(string: snippet, attributes: [ .font : font, .foregroundColor : Colors.text ]))
2019-11-28 06:42:07 +01:00
}
return result
}
}