ui: fix possible suspect for race condition on accept contact (#6121)

This commit is contained in:
spaced4ndy
2025-07-28 15:51:37 +00:00
committed by GitHub
parent 7988463a47
commit 69461c2257
3 changed files with 9 additions and 5 deletions

View File

@@ -1617,7 +1617,8 @@ func networkErrorAlert<R>(_ res: APIResult<R>) -> Alert? {
}
}
func acceptContactRequest(incognito: Bool, contactRequestId: Int64) async {
func acceptContactRequest(incognito: Bool, contactRequestId: Int64, inProgress: Binding<Bool>? = nil) async {
await MainActor.run { inProgress?.wrappedValue = true }
if let contact = await apiAcceptContactRequest(incognito: incognito, contactReqId: contactRequestId) {
let chat = Chat(chatInfo: ChatInfo.direct(contact: contact), chatItems: [])
await MainActor.run {
@@ -1627,6 +1628,7 @@ func acceptContactRequest(incognito: Bool, contactRequestId: Int64) async {
ChatModel.shared.replaceChat(contactRequestChatId(contactRequestId), chat)
}
NetworkModel.shared.setContactNetworkStatus(contact, .connected)
inProgress?.wrappedValue = false
}
if contact.sndReady {
let chatId = chat.id
@@ -1636,6 +1638,8 @@ func acceptContactRequest(incognito: Bool, contactRequestId: Int64) async {
}
}
}
} else {
await MainActor.run { inProgress?.wrappedValue = false }
}
}

View File

@@ -84,10 +84,8 @@ struct ContextContactRequestActionsView: View {
}
private func acceptRequest(incognito: Bool = false) {
inProgress = true
Task {
await acceptContactRequest(incognito: incognito, contactRequestId: contactRequestId)
await MainActor.run { inProgress = false }
await acceptContactRequest(incognito: incognito, contactRequestId: contactRequestId, inProgress: $inProgress)
}
}
}

View File

@@ -707,7 +707,6 @@ fun acceptContactRequest(
withBGApi {
inProgress?.value = true
val contact = chatModel.controller.apiAcceptContactRequest(rhId, incognito, contactRequestId)
inProgress?.value = false
if (contact != null && isCurrentUser) {
val chat = Chat(remoteHostId = rhId, ChatInfo.Direct(contact), listOf())
withContext(Dispatchers.Main) {
@@ -716,9 +715,12 @@ fun acceptContactRequest(
} else {
chatModel.chatsContext.replaceChat(rhId, contactRequestChatId(contactRequestId), chat)
}
inProgress?.value = false
}
chatModel.setContactNetworkStatus(contact, NetworkStatus.Connected())
close?.invoke(chat)
} else {
inProgress?.value = false
}
}
}