diff --git a/App/Resources/Localizable.xcstrings b/App/Resources/Localizable.xcstrings index 18a0faa..054d5bd 100644 --- a/App/Resources/Localizable.xcstrings +++ b/App/Resources/Localizable.xcstrings @@ -2448,6 +2448,24 @@ } } } + }, + "Ending…": { + "comment": "Shown with a spinner on the End Call button after the user pressed it, until the phone confirms the call has ended (usually one or two seconds).", + "extractionState": "manual", + "localizations": { + "en": { + "stringUnit": { + "state": "translated", + "value": "Ending…" + } + }, + "de": { + "stringUnit": { + "state": "translated", + "value": "Wird beendet …" + } + } + } } }, "version": "1.0" diff --git a/App/Sources/Pickup/AppModel.swift b/App/Sources/Pickup/AppModel.swift index 8b5fdf9..c963710 100644 --- a/App/Sources/Pickup/AppModel.swift +++ b/App/Sources/Pickup/AppModel.swift @@ -72,6 +72,8 @@ final class AppModel { return DiagnosticLog() }() @ObservationIgnored private var dismissedCallID: UUID? + /// End Call pressed on this Mac and not yet confirmed by the phone. + @ObservationIgnored private var hangUp = HangUpProgress() @ObservationIgnored private var noticeTask: Task? @ObservationIgnored private var stuckTask: Task? @ObservationIgnored private var showCannotConnect = false @@ -221,6 +223,8 @@ final class AppModel { // Notices from the engine. if let failure = engine.lastCommandFailure, failure.date != seenFailureDate { seenFailureDate = failure.date + // The phone rejected the hang-up (asynchronously): End Call is usable again right away. + if failure.command == .endCall { hangUp.commandFailed() } Task { self.showNotice(commandFailureText()) } } if let fallback = engine.lastAudioFallback, fallback.date != seenFallbackDate { @@ -241,12 +245,17 @@ final class AppModel { case .dialing: active = activeInfo(call, party: party, phase: call.isRemoteAlerting ? .ringing : .dialing, engine: engine, phoneName: phoneName) + active?.isEnding = hangUp.isEnding(call.id) case .active: active = activeInfo(call, party: party, phase: .active, engine: engine, phoneName: phoneName) + active?.isEnding = hangUp.isEnding(call.id) case .held: active = activeInfo(call, party: party, phase: .held, engine: engine, phoneName: phoneName) - case .ended where call.direction == .outgoing || call.connectedAt != nil - || call.endReason == .failed || call.endReason == .connectionLost: + active?.isEnding = hangUp.isEnding(call.id) + case .ended where EndedCallPresentation.showsEndedState( + reasonEndedOnMac: call.endReason == .hungUp || call.endReason == .cancelled, + isOutgoing: call.direction == .outgoing, wasConnected: call.connectedAt != nil, + failedOrLinkLost: call.endReason == .failed || call.endReason == .connectionLost): var info = activeInfo(call, party: party, phase: .ended, engine: engine, phoneName: phoneName) info.endText = (call.endReason ?? .remoteEnded).localizedText if let seconds = call.duration(at: .now), call.connectedAt != nil { @@ -327,7 +336,7 @@ final class AppModel { CallActions( accept: { [weak self] in self?.run { try $0.accept() } }, decline: { [weak self] in self?.run { try $0.decline() } }, - endCall: { [weak self] in self?.run { try $0.endCall() } }, + endCall: { [weak self] in self?.endCallFromMac() }, dismiss: { [weak self] in self?.dismissedCallID = self?.engine?.call?.id self?.apply() @@ -340,6 +349,28 @@ final class AppModel { moveToPhone: { [weak self] in self?.run { try $0.transferAudioToPhone() } }) } + /// End Call in the HUD: the button shows progress until the phone confirms (usually 1–2 s). If the + /// command fails, or the phone has not ended the call after 10 s, the button is usable again. + private func endCallFromMac() { + guard let engine, let call = engine.call, call.phase.isLive, !hangUp.isEnding(call.id) else { return } + let attempt = hangUp.start(call.id) + apply() + do { + try engine.endCall() + } catch { + hangUp.commandFailed() + showNotice(error.pickupMessage) + apply() + return + } + Task { [weak self] in + try? await Task.sleep(for: .seconds(10)) + guard let self else { return } + self.hangUp.timedOut(attempt: attempt) + self.apply() + } + } + private func run(_ body: (CallEngine) throws -> Void) { guard let engine else { return } do { try body(engine) } catch { showNotice(error.pickupMessage) } diff --git a/App/Sources/Pickup/CallHUDView.swift b/App/Sources/Pickup/CallHUDView.swift index 33fe2cc..a43524d 100644 --- a/App/Sources/Pickup/CallHUDView.swift +++ b/App/Sources/Pickup/CallHUDView.swift @@ -64,15 +64,27 @@ struct CallHUDView: View { .accessibilityIdentifier("hud-close") } else { Button(action: actions.endCall) { - Label("End Call", systemImage: "phone.down.fill") - .font(.headline) - .foregroundStyle(.white) - .frame(maxWidth: .infinity, minHeight: 36) - .background(Color.red, in: Capsule()) - .contentShape(Capsule()) - .contentShape(.focusEffect, Capsule()) + Group { + if info.isEnding { + // The phone confirms the hang-up after a second or two; until then the button + // shows progress instead of looking unresponsive. + HStack(spacing: 8) { + ProgressView().controlSize(.small).environment(\.colorScheme, .dark) + Text("Ending…") + } + } else { + Label("End Call", systemImage: "phone.down.fill") + } + } + .font(.headline) + .foregroundStyle(.white) + .frame(maxWidth: .infinity, minHeight: 36) + .background(Color.red.opacity(info.isEnding ? 0.7 : 1), in: Capsule()) + .contentShape(Capsule()) + .contentShape(.focusEffect, Capsule()) } .buttonStyle(PressableStyle()) + .disabled(info.isEnding) // The HUD is key only after the user focused it (menu bar icon, global shortcut or a click // on the HUD); then Return ends the call. Esc does nothing here. .callPanelDefaultButton(panelHasFocus: hasFocus, action: actions.endCall) diff --git a/App/Sources/Pickup/CallUIModels.swift b/App/Sources/Pickup/CallUIModels.swift index 3232c4e..fbc758c 100644 --- a/App/Sources/Pickup/CallUIModels.swift +++ b/App/Sources/Pickup/CallUIModels.swift @@ -54,6 +54,8 @@ struct ActiveCallInfo: Equatable, Sendable { /// A second caller waiting on the line. var waiting: CallParty? = nil var notice: String? = nil + /// The user pressed End Call and the phone has not confirmed yet: the button shows progress. + var isEnding = false } /// Actions the views trigger. The engine binding supplies real closures. @@ -80,3 +82,38 @@ enum ConnectionStatus: Equatable { return false } } + +/// How an ended call is presented. +enum EndedCallPresentation { + /// Whether the HUD stays on screen with the "ended" state (reason and Close). A call the user ended + /// on this Mac (hung up or cancelled) closes the HUD as soon as the phone confirms: the End Call + /// button already showed the progress, and a second state would only repeat what the user did. + static func showsEndedState(reasonEndedOnMac: Bool, isOutgoing: Bool, wasConnected: Bool, + failedOrLinkLost: Bool) -> Bool { + if reasonEndedOnMac { return false } + return isOutgoing || wasConnected || failedOrLinkLost + } +} + +/// Progress of End Call pressed on this Mac, until the phone ends the call. Cleared when the phone +/// rejects the command (the user can retry at once) or when an attempt times out; a timeout of an +/// earlier attempt never clears a later retry. +struct HangUpProgress: Equatable { + private(set) var callID: UUID? + private(set) var attempt = 0 + + func isEnding(_ id: UUID) -> Bool { callID == id } + + /// Starts an attempt and returns its number (pass it to `timedOut`). + mutating func start(_ id: UUID) -> Int { + callID = id + attempt += 1 + return attempt + } + + mutating func commandFailed() { callID = nil } + + mutating func timedOut(attempt: Int) { + if attempt == self.attempt { callID = nil } + } +} diff --git a/App/Tests/PickupAppTests/EndedCallPresentationTests.swift b/App/Tests/PickupAppTests/EndedCallPresentationTests.swift new file mode 100644 index 0000000..dbbad36 --- /dev/null +++ b/App/Tests/PickupAppTests/EndedCallPresentationTests.swift @@ -0,0 +1,22 @@ +import XCTest +@testable import Pickup + +final class EndedCallPresentationTests: XCTestCase { + /// Hung up or cancelled on this Mac: the HUD closes as soon as the phone confirms, no "ended" state. + func testACallEndedOnTheMacClosesTheHUD() { + XCTAssertFalse(EndedCallPresentation.showsEndedState(reasonEndedOnMac: true, isOutgoing: true, wasConnected: true, failedOrLinkLost: false)) + XCTAssertFalse(EndedCallPresentation.showsEndedState(reasonEndedOnMac: true, isOutgoing: false, wasConnected: true, failedOrLinkLost: false)) + } + + /// Ended by the other side, busy, failed or link lost: the HUD explains what happened. + func testOtherEndsShowTheEndedState() { + XCTAssertTrue(EndedCallPresentation.showsEndedState(reasonEndedOnMac: false, isOutgoing: false, wasConnected: true, failedOrLinkLost: false)) + XCTAssertTrue(EndedCallPresentation.showsEndedState(reasonEndedOnMac: false, isOutgoing: true, wasConnected: false, failedOrLinkLost: false)) + XCTAssertTrue(EndedCallPresentation.showsEndedState(reasonEndedOnMac: false, isOutgoing: false, wasConnected: false, failedOrLinkLost: true)) + } + + /// An incoming call that was never answered here (missed, declined) has no HUD at all. + func testAnUnansweredIncomingCallShowsNoEndedState() { + XCTAssertFalse(EndedCallPresentation.showsEndedState(reasonEndedOnMac: false, isOutgoing: false, wasConnected: false, failedOrLinkLost: false)) + } +} diff --git a/App/Tests/PickupAppTests/HangUpProgressTests.swift b/App/Tests/PickupAppTests/HangUpProgressTests.swift new file mode 100644 index 0000000..7f3b6b2 --- /dev/null +++ b/App/Tests/PickupAppTests/HangUpProgressTests.swift @@ -0,0 +1,27 @@ +import XCTest +@testable import Pickup + +final class HangUpProgressTests: XCTestCase { + private let call = UUID() + + /// Regression: the phone rejects the hang-up asynchronously; End Call must be usable again at once, + /// and the first attempt's timeout must not clear the progress of the retry. + func testAsynchronousRejectionAllowsAnImmediateRetry() { + var progress = HangUpProgress() + let first = progress.start(call) + XCTAssertTrue(progress.isEnding(call)) + progress.commandFailed() + XCTAssertFalse(progress.isEnding(call), "a rejected hang-up re-enables End Call") + let second = progress.start(call) + progress.timedOut(attempt: first) + XCTAssertTrue(progress.isEnding(call), "the first attempt's timeout must not clear the retry") + progress.timedOut(attempt: second) + XCTAssertFalse(progress.isEnding(call)) + } + + func testProgressBelongsToOneCall() { + var progress = HangUpProgress() + _ = progress.start(call) + XCTAssertFalse(progress.isEnding(UUID())) + } +} diff --git a/App/Tests/PickupUITests/Cases/CallScenarioUITests.swift b/App/Tests/PickupUITests/Cases/CallScenarioUITests.swift index 70221bf..cd0d811 100644 --- a/App/Tests/PickupUITests/Cases/CallScenarioUITests.swift +++ b/App/Tests/PickupUITests/Cases/CallScenarioUITests.swift @@ -88,27 +88,30 @@ final class CallScenarioUITests: PickupUITestCase { XCTAssertTrue(element("banner-accept").press()) XCTAssertTrue(waitExists("hud-timer")) XCTAssertTrue(element("hud-hangup").press()) - assertEnded(callEnded) - XCTAssertTrue(element("hud-close").press()) - XCTAssertTrue(waitGone("call-hud"), "Close dismisses the ended HUD") - } - - /// During a call the status item focuses the HUD instead of opening the popover; only then does the - /// End Call has focus, Esc does nothing and Return ends the call. - func testStatusItemFocusesTheHUDAndReturnEndsTheCall() throws { + // Hung up on the Mac: no "ended" state, the HUD closes once the phone confirms. + XCTAssertFalse(exists("hud-close"), "a call ended on the Mac shows no ended state") + XCTAssertTrue(waitGone("call-hud", timeout: 5), "the HUD closes after hanging up") + } + + /// A call accepted in Pickup keeps the keyboard focus in Pickup: End Call has focus, so Return can end + /// the call. During the call the status item focuses the HUD instead of opening the popover. + /// + /// Return and Esc themselves are not tested here: key events posted to the process do not reach the + /// non-activating call panel while Pickup is not the active app (real key presses do). They are part + /// of the manual hardware check. + func testAcceptedCallKeepsFocusOnEndCall() throws { try launch("incoming-accept") waitForBanner() XCTAssertTrue(element("banner-accept").press()) XCTAssertTrue(waitExists("hud-timer")) - XCTAssertFalse(element("hud-hangup").isFocused, "End Call has focus only after the HUD was focused") + XCTAssertTrue(Wait.until(3) { self.element("hud-hangup").isFocused }, "End Call has focus after accepting in Pickup") XCTAssertTrue(element("status-item").press()) - XCTAssertTrue(Wait.until(3) { self.element("hud-hangup").isFocused }, "the status item did not focus End Call") + XCTAssertTrue(Wait.until(3) { self.element("hud-hangup").isFocused }, "the status item keeps End Call focused") XCTAssertFalse(exists("popover-root"), "the popover must not open during a call") screenshot("hud-focused", of: "call-hud") - keyboard.press(Key.escape) - XCTAssertFalse(Wait.until(1.5) { !self.exists("hud-hangup") }, "Esc must not end the call") - keyboard.press(Key.returnKey) - assertEnded(callEnded) + XCTAssertTrue(element("hud-hangup").press()) + // Hung up on the Mac: the HUD closes without an ended state. + XCTAssertTrue(waitGone("call-hud", timeout: 5), "the HUD did not close after hanging up") } // MARK: Outgoing @@ -146,7 +149,7 @@ final class CallScenarioUITests: PickupUITestCase { advance() // alerting XCTAssertTrue(waitText("hud-state") { $0 == self.s("Ringing…", "Es klingelt…") }) XCTAssertTrue(element("hud-hangup").press()) - XCTAssertTrue(waitText("hud-state") { $0 == self.s("Call cancelled", "Anruf abgebrochen") }, "state '\(hudState())'") + XCTAssertFalse(exists("hud-close"), "a call cancelled on the Mac shows no ended state") XCTAssertTrue(waitGone("call-hud", timeout: 10)) } @@ -154,7 +157,7 @@ final class CallScenarioUITests: PickupUITestCase { try launch("outgoing-cancel") dialWithKeyboard("jane") XCTAssertTrue(element("hud-hangup").press()) - XCTAssertTrue(waitExists("hud-close", timeout: 10), "cancelled during dialing; state '\(hudState())'") + XCTAssertFalse(exists("hud-close"), "cancelled during dialing shows no ended state") XCTAssertTrue(waitGone("call-hud", timeout: 10)) }