Avoid using multiple threads in the conversation model

This commit is contained in:
Benoit Martins 2025-01-30 16:41:10 +01:00
parent a9854bc378
commit a3c20e3ae7

View file

@ -205,112 +205,106 @@ class ConversationModel: ObservableObject, Identifiable {
} }
func getContentTextMessage() { func getContentTextMessage() {
coreContext.doOnCoreQueue { _ in let lastMessage = self.chatRoom.lastMessageInHistory
let lastMessage = self.chatRoom.lastMessageInHistory if lastMessage != nil {
if lastMessage != nil { var fromAddressFriend = lastMessage!.fromAddress != nil
var fromAddressFriend = lastMessage!.fromAddress != nil ? self.contactsManager.getFriendWithAddress(address: lastMessage!.fromAddress)?.name ?? nil
? self.contactsManager.getFriendWithAddress(address: lastMessage!.fromAddress)?.name ?? nil : nil
: nil
if !lastMessage!.isOutgoing && lastMessage!.chatRoom != nil && !lastMessage!.chatRoom!.hasCapability(mask: ChatRoom.Capabilities.OneToOne.rawValue) {
if !lastMessage!.isOutgoing && lastMessage!.chatRoom != nil && !lastMessage!.chatRoom!.hasCapability(mask: ChatRoom.Capabilities.OneToOne.rawValue) { if fromAddressFriend == nil {
if fromAddressFriend == nil { if lastMessage!.fromAddress!.displayName != nil {
if lastMessage!.fromAddress!.displayName != nil { fromAddressFriend = lastMessage!.fromAddress!.displayName! + ": "
fromAddressFriend = lastMessage!.fromAddress!.displayName! + ": " } else if lastMessage!.fromAddress!.username != nil {
} else if lastMessage!.fromAddress!.username != nil { fromAddressFriend = lastMessage!.fromAddress!.username! + ": "
fromAddressFriend = lastMessage!.fromAddress!.username! + ": "
} else {
fromAddressFriend = String(lastMessage!.fromAddress!.asStringUriOnly().dropFirst(4)) + ": "
}
} else { } else {
fromAddressFriend! += ": " fromAddressFriend = String(lastMessage!.fromAddress!.asStringUriOnly().dropFirst(4)) + ": "
} }
} else { } else {
fromAddressFriend = nil fromAddressFriend! += ": "
} }
let lastMessageTextTmp = (fromAddressFriend ?? "") } else {
+ (lastMessage!.contents.first(where: {$0.isText == true})?.utf8Text ?? (lastMessage!.contents.first(where: {$0.isFile == true || $0.isFileTransfer == true})?.name ?? "")) fromAddressFriend = nil
let lastMessageIsOutgoingTmp = lastMessage?.isOutgoing ?? false
let lastMessageStateTmp = lastMessage?.state.rawValue ?? 0
DispatchQueue.main.async {
self.lastMessageText = lastMessageTextTmp
self.lastMessageIsOutgoing = lastMessageIsOutgoingTmp
self.lastMessageState = lastMessageStateTmp
}
} }
let lastMessageTextTmp = (fromAddressFriend ?? "")
+ (lastMessage!.contents.first(where: {$0.isText == true})?.utf8Text ?? (lastMessage!.contents.first(where: {$0.isFile == true || $0.isFileTransfer == true})?.name ?? ""))
let lastMessageIsOutgoingTmp = lastMessage?.isOutgoing ?? false
let lastMessageStateTmp = lastMessage?.state.rawValue ?? 0
// DispatchQueue.main.async {
self.lastMessageText = lastMessageTextTmp
self.lastMessageIsOutgoing = lastMessageIsOutgoingTmp
self.lastMessageState = lastMessageStateTmp
// }
} }
} }
func getChatRoomSubject() { func getChatRoomSubject() {
coreContext.doOnCoreQueue { _ in let addressFriend = (self.chatRoom.participants.first != nil && self.chatRoom.participants.first!.address != nil)
let addressFriend = (self.chatRoom.participants.first != nil && self.chatRoom.participants.first!.address != nil) ? self.contactsManager.getFriendWithAddress(address: self.chatRoom.participants.first?.address)
? self.contactsManager.getFriendWithAddress(address: self.chatRoom.participants.first?.address) : nil
: nil
var subjectTmp = ""
var subjectTmp = ""
if self.isGroup {
if self.isGroup { subjectTmp = self.chatRoom.subject!
subjectTmp = self.chatRoom.subject! } else if addressFriend != nil {
} else if addressFriend != nil { subjectTmp = addressFriend!.name!
subjectTmp = addressFriend!.name! } else {
} else { if self.chatRoom.participants.first != nil
if self.chatRoom.participants.first != nil && self.chatRoom.participants.first!.address != nil {
&& self.chatRoom.participants.first!.address != nil {
subjectTmp = self.chatRoom.participants.first!.address!.displayName != nil
subjectTmp = self.chatRoom.participants.first!.address!.displayName != nil ? self.chatRoom.participants.first!.address!.displayName!
? self.chatRoom.participants.first!.address!.displayName! : (self.chatRoom.participants.first!.address!.username ?? String(self.chatRoom.participants.first!.address!.asStringUriOnly().dropFirst(4)))
: (self.chatRoom.participants.first!.address!.username ?? String(self.chatRoom.participants.first!.address!.asStringUriOnly().dropFirst(4)))
}
}
let addressTmp = addressFriend?.address?.asStringUriOnly() ?? ""
let avatarModelTmp = addressFriend != nil && !self.isGroup
? ContactsManager.shared.avatarListModel.first(where: {
$0.friend!.name == addressFriend!.name
&& $0.friend!.address!.asStringUriOnly() == addressFriend!.address!.asStringUriOnly()
})
?? ContactAvatarModel(
friend: nil,
name: subjectTmp,
address: addressTmp,
withPresence: false
)
: ContactAvatarModel(
friend: nil,
name: subjectTmp,
address: self.chatRoom.peerAddress?.asStringUriOnly() ?? addressTmp,
withPresence: false
)
var participantsAddressTmp: [String] = []
self.chatRoom.participants.forEach { participant in
participantsAddressTmp.append(participant.address?.asStringUriOnly() ?? "")
}
DispatchQueue.main.async {
self.subject = subjectTmp
self.avatarModel = avatarModelTmp
self.participantsAddress = participantsAddressTmp
} }
} }
let addressTmp = addressFriend?.address?.asStringUriOnly() ?? ""
let avatarModelTmp = addressFriend != nil && !self.isGroup
? ContactsManager.shared.avatarListModel.first(where: {
$0.friend!.name == addressFriend!.name
&& $0.friend!.address!.asStringUriOnly() == addressFriend!.address!.asStringUriOnly()
})
?? ContactAvatarModel(
friend: nil,
name: subjectTmp,
address: addressTmp,
withPresence: false
)
: ContactAvatarModel(
friend: nil,
name: subjectTmp,
address: self.chatRoom.peerAddress?.asStringUriOnly() ?? addressTmp,
withPresence: false
)
var participantsAddressTmp: [String] = []
self.chatRoom.participants.forEach { participant in
participantsAddressTmp.append(participant.address?.asStringUriOnly() ?? "")
}
// DispatchQueue.main.async {
self.subject = subjectTmp
self.avatarModel = avatarModelTmp
self.participantsAddress = participantsAddressTmp
// }
} }
func getUnreadMessagesCount() { func getUnreadMessagesCount() {
coreContext.doOnCoreQueue { _ in let unreadMessagesCountTmp = self.chatRoom.unreadMessagesCount
let unreadMessagesCountTmp = self.chatRoom.unreadMessagesCount // DispatchQueue.main.async {
DispatchQueue.main.async { self.unreadMessagesCount = unreadMessagesCountTmp
self.unreadMessagesCount = unreadMessagesCountTmp // }
}
}
} }
func refreshAvatarModel() { func refreshAvatarModel() {