ChatListCell.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import Foundation
  2. import UIKit
  3. import DcCore
  4. class ChatListCell: UITableViewCell {
  5. let badgeSize: CGFloat = 54
  6. lazy var avatar: InitialsBadge = {
  7. let badge = InitialsBadge(size: badgeSize)
  8. badge.setColor(UIColor.lightGray)
  9. badge.isAccessibilityElement = false
  10. return badge
  11. }()
  12. lazy var stackView: UIStackView = {
  13. let stackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
  14. stackView.axis = .vertical
  15. stackView.translatesAutoresizingMaskIntoConstraints = false
  16. stackView.alignment = .leading
  17. stackView.clipsToBounds = true
  18. return stackView
  19. }()
  20. lazy var titleLabel: UILabel = {
  21. let label = UILabel()
  22. label.font = UIFont.preferredFont(forTextStyle: .headline)
  23. label.adjustsFontForContentSizeCategory = true
  24. label.lineBreakMode = .byTruncatingTail
  25. label.textColor = DcColors.defaultTextColor
  26. label.translatesAutoresizingMaskIntoConstraints = false
  27. return label
  28. }()
  29. lazy var subtitleLabel: UILabel = {
  30. let label = UILabel()
  31. label.textColor = DcColors.middleGray
  32. label.lineBreakMode = .byTruncatingTail
  33. label.font = .preferredFont(forTextStyle: .subheadline)
  34. label.adjustsFontForContentSizeCategory = true
  35. label.translatesAutoresizingMaskIntoConstraints = false
  36. return label
  37. }()
  38. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  39. super.init(style: style, reuseIdentifier: reuseIdentifier)
  40. selectionStyle = .none
  41. backgroundColor = DcColors.contactCellBackgroundColor
  42. contentView.backgroundColor = DcColors.contactCellBackgroundColor
  43. setupSubviews()
  44. }
  45. required init?(coder: NSCoder) {
  46. fatalError("init(coder:) has not been implemented")
  47. }
  48. private func setupSubviews() {
  49. let margin: CGFloat = 10
  50. avatar.translatesAutoresizingMaskIntoConstraints = false
  51. contentView.addSubview(avatar)
  52. contentView.addConstraints([
  53. avatar.constraintWidthTo(badgeSize),
  54. avatar.constraintHeightTo(badgeSize),
  55. avatar.constraintAlignLeadingTo(contentView, paddingLeading: badgeSize / 4),
  56. avatar.constraintCenterYTo(contentView),
  57. ])
  58. contentView.addSubview(stackView)
  59. contentView.addConstraints([
  60. stackView.constraintCenterYTo(contentView),
  61. stackView.constraintToTrailingOf(avatar, paddingLeading: margin),
  62. stackView.constraintAlignTrailingTo(contentView),
  63. ])
  64. }
  65. private func setImage(_ img: UIImage) {
  66. avatar.setImage(img)
  67. }
  68. private func resetBackupImage() {
  69. avatar.setColor(UIColor.clear)
  70. avatar.setName("")
  71. }
  72. private func setBackupImage(name: String, color: UIColor) {
  73. avatar.setColor(color)
  74. avatar.setName(name)
  75. }
  76. private func setColor(_ color: UIColor) {
  77. avatar.setColor(color)
  78. }
  79. // use this update-method to update cell in cellForRowAt whenever it is possible - other set-methods will be set private in progress
  80. func updateCell(cellViewModel: AvatarCellViewModel) {
  81. // subtitle
  82. switch cellViewModel.type {
  83. case .chat(let chatData):
  84. let chat = DcContext.shared.getChat(chatId: chatData.chatId)
  85. titleLabel.attributedText = cellViewModel.title.boldAt(indexes: cellViewModel.titleHighlightIndexes, fontSize: titleLabel.font.pointSize)
  86. if let img = chat.profileImage {
  87. resetBackupImage()
  88. setImage(img)
  89. } else {
  90. setBackupImage(name: chat.name, color: chat.color)
  91. }
  92. subtitleLabel.attributedText = nil
  93. case .contact(let contactData):
  94. let contact = DcContact(id: contactData.contactId)
  95. titleLabel.attributedText = cellViewModel.title.boldAt(indexes: cellViewModel.titleHighlightIndexes, fontSize: titleLabel.font.pointSize)
  96. if let profileImage = contact.profileImage {
  97. avatar.setImage(profileImage)
  98. } else {
  99. setBackupImage(name: cellViewModel.title, color: contact.color)
  100. }
  101. subtitleLabel.attributedText = cellViewModel.subtitle.boldAt(indexes: cellViewModel.subtitleHighlightIndexes,
  102. fontSize: subtitleLabel.font.pointSize)
  103. default:
  104. return
  105. }
  106. }
  107. }