Explorar o código

Cancel cuttent temp target if get a new active one

Ivan Valkou %!s(int64=4) %!d(string=hai) anos
pai
achega
60b275da7d

+ 0 - 87
Dependencies/LoopKit/LoopKit.xcodeproj/xcshareddata/xcschemes/LoopKit Example.xcscheme

@@ -1,87 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<Scheme
-   LastUpgradeVersion = "1240"
-   version = "1.3">
-   <BuildAction
-      parallelizeBuildables = "YES"
-      buildImplicitDependencies = "YES">
-      <BuildActionEntries>
-         <BuildActionEntry
-            buildForTesting = "YES"
-            buildForRunning = "YES"
-            buildForProfiling = "YES"
-            buildForArchiving = "YES"
-            buildForAnalyzing = "YES">
-            <BuildableReference
-               BuildableIdentifier = "primary"
-               BlueprintIdentifier = "430157F61C7EC03B00B64B63"
-               BuildableName = "LoopKit Example.app"
-               BlueprintName = "LoopKit Example"
-               ReferencedContainer = "container:LoopKit.xcodeproj">
-            </BuildableReference>
-         </BuildActionEntry>
-      </BuildActionEntries>
-   </BuildAction>
-   <TestAction
-      buildConfiguration = "Debug"
-      selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
-      selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
-      shouldUseLaunchSchemeArgsEnv = "YES">
-      <MacroExpansion>
-         <BuildableReference
-            BuildableIdentifier = "primary"
-            BlueprintIdentifier = "430157F61C7EC03B00B64B63"
-            BuildableName = "LoopKit Example.app"
-            BlueprintName = "LoopKit Example"
-            ReferencedContainer = "container:LoopKit.xcodeproj">
-         </BuildableReference>
-      </MacroExpansion>
-      <Testables>
-      </Testables>
-   </TestAction>
-   <LaunchAction
-      buildConfiguration = "Debug"
-      selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
-      selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
-      launchStyle = "0"
-      useCustomWorkingDirectory = "NO"
-      ignoresPersistentStateOnLaunch = "NO"
-      debugDocumentVersioning = "YES"
-      debugServiceExtension = "internal"
-      allowLocationSimulation = "YES">
-      <BuildableProductRunnable
-         runnableDebuggingMode = "0">
-         <BuildableReference
-            BuildableIdentifier = "primary"
-            BlueprintIdentifier = "430157F61C7EC03B00B64B63"
-            BuildableName = "LoopKit Example.app"
-            BlueprintName = "LoopKit Example"
-            ReferencedContainer = "container:LoopKit.xcodeproj">
-         </BuildableReference>
-      </BuildableProductRunnable>
-   </LaunchAction>
-   <ProfileAction
-      buildConfiguration = "Release"
-      shouldUseLaunchSchemeArgsEnv = "YES"
-      savedToolIdentifier = ""
-      useCustomWorkingDirectory = "NO"
-      debugDocumentVersioning = "YES">
-      <BuildableProductRunnable
-         runnableDebuggingMode = "0">
-         <BuildableReference
-            BuildableIdentifier = "primary"
-            BlueprintIdentifier = "430157F61C7EC03B00B64B63"
-            BuildableName = "LoopKit Example.app"
-            BlueprintName = "LoopKit Example"
-            ReferencedContainer = "container:LoopKit.xcodeproj">
-         </BuildableReference>
-      </BuildableProductRunnable>
-   </ProfileAction>
-   <AnalyzeAction
-      buildConfiguration = "Debug">
-   </AnalyzeAction>
-   <ArchiveAction
-      buildConfiguration = "Release"
-      revealArchiveInOrganizer = "YES">
-   </ArchiveAction>
-</Scheme>

+ 11 - 0
FreeAPS/Sources/APS/Storage/TempTargetsStorage.swift

@@ -31,6 +31,17 @@ final class BaseTempTargetsStorage: TempTargetsStorage, Injectable {
 
     private func storeTempTargets(_ targets: [TempTarget], isPresets: Bool) {
         processQueue.sync {
+            var targets = targets
+            if !isPresets {
+                if current() != nil, let newActive = targets.last(where: {
+                    $0.createdAt.addingTimeInterval(Int($0.duration).minutes.timeInterval) > Date()
+                        && $0.createdAt <= Date()
+                }) {
+                    // cancel current
+                    targets += [TempTarget.cancel(at: newActive.createdAt.addingTimeInterval(-1))]
+                }
+            }
+
             let file = isPresets ? OpenAPS.FreeAPS.tempTargetsPresets : OpenAPS.Settings.tempTargets
             var uniqEvents: [TempTarget] = []
             self.storage.transaction { storage in

+ 12 - 0
FreeAPS/Sources/Models/TempTarget.swift

@@ -25,6 +25,18 @@ struct TempTarget: JSON, Identifiable, Equatable, Hashable {
     func hash(into hasher: inout Hasher) {
         hasher.combine(createdAt)
     }
+
+    static func cancel(at date: Date) -> TempTarget {
+        TempTarget(
+            name: TempTarget.cancel,
+            createdAt: date,
+            targetTop: 0,
+            targetBottom: 0,
+            duration: 0,
+            enteredBy: TempTarget.manual,
+            reason: TempTarget.cancel
+        )
+    }
 }
 
 extension TempTarget {

+ 1 - 11
FreeAPS/Sources/Modules/AddTempTarget/AddTempTargetStateModel.swift

@@ -45,17 +45,7 @@ extension AddTempTarget {
         }
 
         func cancel() {
-            let entry = TempTarget(
-                name: TempTarget.cancel,
-                createdAt: Date(),
-                targetTop: 0,
-                targetBottom: 0,
-                duration: 0,
-                enteredBy: TempTarget.manual,
-                reason: TempTarget.cancel
-            )
-            storage.storeTempTargets([entry])
-
+            storage.storeTempTargets([TempTarget.cancel(at: Date())])
             showModal(for: nil)
         }