LNIMConversationCell.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // LNIMConversationCell.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/3.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNIMConversationCell: UITableViewCell {
  11. private let avatar = UIImageView()
  12. private let onlineView = LNOnlineView()
  13. private let titleLabel = UILabel()
  14. private let messageLabel = UILabel()
  15. private let timeLabel = UILabel()
  16. private let unreadLabel = UILabel()
  17. private var curItem: V2TIMConversation?
  18. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  19. super.init(style: style, reuseIdentifier: reuseIdentifier)
  20. setupViews()
  21. LNEventDeliver.addObserver(self)
  22. }
  23. func update(_ item: V2TIMConversation) {
  24. if item.userID?.isImOfficialId == true {
  25. if item.userID == LNIMOfficialIds.officialMessage.rawValue {
  26. avatar.image = .icImOfficial
  27. titleLabel.text = .init(key: "A00091")
  28. } else {
  29. avatar.image = .icImUnknownOfficial
  30. titleLabel.text = item.showName
  31. }
  32. titleLabel.textColor = .text_6
  33. onlineView.isHidden = true
  34. } else {
  35. titleLabel.text = item.displayName
  36. titleLabel.textColor = .text_5
  37. onlineView.isHidden = !LNIMManager.shared.isUserOnline(uid: item.userID ?? "")
  38. }
  39. messageLabel.attributedText = item.lastDisplayString
  40. if messageLabel.attributedText?.string.isEmpty != false,
  41. item.userID?.isImOfficialId == true {
  42. messageLabel.attributedText = NSAttributedString(string: .init(key: "A00092"))
  43. }
  44. timeLabel.text = item.lastDisplayDate?.tencentIMTimeDesc ?? ""
  45. if item.unreadCount > 0 {
  46. if item.unreadCount > 99 {
  47. unreadLabel.text = "99+"
  48. } else {
  49. unreadLabel.text = "\(item.unreadCount)"
  50. }
  51. unreadLabel.superview?.isHidden = false
  52. } else {
  53. unreadLabel.superview?.isHidden = true
  54. }
  55. curItem = item
  56. if let userInfo = item.extraInfo?.userInfo {
  57. avatar.showAvatar(userInfo.avatar)
  58. } else if let userId = item.userID, !userId.isImOfficialId {
  59. LNProfileManager.shared.getUserProfileDetail(uid: userId) { [weak self] info in
  60. guard let self else { return }
  61. guard let info else { return }
  62. item.extraInfo?.userInfo = info
  63. guard info.userNo == curItem?.userID else { return }
  64. avatar.showAvatar(info.avatar)
  65. titleLabel.text = item.displayName
  66. }
  67. }
  68. }
  69. required init?(coder: NSCoder) {
  70. fatalError("init(coder:) has not been implemented")
  71. }
  72. }
  73. extension LNIMConversationCell: LNIMManagerNotify {
  74. func onIMUserStatusChanged(uid: String, status: V2TIMUserStatusType) {
  75. guard uid == curItem?.userID else { return }
  76. onlineView.isHidden = status != .USER_STATUS_ONLINE
  77. }
  78. }
  79. extension LNIMConversationCell {
  80. private func setupViews() {
  81. backgroundColor = .clear
  82. avatar.layer.cornerRadius = 23
  83. avatar.clipsToBounds = true
  84. contentView.addSubview(avatar)
  85. avatar.snp.makeConstraints { make in
  86. make.leading.equalToSuperview().offset(16)
  87. make.centerY.equalToSuperview()
  88. make.height.equalToSuperview().inset(9)
  89. make.width.height.equalTo(46)
  90. }
  91. contentView.addSubview(onlineView)
  92. onlineView.snp.makeConstraints { make in
  93. make.edges.equalTo(avatar).inset(-2)
  94. }
  95. let textView = buildTextView()
  96. contentView.addSubview(textView)
  97. let infoView = buildInfoView()
  98. contentView.addSubview(infoView)
  99. infoView.snp.makeConstraints { make in
  100. make.centerY.equalToSuperview()
  101. make.trailing.equalToSuperview().offset(-16)
  102. make.height.equalTo(textView)
  103. }
  104. textView.snp.makeConstraints { make in
  105. make.centerY.equalToSuperview()
  106. make.leading.equalTo(avatar.snp.trailing).offset(10)
  107. make.trailing.equalTo(infoView.snp.leading).offset(-16)
  108. }
  109. }
  110. private func buildTextView() -> UIView {
  111. let container = UIView()
  112. titleLabel.font = .heading_h4
  113. titleLabel.textColor = .text_5
  114. container.addSubview(titleLabel)
  115. titleLabel.snp.makeConstraints { make in
  116. make.horizontalEdges.top.equalToSuperview()
  117. }
  118. messageLabel.font = .body_s
  119. messageLabel.textColor = .text_3
  120. container.addSubview(messageLabel)
  121. messageLabel.snp.makeConstraints { make in
  122. make.horizontalEdges.bottom.equalToSuperview()
  123. make.top.equalTo(titleLabel.snp.bottom).offset(5)
  124. }
  125. return container
  126. }
  127. private func buildInfoView() -> UIView {
  128. let container = UIView()
  129. timeLabel.font = .body_xs
  130. timeLabel.textColor = .text_3
  131. timeLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  132. timeLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
  133. container.addSubview(timeLabel)
  134. timeLabel.snp.makeConstraints { make in
  135. make.trailing.equalToSuperview()
  136. make.top.equalToSuperview()
  137. make.leading.equalToSuperview()
  138. }
  139. let view = UIView()
  140. view.backgroundColor = .init(hex: "#F23051")
  141. view.layer.cornerRadius = 7
  142. container.addSubview(view)
  143. view.snp.makeConstraints { make in
  144. make.trailing.bottom.equalToSuperview()
  145. make.leading.greaterThanOrEqualToSuperview()
  146. make.height.equalTo(14)
  147. }
  148. unreadLabel.font = .body_xs
  149. unreadLabel.textColor = .text_1
  150. unreadLabel.textAlignment = .center
  151. view.addSubview(unreadLabel)
  152. unreadLabel.snp.makeConstraints { make in
  153. make.center.equalToSuperview()
  154. make.width.equalToSuperview().inset(4)
  155. }
  156. return container
  157. }
  158. }