From 6134a6196a040d5d2aaaf617b1dc4acfc2d38b5c Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Mon, 3 Oct 2022 20:13:33 -0700 Subject: [PATCH] Clear Log Buttons for position and metrics --- Meshtastic.xcodeproj/project.pbxproj | 6 +- Meshtastic/Helpers/UpdateCoreData.swift | 32 +++++++++-- Meshtastic/Views/Nodes/DeviceMetricsLog.swift | 56 +++++++++++++++---- .../Views/Nodes/EnvironmentMetricsLog.swift | 53 ++++++++++++++---- Meshtastic/Views/Nodes/PositionLog.swift | 29 +++++----- 5 files changed, 134 insertions(+), 42 deletions(-) diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index 4a870d12..8bef46b3 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -269,12 +269,12 @@ DD47E3CA26F0E50300029299 /* Nodes */ = { isa = PBXGroup; children = ( + DD769E0228D18BF0001A3F05 /* DeviceMetricsLog.swift */, + DD4F23CC28779A3C001D37CB /* EnvironmentMetricsLog.swift */, + DD2E65252767A01F00E45FC5 /* NodeDetail.swift */, DD47E3CD26F103C600029299 /* NodeList.swift */, DD90860D26F69BAE00DC5189 /* NodeMap.swift */, - DD2E65252767A01F00E45FC5 /* NodeDetail.swift */, DD73FD1028750779000852D6 /* PositionLog.swift */, - DD4F23CC28779A3C001D37CB /* EnvironmentMetricsLog.swift */, - DD769E0228D18BF0001A3F05 /* DeviceMetricsLog.swift */, ); path = Nodes; sourceTree = ""; diff --git a/Meshtastic/Helpers/UpdateCoreData.swift b/Meshtastic/Helpers/UpdateCoreData.swift index a42e2adc..1f6aa670 100644 --- a/Meshtastic/Helpers/UpdateCoreData.swift +++ b/Meshtastic/Helpers/UpdateCoreData.swift @@ -3,10 +3,10 @@ // Meshtastic // // Copyright(c) Garth Vander Houwen 10/3/22. + import CoreData public func clearPositions(destNum: Int64, context: NSManagedObjectContext) -> Bool { - let fetchNodeInfoRequest: NSFetchRequest = NSFetchRequest.init(entityName: "NodeInfoEntity") fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(destNum)) @@ -26,12 +26,36 @@ public func clearPositions(destNum: Int64, context: NSManagedObjectContext) -> B context.rollback() return false } - } catch { - print("💥 Fetch NodeInfoEntity Error") return false } - +} + +public func clearTelemetry(destNum: Int64, metricsType: Int32, context: NSManagedObjectContext) -> Bool { + + let fetchNodeInfoRequest: NSFetchRequest = NSFetchRequest.init(entityName: "NodeInfoEntity") + fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(destNum)) + + do { + + let fetchedNode = try context.fetch(fetchNodeInfoRequest) as! [NodeInfoEntity] + + let emptyTelemetry = [TelemetryEntity]() + fetchedNode[0].telemetries? = NSOrderedSet(array: emptyTelemetry) + + do { + try context.save() + return true + + } catch { + context.rollback() + return false + } + + } catch { + print("💥 Fetch NodeInfoEntity Error") + return false + } } diff --git a/Meshtastic/Views/Nodes/DeviceMetricsLog.swift b/Meshtastic/Views/Nodes/DeviceMetricsLog.swift index 77c6dfd9..14290db2 100644 --- a/Meshtastic/Views/Nodes/DeviceMetricsLog.swift +++ b/Meshtastic/Views/Nodes/DeviceMetricsLog.swift @@ -12,6 +12,8 @@ struct DeviceMetricsLog: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @State private var isPresentingClearLogConfirm: Bool = false + @State var isExporting = false @State var exportString = "" @@ -105,19 +107,53 @@ struct DeviceMetricsLog: View { .padding(.trailing, 5) } } - Button { + HStack { + + Button(role: .destructive) { + + isPresentingClearLogConfirm = true + + } label: { + + Label("Clear Log", systemImage: "trash.fill") + } + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding() + .confirmationDialog( + "Are you sure?", + isPresented: $isPresentingClearLogConfirm, + titleVisibility: .visible + ) { + Button("Delete all device metrics?", role: .destructive) { + + if clearTelemetry(destNum: node.num, metricsType: 0, context: context) { - exportString = TelemetryToCsvFile(telemetry: node.telemetries!.array as! [TelemetryEntity], metricsType: 0) - isExporting = true + print("Clear Device Metrics Log Failed") + + } else { + + + } + } + } - } label: { - - Label("Save", systemImage: "square.and.arrow.down") + Button { + + exportString = TelemetryToCsvFile(telemetry: node.telemetries!.array as! [TelemetryEntity], metricsType: 0) + isExporting = true + + } label: { + + Label("Save", systemImage: "square.and.arrow.down") + } + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding() } - .buttonStyle(.bordered) - .buttonBorderShape(.capsule) - .controlSize(.large) - .padding() + .navigationTitle("Device Metrics Log") .navigationBarTitleDisplayMode(.inline) .navigationBarItems(trailing: diff --git a/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift b/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift index 6b17cbbc..3283a01d 100644 --- a/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift +++ b/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift @@ -11,6 +11,8 @@ struct EnvironmentMetricsLog: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @State private var isPresentingClearLogConfirm: Bool = false + @State var isExporting = false @State var exportString = "" @@ -79,19 +81,50 @@ struct EnvironmentMetricsLog: View { .padding(.trailing, 5) } } - Button { + + HStack { + + Button(role: .destructive) { + + isPresentingClearLogConfirm = true + + } label: { + + Label("Clear Log", systemImage: "trash.fill") + } + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding() + .confirmationDialog( + "Are you sure?", + isPresented: $isPresentingClearLogConfirm, + titleVisibility: .visible + ) { + Button("Delete all environment metrics?", role: .destructive) { + + if clearTelemetry(destNum: node.num, metricsType: 1, context: context) { - exportString = TelemetryToCsvFile(telemetry: node.telemetries!.array as! [TelemetryEntity], metricsType: 1) - isExporting = true + print("Clear Environment Metrics Log Failed") + + } + } + } - } label: { - - Label("Save", systemImage: "square.and.arrow.down") + Button { + + exportString = TelemetryToCsvFile(telemetry: node.telemetries!.array as! [TelemetryEntity], metricsType: 1) + isExporting = true + + } label: { + + Label("Save", systemImage: "square.and.arrow.down") + } + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding() } - .buttonStyle(.bordered) - .buttonBorderShape(.capsule) - .controlSize(.large) - .padding() .navigationTitle("Environment Metrics Log") .navigationBarTitleDisplayMode(.inline) .navigationBarItems(trailing: diff --git a/Meshtastic/Views/Nodes/PositionLog.swift b/Meshtastic/Views/Nodes/PositionLog.swift index b4a0d9e3..25072e80 100644 --- a/Meshtastic/Views/Nodes/PositionLog.swift +++ b/Meshtastic/Views/Nodes/PositionLog.swift @@ -73,7 +73,6 @@ struct PositionLog: View { .padding(.trailing, 5) } HStack { - Button(role: .destructive) { @@ -83,7 +82,6 @@ struct PositionLog: View { Label("Clear Log", systemImage: "trash.fill") } - .disabled(bleManager.connectedPeripheral == nil) .buttonStyle(.bordered) .buttonBorderShape(.capsule) .controlSize(.large) @@ -95,7 +93,7 @@ struct PositionLog: View { ) { Button("Delete all positions?", role: .destructive) { - if clearPositions(destNum: bleManager.connectedPeripheral.num, context: context) { + if clearPositions(destNum: node.num, context: context) { print("Clear Position Log Failed") @@ -105,21 +103,22 @@ struct PositionLog: View { } } } - Button { - - exportString = PositionToCsvFile(positions: node.positions!.array as! [PositionEntity]) - isExporting = true - } label: { + Button { + + exportString = PositionToCsvFile(positions: node.positions!.array as! [PositionEntity]) + isExporting = true - Label("Save", systemImage: "square.and.arrow.down") + } label: { + + Label("Save", systemImage: "square.and.arrow.down") + } + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding() } - .buttonStyle(.bordered) - .buttonBorderShape(.capsule) - .controlSize(.large) - .padding() - } - .fileExporter( + .fileExporter( isPresented: $isExporting, document: CsvDocument(emptyCsv: exportString), contentType: .commaSeparatedText,