Open source CapCut alternative.
TypeScript
feat(ios): the phone hands its takes to a paired Mac
David Lea8dad6e303c4
diff --git a/apps/ios/Donkey/DonkeyApp.swift b/apps/ios/Donkey/DonkeyApp.swiftindex b6b5f61c..9e99a5ef 100644--- a/apps/ios/Donkey/DonkeyApp.swift+++ b/apps/ios/Donkey/DonkeyApp.swift@@ -18,7 +18,8 @@ struct DonkeyApp: App {media: wiring.media,projects: wiring.projects,auth: wiring.auth,- analytics: wiring.analytics+ analytics: wiring.analytics,+ link: wiring.link) {CameraPreviewView(host: wiring.cameraController.previewView) {wiring.camera.toggleRecording()@@ -52,6 +53,7 @@ final class AppWiring {let projects: ProjectsModellet auth: AuthModellet analytics: AnalyticsModel+ let link: PhoneLinkModellet cameraController: CameraControllerlet watchLink: WatchLinkControllerlet sync: SyncEngine@@ -89,13 +91,19 @@ final class AppWiring {analytics = AnalyticsModel(service: cloud)projects = ProjectsModel(service: cloud)cameraController = CameraController()+ let link = PhoneLinkModel(tokens: PhoneLinkTokenStore())+ self.link = linknetworkMonitor = NetworkMonitor { sync.network = $0 }cameraController.model = cameracamera.controller = cameraControllerwatchLink = WatchLinkController(app: app, camera: camera, cameraController: cameraController)- cameraController.onRecordingFinished = { [media] url, duration, thumbnail in+ cameraController.onRecordingFinished = { [media, link] url, duration, thumbnail inmedia.ingest(movieAt: url, duration: duration, thumbnail: thumbnail)+ // The cloud queue takes its own copy on its own clock; this is the+ // take going straight to the Mac, which in the field is the only+ // path that resolves at all.+ Task { await link.drain(media) }}}}diff --git a/apps/ios/Donkey/Info.plist b/apps/ios/Donkey/Info.plistindex 5cf2ea68..9741e812 100644--- a/apps/ios/Donkey/Info.plist+++ b/apps/ios/Donkey/Info.plist@@ -17,6 +17,12 @@</array></dict></array>+ <key>NSLocalNetworkUsageDescription</key>+ <string>Donkey sends the video you shoot straight to your Mac over the local network.</string>+ <key>NSBonjourServices</key>+ <array>+ <string>_donkeycut._tcp</string>+ </array><key>CFBundleURLTypes</key><array><dict>diff --git a/apps/ios/Donkey/Support/PhoneLinkTokenStore.swift b/apps/ios/Donkey/Support/PhoneLinkTokenStore.swiftnew file mode 100644index 00000000..7409508d--- /dev/null+++ b/apps/ios/Donkey/Support/PhoneLinkTokenStore.swift@@ -0,0 +1,22 @@+import DonkeyKitModels+import Foundation++/// The Mac-link pairing, in the keychain beside the session token. The token is+/// what lets this phone put video on someone's Mac, so it is kept where the+/// account's own credentials are kept.+final class PhoneLinkTokenStore: PhoneLinkTokenStoring, @unchecked Sendable {+ private static let key = "phoneLinkPairing"++ func readPairing() -> PhoneLinkPairing? {+ guard let data = KeychainStore.read(Self.key) else { return nil }+ return try? JSONDecoder().decode(PhoneLinkPairing.self, from: data)+ }++ func writePairing(_ pairing: PhoneLinkPairing?) {+ guard let pairing, let data = try? JSONEncoder().encode(pairing) else {+ KeychainStore.delete(Self.key)+ return+ }+ KeychainStore.save(data, for: Self.key)+ }+}diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AppModel.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AppModel.swiftindex b31afd52..86cda6cb 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AppModel.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AppModel.swift@@ -32,6 +32,10 @@ public final class AppModel {/// The super-user analytics dashboard, opened from the avatar menu.public var showsAnalytics = false+ /// The Mac link sheet — pairing this phone with a Mac so field takes reach+ /// the editor with no network. Opened from the avatar menu.+ public var showsMacLink = false+private let defaults: UserDefaultsprivate static let appearanceKey = "appearancePreference"private static let mediaOnWiFiOnlyKey = "mediaOnWiFiOnly"diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/PhoneLink.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/PhoneLink.swiftnew file mode 100644index 00000000..940b0517--- /dev/null+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/PhoneLink.swift@@ -0,0 +1,418 @@+#if os(iOS)+import Foundation+import Network+import UIKit++/// The phone's end of the link to a Mac running Donkey: clips shot in the field+/// go straight to the Mac, with no Wi-Fi network, no router and no internet.+///+/// The transport is peer-to-peer Wi-Fi. `includePeerToPeer` browses the Bonjour+/// service over AWDL, the radio AirDrop uses, so the two devices find each other+/// on Bluetooth and then move the bytes at Wi-Fi speed. A minute of 4K is a few+/// hundred megabytes; Bluetooth alone would take the better part of an hour.+///+/// The link is a second way out of the library, beside the cloud queue the+/// SyncEngine drains. They are independent on purpose: a take goes to the Mac+/// the moment one is in range, and to the cloud the moment there is internet,+/// and neither waits for the other.++/// A Mac advertising the service, as the picker shows it.+nonisolated public struct PhoneLinkMac: Identifiable, Equatable, Sendable {+ public var id: String+ public var name: String+}++/// The Mac this phone is paired with. The token is what every later clip+/// carries; the Mac minted it when this phone answered its code.+nonisolated public struct PhoneLinkPairing: Equatable, Sendable, Codable {+ public var macName: String+ public var token: String+}++/// Where the pairing token is kept. The app target backs this with the keychain;+/// the package never reaches for one itself.+public protocol PhoneLinkTokenStoring: AnyObject, Sendable {+ func readPairing() -> PhoneLinkPairing?+ func writePairing(_ pairing: PhoneLinkPairing?)+}++nonisolated public enum PhoneLinkError: Error, Equatable, Sendable {+ case notPaired+ case macUnreachable+ /// The Mac answered, and said no.+ case refused(String)+ case fileUnreadable+}++@Observable+public final class PhoneLinkModel {+ /// The Macs in range right now, newest listing wins.+ public private(set) var macs: [PhoneLinkMac] = []+ /// The Mac this phone is paired with, or nil before anyone has paired.+ public private(set) var pairing: PhoneLinkPairing?+ /// The clip going over the link right now, 0…1, for the Library's badge.+ public private(set) var sending: (recordingId: UUID, progress: Double)?+ /// The last failure, for the pairing sheet to show.+ public private(set) var lastError: String?++ /// Recordings already handed to a Mac. Kept beside the cloud journal rather+ /// than in it: the two destinations are independent, and a clip that went+ /// over the link still owes the cloud a copy.+ @ObservationIgnored private var delivered: Set<UUID>+ /// Takes whose file could not be read this session. They stop being retried+ /// on every pass; a relaunch looks at them again, the way the cloud queue+ /// treats a clip it could not read.+ @ObservationIgnored private var skipped: Set<UUID> = []+ @ObservationIgnored private let defaults: UserDefaults+ @ObservationIgnored private let tokens: any PhoneLinkTokenStoring+ @ObservationIgnored private var browser: NWBrowser?+ @ObservationIgnored private var endpoints: [String: NWEndpoint] = [:]+ @ObservationIgnored private var draining = false++ private static let deliveredKey = "phoneLinkDelivered"++ public init(tokens: any PhoneLinkTokenStoring, defaults: UserDefaults = .standard) {+ self.tokens = tokens+ self.defaults = defaults+ self.pairing = tokens.readPairing()+ let stored = defaults.stringArray(forKey: Self.deliveredKey) ?? []+ self.delivered = Set(stored.compactMap(UUID.init(uuidString:)))+ }++ public var isPaired: Bool { pairing != nil }++ /// Whether this clip has already reached a Mac — the Library's badge.+ public func wasDelivered(_ id: UUID) -> Bool { delivered.contains(id) }++ public func unpair() {+ pairing = nil+ tokens.writePairing(nil)+ }++ // MARK: Finding Macs++ /// Browse for Macs. The camera screen and the pairing sheet hold this open+ /// while they are up; nothing browses in the background, where AWDL costs+ /// battery for a screen nobody is looking at.+ public func startBrowsing() {+ guard browser == nil else { return }+ let parameters = NWParameters()+ parameters.includePeerToPeer = true+ let browser = NWBrowser(+ for: .bonjour(type: PhoneLinkWire.serviceType, domain: nil),+ using: parameters+ )+ browser.browseResultsChangedHandler = { [weak self] results, _ in+ Task { @MainActor [weak self] in self?.apply(results) }+ }+ browser.start(queue: .main)+ self.browser = browser+ }++ public func stopBrowsing() {+ browser?.cancel()+ browser = nil+ }++ private func apply(_ results: Set<NWBrowser.Result>) {+ var found: [PhoneLinkMac] = []+ var byId: [String: NWEndpoint] = [:]+ for result in results {+ guard case let .service(name, _, _, _) = result.endpoint else { continue }+ found.append(PhoneLinkMac(id: name, name: name))+ byId[name] = result.endpoint+ }+ endpoints = byId+ macs = found.sorted { $0.name < $1.name }+ }++ // MARK: Pairing++ /// Answer the code the Mac's settings page is showing. On success the Mac+ /// hands back the token this phone keeps.+ public func pair(with macId: String, code: String) async {+ lastError = nil+ guard let endpoint = endpoints[macId] else {+ lastError = "That Mac is out of range."+ return+ }+ let header = PhoneLinkWire.Header(+ kind: .pair,+ code: code.trimmingCharacters(in: .whitespaces),+ deviceName: UIDeviceName.current()+ )+ do {+ let reply = try await PhoneLinkSession(endpoint: endpoint).send(header: header)+ guard reply.ok, let token = reply.token else {+ lastError = reply.error ?? "That Mac would not pair."+ return+ }+ let pairing = PhoneLinkPairing(macName: macId, token: token)+ self.pairing = pairing+ tokens.writePairing(pairing)+ } catch {+ lastError = "That Mac did not answer."+ }+ }++ // MARK: Sending clips++ /// Hand every take the paired Mac has not seen to it, oldest first, while+ /// one is in range. Returns quietly when nothing is paired or nothing is in+ /// range — the clips stay queued for the next time the Mac is there.+ ///+ /// A verdict about one clip is about that clip: a take the Mac would not+ /// have, or one whose file this phone cannot read, is skipped so the ones+ /// behind it still go. Only losing the Mac ends the pass, because from+ /// there nothing else would go either.+ public func drain(_ media: MediaModel) async {+ guard let pairing, !draining else { return }+ guard let endpoint = endpoints[pairing.macName] else { return }+ draining = true+ defer { draining = false }++ for recording in media.recordings.sorted(by: { $0.createdAt < $1.createdAt }) {+ guard !delivered.contains(recording.id), !skipped.contains(recording.id) else { continue }+ do {+ try await send(recording, from: media, to: endpoint, token: pairing.token)+ markDelivered(recording.id)+ } catch PhoneLinkError.refused(let message) {+ // The Mac took a look and said no — sending it again would get+ // the same answer, so it stops being offered.+ lastError = message+ markDelivered(recording.id)+ } catch PhoneLinkError.fileUnreadable {+ skipped.insert(recording.id)+ } catch {+ // Out of range mid-drain: the rest waits for the next pass.+ return+ }+ }+ }++ private func send(+ _ recording: Recording,+ from media: MediaModel,+ to endpoint: NWEndpoint,+ token: String+ ) async throws {+ let url = media.movieURL(for: recording)+ guard let size = (try? FileManager.default.attributesOfItem(atPath: url.path)[.size]) as? Int,+ size > 0 else {+ throw PhoneLinkError.fileUnreadable+ }+ let header = PhoneLinkWire.Header(+ kind: .clip,+ token: token,+ takeId: recording.id.uuidString,+ fileName: recording.fileName,+ bytes: size,+ // Milliseconds: the engine stamps its own clips with Date.now(), and+ // the two have to be comparable when the editor sorts the inbox.+ capturedAt: recording.createdAt.timeIntervalSince1970 * 1000,+ durationSeconds: recording.duration+ )+ sending = (recording.id, 0)+ defer { sending = nil }++ let reply = try await PhoneLinkSession(endpoint: endpoint).send(+ header: header,+ file: url,+ bytes: size+ ) { [weak self] fraction in+ Task { @MainActor [weak self] in+ self?.sending = (recording.id, fraction)+ }+ }+ guard reply.ok else { throw PhoneLinkError.refused(reply.error ?? "That Mac would not take the clip.") }+ }++ private func markDelivered(_ id: UUID) {+ delivered.insert(id)+ defaults.set(delivered.map(\.uuidString), forKey: Self.deliveredKey)+ }+}++/// The name the Mac files this phone under.+enum UIDeviceName {+ static func current() -> String {+ UIDevice.current.name+ }+}++// MARK: - Wire++/// The frame format both ends speak: a 4-byte big-endian header length, that+/// many bytes of JSON, then the body the header declares. One shape for pairing+/// and for clips, so a clip streams through without either end holding it whole.+nonisolated enum PhoneLinkWire {+ static let serviceType = "_donkeycut._tcp"++ enum Kind: String, Encodable, Sendable {+ case pair+ case clip+ }++ struct Header: Encodable, Sendable {+ var kind: Kind+ var code: String?+ var deviceName: String?+ var token: String?+ /// The take this clip is, stable across re-sends. Delivery is+ /// at-least-once — the bytes can land on the Mac and the+ /// acknowledgement be lost on the way back — so the Mac needs a way to+ /// know a second arrival is the copy it already has.+ var takeId: String?+ var fileName: String?+ var bytes: Int?+ var capturedAt: Double?+ var durationSeconds: Double?+ var width: Int?+ var height: Int?+ }++ struct Reply: Decodable, Sendable {+ var ok: Bool+ var error: String?+ var token: String?+ var deviceId: String?+ var clipId: String?+ }++ static func frame(_ header: Header) throws -> Data {+ let json = try JSONEncoder().encode(header)+ var out = Data()+ var length = UInt32(json.count).bigEndian+ withUnsafeBytes(of: &length) { out.append(contentsOf: $0) }+ out.append(json)+ return out+ }+}++/// Fires once and tells the caller whether it was the one that fired it.+private nonisolated final class PhoneLinkLatch: @unchecked Sendable {+ private let lock = NSLock()+ private var fired = false++ func fire() -> Bool {+ lock.lock()+ defer { lock.unlock() }+ if fired { return false }+ fired = true+ return true+ }+}++/// One conversation with a Mac: connect, send a frame, read the reply, hang up.+///+/// A clip is read off disk and sent a chunk at a time, each chunk waiting on the+/// last to go out, so the phone never holds a gigabyte take in memory and the+/// connection's own backpressure paces the shoot.+actor PhoneLinkSession {+ private let endpoint: NWEndpoint+ private static let chunkBytes = 512 * 1024+ /// The wait starts once the whole clip is across, so it covers the Mac+ /// moving the file into the inbox and nothing else. Long enough for a+ /// gigabyte take to land on a slow disk, short enough that a Mac that+ /// walked away is reported instead of hung on.+ private static let replyTimeout: Duration = .seconds(90)++ init(endpoint: NWEndpoint) {+ self.endpoint = endpoint+ }++ func send(+ header: PhoneLinkWire.Header,+ file: URL? = nil,+ bytes: Int = 0,+ progress: (@Sendable (Double) -> Void)? = nil+ ) async throws -> PhoneLinkWire.Reply {+ let parameters = NWParameters.tcp+ parameters.includePeerToPeer = true+ let connection = NWConnection(to: endpoint, using: parameters)+ defer { connection.cancel() }++ try await withCheckedThrowingContinuation { (done: CheckedContinuation<Void, Error>) in+ // Readiness and failure both arrive on the connection's own queue,+ // and a continuation may be resumed exactly once.+ let latch = PhoneLinkLatch()+ connection.stateUpdateHandler = { state in+ switch state {+ case .ready:+ if latch.fire() { done.resume() }+ case .failed, .cancelled:+ if latch.fire() { done.resume(throwing: PhoneLinkError.macUnreachable) }+ default:+ break+ }+ }+ connection.start(queue: .global(qos: .userInitiated))+ }++ try await write(connection, PhoneLinkWire.frame(header))++ if let file, bytes > 0 {+ let handle = try FileHandle(forReadingFrom: file)+ defer { try? handle.close() }+ var sent = 0+ while sent < bytes {+ let chunk = try handle.read(upToCount: Self.chunkBytes) ?? Data()+ if chunk.isEmpty { break }+ try await write(connection, chunk)+ sent += chunk.count+ progress?(Double(sent) / Double(bytes))+ }+ }++ return try await readReply(connection)+ }++ private func write(_ connection: NWConnection, _ data: Data) async throws {+ try await withCheckedThrowingContinuation { (done: CheckedContinuation<Void, Error>) in+ connection.send(content: data, completion: .contentProcessed { error in+ if error != nil {+ done.resume(throwing: PhoneLinkError.macUnreachable)+ } else {+ done.resume()+ }+ })+ }+ }++ /// Read the length prefix, then that many bytes of JSON.+ private func readReply(_ connection: NWConnection) async throws -> PhoneLinkWire.Reply {+ let lengthBytes = try await read(connection, exactly: 4)+ let length = Int(lengthBytes.reduce(UInt32(0)) { ($0 << 8) | UInt32($1) })+ guard length > 0, length <= 64 * 1024 else { throw PhoneLinkError.macUnreachable }+ let json = try await read(connection, exactly: length)+ guard let reply = try? JSONDecoder().decode(PhoneLinkWire.Reply.self, from: json) else {+ throw PhoneLinkError.macUnreachable+ }+ return reply+ }++ private func read(_ connection: NWConnection, exactly count: Int) async throws -> Data {+ try await withThrowingTaskGroup(of: Data.self) { group in+ group.addTask {+ try await withCheckedThrowingContinuation { (done: CheckedContinuation<Data, Error>) in+ connection.receive(minimumIncompleteLength: count, maximumLength: count) {+ data, _, _, error in+ if let data, data.count == count, error == nil {+ done.resume(returning: data)+ } else {+ done.resume(throwing: PhoneLinkError.macUnreachable)+ }+ }+ }+ }+ group.addTask {+ try await Task.sleep(for: Self.replyTimeout)+ throw PhoneLinkError.macUnreachable+ }+ guard let first = try await group.next() else { throw PhoneLinkError.macUnreachable }+ group.cancelAll()+ return first+ }+ }+}+#endifdiff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/Components.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/Components.swiftindex ddf5b036..5061afc6 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/Components.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/Components.swift@@ -51,6 +51,9 @@ struct AvatarMenu: View {Button("Analytics", systemImage: "chart.xyaxis.line") {app.showsAnalytics = true}+ Button("Mac Link", systemImage: "laptopcomputer.and.iphone") {+ app.showsMacLink = true+ }}Divider()Button("Log Out", systemImage: "rectangle.portrait.and.arrow.right", role: .destructive) {diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MacLinkSheet.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MacLinkSheet.swiftnew file mode 100644index 00000000..d20c7467--- /dev/null+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MacLinkSheet.swift@@ -0,0 +1,140 @@+#if os(iOS)+import DonkeyKitModels+import SwiftUI++/// Pairing this phone with a Mac, and what the link is doing once it is paired.+///+/// The Mac mints the code — Donkey Cut's settings, under Super user — and this+/// screen is where it gets typed. After that the phone sends every take it+/// shoots to that Mac whenever one is in range, and this screen is only there+/// to show which Mac and to break the pairing.+struct MacLinkSheet: View {+ var link: PhoneLinkModel+ var media: MediaModel+ /// Whether the browse stays on once this sheet closes — the camera screen+ /// wants it, and nothing else does.+ var onDismiss: () -> Void++ @Environment(\.dismiss) private var dismiss+ @State private var selectedMac: String?+ @State private var code = ""+ @State private var pairing = false+ @FocusState private var codeFocused: Bool++ var body: some View {+ NavigationStack {+ Form {+ if let paired = link.pairing {+ pairedSection(paired)+ } else {+ pairingSection+ }+ }+ .navigationTitle("Mac Link")+ .navigationBarTitleDisplayMode(.inline)+ .toolbar {+ ToolbarItem(placement: .confirmationAction) {+ Button("Done") { dismiss() }+ }+ }+ }+ .task {+ link.startBrowsing()+ }+ .onDisappear {+ // The camera screen holds its own browse while a shoot is on; this+ // one only held it for the picker, and a browse nobody is watching+ // is AWDL running for a screen that is gone.+ onDismiss()+ }+ }++ @ViewBuilder+ private func pairedSection(_ paired: PhoneLinkPairing) -> some View {+ Section {+ LabeledContent("Mac", value: paired.macName)+ LabeledContent("In range", value: inRange ? "Yes" : "No")+ LabeledContent("Waiting", value: "\(undelivered) \(undelivered == 1 ? "clip" : "clips")")+ } header: {+ Text("Paired")+ } footer: {+ Text("Clips go to this Mac over Wi-Fi Direct whenever it is nearby, with or without a network. They still upload to the cloud separately once there is internet.")+ }++ Section {+ Button("Send waiting clips now") {+ Task { await link.drain(media) }+ }+ .disabled(!inRange || undelivered == 0)+ Button("Unpair", role: .destructive) {+ link.unpair()+ }+ }+ }++ @ViewBuilder+ private var pairingSection: some View {+ Section {+ if link.macs.isEmpty {+ HStack {+ ProgressView()+ Text("Looking for Macs…").foregroundStyle(.secondary)+ }+ }+ ForEach(link.macs) { mac in+ Button {+ selectedMac = mac.id+ codeFocused = true+ } label: {+ HStack {+ Label(mac.name, systemImage: "laptopcomputer")+ Spacer()+ if selectedMac == mac.id {+ Image(systemName: "checkmark").foregroundStyle(.tint)+ }+ }+ }+ .tint(.primary)+ }+ } header: {+ Text("Macs nearby")+ } footer: {+ Text("Both devices need Wi-Fi and Bluetooth on. They do not need to be on a network.")+ }++ Section {+ TextField("000-000", text: $code)+ .keyboardType(.numbersAndPunctuation)+ .textContentType(.oneTimeCode)+ .focused($codeFocused)+ Button(pairing ? "Pairing…" : "Pair") {+ guard let selectedMac else { return }+ pairing = true+ Task {+ await link.pair(with: selectedMac, code: code)+ pairing = false+ if link.pairing != nil { code = "" }+ }+ }+ .disabled(selectedMac == nil || code.count < 6 || pairing)+ } header: {+ Text("Pairing code")+ } footer: {+ if let error = link.lastError {+ Text(error).foregroundStyle(.red)+ } else {+ Text("On the Mac, open Donkey Cut settings → Profile → Super user → Phone link, and start pairing.")+ }+ }+ }++ private var inRange: Bool {+ guard let paired = link.pairing else { return false }+ return link.macs.contains { $0.id == paired.macName }+ }++ private var undelivered: Int {+ media.recordings.count { !link.wasDelivered($0.id) }+ }+}+#endifdiff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/RootView.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/RootView.swiftindex e9e5b7b6..922811f3 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/RootView.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/RootView.swift@@ -14,6 +14,7 @@ public struct RootView<CameraPreview: View>: View {var projects: ProjectsModelvar auth: AuthModelvar analytics: AnalyticsModel+ var link: PhoneLinkModellet cameraPreview: () -> CameraPreview@Environment(\.scenePhase) private var scenePhase@@ -26,6 +27,7 @@ public struct RootView<CameraPreview: View>: View {projects: ProjectsModel,auth: AuthModel,analytics: AnalyticsModel,+ link: PhoneLinkModel,@ViewBuilder cameraPreview: @escaping () -> CameraPreview) {self.app = app@@ -35,6 +37,7 @@ public struct RootView<CameraPreview: View>: View {self.projects = projectsself.auth = authself.analytics = analytics+ self.link = linkself.cameraPreview = cameraPreview}@@ -71,6 +74,13 @@ public struct RootView<CameraPreview: View>: View {.tint(.accentBlue).preferredColorScheme(app.appearance.colorScheme)}+ .sheet(isPresented: $app.showsMacLink) {+ MacLinkSheet(link: link, media: media) {+ if !cameraShouldRun { link.stopBrowsing() }+ }+ .tint(.accentBlue)+ .preferredColorScheme(app.appearance.colorScheme)+ }.task { await auth.restore() }// Notes and folders are written at the desk as well as here, so the// phone looks at the cloud on its own clock for as long as it is on@@ -105,6 +115,7 @@ public struct RootView<CameraPreview: View>: View {.onChange(of: auth.isSignedIn) { _, signedIn inguard signedIn else {app.showsAnalytics = false+ app.showsMacLink = false// The listing on disk belongs to the account that just left.projects.forget()return@@ -115,10 +126,16 @@ public struct RootView<CameraPreview: View>: View {.onChange(of: cameraShouldRun) { _, run inif run {camera.appeared()+ link.startBrowsing()} else {camera.disappeared()+ if !app.showsMacLink { link.stopBrowsing() }}}+ // A Mac coming into range mid-shoot is the moment the queue drains.+ .onChange(of: link.macs) { _, _ in+ Task { await link.drain(media) }+ }}private var cameraShouldRun: Bool {