Skip to content

Commit

Permalink
Cancel cuttent temp target if get a new active one
Browse files Browse the repository at this point in the history
  • Loading branch information
ivalkou committed Jan 19, 2022
1 parent 9104ae4 commit 012af03
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 98 deletions.

This file was deleted.

11 changes: 11 additions & 0 deletions FreeAPS/Sources/APS/Storage/TempTargetsStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions FreeAPS/Sources/Models/TempTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

4 comments on commit 012af03

@bjornoleh
Copy link

@bjornoleh bjornoleh commented on 012af03 Jan 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix @ivalkou.

When testing this now with a clean build of dev to a backup phone, there seems to be a display issue still:

Here, I activated a TT 7,0 / 60 min, and after a very short time I changed this to TT 4,0 / 3 min.

After the short 4,0 TT expired, the 7,0 TT is displayed as active again.

The scheduled target of 5,5 mmol/L is used in Enacted, so it seems the first TT is correctly cancelled by starting the next one.

Screenshot 2022-01-19 at 21 54 47

I have checked this twice now.

@mountrcg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, can confirm.

@mountrcg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9be9154 fixes the issue, thanks a lot.

@bjornoleh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9be9154 fixes the issue, thanks a lot.

Confirmed! Thanks!

Please sign in to comment.