| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- //
- // V2TIMConversation+Extension.swift
- // Lanu
- //
- // Created by OneeChan on 2025/12/14.
- //
- import Foundation
- extension V2TIMConversation {
- var lastDisplayDate: Date? {
- if draftText?.isEmpty == false {
- draftTimestamp
- } else if let lastMessage {
- lastMessage.timestamp
- } else {
- Date.distantPast
- }
- }
-
- var lastDisplayString: NSMutableAttributedString? {
- let atStr = groupAtTipString
- let attrString = NSMutableAttributedString(string: atStr)
- attrString.setAttributes([.foregroundColor: UIColor.systemRed], range: .init(location: 0, length: atStr.count))
-
- let hasRisk = lastMessage?.hasRiskContent == true
- let isRevoked = lastMessage?.status == .MSG_STATUS_LOCAL_REVOKED
-
- if draftText?.count ?? 0 > 0 {
- let draft = NSAttributedString(string: TIMLocalizedText.shared.commonText("TUIKitMessageTypeDraftFormat"), attributes: [.foregroundColor: UIColor(hex: "#FA5151")])
- attrString.append(draft)
- let draftContentStr = draftContent.getEmojiDescContent
- let draftContent = NSAttributedString(string: draftContentStr, attributes: [.foregroundColor: UIColor.systemGray])
- attrString.append(draftContent)
- } else {
- if let lastMessage {
- let imMessage = LNIMMessageData(imMessage: lastMessage)
- if case .official = imMessage.type {
- attrString.append(.init(string: .init(key: "A00019")))
- } else if case .order = imMessage.type {
- attrString.append(.init(string: .init(key: "A00020")))
- } else {
- let lastMsgStr = lastMessage.displayString
- guard let lastMsgStr else { return nil }
- if hasRisk, !isRevoked {
- attrString.append(.init(string: lastMsgStr, attributes: [.foregroundColor: UIColor(hex: "#E94444")]))
- } else {
- attrString.append(.init(string: lastMsgStr))
- }
- }
- }
- }
-
- if groupType == GroupType_Meeting,
- recvOpt == .RECEIVE_NOT_NOTIFY_MESSAGE,
- unreadCount > 0 {
- let unreadStr = NSAttributedString(string: String(format: "[%d %@]", unreadCount, TIMLocalizedText.shared.commonText("TUIKitMessageTypeLastMsgCountFormat")))
- attrString.insert(unreadStr, at: 0)
- }
-
- if draftText == nil, let lastMessage,
- lastMessage.status == .MSG_STATUS_SENDING
- || lastMessage.status == .MSG_STATUS_SEND_FAIL
- || hasRisk,
- !isRevoked {
- let font = UIFont.systemFont(ofSize: 14)
- let spaceStr = NSAttributedString(string: " ", attributes: [.font: font])
- attrString.insert(spaceStr, at: 0)
-
- let image: UIImage = if lastMessage.status == .MSG_STATUS_SENDING {
- .msgSendingForConv
- } else {
- .msgErrorForConv
- }
- let attachment = NSTextAttachment(image: image)
- attachment.bounds = .init(x: 0, y: -(font.lineHeight - font.pointSize) / 2, width: font.pointSize, height: font.pointSize)
- let imageStr = NSAttributedString(attachment: attachment)
- attrString.insert(imageStr, at: 0)
- }
-
- return attrString
- }
- }
- extension V2TIMConversation {
- private var groupAtTipString: String {
- var atMe = false
- var atAll = false
- self.groupAtInfolist?.forEach {
- switch ($0.atType) {
- case .AT_ME: atMe = true
- case .AT_ALL: atAll = true
- case .AT_ALL_AT_ME:
- atMe = true
- atAll = true
- default:
- break
- }
- }
- var atTipsStr = ""
- if atMe, !atAll {
- atTipsStr = TIMLocalizedText.shared.commonText("TUIKitConversationTipsAtMe")
- }
- if !atMe, atAll {
- atTipsStr = TIMLocalizedText.shared.commonText("TUIKitConversationTipsAtAll")
- }
- if atMe, atAll {
- atTipsStr = TIMLocalizedText.shared.commonText("TUIKitConversationTipsAtMeAndAll")
- }
-
- return atTipsStr
- }
-
- private var draftContent: String {
- guard let draftText, !draftText.isEmpty else { return "" }
- guard let data = draftText.data(using: .utf8) else { return "" }
- let jsonDic = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String: String]
-
- return jsonDic?["content"] ?? ""
- }
- }
- private var userInfoKey: UInt8 = 0
- extension V2TIMConversation {
- var userInfo: LNUserProfileVO? {
- get {
- objc_getAssociatedObject(self, &userInfoKey) as? LNUserProfileVO
- }
- set {
- objc_setAssociatedObject(self, &userInfoKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- }
- }
- }
|