mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Clean up spaces
PiperOrigin-RevId: 521464768
This commit is contained in:
committed by
Copybara-Service
parent
44fb4a00a2
commit
78d5246060
@@ -20,7 +20,7 @@ struct ConnectedView: View {
|
||||
var connection: ConnectedEndpoint
|
||||
var onSendBytes : () -> ()
|
||||
var onDisconnect : () -> ()
|
||||
|
||||
|
||||
var body: some View {
|
||||
Section("Connection Actions") {
|
||||
Button("Send Bytes") {
|
||||
|
||||
@@ -20,9 +20,9 @@ struct ConnectionRequestView: View {
|
||||
var connectionRequest: ConnectionRequest
|
||||
var onAcceptConnection : () -> ()
|
||||
var onRejectConnection : () -> ()
|
||||
|
||||
|
||||
@State private var hasResponded = false
|
||||
|
||||
|
||||
var body: some View {
|
||||
Section(footer: Text("Security code: \(connectionRequest.pin)")) {
|
||||
Button("Accept") {
|
||||
|
||||
@@ -19,7 +19,7 @@ import NearbyConnections
|
||||
|
||||
struct ContentView: View {
|
||||
@EnvironmentObject private var model: Model
|
||||
|
||||
|
||||
let strategies: [Strategy: String] = [
|
||||
.cluster: "Cluster",
|
||||
.pointToPoint: "Point to Point",
|
||||
|
||||
+2
-2
@@ -19,9 +19,9 @@ import SwiftUI
|
||||
struct DiscoveredEndpointView: View {
|
||||
var endpoint: DiscoveredEndpoint
|
||||
var onRequestConnection : () -> ()
|
||||
|
||||
|
||||
@State private var hasResponded = false
|
||||
|
||||
|
||||
var body: some View {
|
||||
Section {
|
||||
Button("Connect") {
|
||||
|
||||
@@ -18,22 +18,22 @@ import SwiftUI
|
||||
|
||||
struct EndpointDetail: View {
|
||||
var endpointID: String
|
||||
|
||||
|
||||
@EnvironmentObject private var model: Model
|
||||
|
||||
|
||||
var body: some View {
|
||||
let endpoint = model.endpoints.first { $0.id == endpointID }
|
||||
let connectionRequest = model.requests.first { $0.id == endpointID }
|
||||
let connection = model.connections.first { $0.id == endpointID }
|
||||
let name = connectionRequest?.endpointName ?? connection?.endpointName ?? endpoint?.endpointName
|
||||
|
||||
|
||||
Form {
|
||||
if let endpoint {
|
||||
DiscoveredEndpointView(endpoint: endpoint, onRequestConnection: {
|
||||
model.requestConnection(to: endpointID)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
if let connectionRequest {
|
||||
ConnectionRequestView(connectionRequest: connectionRequest, onAcceptConnection: {
|
||||
connectionRequest.shouldAccept(true)
|
||||
|
||||
@@ -19,7 +19,7 @@ import SwiftUI
|
||||
@main
|
||||
struct HelloConnectionsApp: App {
|
||||
@StateObject private var model = Model()
|
||||
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
|
||||
@@ -26,7 +26,7 @@ class Config {
|
||||
static let defaultAdvertisingState = false
|
||||
static let defaultDiscoveryState = false
|
||||
static let bytePayload = "hello world"
|
||||
|
||||
|
||||
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||
static let defaultEndpointName = UIDevice.current.name
|
||||
#elseif os(macOS)
|
||||
|
||||
@@ -39,20 +39,20 @@ class Model: ObservableObject {
|
||||
invalidateDiscovery()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Published private(set) var requests: [ConnectionRequest] = []
|
||||
@Published private(set) var connections: [ConnectedEndpoint] = []
|
||||
@Published private(set) var endpoints: [DiscoveredEndpoint] = []
|
||||
|
||||
|
||||
var connectionManager: ConnectionManager!
|
||||
var advertiser: Advertiser?
|
||||
var discoverer: Discoverer?
|
||||
|
||||
|
||||
init() {
|
||||
invalidateAdvertising()
|
||||
invalidateDiscovery()
|
||||
}
|
||||
|
||||
|
||||
private var isAdvertising = Config.defaultAdvertisingState
|
||||
private func invalidateAdvertising() {
|
||||
defer {
|
||||
@@ -66,12 +66,12 @@ class Model: ObservableObject {
|
||||
}
|
||||
connectionManager = ConnectionManager(serviceID: Config.serviceId, strategy: strategy)
|
||||
connectionManager.delegate = self
|
||||
|
||||
|
||||
advertiser = Advertiser(connectionManager: connectionManager)
|
||||
advertiser?.delegate = self
|
||||
advertiser?.startAdvertising(using: endpointName.data(using: .utf8)!)
|
||||
}
|
||||
|
||||
|
||||
private var isDiscovering = Config.defaultDiscoveryState
|
||||
private func invalidateDiscovery() {
|
||||
defer {
|
||||
@@ -85,20 +85,20 @@ class Model: ObservableObject {
|
||||
}
|
||||
connectionManager = ConnectionManager(serviceID: Config.serviceId, strategy: strategy)
|
||||
connectionManager.delegate = self
|
||||
|
||||
|
||||
discoverer = Discoverer(connectionManager: connectionManager)
|
||||
discoverer?.delegate = self
|
||||
discoverer?.startDiscovery()
|
||||
}
|
||||
|
||||
|
||||
func requestConnection(to endpointID: EndpointID) {
|
||||
discoverer?.requestConnection(to: endpointID, using: endpointName.data(using: .utf8)!)
|
||||
}
|
||||
|
||||
|
||||
func disconnect(from endpointID: EndpointID) {
|
||||
connectionManager.disconnect(from: endpointID)
|
||||
}
|
||||
|
||||
|
||||
func sendBytes(to endpointIDs: [EndpointID]) {
|
||||
let payloadID = PayloadID.unique()
|
||||
let token = connectionManager.send(Config.bytePayload.data(using: .utf8)!, to: endpointIDs, id: payloadID)
|
||||
@@ -126,7 +126,7 @@ extension Model: DiscovererDelegate {
|
||||
)
|
||||
endpoints.insert(endpoint, at: 0)
|
||||
}
|
||||
|
||||
|
||||
func discoverer(_ discoverer: Discoverer, didLose endpointID: EndpointID) {
|
||||
guard let index = endpoints.firstIndex(where: { $0.id == endpointID }) else {
|
||||
return
|
||||
@@ -190,7 +190,7 @@ extension Model: ConnectionManagerDelegate {
|
||||
}
|
||||
connections[index].payloads.insert(payload, at: 0)
|
||||
}
|
||||
|
||||
|
||||
func connectionManager(_ connectionManager: ConnectionManager, didStartReceivingResourceWithID payloadID: PayloadID, from endpointID: EndpointID, at localURL: URL, withName name: String, cancellationToken token: CancellationToken) {
|
||||
let payload = Payload(
|
||||
id: payloadID,
|
||||
@@ -204,7 +204,7 @@ extension Model: ConnectionManagerDelegate {
|
||||
}
|
||||
connections[index].payloads.insert(payload, at: 0)
|
||||
}
|
||||
|
||||
|
||||
func connectionManager(_ connectionManager: ConnectionManager, didReceiveTransferUpdate update: TransferUpdate, from endpointID: EndpointID, forPayload payloadID: PayloadID) {
|
||||
guard let connectionIndex = connections.firstIndex(where: { $0.id == endpointID }),
|
||||
let payloadIndex = connections[connectionIndex].payloads.firstIndex(where: { $0.id == payloadID }) else {
|
||||
|
||||
@@ -23,7 +23,7 @@ struct Payload: Identifiable {
|
||||
var status: Status
|
||||
let isIncoming: Bool
|
||||
let cancellationToken: CancellationToken?
|
||||
|
||||
|
||||
enum PayloadType {
|
||||
case bytes, stream, file
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ import SwiftUI
|
||||
|
||||
struct PayloadView: View {
|
||||
var payload: Payload
|
||||
|
||||
|
||||
var body: some View {
|
||||
HStack(alignment: .top) {
|
||||
Image(systemName: payload.isIncoming
|
||||
|
||||
Reference in New Issue
Block a user