mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Add example SwiftUI app for Nearby Connections
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ConnectedView: View {
|
||||
var connection: ConnectedEndpoint
|
||||
var onSendBytes : () -> ()
|
||||
var onDisconnect : () -> ()
|
||||
|
||||
var body: some View {
|
||||
Section("Connection Actions") {
|
||||
Button("Send Bytes") {
|
||||
onSendBytes()
|
||||
}
|
||||
Button("Disconnect", role: .destructive) {
|
||||
onDisconnect()
|
||||
}
|
||||
}
|
||||
|
||||
if !connection.payloads.isEmpty {
|
||||
Section(header: Text("Payloads")) {
|
||||
ForEach(connection.payloads) { payload in
|
||||
PayloadView(payload: payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ConnectedView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Form {
|
||||
ConnectedView(
|
||||
connection: ConnectedEndpoint(
|
||||
id: "",
|
||||
endpointName: "Example"
|
||||
),
|
||||
onSendBytes: {},
|
||||
onDisconnect: {}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
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") {
|
||||
hasResponded = true
|
||||
onAcceptConnection()
|
||||
}.disabled(hasResponded)
|
||||
Button("Reject", role: .destructive) {
|
||||
hasResponded = true
|
||||
onRejectConnection()
|
||||
}.disabled(hasResponded)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ConnectionRequestView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Form {
|
||||
ConnectionRequestView(
|
||||
connectionRequest: ConnectionRequest(
|
||||
id: "",
|
||||
endpointName: "Example",
|
||||
pin: "1234",
|
||||
shouldAccept: { _ in }
|
||||
),
|
||||
onAcceptConnection: {},
|
||||
onRejectConnection: {}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import NearbyConnections
|
||||
|
||||
struct ContentView: View {
|
||||
@EnvironmentObject private var model: Model
|
||||
|
||||
let strategies: [Strategy: String] = [
|
||||
.cluster: "Cluster",
|
||||
.pointToPoint: "Point to Point",
|
||||
.star: "Star"
|
||||
]
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
Form {
|
||||
TextField("Endpoint Name", text: $model.endpointName)
|
||||
|
||||
Picker("Strategy", selection: $model.strategy) {
|
||||
ForEach(Array(strategies), id: \.key) { key, value in
|
||||
Text(value).tag(key)
|
||||
}
|
||||
}
|
||||
|
||||
Toggle("Advertising", isOn: $model.isAdvertisingEnabled)
|
||||
|
||||
Toggle("Discovery", isOn: $model.isDiscoveryEnabled)
|
||||
|
||||
if !model.requests.isEmpty {
|
||||
Section(header: Text("Pending Connections")) {
|
||||
ForEach(model.requests) { request in
|
||||
NavigationLink(value: request.id) {
|
||||
Text(request.endpointName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !model.connections.isEmpty {
|
||||
Section(header: Text("Connections")) {
|
||||
ForEach(model.connections) { connection in
|
||||
NavigationLink(value: connection.id) {
|
||||
Text(connection.endpointName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !model.endpoints.isEmpty {
|
||||
Section(header: Text("Endpoints")) {
|
||||
ForEach(model.endpoints) { endpoint in
|
||||
NavigationLink(value: endpoint.id) {
|
||||
Text(endpoint.endpointName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationDestination(for: String.self) { endpointID in
|
||||
EndpointDetail(endpointID: endpointID)
|
||||
}
|
||||
.navigationTitle("Hello Connections")
|
||||
} detail: {
|
||||
Text("No Endpoints Selected")
|
||||
.font(.headline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct DiscoveredEndpointView: View {
|
||||
var endpoint: DiscoveredEndpoint
|
||||
var onRequestConnection : () -> ()
|
||||
|
||||
@State private var hasResponded = false
|
||||
|
||||
var body: some View {
|
||||
Section {
|
||||
Button("Connect") {
|
||||
hasResponded = true
|
||||
onRequestConnection()
|
||||
}.disabled(hasResponded)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DiscoveredEndpointView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Form {
|
||||
DiscoveredEndpointView(
|
||||
endpoint: DiscoveredEndpoint(
|
||||
id: "",
|
||||
endpointName: "Example"
|
||||
),
|
||||
onRequestConnection: {}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
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)
|
||||
}, onRejectConnection: {
|
||||
connectionRequest.shouldAccept(false)
|
||||
})
|
||||
}
|
||||
|
||||
if let connection {
|
||||
ConnectedView(connection: connection, onSendBytes: {
|
||||
model.sendBytes(to: [endpointID])
|
||||
}, onDisconnect: {
|
||||
model.disconnect(from: endpointID)
|
||||
})
|
||||
}
|
||||
}
|
||||
.navigationTitle(name ?? "")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
@main
|
||||
struct HelloConnectionsApp: App {
|
||||
@StateObject private var model = Model()
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
.environmentObject(model)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||
import UIKit
|
||||
#endif
|
||||
import NearbyConnections
|
||||
|
||||
class Config {
|
||||
static let serviceId = "com.google.location.nearby.apps.helloconnections"
|
||||
static let defaultStategy = Strategy.cluster
|
||||
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)
|
||||
static let defaultEndpointName = Host.current().localizedName ?? "Unknown macOS Device"
|
||||
#else
|
||||
static let defaultEndpointName = "Unknown Device"
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import NearbyConnections
|
||||
|
||||
struct ConnectedEndpoint: Identifiable {
|
||||
let id: EndpointID
|
||||
let endpointName: String
|
||||
var payloads: [Payload] = []
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import NearbyConnections
|
||||
|
||||
struct ConnectionRequest: Identifiable {
|
||||
let id: EndpointID
|
||||
let endpointName: String
|
||||
let pin: String
|
||||
let shouldAccept: ((Bool) -> Void)
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import NearbyConnections
|
||||
|
||||
struct DiscoveredEndpoint: Identifiable {
|
||||
let id: EndpointID
|
||||
let endpointName: String
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import NearbyConnections
|
||||
|
||||
class Model: ObservableObject {
|
||||
@Published var endpointName = Config.defaultEndpointName {
|
||||
didSet {
|
||||
invalidateAdvertising()
|
||||
}
|
||||
}
|
||||
@Published var strategy = Config.defaultStategy {
|
||||
didSet {
|
||||
invalidateAdvertising()
|
||||
invalidateDiscovery()
|
||||
}
|
||||
}
|
||||
@Published var isAdvertisingEnabled = Config.defaultAdvertisingState {
|
||||
didSet {
|
||||
invalidateAdvertising()
|
||||
}
|
||||
}
|
||||
@Published var isDiscoveryEnabled = Config.defaultDiscoveryState {
|
||||
didSet {
|
||||
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 {
|
||||
isAdvertising = isAdvertisingEnabled
|
||||
}
|
||||
if isAdvertising {
|
||||
advertiser?.stopAdvertising()
|
||||
}
|
||||
if !isAdvertisingEnabled {
|
||||
return
|
||||
}
|
||||
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 {
|
||||
isDiscovering = isDiscoveryEnabled
|
||||
}
|
||||
if isDiscovering {
|
||||
discoverer?.stopDiscovery()
|
||||
}
|
||||
if !isDiscoveryEnabled {
|
||||
return
|
||||
}
|
||||
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)
|
||||
let payload = Payload(
|
||||
id: payloadID,
|
||||
type: .bytes,
|
||||
status: .inProgress(Progress()),
|
||||
isIncoming: false,
|
||||
cancellationToken: token
|
||||
)
|
||||
for endpointID in endpointIDs {
|
||||
guard let index = connections.firstIndex(where: { $0.id == endpointID }) else {
|
||||
return
|
||||
}
|
||||
connections[index].payloads.insert(payload, at: 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Model: DiscovererDelegate {
|
||||
func discoverer(_ discoverer: Discoverer, didFind endpointID: EndpointID, with context: Data) {
|
||||
let endpoint = DiscoveredEndpoint(
|
||||
id: endpointID,
|
||||
endpointName: String(data: context, encoding: .utf8)!
|
||||
)
|
||||
endpoints.insert(endpoint, at: 0)
|
||||
}
|
||||
|
||||
func discoverer(_ discoverer: Discoverer, didLose endpointID: EndpointID) {
|
||||
guard let index = endpoints.firstIndex(where: { $0.id == endpointID }) else {
|
||||
return
|
||||
}
|
||||
endpoints.remove(at: index)
|
||||
}
|
||||
}
|
||||
|
||||
extension Model: AdvertiserDelegate {
|
||||
func advertiser(_ advertiser: Advertiser, didReceiveConnectionRequestFrom endpointID: EndpointID, with context: Data, connectionRequestHandler: @escaping (Bool) -> Void) {
|
||||
let endpoint = DiscoveredEndpoint(
|
||||
id: endpointID,
|
||||
endpointName: String(data: context, encoding: .utf8)!
|
||||
)
|
||||
endpoints.insert(endpoint, at: 0)
|
||||
connectionRequestHandler(true)
|
||||
}
|
||||
}
|
||||
|
||||
extension Model: ConnectionManagerDelegate {
|
||||
func connectionManager(_ connectionManager: ConnectionManager, didReceive verificationCode: String, from endpointID: EndpointID, verificationHandler: @escaping (Bool) -> Void) {
|
||||
guard let index = endpoints.firstIndex(where: { $0.id == endpointID }) else {
|
||||
return
|
||||
}
|
||||
let endpoint = endpoints.remove(at: index)
|
||||
let request = ConnectionRequest(
|
||||
id: endpointID,
|
||||
endpointName: endpoint.endpointName,
|
||||
pin: verificationCode,
|
||||
shouldAccept: { accept in
|
||||
verificationHandler(accept)
|
||||
}
|
||||
)
|
||||
requests.insert(request, at: 0)
|
||||
}
|
||||
|
||||
func connectionManager(_ connectionManager: ConnectionManager, didReceive data: Data, withID payloadID: PayloadID, from endpointID: EndpointID) {
|
||||
let payload = Payload(
|
||||
id: payloadID,
|
||||
type: .bytes,
|
||||
status: .success,
|
||||
isIncoming: true,
|
||||
cancellationToken: nil
|
||||
)
|
||||
guard let index = connections.firstIndex(where: { $0.id == endpointID }) else {
|
||||
return
|
||||
}
|
||||
connections[index].payloads.insert(payload, at: 0)
|
||||
}
|
||||
|
||||
func connectionManager(_ connectionManager: ConnectionManager, didReceive stream: InputStream, withID payloadID: PayloadID, from endpointID: EndpointID, cancellationToken token: CancellationToken) {
|
||||
let payload = Payload(
|
||||
id: payloadID,
|
||||
type: .stream,
|
||||
status: .success,
|
||||
isIncoming: true,
|
||||
cancellationToken: token
|
||||
)
|
||||
guard let index = connections.firstIndex(where: { $0.id == endpointID }) else {
|
||||
return
|
||||
}
|
||||
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,
|
||||
type: .file,
|
||||
status: .inProgress(Progress()),
|
||||
isIncoming: true,
|
||||
cancellationToken: token
|
||||
)
|
||||
guard let index = connections.firstIndex(where: { $0.id == endpointID }) else {
|
||||
return
|
||||
}
|
||||
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 {
|
||||
return
|
||||
}
|
||||
switch update {
|
||||
case .success:
|
||||
connections[connectionIndex].payloads[payloadIndex].status = .success
|
||||
case .canceled:
|
||||
connections[connectionIndex].payloads[payloadIndex].status = .canceled
|
||||
case .failure:
|
||||
connections[connectionIndex].payloads[payloadIndex].status = .failure
|
||||
case let .progress(progress):
|
||||
connections[connectionIndex].payloads[payloadIndex].status = .inProgress(progress)
|
||||
}
|
||||
}
|
||||
|
||||
func connectionManager(_ connectionManager: ConnectionManager, didChangeTo state: ConnectionState, for endpointID: EndpointID) {
|
||||
switch (state) {
|
||||
case .connecting:
|
||||
break
|
||||
case .connected:
|
||||
guard let index = requests.firstIndex(where: { $0.id == endpointID }) else {
|
||||
return
|
||||
}
|
||||
let request = requests.remove(at: index)
|
||||
let connection = ConnectedEndpoint(
|
||||
id: endpointID,
|
||||
endpointName: request.endpointName
|
||||
)
|
||||
connections.insert(connection, at: 0)
|
||||
case .disconnected:
|
||||
guard let index = connections.firstIndex(where: { $0.id == endpointID }) else {
|
||||
return
|
||||
}
|
||||
connections.remove(at: index)
|
||||
case .rejected:
|
||||
guard let index = requests.firstIndex(where: { $0.id == endpointID }) else {
|
||||
return
|
||||
}
|
||||
requests.remove(at: index)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import NearbyConnections
|
||||
|
||||
struct Payload: Identifiable {
|
||||
let id: PayloadID
|
||||
var type: PayloadType
|
||||
var status: Status
|
||||
let isIncoming: Bool
|
||||
let cancellationToken: CancellationToken?
|
||||
|
||||
enum PayloadType {
|
||||
case bytes, stream, file
|
||||
}
|
||||
enum Status {
|
||||
case inProgress(Progress), success, failure, canceled
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
//
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct PayloadView: View {
|
||||
var payload: Payload
|
||||
|
||||
var body: some View {
|
||||
HStack(alignment: .top) {
|
||||
Image(systemName: payload.isIncoming
|
||||
? "square.and.arrow.down"
|
||||
: "square.and.arrow.up")
|
||||
VStack(alignment: .leading) {
|
||||
switch payload.type {
|
||||
case .bytes:
|
||||
Text("Bytes")
|
||||
case .file:
|
||||
Text("File")
|
||||
case .stream:
|
||||
Text("Stream")
|
||||
}
|
||||
HStack {
|
||||
switch payload.status {
|
||||
case let .inProgress(progress):
|
||||
Image(systemName: "xmark.circle.fill").onTapGesture {
|
||||
payload.cancellationToken?.cancel()
|
||||
}.foregroundColor(.secondary)
|
||||
ProgressView(value: Float(progress.completedUnitCount), total: Float(progress.totalUnitCount))
|
||||
case .canceled:
|
||||
Text("Canceled")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
case .failure:
|
||||
Text("Failed")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
case .success:
|
||||
Text(payload.isIncoming ? "Received" : "Sent")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct PayloadView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
List {
|
||||
PayloadView(
|
||||
payload: Payload(
|
||||
id: 0,
|
||||
type: .bytes,
|
||||
status: .success,
|
||||
isIncoming: false,
|
||||
cancellationToken: nil
|
||||
)
|
||||
)
|
||||
PayloadView(
|
||||
payload: Payload(
|
||||
id: 0,
|
||||
type: .bytes,
|
||||
status: .success,
|
||||
isIncoming: true,
|
||||
cancellationToken: nil
|
||||
)
|
||||
)
|
||||
PayloadView(
|
||||
payload: Payload(
|
||||
id: 0,
|
||||
type: .bytes,
|
||||
status: .canceled,
|
||||
isIncoming: true,
|
||||
cancellationToken: nil
|
||||
)
|
||||
)
|
||||
PayloadView(
|
||||
payload: Payload(
|
||||
id: 0,
|
||||
type: .bytes,
|
||||
status: .failure,
|
||||
isIncoming: true,
|
||||
cancellationToken: nil
|
||||
)
|
||||
)
|
||||
PayloadView(
|
||||
payload: Payload(
|
||||
id: 0,
|
||||
type: .file,
|
||||
status: .inProgress(Progress(totalUnitCount: 100)),
|
||||
isIncoming: true,
|
||||
cancellationToken: nil
|
||||
)
|
||||
)
|
||||
PayloadView(
|
||||
payload: Payload(
|
||||
id: 0,
|
||||
type: .stream,
|
||||
status: .inProgress(Progress()),
|
||||
isIncoming: true,
|
||||
cancellationToken: nil
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user