-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathConnectionView.swift
92 lines (84 loc) · 1.96 KB
/
ConnectionView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// ConnectionView.swift
// visionOS
//
// Created by Saagar Jha on 10/9/23.
//
import AppleConnect
import Network
import SwiftUI
let service = "_\(Bundle.main.name.lowercased())._tcp"
struct ConnectionView: View {
@Binding
var remote: Remote?
@State
var endpoints: [NWEndpoint] = []
@Environment(\.openWindow)
var openWindow
var body: some View {
NavigationSplitView(
sidebar: {
let icons = ["macbook", "macstudio", "macpro.gen3", "desktopcomputer"]
let gridSize = 9
VStack(spacing: 20) {
ForEach(Array(1...gridSize), id: \.self) { row in
HStack(spacing: 20) {
ForEach(Array(1...gridSize), id: \.self) { column in
Image(systemName: icons[(row * gridSize + column) % icons.count])
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 100, height: 100)
}
}
}
}
.rotationEffect(.radians(0.5))
.toolbar(.hidden)
},
detail: {
VStack {
List(endpoints.indices, id: \.self) {
let endpoint = endpoints[$0]
switch endpoint {
case .service(let name, _, _, _):
Button(name) {
Task {
do {
let connection = try await Connection(endpoint: endpoint, key: Data())
var remote = Remote(connection: connection)
if try await remote.handshake() {
self.remote = remote
}
} catch {
}
}
}
default:
fatalError()
}
}
Text("To get started, open \(Bundle.main.name) on your Mac and select it from the list.")
.padding()
}
.navigationTitle("Available Macs")
.toolbar {
Button(action: {
openWindow(id: "logs")
}) {
Image(systemName: "doc.text.below.ecg")
}
}
}
)
.task {
do {
for try await endpoints in Connection.endpoints(forServiceType: service) {
self.endpoints = endpoints
}
} catch {}
}
}
}
#Preview {
ConnectionView(remote: .constant(nil))
}