session-ios/Session/Shared/ConversationCell.swift

242 lines
12 KiB
Swift
Raw Normal View History

2020-11-09 06:03:59 +01:00
import UIKit
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
2021-03-01 04:04:54 +01:00
// MARK: UI Components
private let accentLineView = UIView()
2019-11-28 06:42:07 +01:00
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
}()
2021-03-01 04:04:54 +01:00
private lazy var unreadCountView: UIView = {
let result = UIView()
result.backgroundColor = Colors.text.withAlphaComponent(Values.veryLowOpacity)
let size = ConversationCell.unreadCountViewSize
result.set(.width, to: size)
result.set(.height, to: size)
result.layer.masksToBounds = true
result.layer.cornerRadius = size / 2
return result
}()
private lazy var unreadCountLabel: UILabel = {
let result = UILabel()
result.font = .boldSystemFont(ofSize: Values.verySmallFontSize)
result.textColor = Colors.text
result.textAlignment = .center
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
2021-01-29 01:46:32 +01:00
result.alpha = Values.lowOpacity
2019-11-29 06:30:01 +01:00
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
2021-03-01 04:04:54 +01:00
result.layer.cornerRadius = ConversationCell.statusIndicatorSize / 2
result.layer.masksToBounds = true
2019-11-29 06:30:01 +01:00
return result
}()
2021-01-29 01:46:32 +01:00
// MARK: Settings
2021-03-01 04:04:54 +01:00
private static let unreadCountViewSize: CGFloat = 20
private static let statusIndicatorSize: CGFloat = 14
2021-01-29 01:46:32 +01:00
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-03-20 00:06:33 +01:00
let cellHeight: CGFloat = 68
2021-03-01 04:04:54 +01:00
// Background color
backgroundColor = Colors.cellBackground
2021-03-01 04:04:54 +01:00
// Highlight color
2019-11-29 06:30:01 +01:00
let selectedBackgroundView = UIView()
selectedBackgroundView.backgroundColor = Colors.cellSelected
2019-11-29 06:30:01 +01:00
self.selectedBackgroundView = selectedBackgroundView
2021-03-01 04:04:54 +01:00
// Accent line view
accentLineView.set(.width, to: Values.accentLineThickness)
accentLineView.set(.height, to: cellHeight)
// Profile picture view
2019-11-28 06:42:07 +01:00
let profilePictureViewSize = Values.mediumProfilePictureSize
profilePictureView.set(.width, to: profilePictureViewSize)
profilePictureView.set(.height, to: profilePictureViewSize)
profilePictureView.size = profilePictureViewSize
2021-03-01 04:04:54 +01:00
// Unread count view
unreadCountView.addSubview(unreadCountLabel)
unreadCountLabel.pin(to: unreadCountView)
// Label stack view
2020-01-23 02:27:58 +01:00
let topLabelSpacer = UIView.hStretchingSpacer()
2021-03-01 04:04:54 +01:00
let topLabelStackView = UIStackView(arrangedSubviews: [ displayNameLabel, unreadCountView, 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)
2021-03-01 04:04:54 +01:00
// Main stack view
let stackView = UIStackView(arrangedSubviews: [ accentLineView, profilePictureView, labelContainerView ])
2019-11-28 06:42:07 +01:00
stackView.axis = .horizontal
stackView.alignment = .center
stackView.spacing = Values.mediumSpacing
contentView.addSubview(stackView)
2021-03-01 04:04:54 +01:00
// Constraints
accentLineView.pin(.top, to: .top, of: contentView)
accentLineView.pin(.bottom, to: .bottom, of: contentView)
timestampLabel.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
// HACK: The six lines below are part of a workaround for a weird layout bug
topLabelStackView.set(.width, to: UIScreen.main.bounds.width - Values.accentLineThickness - profilePictureViewSize - 3 * Values.mediumSpacing)
2020-03-19 23:20:14 +01:00
topLabelStackView.set(.height, to: 20)
topLabelSpacer.set(.height, to: 20)
2021-03-01 04:04:54 +01:00
bottomLabelStackView.set(.width, to: UIScreen.main.bounds.width - Values.accentLineThickness - profilePictureViewSize - 3 * Values.mediumSpacing)
2020-03-19 23:20:14 +01:00
bottomLabelStackView.set(.height, to: 18)
bottomLabelSpacer.set(.height, to: 18)
2021-03-01 04:04:54 +01:00
statusIndicatorView.set(.width, to: ConversationCell.statusIndicatorSize)
statusIndicatorView.set(.height, to: ConversationCell.statusIndicatorSize)
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
2021-03-01 04:04:54 +01:00
// HACK: Not using a stack view for this is part of a workaround for a weird layout bug
2020-01-23 02:27:58 +01:00
topLabelStackView.pin(.leading, to: .leading, of: labelContainerView)
2020-03-20 00:06:33 +01:00
topLabelStackView.pin(.top, to: .top, of: labelContainerView, withInset: 12)
2020-01-23 02:27:58 +01:00
topLabelStackView.pin(.trailing, to: .trailing, of: labelContainerView)
bottomLabelStackView.pin(.leading, to: .leading, of: labelContainerView)
2020-03-20 00:06:33 +01:00
bottomLabelStackView.pin(.top, to: .bottom, of: topLabelStackView, withInset: 6)
labelContainerView.pin(.bottom, to: .bottom, of: bottomLabelStackView, withInset: 12)
2021-03-01 04:04:54 +01:00
// HACK: The two lines below are part of a workaround for a weird layout bug
2020-01-23 02:27:58 +01:00
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)
2021-03-01 04:04:54 +01:00
// HACK: The two lines below are part of a workaround for a weird layout bug
2020-01-23 02:27:58 +01:00
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() {
2020-06-15 05:50:56 +02:00
AssertIsOnMainThread()
2020-11-24 04:10:32 +01:00
guard let thread = threadViewModel?.threadRecord, let threadID = thread.uniqueId else { return }
2020-10-28 03:30:48 +01:00
MentionsManager.populateUserPublicKeyCacheIfNeeded(for: threadID) // FIXME: This is a terrible place to do this
2020-07-21 05:49:41 +02:00
let isBlocked: Bool
2020-10-28 03:30:48 +01:00
if let thread = thread as? TSContactThread {
isBlocked = SSKEnvironment.shared.blockingManager.isRecipientIdBlocked(thread.contactSessionID())
2020-07-21 05:49:41 +02:00
} else {
isBlocked = false
}
if isBlocked {
2021-03-01 04:04:54 +01:00
accentLineView.backgroundColor = Colors.destructive
accentLineView.alpha = 1
2020-07-21 05:49:41 +02:00
} else {
2021-03-01 04:04:54 +01:00
accentLineView.backgroundColor = Colors.accent
accentLineView.alpha = threadViewModel.hasUnreadMessages ? 1 : 0.0001 // Setting the alpha to exactly 0 causes an issue on iOS 12
2020-07-21 05:49:41 +02:00
}
2021-03-01 04:04:54 +01:00
unreadCountView.isHidden = !threadViewModel.hasUnreadMessages
let unreadCount = threadViewModel.unreadCount
unreadCountLabel.text = unreadCount < 100 ? "\(unreadCount)" : "99+"
let fontSize = (unreadCount < 100) ? Values.verySmallFontSize : 8
unreadCountLabel.font = .boldSystemFont(ofSize: fontSize)
2020-10-28 03:30:48 +01:00
profilePictureView.update(for: thread)
2019-11-28 06:42:07 +01:00
displayNameLabel.text = getDisplayName()
timestampLabel.text = DateUtil.formatDate(forDisplay: threadViewModel.lastMessageDate)
2020-10-28 03:30:48 +01:00
if SSKEnvironment.shared.typingIndicators.typingRecipientId(forThread: thread) != nil {
2019-11-28 06:42:07 +01:00
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 {
2020-11-19 00:37:18 +01:00
case .uploading, .sending: image = #imageLiteral(resourceName: "CircleDotDotDot").asTintedImage(color: Colors.text)!
2020-03-17 06:18:53 +01:00
case .sent, .skipped, .delivered: image = #imageLiteral(resourceName: "CircleCheck").asTintedImage(color: Colors.text)!
case .read:
2020-03-17 06:18:53 +01:00
statusIndicatorView.backgroundColor = isLightMode ? .black : .white
image = isLightMode ? #imageLiteral(resourceName: "FilledCircleCheckLightMode") : #imageLiteral(resourceName: "FilledCircleCheckDarkMode")
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 {
2021-02-26 05:56:41 +01:00
return "Unknown Group"
2019-11-28 06:42:07 +01:00
} else {
return threadViewModel.name
}
} else {
if threadViewModel.threadRecord.isNoteToSelf() {
return NSLocalizedString("NOTE_TO_SELF", comment: "")
2019-11-28 06:42:07 +01:00
} else {
let hexEncodedPublicKey = threadViewModel.contactSessionID!
2021-02-26 05:56:41 +01:00
return Storage.shared.getContact(with: hexEncodedPublicKey)?.displayName(for: .regular) ?? 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
}
}