diff --git a/Meshtastic/Views/Bluetooth/Connect.swift b/Meshtastic/Views/Bluetooth/Connect.swift index 799c20e0..4837fbcd 100644 --- a/Meshtastic/Views/Bluetooth/Connect.swift +++ b/Meshtastic/Views/Bluetooth/Connect.swift @@ -36,54 +36,6 @@ struct Connect: View { if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.state == .connected { HStack { Image(systemName: "antenna.radiowaves.left.and.right") - .symbolRenderingMode(.hierarchical) - .imageScale(.large).foregroundColor(.green) - .padding(.trailing) - VStack(alignment: .leading) { - if node != nil { - Text(bleManager.connectedPeripheral.longName).font(.title2) - } - Text("ble.name").font(.caption)+Text(": \(bleManager.connectedPeripheral.peripheral.name ?? NSLocalizedString("unknown", comment: "Unknown"))") - .font(.caption).foregroundColor(Color.gray) - if node != nil { - Text("firmware.version").font(.caption)+Text(": \(node?.myInfo?.firmwareVersion ?? NSLocalizedString("unknown", comment: "Unknown"))") - .font(.caption).foregroundColor(Color.gray) - } - if bleManager.isSubscribed { - Text("subscribed").font(.caption) - .foregroundColor(.green) - } else { - Text("communicating").font(.caption) - .foregroundColor(.orange) - } - } - Spacer() - VStack(alignment: .center) { - Text("preferred.radio").font(.caption2) - .multilineTextAlignment(.center) - .frame(width: 75) - Toggle("preferred.radio", isOn: $bleManager.preferredPeripheral) - .toggleStyle(SwitchToggleStyle(tint: .accentColor)) - .labelsHidden() - .onChange(of: bleManager.preferredPeripheral) { value in - if value { - if bleManager.connectedPeripheral != nil { - userSettings.preferredPeripheralId = bleManager.connectedPeripheral!.peripheral.identifier.uuidString - userSettings.preferredNodeNum = bleManager.connectedPeripheral!.num - bleManager.preferredPeripheral = true - isPreferredRadio = true - } - } else { - - if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.identifier.uuidString == userSettings.preferredPeripheralId { - - userSettings.preferredPeripheralId = "" - userSettings.preferredNodeNum = 0 - bleManager.preferredPeripheral = false - isPreferredRadio = false - } - } - } .resizable() .symbolRenderingMode(.hierarchical) .foregroundColor(.green) @@ -127,15 +79,15 @@ struct Connect: View { if #available(iOS 16.2, *) { Button { if !liveActivityStarted { - #if canImport(ActivityKit) +#if canImport(ActivityKit) print("Start live activity.") startNodeActivity() - #endif +#endif } else { - #if canImport(ActivityKit) +#if canImport(ActivityKit) print("Stop live activity.") endActivity() - #endif +#endif } } label: { Label("Mesh Live Activity", systemImage: liveActivityStarted ? "stop" : "play") @@ -254,8 +206,8 @@ struct Connect: View { HStack(alignment: .center) { Spacer() - #if targetEnvironment(macCatalyst) - +#if targetEnvironment(macCatalyst) + if bleManager.connectedPeripheral != nil { Button(role: .destructive, action: { @@ -275,7 +227,7 @@ struct Connect: View { .controlSize(.large) .padding() } - #endif +#endif Spacer() } .padding(.bottom, 10) @@ -333,66 +285,66 @@ struct Connect: View { }) } #if canImport(ActivityKit) -func startNodeActivity() { - if #available(iOS 16.2, *) { - liveActivityStarted = true - let timerSeconds = 60 - - let mostRecent = node?.telemetries?.lastObject as! TelemetryEntity - - let activityAttributes = MeshActivityAttributes(nodeNum: Int(node?.num ?? 0), name: node?.user?.longName ?? "unknown") - - let future = Date(timeIntervalSinceNow: Double(timerSeconds)) - - let initialContentState = MeshActivityAttributes.ContentState(timerRange: Date.now...future, connected: true, channelUtilization: mostRecent.channelUtilization, airtime: mostRecent.airUtilTx, batteryLevel: UInt32(mostRecent.batteryLevel)) - - let activityContent = ActivityContent(state: initialContentState, staleDate: Calendar.current.date(byAdding: .minute, value: 2, to: Date())!) - - do { - let myActivity = try Activity.request(attributes: activityAttributes, content: activityContent, - pushType: nil) - print(" Requested MyActivity live activity. ID: \(myActivity.id)") - } catch let error { - print("Error requesting live activity: \(error.localizedDescription)") + func startNodeActivity() { + if #available(iOS 16.2, *) { + liveActivityStarted = true + let timerSeconds = 60 + + let mostRecent = node?.telemetries?.lastObject as! TelemetryEntity + + let activityAttributes = MeshActivityAttributes(nodeNum: Int(node?.num ?? 0), name: node?.user?.longName ?? "unknown") + + let future = Date(timeIntervalSinceNow: Double(timerSeconds)) + + let initialContentState = MeshActivityAttributes.ContentState(timerRange: Date.now...future, connected: true, channelUtilization: mostRecent.channelUtilization, airtime: mostRecent.airUtilTx, batteryLevel: UInt32(mostRecent.batteryLevel)) + + let activityContent = ActivityContent(state: initialContentState, staleDate: Calendar.current.date(byAdding: .minute, value: 2, to: Date())!) + + do { + let myActivity = try Activity.request(attributes: activityAttributes, content: activityContent, + pushType: nil) + print(" Requested MyActivity live activity. ID: \(myActivity.id)") + } catch let error { + print("Error requesting live activity: \(error.localizedDescription)") + } } } -} - -func endActivity() { - liveActivityStarted = false - Task { - if #available(iOS 16.2, *) { - for activity in Activity.activities { - // Check if this is the activity associated with this order. - if activity.attributes.nodeNum == node?.num ?? 0 { - await activity.end(nil, dismissalPolicy: .immediate) + + func endActivity() { + liveActivityStarted = false + Task { + if #available(iOS 16.2, *) { + for activity in Activity.activities { + // Check if this is the activity associated with this order. + if activity.attributes.nodeNum == node?.num ?? 0 { + await activity.end(nil, dismissalPolicy: .immediate) + } } } } } -} #endif - + #if os(iOS) -func postNotification() { - let timerSeconds = 60 - let content = UNMutableNotificationContent() - content.title = "Mesh Live Activity Over" - content.body = "Your timed mesh live activity is over." - let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerSeconds), repeats: false) - let uuidString = UUID().uuidString - let request = UNNotificationRequest(identifier: uuidString, - content: content, trigger: trigger) - let notificationCenter = UNUserNotificationCenter.current() - notificationCenter.add(request) { (error) in - if error != nil { - // Handle any errors. - print("Error posting local notification: \(error?.localizedDescription ?? "no description")") - } else { - print("Posted local notification.") - } + func postNotification() { + let timerSeconds = 60 + let content = UNMutableNotificationContent() + content.title = "Mesh Live Activity Over" + content.body = "Your timed mesh live activity is over." + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerSeconds), repeats: false) + let uuidString = UUID().uuidString + let request = UNNotificationRequest(identifier: uuidString, + content: content, trigger: trigger) + let notificationCenter = UNUserNotificationCenter.current() + notificationCenter.add(request) { (error) in + if error != nil { + // Handle any errors. + print("Error posting local notification: \(error?.localizedDescription ?? "no description")") + } else { + print("Posted local notification.") + } + } } -} #endif func didDismissSheet() { diff --git a/Meshtastic/Views/ContentView.swift b/Meshtastic/Views/ContentView.swift index 534062c0..945edcc2 100644 --- a/Meshtastic/Views/ContentView.swift +++ b/Meshtastic/Views/ContentView.swift @@ -1,38 +1,33 @@ /* -Copyright (c) Garth Vander Houwen 2021 -*/ + Copyright (c) Garth Vander Houwen 2021 + */ import SwiftUI struct ContentView: View { - + @EnvironmentObject var userSettings: UserSettings @State private var selection: Tab = .ble - - enum Tab { + + enum Tab { case contacts - case messages - case map - case ble - case nodes - case settings - } + case messages + case map + case ble + case nodes + case settings + } - - - var body: some View { - - TabView(selection: $selection) { + var body: some View { + + TabView(selection: $selection) { - if userSettings.preferredPeripheralId.count > 0 { - - Contacts() + Contacts() .tabItem { Label("messages", systemImage: "message") } .tag(Tab.contacts) - } Connect() .tabItem { Label("bluetooth", systemImage: "antenna.radiowaves.left.and.right") @@ -53,12 +48,12 @@ struct ContentView: View { Label("settings", systemImage: "gear") } .tag(Tab.settings) - } - } + } + } } struct ContentView_Previews: PreviewProvider { - static var previews: some View { - ContentView() - } + static var previews: some View { + ContentView() + } }