| 1 | import SwiftUI |
| 2 | import RastrilloNative |
| 3 | |
| 4 | @main |
| 5 | struct Companion: App { |
| 6 | var body: some Scene { |
| 7 | WindowGroup { CompanionView() } |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | @MainActor |
| 12 | final class CompanionModel: ObservableObject { |
| 13 | @Published var origin = "https://your-app.example" |
| 14 | @Published var status = "Enter your app's address." |
| 15 | private let refresh = CoalescedRunner() |
| 16 | |
| 17 | func connect() async { |
| 18 | await refresh.run { [self] in |
| 19 | guard let base = URL(string: origin), base.scheme == "https", |
| 20 | base.host != nil, base.user == nil, base.password == nil, |
| 21 | base.query == nil, base.fragment == nil, |
| 22 | base.path.isEmpty || base.path == "/" else { |
| 23 | status = "Enter an HTTPS address without a path." |
| 24 | return |
| 25 | } |
| 26 | status = "Connecting…" |
| 27 | do { |
| 28 | let (_, response) = try await URLSession.shared.data(from: base.appendingPathComponent("api/version")) |
| 29 | guard let http = response as? HTTPURLResponse, http.statusCode == 200 else { |
| 30 | status = "Could not connect. Check the address and try again." |
| 31 | return |
| 32 | } |
| 33 | status = "Connected." |
| 34 | } catch { |
| 35 | status = "Could not connect. Check your connection and try again." |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | struct CompanionView: View { |
| 42 | @StateObject private var model = CompanionModel() |
| 43 | var body: some View { |
| 44 | Form { |
| 45 | TextField("App address", text: $model.origin) |
| 46 | .autocorrectionDisabled() |
| 47 | Button("Connect") { Task { await model.connect() } } |
| 48 | Text(model.status) |
| 49 | .accessibilityLabel(model.status) |
| 50 | } |
| 51 | .padding() |
| 52 | .frame(minWidth: 300, minHeight: 180) |
| 53 | } |
| 54 | } |
| 55 | |