| 1 | #if DEBUG && os(iOS) && targetEnvironment(simulator) |
| 2 | import SwiftUI |
| 3 | import UIKit |
| 4 | |
| 5 | /// Real system share sheet, real extension and its keychain grants, with a |
| 6 | /// disposable recipient and transport. Cannot run on a physical iPhone. |
| 7 | public struct ShareFixture: View { |
| 8 | @State private var sharing = false |
| 9 | @State private var file: URL? |
| 10 | let mode = ProcessInfo.processInfo.environment["EQUIPO_SHARE_FIXTURE"] ?? "compose" |
| 11 | public init() {} |
| 12 | public var body: some View { |
| 13 | NavigationStack { |
| 14 | VStack(spacing: 20) { |
| 15 | Image(systemName: "square.and.arrow.up").font(.largeTitle) |
| 16 | Button(EquipoStrings.text("native.share.title")) { sharing = true } |
| 17 | .buttonStyle(.borderedProminent).disabled(file == nil) |
| 18 | }.navigationTitle(EquipoStrings.text("app.name")) |
| 19 | }.task { configure() } |
| 20 | .sheet(isPresented: $sharing) { if let file { ShareHost(items: [file, "The sign-in button is cut off on my phone."]) } } |
| 21 | } |
| 22 | private func configure() { |
| 23 | let accounts = DeviceAccounts(store: KeychainSecretStore.shared) |
| 24 | let origin = URL(string: "https://share-fixture.invalid")! |
| 25 | var account = accounts.all().first { $0.origin == origin } ?? DeviceAccount.new(origin: origin, label: "Fixture") |
| 26 | account.address = "paul@example.com"; account.teamName = "Oficina" |
| 27 | account.credential = DeviceCredential(id: "fixture", token: "fixture-share", audience: "equipo.person.v1", instanceId: "fixture", expiresAt: Date().addingTimeInterval(3600)) |
| 28 | do { |
| 29 | try accounts.commit(.save(account), lifetime: accounts.lifetimes.current) |
| 30 | try accounts.commit(.selectForSharing(account), lifetime: accounts.lifetimes.current) |
| 31 | if mode == "signed-out" { try accounts.commit(.remove(account), lifetime: accounts.lifetimes.current) } |
| 32 | let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] |
| 33 | let large = docs.appendingPathComponent("large.heic") |
| 34 | if mode == "large", FileManager.default.fileExists(atPath: large.path) { file = large } |
| 35 | else { |
| 36 | let path = docs.appendingPathComponent("Home.png") |
| 37 | // Fixture bytes only. Persistent secrets still use commit above. |
| 38 | try ShareFixtureFiles.save(AttachmentsFixture.image, to: path) |
| 39 | file = path |
| 40 | } |
| 41 | } catch { |
| 42 | if case KeychainSecretStore.StoreError.keychain(let status) = error { NSLog("Share fixture keychain status=%d", status) } |
| 43 | file = nil |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | private struct ShareHost: UIViewControllerRepresentable { |
| 48 | let items: [Any] |
| 49 | func makeUIViewController(context: Context) -> UIActivityViewController { UIActivityViewController(activityItems: items, applicationActivities: nil) } |
| 50 | func updateUIViewController(_ view: UIActivityViewController, context: Context) {} |
| 51 | } |
| 52 | #endif |
| 53 | |