From fa22b98b3f0a70166ea7e086135f0771cfb8767c Mon Sep 17 00:00:00 2001 From: Mauro <34335419+Velin92@users.noreply.github.com> Date: Mon, 24 Jun 2024 18:51:43 +0200 Subject: [PATCH] Improve UX when answering EL-Call through the notification (#2965) --- .../Sources/Application/AppCoordinator.swift | 18 ++++++++++-------- .../ElementCall/ElementCallService.swift | 13 +++++++++++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/ElementX/Sources/Application/AppCoordinator.swift b/ElementX/Sources/Application/AppCoordinator.swift index 5f4b985c90..1d56181c26 100644 --- a/ElementX/Sources/Application/AppCoordinator.swift +++ b/ElementX/Sources/Application/AppCoordinator.swift @@ -145,15 +145,17 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg registerBackgroundAppRefresh() - elementCallService.actions.sink { [weak self] action in - switch action { - case .answerCall(let roomID): - self?.handleAppRoute(.call(roomID: roomID)) - case .declineCall: - break + elementCallService.actions + .receive(on: DispatchQueue.main) + .sink { [weak self] action in + switch action { + case .answerCall(let roomID): + self?.handleAppRoute(.call(roomID: roomID)) + case .declineCall: + break + } } - } - .store(in: &cancellables) + .store(in: &cancellables) } func start() { diff --git a/ElementX/Sources/Services/ElementCall/ElementCallService.swift b/ElementX/Sources/Services/ElementCall/ElementCallService.swift index 7a8faa0fb9..19576cfde5 100644 --- a/ElementX/Sources/Services/ElementCall/ElementCallService.swift +++ b/ElementX/Sources/Services/ElementCall/ElementCallService.swift @@ -30,6 +30,8 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe private var incomingCallRoomID: String? + private var endUnansweredCallTask: Task? + private let actionsSubject: PassthroughSubject = .init() var actions: AnyPublisher { actionsSubject.eraseToAnyPublisher() @@ -48,7 +50,7 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe guard ongoingCallID == nil else { return } - + let callID = UUID() ongoingCallID = callID @@ -103,6 +105,8 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe let configuration = CXProviderConfiguration() configuration.supportsVideo = true configuration.includesCallsInRecents = true + // Provide image icon if available + configuration.iconTemplateImageData = nil // https://stackoverflow.com/a/46077628/730924 configuration.supportedHandleTypes = [.generic] @@ -125,8 +129,12 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe completion() } - Task { [weak self, callProvider, callID] in + endUnansweredCallTask = Task { [weak self, callProvider, callID] in try? await Task.sleep(for: .seconds(15)) + guard !Task.isCancelled else { + return + } + if let ongoingCallID = self?.ongoingCallID, ongoingCallID == callID { callProvider.reportCall(with: callID, endedAt: .now, reason: .unanswered) } @@ -146,6 +154,7 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe actionsSubject.send(.answerCall(roomID: incomingCallRoomID)) } self.incomingCallRoomID = nil + endUnansweredCallTask?.cancel() } else { MXLog.error("Failed answering incoming call, missing room ID") }