V2TIMConversation+Extension.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // V2TIMConversation+Extension.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/14.
  6. //
  7. import Foundation
  8. extension V2TIMConversation {
  9. var lastDisplayDate: Date? {
  10. if draftText?.isEmpty == false {
  11. draftTimestamp
  12. } else if let lastMessage {
  13. lastMessage.timestamp
  14. } else {
  15. Date.distantPast
  16. }
  17. }
  18. var lastDisplayString: NSMutableAttributedString? {
  19. let atStr = groupAtTipString
  20. let attrString = NSMutableAttributedString(string: atStr)
  21. attrString.setAttributes([.foregroundColor: UIColor.systemRed], range: .init(location: 0, length: atStr.count))
  22. let hasRisk = lastMessage?.hasRiskContent == true
  23. let isRevoked = lastMessage?.status == .MSG_STATUS_LOCAL_REVOKED
  24. if draftText?.count ?? 0 > 0 {
  25. let draft = NSAttributedString(string: TIMLocalizedText.shared.commonText("TUIKitMessageTypeDraftFormat"), attributes: [.foregroundColor: UIColor(hex: "#FA5151")])
  26. attrString.append(draft)
  27. let draftContentStr = draftContent.getEmojiDescContent
  28. let draftContent = NSAttributedString(string: draftContentStr, attributes: [.foregroundColor: UIColor.systemGray])
  29. attrString.append(draftContent)
  30. } else {
  31. if let lastMessage {
  32. let imMessage = LNIMMessageData(imMessage: lastMessage)
  33. if case .official = imMessage.type {
  34. attrString.append(.init(string: .init(key: "A00019")))
  35. } else if case .order = imMessage.type {
  36. attrString.append(.init(string: .init(key: "A00020")))
  37. } else {
  38. let lastMsgStr = lastMessage.displayString
  39. guard let lastMsgStr else { return nil }
  40. if hasRisk, !isRevoked {
  41. attrString.append(.init(string: lastMsgStr, attributes: [.foregroundColor: UIColor(hex: "#E94444")]))
  42. } else {
  43. attrString.append(.init(string: lastMsgStr))
  44. }
  45. }
  46. }
  47. }
  48. if groupType == GroupType_Meeting,
  49. recvOpt == .RECEIVE_NOT_NOTIFY_MESSAGE,
  50. unreadCount > 0 {
  51. let unreadStr = NSAttributedString(string: String(format: "[%d %@]", unreadCount, TIMLocalizedText.shared.commonText("TUIKitMessageTypeLastMsgCountFormat")))
  52. attrString.insert(unreadStr, at: 0)
  53. }
  54. if draftText == nil, let lastMessage,
  55. lastMessage.status == .MSG_STATUS_SENDING
  56. || lastMessage.status == .MSG_STATUS_SEND_FAIL
  57. || hasRisk,
  58. !isRevoked {
  59. let font = UIFont.systemFont(ofSize: 14)
  60. let spaceStr = NSAttributedString(string: " ", attributes: [.font: font])
  61. attrString.insert(spaceStr, at: 0)
  62. let image: UIImage = if lastMessage.status == .MSG_STATUS_SENDING {
  63. .msgSendingForConv
  64. } else {
  65. .msgErrorForConv
  66. }
  67. let attachment = NSTextAttachment(image: image)
  68. attachment.bounds = .init(x: 0, y: -(font.lineHeight - font.pointSize) / 2, width: font.pointSize, height: font.pointSize)
  69. let imageStr = NSAttributedString(attachment: attachment)
  70. attrString.insert(imageStr, at: 0)
  71. }
  72. return attrString
  73. }
  74. }
  75. extension V2TIMConversation {
  76. private var groupAtTipString: String {
  77. var atMe = false
  78. var atAll = false
  79. self.groupAtInfolist?.forEach {
  80. switch ($0.atType) {
  81. case .AT_ME: atMe = true
  82. case .AT_ALL: atAll = true
  83. case .AT_ALL_AT_ME:
  84. atMe = true
  85. atAll = true
  86. default:
  87. break
  88. }
  89. }
  90. var atTipsStr = ""
  91. if atMe, !atAll {
  92. atTipsStr = TIMLocalizedText.shared.commonText("TUIKitConversationTipsAtMe")
  93. }
  94. if !atMe, atAll {
  95. atTipsStr = TIMLocalizedText.shared.commonText("TUIKitConversationTipsAtAll")
  96. }
  97. if atMe, atAll {
  98. atTipsStr = TIMLocalizedText.shared.commonText("TUIKitConversationTipsAtMeAndAll")
  99. }
  100. return atTipsStr
  101. }
  102. private var draftContent: String {
  103. guard let draftText, !draftText.isEmpty else { return "" }
  104. guard let data = draftText.data(using: .utf8) else { return "" }
  105. let jsonDic = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String: String]
  106. return jsonDic?["content"] ?? ""
  107. }
  108. }
  109. private var userInfoKey: UInt8 = 0
  110. extension V2TIMConversation {
  111. var userInfo: LNUserProfileVO? {
  112. get {
  113. objc_getAssociatedObject(self, &userInfoKey) as? LNUserProfileVO
  114. }
  115. set {
  116. objc_setAssociatedObject(self, &userInfoKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  117. }
  118. }
  119. }