Skip to content

LOOP-5315 Editing custom presets #664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions LoopKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@
89ADE12F226BDED40067222B /* DateRelativeBolusEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89ADE12E226BDED40067222B /* DateRelativeBolusEntry.swift */; };
89ADE134226BF0490067222B /* TestingScenarioInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89ADE133226BF0490067222B /* TestingScenarioInstance.swift */; };
89ADE136226BF0BE0067222B /* DateRelativeGlucoseSample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89ADE135226BF0BE0067222B /* DateRelativeGlucoseSample.swift */; };
89AE2226228BC54C00BDFD85 /* TemporaryScheduleOverridePreset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE221F228BC54C00BDFD85 /* TemporaryScheduleOverridePreset.swift */; };
89AE2226228BC54C00BDFD85 /* TemporaryPreset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE221F228BC54C00BDFD85 /* TemporaryPreset.swift */; };
89AE2227228BC54C00BDFD85 /* TemporaryScheduleOverride.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE2223228BC54C00BDFD85 /* TemporaryScheduleOverride.swift */; };
89AE2228228BC54C00BDFD85 /* TemporaryScheduleOverrideSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE2224228BC54C00BDFD85 /* TemporaryScheduleOverrideSettings.swift */; };
89AE2228228BC54C00BDFD85 /* TemporaryPresetSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE2224228BC54C00BDFD85 /* TemporaryPresetSettings.swift */; };
89AE2229228BC54C00BDFD85 /* TemporaryScheduleOverrideHistory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE2225228BC54C00BDFD85 /* TemporaryScheduleOverrideHistory.swift */; };
89AE222B228BC56A00BDFD85 /* WeakSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE222A228BC56A00BDFD85 /* WeakSet.swift */; };
89AE222C228BC66E00BDFD85 /* Locked.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AC792E224C781200B8E9BA /* Locked.swift */; };
Expand All @@ -391,8 +391,8 @@
89AE2237228BCAAB00BDFD85 /* EGPSchedule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 891A3FD8224BEB4500378B27 /* EGPSchedule.swift */; };
89AE2238228BCAAB00BDFD85 /* TemporaryScheduleOverride.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE2223228BC54C00BDFD85 /* TemporaryScheduleOverride.swift */; };
89AE2239228BCAAB00BDFD85 /* TemporaryScheduleOverrideHistory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE2225228BC54C00BDFD85 /* TemporaryScheduleOverrideHistory.swift */; };
89AE223A228BCAAB00BDFD85 /* TemporaryScheduleOverridePreset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE221F228BC54C00BDFD85 /* TemporaryScheduleOverridePreset.swift */; };
89AE223B228BCAAB00BDFD85 /* TemporaryScheduleOverrideSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE2224228BC54C00BDFD85 /* TemporaryScheduleOverrideSettings.swift */; };
89AE223A228BCAAB00BDFD85 /* TemporaryPreset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE221F228BC54C00BDFD85 /* TemporaryPreset.swift */; };
89AE223B228BCAAB00BDFD85 /* TemporaryPresetSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE2224228BC54C00BDFD85 /* TemporaryPresetSettings.swift */; };
89AE223C228BCAAB00BDFD85 /* WeakSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AE222A228BC56A00BDFD85 /* WeakSet.swift */; };
89AE223E228BD3C400BDFD85 /* SetConstrainedScheduleEntryTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 892A5D62222F6B13008961AB /* SetConstrainedScheduleEntryTableViewCell.xib */; };
89AF78C02447E285002B4FCC /* CardStackBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89AF78BF2447E285002B4FCC /* CardStackBuilder.swift */; };
Expand Down Expand Up @@ -1421,9 +1421,9 @@
89ADE12E226BDED40067222B /* DateRelativeBolusEntry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateRelativeBolusEntry.swift; sourceTree = "<group>"; };
89ADE133226BF0490067222B /* TestingScenarioInstance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestingScenarioInstance.swift; sourceTree = "<group>"; };
89ADE135226BF0BE0067222B /* DateRelativeGlucoseSample.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateRelativeGlucoseSample.swift; sourceTree = "<group>"; };
89AE221F228BC54C00BDFD85 /* TemporaryScheduleOverridePreset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TemporaryScheduleOverridePreset.swift; sourceTree = "<group>"; };
89AE221F228BC54C00BDFD85 /* TemporaryPreset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TemporaryPreset.swift; sourceTree = "<group>"; };
89AE2223228BC54C00BDFD85 /* TemporaryScheduleOverride.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TemporaryScheduleOverride.swift; sourceTree = "<group>"; };
89AE2224228BC54C00BDFD85 /* TemporaryScheduleOverrideSettings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TemporaryScheduleOverrideSettings.swift; sourceTree = "<group>"; };
89AE2224228BC54C00BDFD85 /* TemporaryPresetSettings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TemporaryPresetSettings.swift; sourceTree = "<group>"; };
89AE2225228BC54C00BDFD85 /* TemporaryScheduleOverrideHistory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TemporaryScheduleOverrideHistory.swift; sourceTree = "<group>"; };
89AE222A228BC56A00BDFD85 /* WeakSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WeakSet.swift; sourceTree = "<group>"; };
89AF78BF2447E285002B4FCC /* CardStackBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardStackBuilder.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2531,8 +2531,8 @@
A932803A2798D63B0091D0A1 /* SyncAlertObject.swift */,
89AE2223228BC54C00BDFD85 /* TemporaryScheduleOverride.swift */,
89AE2225228BC54C00BDFD85 /* TemporaryScheduleOverrideHistory.swift */,
89AE221F228BC54C00BDFD85 /* TemporaryScheduleOverridePreset.swift */,
89AE2224228BC54C00BDFD85 /* TemporaryScheduleOverrideSettings.swift */,
89AE221F228BC54C00BDFD85 /* TemporaryPreset.swift */,
89AE2224228BC54C00BDFD85 /* TemporaryPresetSettings.swift */,
1D1FCE2824BE4F11000300A8 /* TherapySetting.swift */,
E9086B3824B3CB4B0062F5C8 /* TherapySettings.swift */,
C1ABE38D2245CFCD00570E82 /* UnfairLock.swift */,
Expand Down Expand Up @@ -4150,7 +4150,7 @@
4322B783202FA2AF0002837D /* Reservoir.swift in Sources */,
1DA649AB2445174400F61E75 /* Alert.swift in Sources */,
4322B782202FA2AF0002837D /* PumpEventType.swift in Sources */,
89AE2228228BC54C00BDFD85 /* TemporaryScheduleOverrideSettings.swift in Sources */,
89AE2228228BC54C00BDFD85 /* TemporaryPresetSettings.swift in Sources */,
43D8FDFA1C7290350073BE78 /* GlucoseRangeSchedule.swift in Sources */,
4322B77E202FA2AF0002837D /* PersistedPumpEvent.swift in Sources */,
437AFEEE2036A156008C4892 /* CachedCarbObject+CoreDataProperties.swift in Sources */,
Expand All @@ -4159,7 +4159,7 @@
4322B77B202FA2790002837D /* StoredCarbEntry.swift in Sources */,
4322B78C202FA2B30002837D /* HKQuantitySample+InsulinKit.swift in Sources */,
C187339229B9486200519CDF /* MutableCollection.swift in Sources */,
89AE2226228BC54C00BDFD85 /* TemporaryScheduleOverridePreset.swift in Sources */,
89AE2226228BC54C00BDFD85 /* TemporaryPreset.swift in Sources */,
434C5F9C2098352500B2FD1A /* QuantityFormatter.swift in Sources */,
43F503642106C78C009FA89A /* ServiceAuthentication.swift in Sources */,
A93761C125ED670200F6BE43 /* BluetoothProvider.swift in Sources */,
Expand Down Expand Up @@ -4407,8 +4407,8 @@
A933DB8924BF97CC009B417A /* CriticalEventLog.swift in Sources */,
89AE2238228BCAAB00BDFD85 /* TemporaryScheduleOverride.swift in Sources */,
89AE2239228BCAAB00BDFD85 /* TemporaryScheduleOverrideHistory.swift in Sources */,
89AE223A228BCAAB00BDFD85 /* TemporaryScheduleOverridePreset.swift in Sources */,
89AE223B228BCAAB00BDFD85 /* TemporaryScheduleOverrideSettings.swift in Sources */,
89AE223A228BCAAB00BDFD85 /* TemporaryPreset.swift in Sources */,
89AE223B228BCAAB00BDFD85 /* TemporaryPresetSettings.swift in Sources */,
A987CD4724A5893500439ADC /* JSONStreamEncoder.swift in Sources */,
C1CEBFD929BA9D81007FD8A3 /* TimeZone.swift in Sources */,
89AE223C228BCAAB00BDFD85 /* WeakSet.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion LoopKit/DailyQuantitySchedule+Override.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extension DailyValueSchedule where T == Double {
fileprivate func applyingOverride(
_ override: TemporaryScheduleOverride,
relativeTo date: Date,
multiplier multiplierKeyPath: KeyPath<TemporaryScheduleOverrideSettings, Double?>
multiplier multiplierKeyPath: KeyPath<TemporaryPresetSettings, Double?>
) -> DailyValueSchedule {
guard let multiplier = override.settings[keyPath: multiplierKeyPath] else {
return self
Expand Down
6 changes: 3 additions & 3 deletions LoopKit/SettingsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public struct StoredSettings: Equatable {
public var preMealTargetRange: ClosedRange<LoopQuantity>?
public var workoutTargetRange: ClosedRange<LoopQuantity>?
public var workoutDefaultDuration: TemporaryScheduleOverride.Duration?
public var overridePresets: [TemporaryScheduleOverridePreset]
public var overridePresets: [TemporaryPreset]
public var maximumBasalRatePerHour: Double?
public var maximumBolus: Double?
public var suspendThreshold: GlucoseThreshold?
Expand All @@ -322,7 +322,7 @@ public struct StoredSettings: Equatable {
preMealTargetRange: ClosedRange<LoopQuantity>? = nil,
workoutTargetRange: ClosedRange<LoopQuantity>? = nil,
workoutDefaultDuration: TemporaryScheduleOverride.Duration? = nil,
overridePresets: [TemporaryScheduleOverridePreset] = [],
overridePresets: [TemporaryPreset] = [],
maximumBasalRatePerHour: Double? = nil,
maximumBolus: Double? = nil,
suspendThreshold: GlucoseThreshold? = nil,
Expand Down Expand Up @@ -380,7 +380,7 @@ extension StoredSettings: Codable {
preMealTargetRange: try container.decodeIfPresent(DoubleRange.self, forKey: .preMealTargetRange)?.quantityRange(for: bloodGlucoseUnit),
workoutTargetRange: try container.decodeIfPresent(DoubleRange.self, forKey: .workoutTargetRange)?.quantityRange(for: bloodGlucoseUnit),
workoutDefaultDuration: try container.decodeIfPresent(TemporaryScheduleOverride.Duration.self, forKey: .workoutDefaultDuration),
overridePresets: try container.decodeIfPresent([TemporaryScheduleOverridePreset].self, forKey: .overridePresets) ?? [],
overridePresets: try container.decodeIfPresent([TemporaryPreset].self, forKey: .overridePresets) ?? [],
maximumBasalRatePerHour: try container.decodeIfPresent(Double.self, forKey: .maximumBasalRatePerHour),
maximumBolus: try container.decodeIfPresent(Double.self, forKey: .maximumBolus),
suspendThreshold: try container.decodeIfPresent(GlucoseThreshold.self, forKey: .suspendThreshold),
Expand Down
137 changes: 137 additions & 0 deletions LoopKit/TemporaryPreset.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//
// TemporaryPreset.swift
// Loop
//
// Created by Michael Pangburn on 1/2/19.
// Copyright © 2019 LoopKit Authors. All rights reserved.
//

import Foundation

public struct PresetScheduleRepeatOptions: OptionSet, Sendable, Hashable {
public let rawValue: UInt8

public static let none = PresetScheduleRepeatOptions([])
public static let sunday = PresetScheduleRepeatOptions(rawValue: 1 << 0)
public static let monday = PresetScheduleRepeatOptions(rawValue: 1 << 1)
public static let tuesday = PresetScheduleRepeatOptions(rawValue: 1 << 2)
public static let wednesday = PresetScheduleRepeatOptions(rawValue: 1 << 3)
public static let thursday = PresetScheduleRepeatOptions(rawValue: 1 << 4)
public static let friday = PresetScheduleRepeatOptions(rawValue: 1 << 5)
public static let saturday = PresetScheduleRepeatOptions(rawValue: 1 << 6)

public static let allCases: [PresetScheduleRepeatOptions] = [
.sunday,
.monday,
.tuesday,
.wednesday,
.thursday,
.friday,
.saturday,
]
Comment on lines +23 to +31

Choose a reason for hiding this comment

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

what about weekend and weekdays?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I think that'd be a nice feature, but not part of the current design. It's a multiselect, so you can use this to do that.


public init(rawValue: UInt8) {
self.rawValue = rawValue
}

// Helper to map OptionSet to calendar weekday index (Sunday = 1 in Calendar)
public var calendarWeekdayIndex: Int? {
switch self {
case .sunday: return 1
case .monday: return 2
case .tuesday: return 3
case .wednesday: return 4
case .thursday: return 5
case .friday: return 6
case .saturday: return 7
default: return nil
}
}
}

extension PresetScheduleRepeatOptions: Codable {}

public struct TemporaryPreset: Hashable, Sendable {
public let id: UUID
public var symbol: String
public var name: String
public var settings: TemporaryPresetSettings
public var duration: TemporaryScheduleOverride.Duration
public var scheduleStartDate: Date?
public var repeatOptions: PresetScheduleRepeatOptions?

public init(id: UUID = UUID(), symbol: String, name: String, settings: TemporaryPresetSettings, duration: TemporaryScheduleOverride.Duration, scheduleStartDate: Date? = nil, repeatOptions: PresetScheduleRepeatOptions = .none) {
self.id = id
self.symbol = symbol
self.name = name
self.settings = settings
self.duration = duration
self.scheduleStartDate = scheduleStartDate
self.repeatOptions = repeatOptions
}

public func createOverride(enactTrigger: TemporaryScheduleOverride.EnactTrigger, beginningAt date: Date = Date()) -> TemporaryScheduleOverride {
return TemporaryScheduleOverride(
context: .preset(self),
settings: settings,
startDate: date,
duration: duration,
enactTrigger: enactTrigger,
syncIdentifier: UUID()
)
}
}

extension TemporaryPreset: RawRepresentable {
public typealias RawValue = [String: Any]

public init?(rawValue: RawValue) {
guard
let idString = rawValue["id"] as? String,
let id = UUID(uuidString: idString),
let symbol = rawValue["symbol"] as? String,
let name = rawValue["name"] as? String,
let settingsRawValue = rawValue["settings"] as? TemporaryPresetSettings.RawValue,
let settings = TemporaryPresetSettings(rawValue: settingsRawValue),
let durationRawValue = rawValue["duration"] as? TemporaryScheduleOverride.Duration.RawValue,
let duration = TemporaryScheduleOverride.Duration(rawValue: durationRawValue)
else {
return nil
}

let scheduleStartDate = rawValue["scheduleStartDate"] as? Date
let rawRepeatOptions = rawValue["repeatOptions"] as? PresetScheduleRepeatOptions.RawValue

self.init(
id: id,
symbol: symbol,
name: name,
settings: settings,
duration: duration,
scheduleStartDate: scheduleStartDate,
repeatOptions: rawRepeatOptions.flatMap(PresetScheduleRepeatOptions.init) ?? .none
)
}

public var rawValue: RawValue {
var rval: RawValue = [
"id": id.uuidString,
"symbol": symbol,
"name": name,
"settings": settings.rawValue,
"duration": duration.rawValue
]

if let scheduleStartDate {
rval["scheduleStartDate"] = scheduleStartDate
}

if let repeatOptions {
rval["repeatOptions"] = repeatOptions.rawValue
}

return rval
}
}

extension TemporaryPreset: Codable {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import HealthKit
import LoopAlgorithm

public struct TemporaryScheduleOverrideSettings: Hashable, Sendable {
public struct TemporaryPresetSettings: Hashable, Sendable {
private var targetRangeInMgdl: DoubleRange?
public var insulinNeedsScaleFactor: Double?

Expand Down Expand Up @@ -43,7 +43,7 @@ public struct TemporaryScheduleOverrideSettings: Hashable, Sendable {
}
}

extension TemporaryScheduleOverrideSettings: RawRepresentable {
extension TemporaryPresetSettings: RawRepresentable {
public typealias RawValue = [String: Any]

private enum Key {
Expand Down Expand Up @@ -84,4 +84,4 @@ extension TemporaryScheduleOverrideSettings: RawRepresentable {
}
}

extension TemporaryScheduleOverrideSettings: Codable {}
extension TemporaryPresetSettings: Codable {}
16 changes: 8 additions & 8 deletions LoopKit/TemporaryScheduleOverride.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public struct TemporaryScheduleOverride: Hashable, Sendable {
public enum Context: Hashable, Sendable {
case preMeal
case legacyWorkout
case preset(TemporaryScheduleOverridePreset)
case preset(TemporaryPreset)
case custom
}

Expand Down Expand Up @@ -50,7 +50,7 @@ public struct TemporaryScheduleOverride: Hashable, Sendable {
}

public var context: Context
public var settings: TemporaryScheduleOverrideSettings
public var settings: TemporaryPresetSettings
public var startDate: Date
public let enactTrigger: EnactTrigger
public let syncIdentifier: UUID
Expand Down Expand Up @@ -112,7 +112,7 @@ public struct TemporaryScheduleOverride: Hashable, Sendable {

public init(
context: Context,
settings: TemporaryScheduleOverrideSettings,
settings: TemporaryPresetSettings,
startDate: Date,
duration: Duration,
enactTrigger: EnactTrigger,
Expand Down Expand Up @@ -141,8 +141,8 @@ extension TemporaryScheduleOverride: RawRepresentable {
guard
let contextRawValue = rawValue["context"] as? Context.RawValue,
let context = Context(rawValue: contextRawValue),
let settingsRawValue = rawValue["settings"] as? TemporaryScheduleOverrideSettings.RawValue,
let settings = TemporaryScheduleOverrideSettings(rawValue: settingsRawValue),
let settingsRawValue = rawValue["settings"] as? TemporaryPresetSettings.RawValue,
let settings = TemporaryPresetSettings(rawValue: settingsRawValue),
let startDateSeconds = rawValue["startDate"] as? TimeInterval,
let durationRawValue = rawValue["duration"] as? Duration.RawValue,
let duration = Duration(rawValue: durationRawValue)
Expand Down Expand Up @@ -201,8 +201,8 @@ extension TemporaryScheduleOverride.Context: RawRepresentable {
self = .legacyWorkout
case "preset":
guard
let presetRawValue = rawValue["preset"] as? TemporaryScheduleOverridePreset.RawValue,
let preset = TemporaryScheduleOverridePreset(rawValue: presetRawValue)
let presetRawValue = rawValue["preset"] as? TemporaryPreset.RawValue,
let preset = TemporaryPreset(rawValue: presetRawValue)
else {
return nil
}
Expand Down Expand Up @@ -272,7 +272,7 @@ extension TemporaryScheduleOverride.Context: Codable {
}

private struct Preset: Codable {
let preset: TemporaryScheduleOverridePreset
let preset: TemporaryPreset
}

private enum CodableKeys: String, CodingKey {
Expand Down
2 changes: 1 addition & 1 deletion LoopKit/TemporaryScheduleOverrideHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public class TemporaryScheduleOverrideHistoryContainer {

return persisted
} catch {
fatalError()
fatalError(error.localizedDescription)
}
}
}
Loading