forked from mirrors/linphone-iphone
Refactor the updateChatRoom and updateChatRoomsList functions in ConversationsListViewModel to fix crashes
This commit is contained in:
parent
abf294625a
commit
1510a1b045
1 changed files with 66 additions and 62 deletions
|
|
@ -72,47 +72,49 @@ class ConversationsListViewModel: ObservableObject {
|
||||||
self.conversationsListTmp.forEach { conversationModel in
|
self.conversationsListTmp.forEach { conversationModel in
|
||||||
if conversationModel.participantsAddress.contains(contactAvatarModel.address) {
|
if conversationModel.participantsAddress.contains(contactAvatarModel.address) {
|
||||||
if conversationModel.isGroup && conversationModel.participantsAddress.count > 1 {
|
if conversationModel.isGroup && conversationModel.participantsAddress.count > 1 {
|
||||||
let lastMessage = conversationModel.chatRoom.lastMessageInHistory
|
if let lastMessage = conversationModel.chatRoom.lastMessageInHistory, let fromAddress = lastMessage.fromAddress, fromAddress.asStringUriOnly().contains(contactAvatarModel.address) {
|
||||||
if lastMessage != nil && lastMessage!.fromAddress != nil && lastMessage!.fromAddress!.asStringUriOnly().contains(contactAvatarModel.address) {
|
var fromAddressFriend = self.contactsManager.getFriendWithAddress(address: fromAddress)?.name
|
||||||
var fromAddressFriend = lastMessage!.fromAddress != nil
|
|
||||||
? self.contactsManager.getFriendWithAddress(address: lastMessage!.fromAddress)?.name ?? 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 let displayName = fromAddress.displayName {
|
||||||
fromAddressFriend = lastMessage!.fromAddress!.displayName! + ": "
|
fromAddressFriend = displayName + ": "
|
||||||
} else if lastMessage!.fromAddress!.username != nil {
|
} else if let username = fromAddress.username {
|
||||||
fromAddressFriend = lastMessage!.fromAddress!.username! + ": "
|
fromAddressFriend = username + ": "
|
||||||
} else {
|
} else {
|
||||||
fromAddressFriend = String(lastMessage!.fromAddress!.asStringUriOnly().dropFirst(4)) + ": "
|
fromAddressFriend = String(fromAddress.asStringUriOnly().dropFirst(4)) + ": "
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fromAddressFriend! += ": "
|
fromAddressFriend! += ": "
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fromAddressFriend = nil
|
fromAddressFriend = nil
|
||||||
}
|
}
|
||||||
let lastMessageTextTmp = (fromAddressFriend ?? "")
|
|
||||||
+ (lastMessage!.contents.first(where: {$0.isText == true})?.utf8Text ?? (lastMessage!.contents.first(where: {$0.isFile == true || $0.isFileTransfer == true})?.name ?? ""))
|
|
||||||
|
|
||||||
let index = self.conversationsList.firstIndex(where: { $0.chatRoom === conversationModel.chatRoom })
|
let lastMessageTextTmp = (fromAddressFriend ?? "") + (lastMessage.contents.first(where: { $0.isText })?.utf8Text ?? (lastMessage.contents.first(where: { $0.isFile || $0.isFileTransfer })?.name ?? ""))
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
if let index = self.conversationsList.firstIndex(where: { $0.chatRoom === conversationModel.chatRoom }) {
|
||||||
conversationModel.lastMessageText = lastMessageTextTmp
|
DispatchQueue.main.async {
|
||||||
if index != nil {
|
conversationModel.lastMessageText = lastMessageTextTmp
|
||||||
self.conversationsList[index!].lastMessageText = lastMessageTextTmp
|
self.conversationsList[index].lastMessageText = lastMessageTextTmp
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
conversationModel.lastMessageText = lastMessageTextTmp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if !conversationModel.isGroup && conversationModel.participantsAddress.first != nil && conversationModel.participantsAddress.first!.contains(contactAvatarModel.address) {
|
} else if !conversationModel.isGroup, let firstParticipantAddress = conversationModel.participantsAddress.first, firstParticipantAddress.contains(contactAvatarModel.address) {
|
||||||
let index = self.conversationsList.firstIndex(where: { $0.chatRoom === conversationModel.chatRoom })
|
if let index = self.conversationsList.firstIndex(where: { $0.chatRoom === conversationModel.chatRoom }) {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
conversationModel.avatarModel = contactAvatarModel
|
conversationModel.avatarModel = contactAvatarModel
|
||||||
conversationModel.subject = contactAvatarModel.name
|
conversationModel.subject = contactAvatarModel.name
|
||||||
if index != nil {
|
self.conversationsList[index].avatarModel = contactAvatarModel
|
||||||
self.conversationsList[index!].avatarModel = contactAvatarModel
|
}
|
||||||
|
} else {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
conversationModel.avatarModel = contactAvatarModel
|
||||||
|
conversationModel.subject = contactAvatarModel.name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -124,53 +126,54 @@ class ConversationsListViewModel: ObservableObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateChatRoom(address: String) {
|
func updateChatRoom(address: String) {
|
||||||
CoreContext.shared.doOnCoreQueue { _ in
|
CoreContext.shared.doOnCoreQueue { _ in
|
||||||
if let contactAvatarModel = self.contactsManager.avatarListModel.first(where: {$0.addresses.contains(address)}) {
|
if let contactAvatarModel = self.contactsManager.avatarListModel.first(where: { $0.addresses.contains(address) }) {
|
||||||
self.conversationsListTmp.forEach { conversationModel in
|
self.conversationsListTmp.forEach { conversationModel in
|
||||||
if conversationModel.participantsAddress.contains(contactAvatarModel.address) {
|
if conversationModel.participantsAddress.contains(contactAvatarModel.address) {
|
||||||
if conversationModel.isGroup && conversationModel.participantsAddress.count > 1 {
|
if conversationModel.isGroup && conversationModel.participantsAddress.count > 1 {
|
||||||
let lastMessage = conversationModel.chatRoom.lastMessageInHistory
|
if let lastMessage = conversationModel.chatRoom.lastMessageInHistory, let fromAddress = lastMessage.fromAddress, fromAddress.asStringUriOnly().contains(contactAvatarModel.address) {
|
||||||
if lastMessage != nil && lastMessage!.fromAddress != nil && lastMessage!.fromAddress!.asStringUriOnly().contains(contactAvatarModel.address) {
|
var fromAddressFriend = self.contactsManager.getFriendWithAddress(address: fromAddress)?.name
|
||||||
var fromAddressFriend = lastMessage!.fromAddress != nil
|
|
||||||
? self.contactsManager.getFriendWithAddress(address: lastMessage!.fromAddress)?.name ?? nil
|
if !lastMessage.isOutgoing && lastMessage.chatRoom != nil && !lastMessage.chatRoom!.hasCapability(mask: ChatRoom.Capabilities.OneToOne.rawValue) {
|
||||||
: nil
|
if fromAddressFriend == nil {
|
||||||
|
if let displayName = fromAddress.displayName {
|
||||||
if !lastMessage!.isOutgoing && lastMessage!.chatRoom != nil && !lastMessage!.chatRoom!.hasCapability(mask: ChatRoom.Capabilities.OneToOne.rawValue) {
|
fromAddressFriend = displayName + ": "
|
||||||
if fromAddressFriend == nil {
|
} else if let username = fromAddress.username {
|
||||||
if lastMessage!.fromAddress!.displayName != nil {
|
fromAddressFriend = username + ": "
|
||||||
fromAddressFriend = lastMessage!.fromAddress!.displayName! + ": "
|
|
||||||
} else if lastMessage!.fromAddress!.username != nil {
|
|
||||||
fromAddressFriend = lastMessage!.fromAddress!.username! + ": "
|
|
||||||
} else {
|
|
||||||
fromAddressFriend = String(lastMessage!.fromAddress!.asStringUriOnly().dropFirst(4)) + ": "
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
fromAddressFriend! += ": "
|
fromAddressFriend = String(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 index = self.conversationsList.firstIndex(where: { $0.chatRoom === conversationModel.chatRoom })
|
|
||||||
|
let lastMessageTextTmp = (fromAddressFriend ?? "") + (lastMessage.contents.first(where: { $0.isText })?.utf8Text ?? (lastMessage.contents.first(where: { $0.isFile || $0.isFileTransfer })?.name ?? ""))
|
||||||
|
|
||||||
|
if let index = self.conversationsList.firstIndex(where: { $0.chatRoom === conversationModel.chatRoom }) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
conversationModel.lastMessageText = lastMessageTextTmp
|
||||||
|
self.conversationsList[index].lastMessageText = lastMessageTextTmp
|
||||||
|
}
|
||||||
|
} else {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
conversationModel.lastMessageText = lastMessageTextTmp
|
conversationModel.lastMessageText = lastMessageTextTmp
|
||||||
if index != nil {
|
|
||||||
self.conversationsList[index!].lastMessageText = lastMessageTextTmp
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if !conversationModel.isGroup && conversationModel.participantsAddress.first != nil && conversationModel.participantsAddress.first!.contains(contactAvatarModel.address) {
|
}
|
||||||
let index = self.conversationsList.firstIndex(where: { $0.chatRoom === conversationModel.chatRoom })
|
} else if !conversationModel.isGroup, let firstParticipantAddress = conversationModel.participantsAddress.first, firstParticipantAddress.contains(contactAvatarModel.address) {
|
||||||
|
if let index = self.conversationsList.firstIndex(where: { $0.chatRoom === conversationModel.chatRoom }) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
conversationModel.avatarModel = contactAvatarModel
|
||||||
|
conversationModel.subject = contactAvatarModel.name
|
||||||
|
self.conversationsList[index].avatarModel = contactAvatarModel
|
||||||
|
}
|
||||||
|
} else {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
conversationModel.avatarModel = contactAvatarModel
|
conversationModel.avatarModel = contactAvatarModel
|
||||||
conversationModel.subject = contactAvatarModel.name
|
conversationModel.subject = contactAvatarModel.name
|
||||||
if index != nil {
|
|
||||||
self.conversationsList[index!].avatarModel = contactAvatarModel
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -178,6 +181,7 @@ class ConversationsListViewModel: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func addConversationDelegate() {
|
func addConversationDelegate() {
|
||||||
coreContext.doOnCoreQueue { core in
|
coreContext.doOnCoreQueue { core in
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue