feat(apple/M1+M3+M4): widgets, Live Activity, and Siri/Shortcuts intents
The extension-side + App Intents surface for design/apple-live-activities-and- widgets.md. The iOS-framework code (WidgetKit/ActivityKit/AppIntents) can't be compiled by the macOS `swift build` CI target and needs the Xcode widget- extension target that only exists once created in the GUI — see the checklist in the memory note. What macOS DID verify: HostEntity (AppIntents is available on macOS), the shared attribute/notification plumbing, and that nothing regressed (142 tests green). Shared (PunktfunkShared): - PunktfunkSessionAttributes (ActivityAttributes) — the one type app + extension share; gated os(iOS) (ActivityKit imports on macOS but its types are unavailable, so canImport would wrongly admit it). - EndStreamIntent (LiveActivityIntent) — posts .punktfunkEndActiveSession. - HostEntity + HostEntityQuery (AppEntity over the shared store) — the intent / widget-config parameter type; canImport(AppIntents), so macOS type-checks it. - New notifications: end-active-session, open-deep-link. M1 widget extension sources (Sources/PunktfunkWidgets/, NOT a SwiftPM target — `swift build` ignores the dir): - PunktfunkWidgetBundle (@main): HostsWidget + PunktfunkSessionLiveActivity. - HostsWidget (kind "PunktfunkHosts"): reads the shared-suite store, sorts by recency, deep-links each host; small/medium/accessory families; empty state. - SessionLiveActivity: Lock-Screen banner + Dynamic Island (elapsed timer, mode line, background countdown, End button). M3 controller (app, iOS): SessionActivityController owns the Activity lifecycle (request/update/end + launch orphan-sweep + staleDate); ContentView drives it from the model's phase/isBackgrounded/backgroundDeadline (which SessionModel now publishes), keeping ActivityKit out of the cross-platform model. M4 (app, iOS): ConnectToHost/WakeHost intents + AppShortcutsProvider; Connect routes via .punktfunkOpenDeepLink into the same onOpenURL router (one set of guards); Wake reuses the WoL path; End surfaced to Shortcuts too. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -55,6 +55,11 @@ struct ContentView: View {
|
||||
/// a live session is already up. Surfaced as an informational alert (distinct from the
|
||||
/// "Connection failed" one, which is for actual connect errors).
|
||||
@State private var deepLinkNotice: String?
|
||||
#if os(iOS)
|
||||
/// Owns the Live Activity for the running session (Lock Screen / Dynamic Island). Driven from
|
||||
/// the session model's published state below; iPhone/iPad only.
|
||||
@State private var liveActivity = SessionActivityController()
|
||||
#endif
|
||||
@State private var pairingTarget: StoredHost?
|
||||
/// A fresh `pair=required`/unknown host the user tapped: drives the choice between no-PIN
|
||||
/// delegated approval ("Request Access") and the SPAKE2 PIN ceremony (rule 3b).
|
||||
@@ -125,6 +130,9 @@ struct ContentView: View {
|
||||
.onAppear {
|
||||
seedDefaultModeIfNeeded()
|
||||
autoConnectIfAsked()
|
||||
#if os(iOS)
|
||||
SessionActivityController.sweepOrphans() // end any Activity a prior killed launch left
|
||||
#endif
|
||||
}
|
||||
// Deep links (widget quick-launch, Siri/Shortcuts): route into the SAME connect path a card
|
||||
// tap uses, so trust policy / WoL / the approval sheet all come along. Never starts a
|
||||
@@ -145,6 +153,38 @@ struct ContentView: View {
|
||||
break
|
||||
}
|
||||
}
|
||||
// Live Activity lifecycle, driven from the model's published state.
|
||||
.onChange(of: model.phase) { _, phase in
|
||||
switch phase {
|
||||
case .streaming:
|
||||
if let host = model.activeHost {
|
||||
liveActivity.begin(
|
||||
hostID: host.id, hostName: host.displayName,
|
||||
launchTitle: nil, // no live foreground-app title mid-session (v1)
|
||||
modeLine: currentModeLine(), startedAt: Date())
|
||||
}
|
||||
case .idle:
|
||||
liveActivity.end()
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
.onChange(of: model.isBackgrounded) { _, backgrounded in
|
||||
liveActivity.update {
|
||||
$0.stage = backgrounded ? .background : .streaming
|
||||
$0.backgroundDeadline = model.backgroundDeadline
|
||||
}
|
||||
}
|
||||
// The Live Activity's / Shortcuts' End button runs EndStreamIntent in-process, which posts
|
||||
// this — tear the session down deliberately (quit-close the host).
|
||||
.onReceive(NotificationCenter.default.publisher(for: .punktfunkEndActiveSession)) { _ in
|
||||
model.disconnect(deliberate: true)
|
||||
}
|
||||
// Connect App Intent (Siri/Shortcuts): route its punktfunk:// URL through the same handler
|
||||
// as a widget tap.
|
||||
.onReceive(NotificationCenter.default.publisher(for: .punktfunkOpenDeepLink)) { note in
|
||||
if let url = note.object as? URL { handleDeepLink(url) }
|
||||
}
|
||||
#endif
|
||||
.onChange(of: model.phase) { _, phase in
|
||||
switch phase {
|
||||
@@ -307,6 +347,25 @@ struct ContentView: View {
|
||||
}
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
/// The Live Activity mode line, e.g. "2560×1440 @120 · HEVC · HDR", from the live connection.
|
||||
private func currentModeLine() -> String {
|
||||
guard let c = model.connection else { return "" }
|
||||
let codec: String
|
||||
switch c.videoCodec {
|
||||
case .h264: codec = "H.264"
|
||||
case .hevc: codec = "HEVC"
|
||||
case .av1: codec = "AV1"
|
||||
case .pyrowave: codec = "PyroWave"
|
||||
}
|
||||
var line = "\(c.width)×\(c.height)"
|
||||
if c.refreshHz > 0 { line += " @\(c.refreshHz)" }
|
||||
line += " · \(codec)"
|
||||
if c.isHDR { line += " · HDR" }
|
||||
return line
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Route a `punktfunk://` deep link into the existing connect path. Rules (per design):
|
||||
/// unknown host → notice + no-op; a live session is up → ignore if it's the same host, else
|
||||
/// tell the user to end the current one first (NEVER tear down a live session on a background
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
// Siri / Shortcuts / Spotlight surface (design §M4). Deliberately thin: every action already has an
|
||||
// internal entry point — M0's deep-link router (connect / connect-and-launch), M3's in-process
|
||||
// end-session hook, and the existing Wake-on-LAN path — so these intents only wrap them.
|
||||
//
|
||||
// Gated os(iOS): the AppShortcutsProvider bundles `EndStreamIntent`, which is a LiveActivityIntent
|
||||
// (iPhone/iPad only). Connect/Wake themselves are plain AppIntents; they live here with the
|
||||
// provider rather than being split across platforms. `HostEntity` (the parameter type) is in
|
||||
// PunktfunkShared so the widget's configuration intent can share it.
|
||||
|
||||
#if os(iOS)
|
||||
import AppIntents
|
||||
import Foundation
|
||||
import PunktfunkKit
|
||||
|
||||
/// Load a full saved host (MACs, address) from the shared App-Group store by id — HostEntity only
|
||||
/// carries id + name.
|
||||
private func loadStoredHost(_ id: UUID) -> StoredHost? {
|
||||
guard let data = AppGroup.defaults.data(forKey: DefaultsKey.hosts),
|
||||
let hosts = try? JSONDecoder().decode([StoredHost].self, from: data)
|
||||
else { return nil }
|
||||
return hosts.first { $0.id == id }
|
||||
}
|
||||
|
||||
/// Start a session with a stored host (optionally launching a title). Foregrounds the app and
|
||||
/// routes through the SAME `.onOpenURL` path a widget tap uses — trust policy, WoL and the approval
|
||||
/// sheet all apply, and its guards (unknown host, already-streaming) hold.
|
||||
struct ConnectToHostIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Connect to Host"
|
||||
static let description = IntentDescription("Start a Punktfunk streaming session with a host.")
|
||||
static let openAppWhenRun = true
|
||||
|
||||
@Parameter(title: "Host") var host: HostEntity
|
||||
@Parameter(title: "Game ID", description: "Optional store id like steam:570")
|
||||
var launchID: String?
|
||||
|
||||
func perform() async throws -> some IntentResult {
|
||||
let url = DeepLink.connect(host: host.id, launchID: launchID).url
|
||||
await MainActor.run {
|
||||
NotificationCenter.default.post(name: .punktfunkOpenDeepLink, object: url)
|
||||
}
|
||||
return .result()
|
||||
}
|
||||
}
|
||||
|
||||
/// Wake a sleeping host (magic packet). No `openAppWhenRun` — usable in automations ("when I get
|
||||
/// home, wake the tower") without foregrounding the app.
|
||||
struct WakeHostIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Wake Host"
|
||||
static let description = IntentDescription("Send a Wake-on-LAN magic packet to a host.")
|
||||
|
||||
@Parameter(title: "Host") var host: HostEntity
|
||||
|
||||
func perform() async throws -> some IntentResult {
|
||||
guard let stored = loadStoredHost(host.id), !stored.wakeMacs.isEmpty else {
|
||||
throw IntentError.noWakeAddress
|
||||
}
|
||||
PunktfunkConnection.wakeOnLAN(macs: stored.wakeMacs, lastKnownIP: stored.address)
|
||||
return .result()
|
||||
}
|
||||
}
|
||||
|
||||
/// Errors surfaced to Siri/Shortcuts. `CustomLocalizedStringResourceConvertible` makes the message
|
||||
/// show as the intent's failure text.
|
||||
enum IntentError: Error, CustomLocalizedStringResourceConvertible {
|
||||
case noWakeAddress
|
||||
|
||||
var localizedStringResource: LocalizedStringResource {
|
||||
switch self {
|
||||
case .noWakeAddress:
|
||||
return "That host has no saved Wake-on-LAN address yet. Connect to it once so Punktfunk "
|
||||
+ "can learn it."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Zero-setup Siri / Spotlight phrases. Parameterized phrases resolve a `HostEntity` by name; stays
|
||||
/// well under the 10-shortcut cap.
|
||||
struct PunktfunkShortcuts: AppShortcutsProvider {
|
||||
static var appShortcuts: [AppShortcut] {
|
||||
AppShortcut(
|
||||
intent: ConnectToHostIntent(),
|
||||
phrases: [
|
||||
"Connect to \(\.$host) in \(.applicationName)",
|
||||
"Stream \(\.$host) with \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Connect", systemImageName: "play.tv.fill")
|
||||
AppShortcut(
|
||||
intent: WakeHostIntent(),
|
||||
phrases: [
|
||||
"Wake \(\.$host) with \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Wake Host", systemImageName: "power")
|
||||
AppShortcut(
|
||||
intent: EndStreamIntent(),
|
||||
phrases: [
|
||||
"End the \(.applicationName) stream",
|
||||
],
|
||||
shortTitle: "End Stream", systemImageName: "stop.fill")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,87 @@
|
||||
// Owns the ActivityKit Live Activity lifecycle for a streaming session (iPhone/iPad only). Driven
|
||||
// by ContentView from the session model's published state (phase / isBackgrounded / deadline) so
|
||||
// none of this leaks into the cross-platform SessionModel. Local updates only (`pushType: nil`) —
|
||||
// the app process is alive whenever there's a session to report, so there's no push token plumbing.
|
||||
//
|
||||
// Gated os(iOS): ActivityKit is iPhone/iPad only. Minimum deployment is iOS 17, so no @available
|
||||
// guards are needed (Activity has existed since 16.1).
|
||||
|
||||
#if os(iOS)
|
||||
import ActivityKit
|
||||
import Foundation
|
||||
import PunktfunkShared
|
||||
|
||||
@MainActor
|
||||
final class SessionActivityController {
|
||||
private var activity: Activity<PunktfunkSessionAttributes>?
|
||||
/// The last pushed state, so an update can mutate one field and keep the rest (notably
|
||||
/// `startedAt`, which the Lock-Screen timer ticks from).
|
||||
private var state: PunktfunkSessionAttributes.ContentState?
|
||||
|
||||
/// How far past the next expected update to mark the content stale — a frozen opt-out session
|
||||
/// then greys out instead of showing a lying clock.
|
||||
private static let staleWindow: TimeInterval = 90
|
||||
|
||||
var isActive: Bool { activity != nil }
|
||||
|
||||
/// End any Activity left over from a previous launch that was killed mid-session. Call once at
|
||||
/// app start (ContentView.onAppear).
|
||||
static func sweepOrphans() {
|
||||
Task {
|
||||
for activity in Activity<PunktfunkSessionAttributes>.activities {
|
||||
await activity.end(nil, dismissalPolicy: .immediate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Start the Live Activity for a freshly-streaming session. No-op if the user disabled Live
|
||||
/// Activities for the app, or one is already up.
|
||||
func begin(hostID: UUID, hostName: String, launchTitle: String?, modeLine: String, startedAt: Date) {
|
||||
guard ActivityAuthorizationInfo().areActivitiesEnabled, activity == nil else { return }
|
||||
let attributes = PunktfunkSessionAttributes(
|
||||
hostID: hostID, hostName: hostName, launchTitle: launchTitle)
|
||||
let initial = PunktfunkSessionAttributes.ContentState(
|
||||
stage: .streaming, startedAt: startedAt, modeLine: modeLine)
|
||||
state = initial
|
||||
do {
|
||||
activity = try Activity.request(
|
||||
attributes: attributes,
|
||||
content: content(initial),
|
||||
pushType: nil)
|
||||
} catch {
|
||||
activity = nil
|
||||
state = nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Coalesced update: mutate the running state in place (keeps `startedAt` etc.) and push once.
|
||||
/// No-op when there's no live Activity.
|
||||
func update(_ mutate: (inout PunktfunkSessionAttributes.ContentState) -> Void) {
|
||||
guard let activity, var next = state else { return }
|
||||
mutate(&next)
|
||||
state = next
|
||||
Task { await activity.update(content(next)) }
|
||||
}
|
||||
|
||||
/// End with a final "ended" state, dismissed a few seconds later.
|
||||
func end() {
|
||||
guard let activity, var final = state else {
|
||||
self.activity = nil
|
||||
state = nil
|
||||
return
|
||||
}
|
||||
self.activity = nil
|
||||
state = nil
|
||||
final.stage = .ending
|
||||
final.backgroundDeadline = nil
|
||||
Task {
|
||||
await activity.end(content(final), dismissalPolicy: .after(.now + 4))
|
||||
}
|
||||
}
|
||||
|
||||
private func content(_ s: PunktfunkSessionAttributes.ContentState)
|
||||
-> ActivityContent<PunktfunkSessionAttributes.ContentState> {
|
||||
ActivityContent(state: s, staleDate: Date().addingTimeInterval(Self.staleWindow))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -152,6 +152,9 @@ final class SessionModel: ObservableObject {
|
||||
/// (audio plays, video dropped, timeout armed). Drives the Live Activity's stage/countdown (M3)
|
||||
/// and is cleared on foreground or teardown. iOS/iPadOS only in practice.
|
||||
@Published private(set) var isBackgrounded = false
|
||||
/// When the backgrounded keep-alive will auto-disconnect (nil unless backgrounded) — drives the
|
||||
/// Live Activity countdown. Set alongside `backgroundTimer`.
|
||||
@Published private(set) var backgroundDeadline: Date?
|
||||
/// Bounded auto-disconnect for a backgrounded keep-alive session. Fires on `.main`.
|
||||
private var backgroundTimer: DispatchSourceTimer?
|
||||
|
||||
@@ -353,6 +356,7 @@ final class SessionModel: ObservableObject {
|
||||
// Non-deliberate on fire (keep the host linger) so a user who returns late reconnects fast,
|
||||
// exactly like today's network-drop path. min 1 minute guards a nonsense setting.
|
||||
let minutes = max(1, timeoutMinutes)
|
||||
backgroundDeadline = Date().addingTimeInterval(TimeInterval(minutes * 60))
|
||||
let timer = DispatchSource.makeTimerSource(queue: .main)
|
||||
timer.schedule(deadline: .now() + .seconds(minutes * 60))
|
||||
timer.setEventHandler { [weak self] in
|
||||
@@ -370,6 +374,7 @@ final class SessionModel: ObservableObject {
|
||||
func exitBackground() {
|
||||
guard isBackgrounded else { return }
|
||||
isBackgrounded = false
|
||||
backgroundDeadline = nil
|
||||
backgroundTimer?.cancel()
|
||||
backgroundTimer = nil
|
||||
audio?.setMicMuted(false)
|
||||
@@ -400,6 +405,7 @@ final class SessionModel: ObservableObject {
|
||||
backgroundTimer?.cancel()
|
||||
backgroundTimer = nil
|
||||
isBackgrounded = false
|
||||
backgroundDeadline = nil
|
||||
let audio = self.audio
|
||||
self.audio = nil
|
||||
// Gamepad capture is main-actor (releases held buttons on the wire while the
|
||||
|
||||
Reference in New Issue
Block a user