Replace publisher with delegate in StartConversationViewModel

This commit is contained in:
QuentinArguillere 2024-09-26 16:20:16 +02:00
parent 62ab791cd0
commit cb58b50e84

View file

@ -38,7 +38,7 @@ class StartConversationViewModel: ObservableObject {
@Published var operationInProgress: Bool = false
@Published var displayedConversation: ConversationModel?
private var chatRoomSuscriptions = Set<AnyCancellable?>()
private var chatRoomDelegate: ChatRoomDelegate?
init() {
coreContext.doOnCoreQueue { core in
@ -302,13 +302,31 @@ class StartConversationViewModel: ObservableObject {
}
func chatRoomAddDelegate(core: Core, chatRoom: ChatRoom) {
self.chatRoomSuscriptions.insert(chatRoom.publisher?.onConferenceJoined?.postOnCoreQueue { (chatRoom: ChatRoom, _: EventLog) in
self.chatRoomDelegate = ChatRoomDelegateStub(onStateChanged: { (chatRoom: ChatRoom, state: ChatRoom.State) in
let state = chatRoom.state
let id = LinphoneUtils.getChatRoomId(room: chatRoom)
if state == ChatRoom.State.CreationFailed {
Log.error("\(StartConversationViewModel.TAG) Conversation \(id) creation has failed!")
if let chatRoomDelegate = self.chatRoomDelegate {
chatRoom.removeDelegate(delegate: chatRoomDelegate)
}
self.chatRoomDelegate = nil
DispatchQueue.main.async {
self.operationInProgress = false
ToastViewModel.shared.toastMessage = "Failed_to_create_conversation_error"
ToastViewModel.shared.displayToast = true
}
}
}, onConferenceJoined: { (chatRoom: ChatRoom, _: EventLog) in
let state = chatRoom.state
let id = LinphoneUtils.getChatRoomId(room: chatRoom)
Log.info("\(StartConversationViewModel.TAG) Conversation \(id) \(chatRoom.subject ?? "") state changed: \(state)")
if state == ChatRoom.State.Created {
Log.info("\(StartConversationViewModel.TAG) Conversation \(id) successfully created")
self.chatRoomSuscriptions.removeAll()
if let chatRoomDelegate = self.chatRoomDelegate {
chatRoom.removeDelegate(delegate: chatRoomDelegate)
}
self.chatRoomDelegate = nil
let model = ConversationModel(chatRoom: chatRoom)
if self.operationInProgress == false {
@ -328,21 +346,10 @@ class StartConversationViewModel: ObservableObject {
}
} else if state == ChatRoom.State.CreationFailed {
Log.error("\(StartConversationViewModel.TAG) Conversation \(id) creation has failed!")
self.chatRoomSuscriptions.removeAll()
DispatchQueue.main.async {
self.operationInProgress = false
ToastViewModel.shared.toastMessage = "Failed_to_create_conversation_error"
ToastViewModel.shared.displayToast = true
}
}
})
self.chatRoomSuscriptions.insert(chatRoom.publisher?.onStateChanged?.postOnCoreQueue { (chatRoom: ChatRoom, state: ChatRoom.State) in
let state = chatRoom.state
let id = LinphoneUtils.getChatRoomId(room: chatRoom)
if state == ChatRoom.State.CreationFailed {
Log.error("\(StartConversationViewModel.TAG) Conversation \(id) creation has failed!")
self.chatRoomSuscriptions.removeAll()
if let chatRoomDelegate = self.chatRoomDelegate {
chatRoom.removeDelegate(delegate: chatRoomDelegate)
}
self.chatRoomDelegate = nil
DispatchQueue.main.async {
self.operationInProgress = false
ToastViewModel.shared.toastMessage = "Failed_to_create_conversation_error"
@ -350,6 +357,7 @@ class StartConversationViewModel: ObservableObject {
}
}
})
chatRoom.addDelegate(delegate: self.chatRoomDelegate!)
}
public static func isEndToEndEncryptionMandatory() -> Bool {