Ver código fonte

Fix bogus UI error for TT and OR; adjust bg task handling

Deniz Cengiz 1 ano atrás
pai
commit
f3546f8a6a

+ 59 - 23
Trio/Sources/Shortcuts/Override/OverridePresetsIntentRequest.swift

@@ -9,6 +9,8 @@ import UIKit
         case noActiveOverride
     }
 
+    private var intentSuccess: Bool = false
+
     func fetchAndProcessOverrides() async throws -> [OverridePreset] {
         do {
             // Fetch all Override Presets via OverrideStorage
@@ -82,9 +84,12 @@ import UIKit
     }
 
     @MainActor func enactOverride(_ preset: OverridePreset) async -> Bool {
+        debug(.default, "Enacting override: \(preset.name)")
+        intentSuccess = false
+
         // Start background task
         var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid
-        backgroundTaskID = startBackgroundTask(withName: "Override Upload")
+        backgroundTaskID = startBackgroundTask(withName: "Override Enact")
 
         do {
             // Get NSManagedObjectID of Preset
@@ -99,43 +104,55 @@ import UIKit
             overrideObject.isUploadedToNS = false
 
             // Disable previous overrides if necessary
-            await disableAllActiveOverrides(except: overrideID, createOverrideRunEntry: true, shouldStartBackgroundTask: false)
+            await disableAllActiveOverrides(shouldStartBackgroundTask: false)
 
             if viewContext.hasChanges {
+                debug(.default, "Saving changes...")
+
                 try viewContext.save()
+
                 Foundation.NotificationCenter.default.post(name: .willUpdateOverrideConfiguration, object: nil)
+
+                debug(.default, "Waiting for notification...")
+
                 await awaitNotification(.didUpdateOverrideConfiguration)
-                return true
+                intentSuccess = true
+                debug(.default, "Notification received, continuing...")
             }
 
-            endBackgroundTaskSafely(&backgroundTaskID, taskName: "Override Upload")
+            endBackgroundTaskSafely(&backgroundTaskID, taskName: "Override Enact")
 
-            return false
+            debug(.default, "Finished. Override enacted via Shortcut.")
+
+            return intentSuccess
         } catch {
             debug(
                 .default,
                 "\(DebuggingIdentifiers.failed) Failed to enact override: \(error.localizedDescription)"
             )
 
-            endBackgroundTaskSafely(&backgroundTaskID, taskName: "Override Upload")
+            endBackgroundTaskSafely(&backgroundTaskID, taskName: "Override Enact")
 
-            return false
+            intentSuccess = false
+            return intentSuccess
         }
     }
 
     func cancelOverride() async {
-        await disableAllActiveOverrides(createOverrideRunEntry: true, shouldStartBackgroundTask: true)
+        await disableAllActiveOverrides(shouldStartBackgroundTask: true)
     }
 
     @MainActor func disableAllActiveOverrides(
-        except overrideID: NSManagedObjectID? = nil,
-        createOverrideRunEntry: Bool,
         shouldStartBackgroundTask: Bool = true
     ) async {
-        var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid
+        debug(.default, "Disabling all active overrides")
+
+        var backgroundTaskID: UIBackgroundTaskIdentifier?
 
         if shouldStartBackgroundTask {
+            debug(.default, "Starting background task for override cancel")
             // Start background task
+            backgroundTaskID = .invalid
             backgroundTaskID = startBackgroundTask(withName: "Override Cancel")
         }
 
@@ -148,10 +165,18 @@ import UIKit
             }
 
             // Return early if no results
-            guard !results.isEmpty else { return }
+            guard !results.isEmpty else {
+                debug(.default, "No active overrides to cancel… returning early")
+
+                if var backgroundTaskID = backgroundTaskID {
+                    debug(.default, "Ending background task for override cancel")
+                    endBackgroundTaskSafely(&backgroundTaskID, taskName: "Override Cancel")
+                }
+                return
+            }
 
             // Create OverrideRunStored entry if needed
-            if createOverrideRunEntry, let canceledOverride = results.first {
+            if let canceledOverride = results.first {
                 let newOverrideRunStored = OverrideRunStored(context: viewContext)
                 newOverrideRunStored.id = UUID()
                 newOverrideRunStored.name = canceledOverride.name
@@ -164,33 +189,44 @@ import UIKit
                 newOverrideRunStored.isUploadedToNS = false
             }
 
-            // Disable all overrides except the one specified
+            // Disable all active overrides
             for overrideToCancel in results {
-                if overrideToCancel.objectID != overrideID {
-                    overrideToCancel.enabled = false
-                    overrideToCancel.isUploadedToNS = false
-                }
+                let endTime = overrideToCancel.date?
+                    .addingTimeInterval(TimeInterval(truncating: overrideToCancel.duration ?? 0))
+
+                debugPrint(
+                    "Disabling override: \(overrideToCancel.name ?? "Unnamed") with end time: \(endTime?.description ?? "Unknown")"
+                )
+
+                overrideToCancel.enabled = false
+                overrideToCancel.isUploadedToNS = false
             }
 
             if viewContext.hasChanges {
                 try viewContext.save()
-
                 // Update State variables in OverrideView
                 Foundation.NotificationCenter.default.post(name: .willUpdateOverrideConfiguration, object: nil)
             }
 
             // Await the notification
-            print("Waiting for notification...")
+            debug(.default, "Waiting for notification...")
+
             await awaitNotification(.didUpdateOverrideConfiguration)
-            print("Notification received, continuing...")
 
-            endBackgroundTaskSafely(&backgroundTaskID, taskName: "Override Cancel")
+            debug(.default, "Notification received, continuing...")
+
+            if var backgroundTaskID = backgroundTaskID {
+                debug(.default, "Ending background task for override cancel")
+                endBackgroundTaskSafely(&backgroundTaskID, taskName: "Override Cancel")
+            }
         } catch {
             debugPrint(
                 "\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to disable active Overrides with error: \(error.localizedDescription)"
             )
 
-            endBackgroundTaskSafely(&backgroundTaskID, taskName: "Override Cancel")
+            if var backgroundTaskID = backgroundTaskID {
+                endBackgroundTaskSafely(&backgroundTaskID, taskName: "Override Cancel")
+            }
         }
     }
 }

