ChatListCell.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. let titleLabel: UILabel = {
  13. let label = UILabel()
  14. label.font = UIFont.systemFont(ofSize: 16, weight: .medium)
  15. label.lineBreakMode = .byTruncatingTail
  16. label.textColor = DcColors.defaultTextColor
  17. label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1), for: NSLayoutConstraint.Axis.horizontal)
  18. label.translatesAutoresizingMaskIntoConstraints = false
  19. return label
  20. }()
  21. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  22. super.init(style: style, reuseIdentifier: reuseIdentifier)
  23. selectionStyle = .none
  24. backgroundColor = DcColors.contactCellBackgroundColor
  25. contentView.backgroundColor = DcColors.contactCellBackgroundColor
  26. setupSubviews()
  27. }
  28. required init?(coder: NSCoder) {
  29. fatalError("init(coder:) has not been implemented")
  30. }
  31. private func setupSubviews() {
  32. let margin: CGFloat = 10
  33. avatar.translatesAutoresizingMaskIntoConstraints = false
  34. contentView.addSubview(avatar)
  35. contentView.addConstraints([
  36. avatar.constraintWidthTo(badgeSize),
  37. avatar.constraintHeightTo(badgeSize),
  38. avatar.constraintAlignLeadingTo(contentView, paddingLeading: badgeSize / 4),
  39. avatar.constraintCenterYTo(contentView),
  40. ])
  41. contentView.addSubview(titleLabel)
  42. contentView.addConstraints([
  43. titleLabel.constraintCenterYTo(contentView),
  44. titleLabel.constraintToTrailingOf(avatar, paddingLeading: margin),
  45. titleLabel.constraintAlignTrailingTo(contentView)
  46. ])
  47. }
  48. private func setImage(_ img: UIImage) {
  49. avatar.setImage(img)
  50. }
  51. private func resetBackupImage() {
  52. avatar.setColor(UIColor.clear)
  53. avatar.setName("")
  54. }
  55. private func setBackupImage(name: String, color: UIColor) {
  56. avatar.setColor(color)
  57. avatar.setName(name)
  58. }
  59. private func setColor(_ color: UIColor) {
  60. avatar.setColor(color)
  61. }
  62. // use this update-method to update cell in cellForRowAt whenever it is possible - other set-methods will be set private in progress
  63. func updateCell(chatId: Int) {
  64. let chat = DcContext.shared.getChat(chatId: chatId)
  65. titleLabel.text = chat.name
  66. backgroundColor = DcColors.contactCellBackgroundColor
  67. contentView.backgroundColor = DcColors.contactCellBackgroundColor
  68. if let img = chat.profileImage {
  69. resetBackupImage()
  70. setImage(img)
  71. } else {
  72. setBackupImage(name: chat.name, color: chat.color)
  73. }
  74. }
  75. }