From 832154a103151eb6d7704224101d9adb770f5999 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Thu, 2 Mar 2023 22:58:49 -0800 Subject: [PATCH] Additional live activity cleanup --- Meshtastic/Helpers/MeshPackets.swift | 17 ++++++----- Meshtastic/Views/Bluetooth/Connect.swift | 2 +- Widgets/MeshActivityAttributes.swift | 2 +- Widgets/WidgetsLiveActivity.swift | 38 ++++++++++++++++-------- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index 299dfae1..07e02eb5 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -681,20 +681,21 @@ func telemetryPacket(packet: MeshPacket, connectedNode: Int64, context: NSManage if connectedNode != Int64(packet.from) { print("💾 Telemetry Saved for Node: \(packet.from)") } else if telemetry.metricsType == 0 { + // Update our live activity if there is one running if #available(iOS 16.2, *) { - var future = Calendar.current.date(byAdding: .minute, value: (Int(0) ), to: Date())! - future = Calendar.current.date(byAdding: .second, value: (Int(60) ), to: future)! - let date = Date.now...future - let updatedMeshStatus = MeshActivityAttributes.MyActivityStatus(timerRange: date, connected: true, channelUtilization: telemetry.channelUtilization, airtime: telemetry.airUtilTx, batteryLevel: UInt32(telemetry.batteryLevel)) + let oneMinuteLater = Calendar.current.date(byAdding: .minute, value: (Int(1) ), to: Date())! + let date = Date.now...oneMinuteLater + let updatedMeshStatus = MeshActivityAttributes.MeshActivityStatus(timerRange: date, connected: true, channelUtilization: telemetry.channelUtilization, airtime: telemetry.airUtilTx, batteryLevel: UInt32(telemetry.batteryLevel)) - let alertConfiguration = AlertConfiguration(title: "Mesh activity update", body: "Updated Metrics Data.", sound: .default) + let alertConfiguration = AlertConfiguration(title: "Mesh activity update", body: "Updated Device Metrics Data.", sound: .default) let updatedContent = ActivityContent(state: updatedMeshStatus, staleDate: nil) print("Update live activity.") - let stuff = Activity.activities.first(where: { $0.attributes.nodeNum == connectedNode }) - if stuff != nil { + let meshActivity = Activity.activities.first(where: { $0.attributes.nodeNum == connectedNode }) + if meshActivity != nil { Task { - await stuff?.update(updatedContent, alertConfiguration: alertConfiguration) + await meshActivity?.update(updatedContent, alertConfiguration: alertConfiguration) + //await meshActivity?.update(updatedContent) } } } diff --git a/Meshtastic/Views/Bluetooth/Connect.swift b/Meshtastic/Views/Bluetooth/Connect.swift index b9129950..007afad9 100644 --- a/Meshtastic/Views/Bluetooth/Connect.swift +++ b/Meshtastic/Views/Bluetooth/Connect.swift @@ -311,7 +311,7 @@ struct Connect: View { func startNodeActivity() { if #available(iOS 16.2, *) { liveActivityStarted = true - let timerSeconds = 300 + let timerSeconds = 60 let mostRecent = node?.telemetries?.lastObject as! TelemetryEntity diff --git a/Widgets/MeshActivityAttributes.swift b/Widgets/MeshActivityAttributes.swift index 4d46cca0..5159898f 100644 --- a/Widgets/MeshActivityAttributes.swift +++ b/Widgets/MeshActivityAttributes.swift @@ -12,7 +12,7 @@ import WidgetKit import SwiftUI struct MeshActivityAttributes: ActivityAttributes { - public typealias MyActivityStatus = ContentState + public typealias MeshActivityStatus = ContentState public struct ContentState: Codable, Hashable { // Dynamic stateful properties about your activity go here! var timerRange: ClosedRange diff --git a/Widgets/WidgetsLiveActivity.swift b/Widgets/WidgetsLiveActivity.swift index 3a092829..744539f7 100644 --- a/Widgets/WidgetsLiveActivity.swift +++ b/Widgets/WidgetsLiveActivity.swift @@ -14,13 +14,13 @@ struct WidgetsLiveActivity: Widget { var body: some WidgetConfiguration { ActivityConfiguration(for: MeshActivityAttributes.self) { context in - LiveActivityView(nodeName: context.attributes.name, connected: context.state.connected, channelUtilization: context.state.channelUtilization, airtime: context.state.airtime, batteryLevel: context.state.batteryLevel, timerRange: context.state.timerRange) + LiveActivityView(nodeName: context.attributes.name, channelUtilization: context.state.channelUtilization, airtime: context.state.airtime, batteryLevel: context.state.batteryLevel, timerRange: context.state.timerRange) .widgetURL(URL(string: "meshtastic://node/\(context.attributes.name)")) } dynamicIsland: { context in DynamicIsland { DynamicIslandExpandedRegion(.leading) { - NodeInfoView(nodeName: context.attributes.name, connected: context.state.connected, channelUtilization: context.state.channelUtilization, airtime: context.state.airtime, batteryLevel: context.state.batteryLevel) + NodeInfoView(nodeName: context.attributes.name, timerRange: context.state.timerRange, channelUtilization: context.state.channelUtilization, airtime: context.state.airtime, batteryLevel: context.state.batteryLevel) .tint(Color("LightIndigo")) .padding(.top) } @@ -91,7 +91,7 @@ struct LiveActivityView: View { @Environment(\.isLuminanceReduced) var isLuminanceReduced var nodeName: String - var connected: Bool + //var connected: Bool var channelUtilization: Float var airtime: Float var batteryLevel: UInt32 @@ -102,7 +102,7 @@ struct LiveActivityView: View { Image(colorScheme == .light ? "logo-black" : "logo-white") .clipShape(ContainerRelativeShape()) .opacity(isLuminanceReduced ? 0.5 : 1.0) - NodeInfoView(nodeName: nodeName, connected: connected, channelUtilization: channelUtilization, airtime: airtime, batteryLevel: batteryLevel) + NodeInfoView(nodeName: nodeName, timerRange: timerRange, channelUtilization: channelUtilization, airtime: airtime, batteryLevel: batteryLevel) Spacer() TimerView(timerRange: timerRange) } @@ -118,7 +118,7 @@ struct NodeInfoView: View { @Environment(\.isLuminanceReduced) var isLuminanceReduced var nodeName: String - var connected: Bool + var timerRange: ClosedRange var channelUtilization: Float var airtime: Float var batteryLevel: UInt32 @@ -130,11 +130,6 @@ struct NodeInfoView: View { .fontWeight(.semibold) .foregroundStyle(.tint) .fixedSize() - Text(connected ? "Connected" : "Not Connected") - .font(.callout) - .fontWeight(.semibold) - .foregroundStyle(.tint) - .fixedSize() Text("\(String(format: "Ch. Util: %.2f", channelUtilization))%") .font(.subheadline) .fontWeight(.medium) @@ -153,6 +148,12 @@ struct NodeInfoView: View { .foregroundStyle(.secondary) .opacity(isLuminanceReduced ? 0.5 : 1.0) .fixedSize() + Text(Date().formatted()) + .font(.subheadline) + .fontWeight(.medium) + .foregroundStyle(.secondary) + .opacity(isLuminanceReduced ? 0.5 : 1.0) + .fixedSize() } } } @@ -163,10 +164,22 @@ struct TimerView: View { var timerRange: ClosedRange var body: some View { - VStack(alignment: .trailing) { + VStack(alignment: .center) { + Text("NEXT") + .font(.caption) + .fontWeight(.medium) + .foregroundStyle(.secondary) + .opacity(isLuminanceReduced ? 0.5 : 1.0) + .fixedSize() + Text("UPDATE") + .font(.caption) + .fontWeight(.medium) + .foregroundStyle(.secondary) + .opacity(isLuminanceReduced ? 0.5 : 1.0) + .fixedSize() Text(timerInterval: timerRange, countsDown: true) .monospacedDigit() - .multilineTextAlignment(.trailing) + .multilineTextAlignment(.center) .frame(width: 80) .font(.callout) .fontWeight(.semibold) @@ -175,7 +188,6 @@ struct TimerView: View { .resizable() .foregroundStyle(.secondary) .frame(width: 30, height: 30) - .padding(.trailing, 7) .opacity(isLuminanceReduced ? 0.5 : 1.0) } }