Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions App/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 34 additions & 3 deletions App/Sources/Pickup/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void, Never>?
@ObservationIgnored private var stuckTask: Task<Void, Never>?
@ObservationIgnored private var showCannotConnect = false
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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) }
Expand Down
26 changes: 19 additions & 7 deletions App/Sources/Pickup/CallHUDView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions App/Sources/Pickup/CallUIModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 }
}
}
22 changes: 22 additions & 0 deletions App/Tests/PickupAppTests/EndedCallPresentationTests.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}
27 changes: 27 additions & 0 deletions App/Tests/PickupAppTests/HangUpProgressTests.swift
Original file line number Diff line number Diff line change
@@ -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()))
}
}
35 changes: 19 additions & 16 deletions App/Tests/PickupUITests/Cases/CallScenarioUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -146,15 +149,15 @@ 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))
}

func testCancelDuringDialingBeforeTheRemoteSideAlerts() throws {
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))
}

Expand Down
Loading