+ 48 - 18
Trio/Sources/Shortcuts/TempPresets/TempPresetsIntentRequest.swift

@@ -8,6 +8,8 @@ final class TempPresetsIntentRequest: BaseIntentsRequest {
         case noDurationDefined
     }
 
+    private var intentSuccess: Bool = false
+
     func fetchAndProcessTempTargets() async throws -> [TempPreset] {
         // Fetch all Temp Target Presets via TempTargetStorage
         let allTempTargetPresetsIDs = try await tempTargetsStorage.fetchForTempTargetPresets()
@@ -85,9 +87,12 @@ final class TempPresetsIntentRequest: BaseIntentsRequest {
     }
 
     @MainActor func enactTempTarget(_ preset: TempPreset) async -> Bool {
+        debug(.default, "Enacting Temp Target: \(preset.name)")
+        intentSuccess = false
+
         // Start background task
         var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid
-        backgroundTaskID = startBackgroundTask(withName: "TempTarget Upload")
+        backgroundTaskID = startBackgroundTask(withName: "TempTarget Enact")
 
         do {
             // Get NSManagedObjectID of Preset
@@ -108,13 +113,15 @@ final class TempPresetsIntentRequest: BaseIntentsRequest {
             )
 
             if viewContext.hasChanges {
+                debug(.default, "Saving changes...")
+
                 try viewContext.save()
 
                 // Update State variables in TempTargetView
                 Foundation.NotificationCenter.default.post(name: .willUpdateTempTargetConfiguration, object: nil)
 
                 // Await the notification
-                print("Waiting for notification...")
+                debug(.default, "Waiting for notification...")
 
                 guard let tempTargetDate = tempTargetObject.date, let tempTarget = tempTargetObject.target,
                       let tempTargetDuration = tempTargetObject.duration else { return false }
@@ -136,22 +143,27 @@ final class TempPresetsIntentRequest: BaseIntentsRequest {
                 tempTargetsStorage.saveTempTargetsToStorage([tempTargetToStoreAsJSON])
 
                 await awaitNotification(.didUpdateTempTargetConfiguration)
-                print("Notification received, continuing...")
 
-                endBackgroundTaskSafely(&backgroundTaskID, taskName: "TempTarget Upload")
-
-                return true
+                debug(.default, "Notification received, continuing...")
+                intentSuccess = true
             }
+
+            endBackgroundTaskSafely(&backgroundTaskID, taskName: "TempTarget Enact")
+
+            debug(.default, "Finished. Temp Target enacted via Shortcut.")
+
+            return intentSuccess
         } catch {
             // Handle error and ensure background task is ended
-            debugPrint("Failed to enact TempTarget: \(error.localizedDescription)")
+            debugPrint(
+                "\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to enact Temp Targett with error: \(error.localizedDescription)"
+            )
 
-            endBackgroundTaskSafely(&backgroundTaskID, taskName: "TempTarget Upload")
+            endBackgroundTaskSafely(&backgroundTaskID, taskName: "TempTarget Enact")
 
-            return false
+            intentSuccess = false
+            return intentSuccess
         }
-
-        return false
     }
 
     func cancelTempTarget() async {
@@ -164,10 +176,11 @@ final class TempPresetsIntentRequest: BaseIntentsRequest {
         createTempTargetRunEntry: Bool,
         shouldStartBackgroundTask: Bool = true
     ) async {
-        var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid
+        var backgroundTaskID: UIBackgroundTaskIdentifier?
 
         if shouldStartBackgroundTask {
             // Start background task
+            backgroundTaskID = .invalid
             backgroundTaskID = startBackgroundTask(withName: "TempTarget Cancel")
         }
 
@@ -180,7 +193,15 @@ final class TempPresetsIntentRequest: BaseIntentsRequest {
             }
 
             // Return early if no results
-            guard !results.isEmpty else { return }
+            guard !results.isEmpty else {
+                debug(.default, "No active temp targets to cancel... returning early")
+
+                if var backgroundTaskID = backgroundTaskID {
+                    debug(.default, "Ending background task for temp target cancel")
+                    endBackgroundTaskSafely(&backgroundTaskID, taskName: "TempTarget Cancel")
+                }
+                return
+            }
 
             // Create TempTargetRunStored entry if needed
             if createTempTargetRunEntry {
@@ -214,17 +235,26 @@ final class TempPresetsIntentRequest: BaseIntentsRequest {
             }
 
             // Await the notification
-            print("Waiting for notification...")
-            await awaitNotification(.didUpdateTempTargetConfiguration)
-            print("Notification received, continuing...")
+            // Await the notification
+            debug(.default, "Waiting for notification...")
 
-            endBackgroundTaskSafely(&backgroundTaskID, taskName: "TempTarget Cancel")
+            await awaitNotification(.didUpdateOverrideConfiguration)
+
+            debug(.default, "Notification received, continuing...")
+
+            if var backgroundTaskID = backgroundTaskID {
+                debug(.default, "Ending background task for temp target cancel")
+                endBackgroundTaskSafely(&backgroundTaskID, taskName: "TempTarget Cancel")
+            }
         } catch {
             debugPrint(
                 "\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to disable active Temp Targets with error: \(error.localizedDescription)"
             )
 
-            endBackgroundTaskSafely(&backgroundTaskID, taskName: "TempTarget Cancel")
+            if var backgroundTaskID = backgroundTaskID {
+                debug(.default, "Ending background task for temp target cancel")
+                endBackgroundTaskSafely(&backgroundTaskID, taskName: "TempTarget Cancel")
+            }
         }
     }
 }