Skip to content

Commit

Permalink
[LOOP-4754] Presets Storage (#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
Camji55 authored Dec 9, 2024
1 parent f1cf4bf commit 84ebeb3
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 22 deletions.
10 changes: 7 additions & 3 deletions Loop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
84E8BBCE2CCA1E070078E6CF /* PresetsTrainingCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84E8BBCD2CCA1E070078E6CF /* PresetsTrainingCard.swift */; };
84E8BBD02CCA279B0078E6CF /* Image+Exists.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84E8BBCF2CCA27960078E6CF /* Image+Exists.swift */; };
84EC162E2C9115CA00D220C5 /* DIYLoopUnitTestPlan.xctestplan in Resources */ = {isa = PBXBuildFile; fileRef = 84EC162D2C9115CA00D220C5 /* DIYLoopUnitTestPlan.xctestplan */; };
84FA9D332CF7FD0D004162B4 /* PresetsHistoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84FA9D322CF7FD0D004162B4 /* PresetsHistoryView.swift */; };
891B508524342BE1005DA578 /* CarbAndBolusFlowViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 891B508424342BE1005DA578 /* CarbAndBolusFlowViewModel.swift */; };
892A5D59222F0A27008961AB /* Debug.swift in Sources */ = {isa = PBXBuildFile; fileRef = 892A5D58222F0A27008961AB /* Debug.swift */; };
892A5D692230C41D008961AB /* RangeReplaceableCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 892A5D682230C41D008961AB /* RangeReplaceableCollection.swift */; };
Expand Down Expand Up @@ -1145,6 +1146,7 @@
84E8BBCD2CCA1E070078E6CF /* PresetsTrainingCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PresetsTrainingCard.swift; sourceTree = "<group>"; };
84E8BBCF2CCA27960078E6CF /* Image+Exists.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Image+Exists.swift"; sourceTree = "<group>"; };
84EC162D2C9115CA00D220C5 /* DIYLoopUnitTestPlan.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = DIYLoopUnitTestPlan.xctestplan; sourceTree = "<group>"; };
84FA9D322CF7FD0D004162B4 /* PresetsHistoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PresetsHistoryView.swift; sourceTree = "<group>"; };
891B508424342BE1005DA578 /* CarbAndBolusFlowViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CarbAndBolusFlowViewModel.swift; sourceTree = "<group>"; };
892A5D29222EF60A008961AB /* MockKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = MockKit.framework; path = Carthage/Build/iOS/MockKit.framework; sourceTree = SOURCE_ROOT; };
892A5D2B222EF60A008961AB /* MockKitUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = MockKitUI.framework; path = Carthage/Build/iOS/MockKitUI.framework; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -2484,6 +2486,7 @@
isa = PBXGroup;
children = (
84E8BBC92CCA16290078E6CF /* PresetsView.swift */,
84FA9D322CF7FD0D004162B4 /* PresetsHistoryView.swift */,
84E8BBB02CC9793C0078E6CF /* PresetsTrainingView.swift */,
84E8BBC22CC9B9780078E6CF /* Components */,
84E8BBB62CC990480078E6CF /* Training Content */,
Expand Down Expand Up @@ -3657,6 +3660,7 @@
84E8BBC82CC9D34B0078E6CF /* TherapySettingsExampleView.swift in Sources */,
1D05219B2469E9DF000EBBDE /* StoredAlert.swift in Sources */,
E9B0802B253BBDFF00BAD8F8 /* IntentExtensionInfo.swift in Sources */,
84FA9D332CF7FD0D004162B4 /* PresetsHistoryView.swift in Sources */,
C1E3862628247C6100F561A4 /* StoredLoopNotRunningNotification.swift in Sources */,
A97F250825E056D500F0EE19 /* OnboardingManager.swift in Sources */,
438D42F91D7C88BC003244B0 /* PredictionInputEffect.swift in Sources */,
Expand Down Expand Up @@ -4751,7 +4755,7 @@
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
WARNING_CFLAGS = "-Wall";
WATCHOS_DEPLOYMENT_TARGET = 8.0;
WATCHOS_DEPLOYMENT_TARGET = 10.6;
};
name = Debug;
};
Expand Down Expand Up @@ -4861,7 +4865,7 @@
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
WARNING_CFLAGS = "-Wall";
WATCHOS_DEPLOYMENT_TARGET = 8.0;
WATCHOS_DEPLOYMENT_TARGET = 10.6;
};
name = Release;
};
Expand Down Expand Up @@ -5297,7 +5301,7 @@
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
WARNING_CFLAGS = "-Wall";
WATCHOS_DEPLOYMENT_TARGET = 8.0;
WATCHOS_DEPLOYMENT_TARGET = 10.6;
};
name = Testflight;
};
Expand Down
3 changes: 2 additions & 1 deletion Loop/Managers/LoopAppManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,8 @@ extension LoopAppManager: UNUserNotificationCenterDelegate {

extension LoopAppManager: TemporaryScheduleOverrideHistoryDelegate {
func temporaryScheduleOverrideHistoryDidUpdate(_ history: TemporaryScheduleOverrideHistory) {
UserDefaults.appGroup?.overrideHistory = history
TemporaryScheduleOverrideHistoryContainer.shared.deleteAll()
TemporaryScheduleOverrideHistoryContainer.shared.context.insert(history)
remoteDataServicesManager.triggerUpload(for: .overrides)
}
}
Expand Down
8 changes: 5 additions & 3 deletions Loop/Managers/TemporaryPresetsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ class TemporaryPresetsManager {

private var settingsProvider: SettingsProvider

var overrideHistory = UserDefaults.appGroup?.overrideHistory ?? TemporaryScheduleOverrideHistory.init()
var overrideHistory: TemporaryScheduleOverrideHistory

private var presetActivationObservers: [PresetActivationObserver] = []

private var overrideIntentObserver: NSKeyValueObservation? = nil

@MainActor
init(settingsProvider: SettingsProvider) {
self.settingsProvider = settingsProvider

self.overrideHistory.relevantTimeWindow = LoopCoreConstants.defaultCarbAbsorptionTimes.slow * 2

self.overrideHistory = TemporaryScheduleOverrideHistoryContainer.shared.fetch()
TemporaryScheduleOverrideHistory.relevantTimeWindow = Bundle.main.localCacheDuration

scheduleOverride = overrideHistory.activeOverride(at: Date())

Expand Down
52 changes: 52 additions & 0 deletions Loop/Views/Presets/PresetsHistoryView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// PresetsHistoryView.swift
// Loop
//
// Created by Cameron Ingham on 11/27/24.
// Copyright © 2024 LoopKit Authors. All rights reserved.
//

import LoopKit
import SwiftUI

struct PresetsHistoryView: View {

@State var history: TemporaryScheduleOverrideHistory

init () {
self.history = TemporaryScheduleOverrideHistoryContainer.shared.fetch()
}

let formatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.unitsStyle = .short
return formatter
}()

var body: some View {
List {
Section("Recent Events") {
ForEach(history.recentEvents.sorted(by: { $0.override.actualEndDate > $1.override.actualEndDate }), id: \.self) { recentEvent in

let scheduledDuration = recentEvent.override.duration.timeInterval
let actualDuration = recentEvent.override.actualDuration.timeInterval

let value = scheduledDuration == actualDuration ? "\(formatter.string(from: scheduledDuration) ?? "")" : "\(formatter.string(from: actualDuration) ?? "") / \(formatter.string(from: scheduledDuration) ?? "")"

LabeledContent {
Text(value)
} label: {
Text(recentEvent.override.presetId)

Text(recentEvent.override.startDate.formatted(date: .abbreviated, time: .shortened))
}
}
}
}
}
}

#Preview {
PresetsHistoryView()
}
5 changes: 3 additions & 2 deletions Loop/Views/Presets/PresetsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
// Copyright © 2024 LoopKit Authors. All rights reserved.
//

import SwiftUI
import Foundation
import LoopKit
import SwiftUI

enum PresetSortOption: Int, CaseIterable {
case name
Expand Down Expand Up @@ -107,7 +108,7 @@ struct PresetsView: View {
Text("Support")
.font(.title2.bold())

NavigationLink(destination: EmptyView()) {
NavigationLink(destination: PresetsHistoryView()) {
HStack {
Image(systemName: "list.bullet")
.foregroundColor(.white)
Expand Down
12 changes: 0 additions & 12 deletions LoopCore/NSUserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,6 @@ extension UserDefaults {
}
}

public var overrideHistory: TemporaryScheduleOverrideHistory? {
get {
if let rawValue = object(forKey: Key.overrideHistory.rawValue) as? TemporaryScheduleOverrideHistory.RawValue {
return TemporaryScheduleOverrideHistory(rawValue: rawValue)
} else {
return nil
}
}
set {
set(newValue?.rawValue, forKey: Key.overrideHistory.rawValue)
}
}

public var lastBedtimeQuery: Date? {
get {
Expand Down
2 changes: 1 addition & 1 deletion LoopTests/Managers/TemporaryPresetsManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TemporaryPresetsManagerTests: XCTestCase {

override func setUp() async throws {
let settingsProvider = MockSettingsProvider(settings: settings)
manager = TemporaryPresetsManager(settingsProvider: settingsProvider)
manager = await TemporaryPresetsManager(settingsProvider: settingsProvider)
}

func testPreMealOverride() {
Expand Down

0 comments on commit 84ebeb3

Please sign in to comment.