From d780f103ad4b2ae504340e0b5ad6017da3e01ba6 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sun, 21 Apr 2024 20:36:29 -0700 Subject: [PATCH] Set provide location default to 30 seconds Don't delete routes unless doing a factory reset Close route recorder modal when finished --- Meshtastic/Extensions/UserDefaults.swift | 2 +- Meshtastic/Persistence/UpdateCoreData.swift | 16 +++++++++++++--- Meshtastic/Views/Bluetooth/Connect.swift | 2 +- Meshtastic/Views/Settings/AppSettings.swift | 2 +- .../Views/Settings/Config/DeviceConfig.swift | 4 ++-- Meshtastic/Views/Settings/RouteRecorder.swift | 1 + 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Meshtastic/Extensions/UserDefaults.swift b/Meshtastic/Extensions/UserDefaults.swift index 1a3e05b2..49757d69 100644 --- a/Meshtastic/Extensions/UserDefaults.swift +++ b/Meshtastic/Extensions/UserDefaults.swift @@ -86,7 +86,7 @@ extension UserDefaults { @UserDefault(.provideLocation, defaultValue: false) static var provideLocation: Bool - @UserDefault(.provideLocationInterval, defaultValue: 0) + @UserDefault(.provideLocationInterval, defaultValue: 30) static var provideLocationInterval: Int @UserDefault(.mapLayer, defaultValue: .standard) diff --git a/Meshtastic/Persistence/UpdateCoreData.swift b/Meshtastic/Persistence/UpdateCoreData.swift index 90a35cce..0b5428fc 100644 --- a/Meshtastic/Persistence/UpdateCoreData.swift +++ b/Meshtastic/Persistence/UpdateCoreData.swift @@ -106,14 +106,24 @@ public func deleteUserMessages(user: UserEntity, context: NSManagedObjectContext } } -public func clearCoreDataDatabase(context: NSManagedObjectContext) { +public func clearCoreDataDatabase(context: NSManagedObjectContext, includeRoutes: Bool) { let persistenceController = PersistenceController.shared.container for i in 0...persistenceController.managedObjectModel.entities.count-1 { + let entity = persistenceController.managedObjectModel.entities[i] let query = NSFetchRequest(entityName: entity.name!) - let deleteRequest = NSBatchDeleteRequest(fetchRequest: query) - + var deleteRequest = NSBatchDeleteRequest(fetchRequest: query) + let entityName = entity.name ?? "UNK" + + if includeRoutes { + deleteRequest = NSBatchDeleteRequest(fetchRequest: query) + } else if !includeRoutes { + if !(entityName.contains("RouteEntity") || entityName.contains("LocationEntity")) { + print(entity.name?.lowercased()) + deleteRequest = NSBatchDeleteRequest(fetchRequest: query) + } + } do { try context.executeAndMergeChanges(using: deleteRequest) } catch let error as NSError { diff --git a/Meshtastic/Views/Bluetooth/Connect.swift b/Meshtastic/Views/Bluetooth/Connect.swift index 201ec969..498496ce 100644 --- a/Meshtastic/Views/Bluetooth/Connect.swift +++ b/Meshtastic/Views/Bluetooth/Connect.swift @@ -232,7 +232,7 @@ struct Connect: View { if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.state == CBPeripheralState.connected { bleManager.disconnectPeripheral() } - clearCoreDataDatabase(context: context) + clearCoreDataDatabase(context: context, includeRoutes: false) let radio = bleManager.peripherals.first(where: { $0.peripheral.identifier.uuidString == selectedPeripherialId }) if radio != nil { diff --git a/Meshtastic/Views/Settings/AppSettings.swift b/Meshtastic/Views/Settings/AppSettings.swift index e614153a..ba5d46f9 100644 --- a/Meshtastic/Views/Settings/AppSettings.swift +++ b/Meshtastic/Views/Settings/AppSettings.swift @@ -75,7 +75,7 @@ struct AppSettings: View { ) { Button("Erase all app data?", role: .destructive) { bleManager.disconnectPeripheral() - clearCoreDataDatabase(context: context) + clearCoreDataDatabase(context: context, includeRoutes: true) context.refreshAllObjects() UserDefaults.standard.reset() } diff --git a/Meshtastic/Views/Settings/Config/DeviceConfig.swift b/Meshtastic/Views/Settings/Config/DeviceConfig.swift index 68596dab..1d438cbe 100644 --- a/Meshtastic/Views/Settings/Config/DeviceConfig.swift +++ b/Meshtastic/Views/Settings/Config/DeviceConfig.swift @@ -153,7 +153,7 @@ struct DeviceConfig: View { if bleManager.sendNodeDBReset(fromUser: node!.user!, toUser: node!.user!) { DispatchQueue.main.asyncAfter(deadline: .now() + 1) { bleManager.disconnectPeripheral() - clearCoreDataDatabase(context: context) + clearCoreDataDatabase(context: context, includeRoutes: false) } } else { @@ -178,7 +178,7 @@ struct DeviceConfig: View { if bleManager.sendFactoryReset(fromUser: node!.user!, toUser: node!.user!) { DispatchQueue.main.asyncAfter(deadline: .now() + 1) { bleManager.disconnectPeripheral() - clearCoreDataDatabase(context: context) + clearCoreDataDatabase(context: context, includeRoutes: false) } } else { print("Factory Reset Failed") diff --git a/Meshtastic/Views/Settings/RouteRecorder.swift b/Meshtastic/Views/Settings/RouteRecorder.swift index 63ca1697..766b72d5 100644 --- a/Meshtastic/Views/Settings/RouteRecorder.swift +++ b/Meshtastic/Views/Settings/RouteRecorder.swift @@ -248,6 +248,7 @@ struct RouteRecorder: View { let nsError = error as NSError print("💥 Error Saving RouteEntity from the Route Recorder \(nsError)") } + isShowingDetails = false } label: { Label("finish", systemImage: "flag.checkered") }