session-ios/Session/Shared/UserCell.swift

106 lines
4.1 KiB
Swift
Raw Normal View History

2020-11-09 06:03:59 +01:00
import UIKit
2020-09-28 03:18:18 +02:00
final class UserCell : UITableViewCell {
var accessory = Accessory.none
var publicKey = ""
2021-04-16 03:01:33 +02:00
var isZombie = false
2020-09-28 03:18:18 +02:00
// MARK: Accessory
enum Accessory {
case none
2021-01-06 00:37:26 +01:00
case lock
2020-09-28 03:18:18 +02:00
case tick(isSelected: Bool)
}
// MARK: Components
private lazy var profilePictureView = ProfilePictureView()
private lazy var displayNameLabel: UILabel = {
let result = UILabel()
result.textColor = Colors.text
result.font = .boldSystemFont(ofSize: Values.mediumFontSize)
result.lineBreakMode = .byTruncatingTail
return result
}()
2021-01-06 00:37:26 +01:00
private lazy var accessoryImageView: UIImageView = {
2020-09-28 03:18:18 +02:00
let result = UIImageView()
result.contentMode = .scaleAspectFit
let size: CGFloat = 24
result.set(.width, to: size)
result.set(.height, to: size)
return result
}()
private lazy var separator: UIView = {
let result = UIView()
result.backgroundColor = Colors.separator
result.set(.height, to: Values.separatorThickness)
return result
}()
// 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() {
2021-03-04 01:12:08 +01:00
// Background color
2020-09-28 03:18:18 +02:00
backgroundColor = Colors.cellBackground
2021-03-04 01:12:08 +01:00
// Highlight color
2020-09-28 03:18:18 +02:00
let selectedBackgroundView = UIView()
selectedBackgroundView.backgroundColor = .clear // Disabled for now
self.selectedBackgroundView = selectedBackgroundView
2021-03-04 01:12:08 +01:00
// Profile picture image view
2020-09-28 03:18:18 +02:00
let profilePictureViewSize = Values.smallProfilePictureSize
profilePictureView.set(.width, to: profilePictureViewSize)
profilePictureView.set(.height, to: profilePictureViewSize)
profilePictureView.size = profilePictureViewSize
2021-03-04 01:12:08 +01:00
// Main stack view
let spacer = UIView.hStretchingSpacer()
spacer.widthAnchor.constraint(greaterThanOrEqualToConstant: Values.mediumSpacing).isActive = true
let stackView = UIStackView(arrangedSubviews: [ profilePictureView, UIView.hSpacer(Values.mediumSpacing), displayNameLabel, spacer, accessoryImageView ])
2020-09-28 03:18:18 +02:00
stackView.axis = .horizontal
stackView.alignment = .center
2021-03-04 01:12:08 +01:00
stackView.isLayoutMarginsRelativeArrangement = true
stackView.layoutMargins = UIEdgeInsets(uniform: Values.mediumSpacing)
2020-09-28 03:18:18 +02:00
contentView.addSubview(stackView)
2021-03-04 01:12:08 +01:00
stackView.pin(to: contentView)
stackView.set(.width, to: UIScreen.main.bounds.width)
2020-09-28 03:18:18 +02:00
// Set up the separator
contentView.addSubview(separator)
2021-03-04 01:12:08 +01:00
separator.pin([ UIView.HorizontalEdge.leading, UIView.VerticalEdge.bottom, UIView.HorizontalEdge.trailing ], to: contentView)
2020-09-28 03:18:18 +02:00
}
// MARK: Updating
2020-09-28 03:43:15 +02:00
func update() {
2021-02-26 04:42:46 +01:00
profilePictureView.publicKey = publicKey
2020-09-28 03:18:18 +02:00
profilePictureView.update()
2021-02-26 05:56:41 +01:00
displayNameLabel.text = Storage.shared.getContact(with: publicKey)?.displayName(for: .regular) ?? publicKey
2020-09-28 03:18:18 +02:00
switch accessory {
case .none: accessoryImageView.isHidden = true
case .lock:
accessoryImageView.isHidden = false
accessoryImageView.image = #imageLiteral(resourceName: "ic_lock_outline").withRenderingMode(.alwaysTemplate)
accessoryImageView.tintColor = Colors.text.withAlphaComponent(Values.mediumOpacity)
case .tick(let isSelected):
let icon: UIImage = (isSelected ? #imageLiteral(resourceName: "CircleCheck") : #imageLiteral(resourceName: "Circle"))
accessoryImageView.isHidden = false
accessoryImageView.image = icon.withRenderingMode(.alwaysTemplate)
accessoryImageView.tintColor = Colors.text
2020-09-28 03:18:18 +02:00
}
2021-04-16 03:01:33 +02:00
let alpha: CGFloat = isZombie ? 0.5 : 1
[ profilePictureView, displayNameLabel, accessoryImageView ].forEach { $0.alpha = alpha }
2020-09-28 03:18:18 +02:00
}
}