forked from loopandlearn/customization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain380_basal_lock.patch
More file actions
1193 lines (1183 loc) · 51.4 KB
/
main380_basal_lock.patch
File metadata and controls
1193 lines (1183 loc) · 51.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Submodule Loop contains modified content
diff --git a/Loop/Loop.xcodeproj/project.pbxproj b/Loop/Loop.xcodeproj/project.pbxproj
index 11819516..a22646b3 100644
--- a/Loop/Loop.xcodeproj/project.pbxproj
+++ b/Loop/Loop.xcodeproj/project.pbxproj
@@ -489,6 +489,7 @@
C1FB428F217921D600FAB378 /* PumpManagerUI.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1FB428E217921D600FAB378 /* PumpManagerUI.swift */; };
C1FB4290217922A100FAB378 /* PumpManagerUI.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1FB428E217921D600FAB378 /* PumpManagerUI.swift */; };
DD3DBD292A33AFE9000F8B5B /* IntegralRetrospectiveCorrectionSelectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD3DBD282A33AFE9000F8B5B /* IntegralRetrospectiveCorrectionSelectionView.swift */; };
+ DDC065142B65871E0033FD88 /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC065132B65871E0033FD88 /* Preferences.swift */; };
DDC389F62A2B61750066E2E8 /* ApplicationFactorStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC389F52A2B61750066E2E8 /* ApplicationFactorStrategy.swift */; };
DDC389F82A2B620B0066E2E8 /* GlucoseBasedApplicationFactorStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC389F72A2B620B0066E2E8 /* GlucoseBasedApplicationFactorStrategy.swift */; };
DDC389FA2A2B62470066E2E8 /* ConstantApplicationFactorStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC389F92A2B62470066E2E8 /* ConstantApplicationFactorStrategy.swift */; };
@@ -1292,6 +1293,7 @@
C1FB428B217806A300FAB378 /* StateColorPalette.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateColorPalette.swift; sourceTree = "<group>"; };
C1FB428E217921D600FAB378 /* PumpManagerUI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PumpManagerUI.swift; sourceTree = "<group>"; };
DD3DBD282A33AFE9000F8B5B /* IntegralRetrospectiveCorrectionSelectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegralRetrospectiveCorrectionSelectionView.swift; sourceTree = "<group>"; };
+ DDC065132B65871E0033FD88 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; };
DDC389F52A2B61750066E2E8 /* ApplicationFactorStrategy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationFactorStrategy.swift; sourceTree = "<group>"; };
DDC389F72A2B620B0066E2E8 /* GlucoseBasedApplicationFactorStrategy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlucoseBasedApplicationFactorStrategy.swift; sourceTree = "<group>"; };
DDC389F92A2B62470066E2E8 /* ConstantApplicationFactorStrategy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConstantApplicationFactorStrategy.swift; sourceTree = "<group>"; };
@@ -1960,6 +1962,7 @@
C1E3862428247B7100F561A4 /* StoredLoopNotRunningNotification.swift */,
4328E0311CFC068900E199AA /* WatchContext+LoopKit.swift */,
A987CD4824A58A0100439ADC /* ZipArchive.swift */,
+ DDC065132B65871E0033FD88 /* Preferences.swift */,
);
path = Models;
sourceTree = "<group>";
@@ -3742,6 +3745,7 @@
E98A55EF24EDD6E60008715D /* DosingDecisionStoreProtocol.swift in Sources */,
B4001CEE28CBBC82002FB414 /* AlertManagementView.swift in Sources */,
E9C00EF524C623EF00628F35 /* LoopSettings+Loop.swift in Sources */,
+ DDC065142B65871E0033FD88 /* Preferences.swift in Sources */,
4389916B1E91B689000EEF90 /* ChartSettings+Loop.swift in Sources */,
C178249A1E1999FA00D9D25C /* CaseCountable.swift in Sources */,
B4F3D25124AF890C0095CE44 /* BluetoothStateManager.swift in Sources */,
diff --git a/Loop/Loop/Managers/LoopDataManager.swift b/Loop/Loop/Managers/LoopDataManager.swift
index 2319f4ec..c3f0c422 100644
--- a/Loop/Loop/Managers/LoopDataManager.swift
+++ b/Loop/Loop/Managers/LoopDataManager.swift
@@ -1549,7 +1549,8 @@ extension LoopDataManager {
model: model,
pendingInsulin: 0, // Pending insulin is already reflected in the prediction
maxBolus: maxBolus,
- volumeRounder: volumeRounder
+ volumeRounder: volumeRounder,
+ preferences: Preferences.shared
)
}
@@ -1832,7 +1833,8 @@ extension LoopDataManager {
lastTempBasal: lastTempBasal,
volumeRounder: volumeRounder,
rateRounder: rateRounder,
- isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true
+ isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true,
+ preferences: Preferences.shared
)
case .tempBasalOnly:
@@ -1847,7 +1849,8 @@ extension LoopDataManager {
additionalActiveInsulinClamp: iobHeadroom,
lastTempBasal: lastTempBasal,
rateRounder: rateRounder,
- isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true
+ isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true,
+ preferences: Preferences.shared
)
dosingRecommendation = AutomaticDoseRecommendation(basalAdjustment: temp)
}
diff --git a/Loop/Loop/Models/Preferences.swift b/Loop/Loop/Models/Preferences.swift
new file mode 100644
index 00000000..dc9150f8
--- /dev/null
+++ b/Loop/Loop/Models/Preferences.swift
@@ -0,0 +1,50 @@
+//
+// Preferences.swift
+// Loop
+//
+// Created by Jonas Björkert on 2024-02-25.
+// Copyright © 2024 LoopKit Authors. All rights reserved.
+//
+
+import Foundation
+import LoopKit
+import HealthKit
+
+struct Preferences: PreferencesProvider {
+
+ static var shared = Preferences()
+
+ private init() {}
+
+ // Basal Lock Threshold
+ var basalLockThreshold: HKQuantity {
+ get {
+ let key = "basalLockThreshold"
+ if let value = UserDefaults.standard.value(forKey: key) as? Double {
+ return HKQuantity(unit: .milligramsPerDeciliter, doubleValue: value)
+ } else {
+ return HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 250)
+ }
+ }
+ set {
+ let key = "basalLockThreshold"
+ let value = newValue.doubleValue(for: .milligramsPerDeciliter)
+ UserDefaults.standard.set(value, forKey: key)
+ }
+ }
+
+ // Basal Lock Enabled
+ var isBasalLockEnabled: Bool {
+ get {
+ let key = "isBasalLockEnabled"
+ if UserDefaults.standard.object(forKey: key) == nil {
+ return false
+ }
+ return UserDefaults.standard.bool(forKey: key)
+ }
+ set {
+ let key = "isBasalLockEnabled"
+ UserDefaults.standard.set(newValue, forKey: key)
+ }
+ }
+}
diff --git a/Loop/Loop/Views/SettingsView.swift b/Loop/Loop/Views/SettingsView.swift
index c3ec98b8..60e36b9a 100644
--- a/Loop/Loop/Views/SettingsView.swift
+++ b/Loop/Loop/Views/SettingsView.swift
@@ -51,6 +51,7 @@ public struct SettingsView: View {
case favoriteFoods
case therapySettings
+ case preferences
}
}
@@ -85,6 +86,9 @@ public struct SettingsView: View {
if FeatureFlags.allowExperimentalFeatures {
favoriteFoodsSection
}
+ if FeatureFlags.allowExperimentalFeatures {
+ preferencesSection
+ }
if (viewModel.pumpManagerSettingsViewModel.isTestingDevice || viewModel.cgmManagerSettingsViewModel.isTestingDevice) && viewModel.showDeleteTestData {
deleteDataSection
}
@@ -157,6 +161,8 @@ public struct SettingsView: View {
.environment(\.insulinTintColor, self.insulinTintColor)
case .favoriteFoods:
FavoriteFoodsView()
+ case .preferences:
+ PreferencesView(viewModel: PreferencesViewModel(preferencesProvider: Preferences.shared)).environmentObject(displayGlucosePreference)
}
}
}
@@ -374,6 +380,16 @@ extension SettingsView {
}
}
+ private var preferencesSection: some View {
+ Section {
+ LargeButton(action: { sheet = .preferences },
+ includeArrow: true,
+ imageView: AnyView(Image(systemName: "gearshape.fill").font(.system(size: 30, weight: .bold))),
+ label: NSLocalizedString("Preferences", comment: "Title text for button to Preferences"),
+ descriptiveText: NSLocalizedString("Customize your Loop experience by adjusting additional settings", comment: "Descriptive text for Preferences"))
+ }
+ }
+
private var cgmChoices: [ActionSheet.Button] {
var result = viewModel.cgmManagerSettingsViewModel.availableDevices
.sorted(by: {$0.localizedTitle < $1.localizedTitle})
Submodule LoopKit contains modified content
diff --git a/LoopKit/LoopKit.xcodeproj/project.pbxproj b/LoopKit/LoopKit.xcodeproj/project.pbxproj
index 2ecb6c53..6e177b30 100644
--- a/LoopKit/LoopKit.xcodeproj/project.pbxproj
+++ b/LoopKit/LoopKit.xcodeproj/project.pbxproj
@@ -863,6 +863,13 @@
C1FAEC1D264AD6B400A3250B /* DeviceStatusBadge.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1FAEC1C264AD6B400A3250B /* DeviceStatusBadge.swift */; };
C1FAEC1F264AE12700A3250B /* UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = B47ECF8725DC20810024A54D /* UIImage.swift */; };
C1FAEC21264AEEA300A3250B /* UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1FAEC20264AEEA300A3250B /* UIImage.swift */; };
+ DD13BC702C3C71A70062313B /* BasalLockEditor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD13BC6F2C3C71A70062313B /* BasalLockEditor.swift */; };
+ DD13BC722C3C74CD0062313B /* PreferencesGuardrailWarning.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD13BC712C3C74CD0062313B /* PreferencesGuardrailWarning.swift */; };
+ DD28B3712B80C074001044D4 /* PreferencesSetting.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD28B3702B80C074001044D4 /* PreferencesSetting.swift */; };
+ DD545A5B2B80ECB900915F95 /* PreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD545A5A2B80ECB900915F95 /* PreferencesView.swift */; };
+ DD545A5F2B80EFA900915F95 /* PreferencesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD545A5E2B80EFA900915F95 /* PreferencesViewModel.swift */; };
+ DD545A652B814CA800915F95 /* PreferencesProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD545A642B814CA800915F95 /* PreferencesProvider.swift */; };
+ DD545A692B8BCF8E00915F95 /* Guardrail+Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD545A682B8BCF8E00915F95 /* Guardrail+Preferences.swift */; };
E9077D2724ACD59F0066A88D /* InformationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9077D2624ACD59F0066A88D /* InformationView.swift */; };
E9077D2A24ACDE2C0066A88D /* CorrectionRangeInformationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9077D2924ACDE2C0066A88D /* CorrectionRangeInformationView.swift */; };
E9086B2924B39EDC0062F5C8 /* ChartsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9086B2824B39EDC0062F5C8 /* ChartsTableViewController.swift */; };
@@ -1790,6 +1797,13 @@
C1FAC06228C7B0A100754AE2 /* reservoir_iob_test.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = reservoir_iob_test.json; sourceTree = "<group>"; };
C1FAEC1C264AD6B400A3250B /* DeviceStatusBadge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeviceStatusBadge.swift; sourceTree = "<group>"; };
C1FAEC20264AEEA300A3250B /* UIImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIImage.swift; sourceTree = "<group>"; };
+ DD13BC6F2C3C71A70062313B /* BasalLockEditor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasalLockEditor.swift; sourceTree = "<group>"; };
+ DD13BC712C3C74CD0062313B /* PreferencesGuardrailWarning.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesGuardrailWarning.swift; sourceTree = "<group>"; };
+ DD28B3702B80C074001044D4 /* PreferencesSetting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesSetting.swift; sourceTree = "<group>"; };
+ DD545A5A2B80ECB900915F95 /* PreferencesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesView.swift; sourceTree = "<group>"; };
+ DD545A5E2B80EFA900915F95 /* PreferencesViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesViewModel.swift; sourceTree = "<group>"; };
+ DD545A642B814CA800915F95 /* PreferencesProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesProvider.swift; sourceTree = "<group>"; };
+ DD545A682B8BCF8E00915F95 /* Guardrail+Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Guardrail+Preferences.swift"; sourceTree = "<group>"; };
E9077D2624ACD59F0066A88D /* InformationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InformationView.swift; sourceTree = "<group>"; };
E9077D2924ACDE2C0066A88D /* CorrectionRangeInformationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CorrectionRangeInformationView.swift; sourceTree = "<group>"; };
E9086B2824B39EDC0062F5C8 /* ChartsTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChartsTableViewController.swift; sourceTree = "<group>"; };
@@ -2271,6 +2285,7 @@
4369F090208B0D68000E3E45 /* Views */ = {
isa = PBXGroup;
children = (
+ DD545A592B80EC9500915F95 /* Preferences Editors */,
B4C004B2241085DB00B40429 /* ActionButton.swift */,
89AF78C524482268002B4FCC /* ActionButtonStyle.swift */,
C1E4B307242E995200E70CCB /* ActivityIndicator.swift */,
@@ -2441,6 +2456,7 @@
C187338229B9486200519CDF /* TimeInterval.swift */,
C187339629B9488300519CDF /* TimeZone.swift */,
C187339729B9488300519CDF /* UUID.swift */,
+ DD545A682B8BCF8E00915F95 /* Guardrail+Preferences.swift */,
);
path = Extensions;
sourceTree = "<group>";
@@ -2649,6 +2665,8 @@
C1814B8B226371DF008D2D8E /* WeakSynchronizedDelegate.swift */,
43CACE0D2247F89100F90AF5 /* WeakSynchronizedSet.swift */,
B4A2ABEE2AAB5160007E3EC1 /* Pluggable.swift */,
+ DD28B3702B80C074001044D4 /* PreferencesSetting.swift */,
+ DD545A642B814CA800915F95 /* PreferencesProvider.swift */,
);
path = LoopKit;
sourceTree = "<group>";
@@ -3169,6 +3187,7 @@
B455F48025FF9A8B000ED456 /* InsulinSensitivityScheduleEditorViewModel.swift */,
B455F2A125FBE985000ED456 /* SuspendThresholdEditorViewModel.swift */,
1D1FCE2424BD42EF000300A8 /* TherapySettingsViewModel.swift */,
+ DD545A5E2B80EFA900915F95 /* PreferencesViewModel.swift */,
);
path = ViewModels;
sourceTree = "<group>";
@@ -3294,6 +3313,16 @@
path = LoopAlgorithm;
sourceTree = "<group>";
};
+ DD545A592B80EC9500915F95 /* Preferences Editors */ = {
+ isa = PBXGroup;
+ children = (
+ DD545A5A2B80ECB900915F95 /* PreferencesView.swift */,
+ DD13BC6F2C3C71A70062313B /* BasalLockEditor.swift */,
+ DD13BC712C3C74CD0062313B /* PreferencesGuardrailWarning.swift */,
+ );
+ path = "Preferences Editors";
+ sourceTree = "<group>";
+ };
E9077D2824ACD5AA0066A88D /* Information Screens */ = {
isa = PBXGroup;
children = (
@@ -4052,6 +4081,7 @@
B4A2AAB1240830A30066563F /* LabeledTextField.swift in Sources */,
A9D3FF0B2A6C198C000C891D /* CGPoint.swift in Sources */,
B429D66E24BF7255003E1B4A /* UIImage.swift in Sources */,
+ DD13BC722C3C74CD0062313B /* PreferencesGuardrailWarning.swift in Sources */,
B46B62A923FF05F8001E69BA /* LabeledNumberInput.swift in Sources */,
892155152245C57E009112BC /* SegmentedGaugeBarLayer.swift in Sources */,
C1DD512B259FD8A600DE27AE /* InsulinTypeChooser.swift in Sources */,
@@ -4087,6 +4117,7 @@
A9D3FF042A6C1944000C891D /* PredictedGlucoseChart.swift in Sources */,
C1DE4C2125A253BD007065F8 /* Color.swift in Sources */,
898E6E702241EDB70019E459 /* PercentageTextFieldTableViewController.swift in Sources */,
+ DD13BC702C3C71A70062313B /* BasalLockEditor.swift in Sources */,
89CAB36D24C9EC98009EE3CE /* Keyboard.swift in Sources */,
89186C0724BF7FC70003D0F3 /* Guardrail+UI.swift in Sources */,
43FB60E520DCBA02002B996B /* SetupTableViewController.swift in Sources */,
@@ -4137,6 +4168,7 @@
B46B62B323FF0E62001E69BA /* SelectableLabel.swift in Sources */,
89FC6893245A2D680075CF59 /* ScheduleItemView.swift in Sources */,
43BA716F201E49220058961E /* FoodEmojiDataSource.swift in Sources */,
+ DD545A5F2B80EFA900915F95 /* PreferencesViewModel.swift in Sources */,
E9077D2724ACD59F0066A88D /* InformationView.swift in Sources */,
E94141D024C8F31C0096C326 /* DeliveryLimitsEditor.swift in Sources */,
1452F4BA2A85266500F8B9E4 /* CarbQuantityRow.swift in Sources */,
@@ -4200,6 +4232,7 @@
43BA717D201EE7090058961E /* GlucoseRangeTableViewCell.swift in Sources */,
B455F3A425FF7FF0000ED456 /* CorrectionRangeOverridesEditorViewModel.swift in Sources */,
A9D3FF002A6C1944000C891D /* ChartConstants.swift in Sources */,
+ DD545A5B2B80ECB900915F95 /* PreferencesView.swift in Sources */,
89BE75CB2464BC2000B145D9 /* AlertContent.swift in Sources */,
43BA7184201EE7090058961E /* TextFieldTableViewController.swift in Sources */,
1452F4B22A8521CD00F8B9E4 /* TextFieldRow.swift in Sources */,
@@ -4288,6 +4321,7 @@
89AE222F228BC68000BDFD85 /* DoseProgressTimerEstimator.swift in Sources */,
43D8FDF61C7290350073BE78 /* DailyQuantitySchedule.swift in Sources */,
43D8FDF51C7290350073BE78 /* CarbRatioSchedule.swift in Sources */,
+ DD545A652B814CA800915F95 /* PreferencesProvider.swift in Sources */,
4322B76F202FA26F0002837D /* GlucoseSampleValue.swift in Sources */,
89AE222C228BC66E00BDFD85 /* Locked.swift in Sources */,
432CF87120D76D5A0066B889 /* GlucoseDisplayable.swift in Sources */,
@@ -4357,6 +4391,7 @@
A93761C125ED670200F6BE43 /* BluetoothProvider.swift in Sources */,
433BC7A720523DB7000B1200 /* NewGlucoseSample.swift in Sources */,
4322B78E202FA2B30002837D /* InsulinMath.swift in Sources */,
+ DD28B3712B80C074001044D4 /* PreferencesSetting.swift in Sources */,
C17F39D023CE34B100FA1113 /* StoredDeviceLogEntry.swift in Sources */,
43B17C89208EEC0B00AC27E9 /* HealthStoreUnitCache.swift in Sources */,
A912BE29245B9CD500CBE199 /* SettingsObject+CoreDataClass.swift in Sources */,
@@ -4383,6 +4418,7 @@
A932803B2798D63B0091D0A1 /* SyncAlertObject.swift in Sources */,
1D841AAD24577EE10069DBFF /* AlertSoundPlayer.swift in Sources */,
C19E776B2A61FD8A003F06C5 /* RetrospectiveCorrection.swift in Sources */,
+ DD545A692B8BCF8E00915F95 /* Guardrail+Preferences.swift in Sources */,
C187339A29B9488300519CDF /* UUID.swift in Sources */,
C1CAB9E926A3254800A57273 /* StoredInsulinModel.swift in Sources */,
43CACE0E2247F89100F90AF5 /* WeakSynchronizedSet.swift in Sources */,
diff --git a/LoopKit/LoopKit/Extensions/Guardrail+Preferences.swift b/LoopKit/LoopKit/Extensions/Guardrail+Preferences.swift
new file mode 100644
index 00000000..e413f7b5
--- /dev/null
+++ b/LoopKit/LoopKit/Extensions/Guardrail+Preferences.swift
@@ -0,0 +1,18 @@
+//
+// Guardrail+Preferences.swift
+// LoopKit
+//
+// Created by Jonas Björkert on 2024-02-25.
+// Copyright © 2024 LoopKit Authors. All rights reserved.
+//
+
+import Foundation
+import HealthKit
+
+public extension Guardrail where Value == HKQuantity {
+ static let basalLockThreshold = Guardrail(
+ absoluteBounds: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 200)...HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 300),
+ recommendedBounds: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 220)...HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 300),
+ startingSuggestion: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 250)
+ )
+}
diff --git a/LoopKit/LoopKit/LoopAlgorithm/DoseMath.swift b/LoopKit/LoopKit/LoopAlgorithm/DoseMath.swift
index f4b8d44a..1151ff12 100644
--- a/LoopKit/LoopKit/LoopAlgorithm/DoseMath.swift
+++ b/LoopKit/LoopKit/LoopAlgorithm/DoseMath.swift
@@ -231,7 +231,8 @@ extension Collection where Element: GlucoseValue {
at date: Date,
suspendThreshold: HKQuantity,
sensitivity: HKQuantity,
- model: InsulinModel
+ model: InsulinModel,
+ preferences: PreferencesProvider
) -> InsulinCorrection? {
let effectDuration = model.effectDuration
let timeline = [AbsoluteScheduleValue(startDate: date, endDate: date.addingTimeInterval(effectDuration), value: sensitivity)]
@@ -240,7 +241,8 @@ extension Collection where Element: GlucoseValue {
at: date,
suspendThreshold: suspendThreshold,
insulinSensitivityTimeline: timeline,
- model: model)
+ model: model,
+ preferences: preferences)
}
/// For a collection of glucose prediction, determine the least amount of insulin delivered at
@@ -258,7 +260,8 @@ extension Collection where Element: GlucoseValue {
at date: Date,
suspendThreshold: HKQuantity,
insulinSensitivityTimeline: [AbsoluteScheduleValue<HKQuantity>],
- model: InsulinModel
+ model: InsulinModel,
+ preferences: PreferencesProvider
) -> InsulinCorrection? {
var minGlucose: GlucoseValue?
var eventualGlucose: GlucoseValue?
@@ -402,14 +405,16 @@ extension Collection where Element: GlucoseValue {
rateRounder: ((Double) -> Double)? = nil,
isBasalRateScheduleOverrideActive: Bool = false,
duration: TimeInterval = TimeInterval(30 * 60),
- continuationInterval: TimeInterval = TimeInterval(60 * 11)
+ continuationInterval: TimeInterval = TimeInterval(60 * 11),
+ preferences: PreferencesProvider
) -> TempBasalRecommendation? {
let correction = self.insulinCorrection(
to: correctionRange,
at: date,
suspendThreshold: suspendThreshold ?? correctionRange.quantityRange(at: date).lowerBound,
sensitivity: sensitivity.quantity(at: date),
- model: model
+ model: model,
+ preferences: preferences
)
let scheduledBasalRate = basalRates.value(at: date)
@@ -427,13 +432,22 @@ extension Collection where Element: GlucoseValue {
maxBasalRate = Swift.min(maxThirtyMinuteRateToKeepIOBBelowLimit, maxBasalRate)
}
- let temp = correction?.asTempBasal(
+ var temp = correction?.asTempBasal(
scheduledBasalRate: scheduledBasalRate,
maxBasalRate: maxBasalRate,
duration: duration,
rateRounder: rateRounder
)
+ if (preferences.isBasalLockEnabled && ( temp?.unitsPerHour ?? scheduledBasalRate < scheduledBasalRate ||
+ lastTempBasal?.unitsPerHour ?? scheduledBasalRate < scheduledBasalRate
+ ) &&
+ self[0 as! Self.Index].quantity > preferences.basalLockThreshold)
+ {
+ print("####### Temp Basal Lock On #########")
+ temp = TempBasalRecommendation(unitsPerHour: scheduledBasalRate, duration: 1800)
+ }
+
return temp?.ifNecessary(
at: date,
scheduledBasalRate: scheduledBasalRate,
@@ -475,14 +489,16 @@ extension Collection where Element: GlucoseValue {
rateRounder: ((Double) -> Double)? = nil,
isBasalRateScheduleOverrideActive: Bool = false,
duration: TimeInterval = TimeInterval(30 * 60),
- continuationInterval: TimeInterval = TimeInterval(11 * 60)
+ continuationInterval: TimeInterval = TimeInterval(11 * 60),
+ preferences: PreferencesProvider
) -> AutomaticDoseRecommendation? {
guard let correction = self.insulinCorrection(
to: correctionRange,
at: date,
suspendThreshold: suspendThreshold ?? correctionRange.quantityRange(at: date).lowerBound,
sensitivity: sensitivity.quantity(at: date),
- model: model
+ model: model,
+ preferences: preferences
) else {
return nil
}
@@ -517,6 +533,15 @@ extension Collection where Element: GlucoseValue {
volumeRounder: volumeRounder
)
+ if (preferences.isBasalLockEnabled && (temp?.unitsPerHour ?? scheduledBasalRate < scheduledBasalRate ||
+ lastTempBasal?.unitsPerHour ?? scheduledBasalRate < scheduledBasalRate
+ ) &&
+ self[0 as! Self.Index].quantity > preferences.basalLockThreshold)
+ {
+ print("####### Temp Basal Lock On #########")
+ temp = TempBasalRecommendation(unitsPerHour: scheduledBasalRate, duration: 1800)
+ }
+
if temp != nil || bolusUnits > 0 {
return AutomaticDoseRecommendation(basalAdjustment: temp, bolusUnits: bolusUnits)
}
@@ -545,14 +570,16 @@ extension Collection where Element: GlucoseValue {
model: InsulinModel,
pendingInsulin: Double,
maxBolus: Double,
- volumeRounder: ((Double) -> Double)? = nil
+ volumeRounder: ((Double) -> Double)? = nil,
+ preferences: PreferencesProvider
) -> ManualBolusRecommendation {
guard let correction = self.insulinCorrection(
to: correctionRange,
at: date,
suspendThreshold: suspendThreshold ?? correctionRange.quantityRange(at: date).lowerBound,
sensitivity: sensitivity.quantity(at: date),
- model: model
+ model: model,
+ preferences: preferences
) else {
return ManualBolusRecommendation(amount: 0, pendingInsulin: pendingInsulin)
}
diff --git a/LoopKit/LoopKit/PreferencesProvider.swift b/LoopKit/LoopKit/PreferencesProvider.swift
new file mode 100644
index 00000000..a164defb
--- /dev/null
+++ b/LoopKit/LoopKit/PreferencesProvider.swift
@@ -0,0 +1,15 @@
+//
+// PreferencesProvider.swift
+// LoopKit
+//
+// Created by Jonas Björkert on 2024-02-25.
+// Copyright © 2024 LoopKit Authors. All rights reserved.
+//
+
+import Foundation
+import HealthKit
+
+public protocol PreferencesProvider {
+ var basalLockThreshold: HKQuantity { get set }
+ var isBasalLockEnabled: Bool { get set }
+}
diff --git a/LoopKit/LoopKit/PreferencesSetting.swift b/LoopKit/LoopKit/PreferencesSetting.swift
new file mode 100644
index 00000000..0de6928d
--- /dev/null
+++ b/LoopKit/LoopKit/PreferencesSetting.swift
@@ -0,0 +1,60 @@
+//
+// PreferencesSetting.swift
+// LoopKit
+//
+// Created by Jonas Björkert on 2024-02-25.
+// Copyright © 2024 LoopKit Authors. All rights reserved.
+//
+
+import Foundation
+
+public enum PreferencesSetting {
+ case basalLock
+}
+
+extension PreferencesSetting: Equatable { }
+
+public extension PreferencesSetting {
+ var title: String {
+ switch self {
+ case .basalLock:
+ return LocalizedString("Basal Lock", comment: "Title text for basal lock setting")
+ }
+ }
+
+ var smallTitle: String {
+ return title
+ }
+
+ func descriptiveText(appName: String) -> String {
+ switch self {
+ case .basalLock:
+ return String(format: LocalizedString("Basal Lock prevents the basal rate from being throttled if the blood glucose is above a certain level.", comment: "Descriptive text for basal lock (1: app name)"), appName)
+ }
+ }
+}
+
+// MARK: Guardrails
+public extension PreferencesSetting {
+ var guardrailCaptionForLowValue: String {
+ switch self {
+ case .basalLock:
+ return LocalizedString("The value you have entered is lower than what is typically recommended.", comment: "Descriptive text for guardrail low value warning for basal lock")
+ }
+ }
+
+ var guardrailCaptionForHighValue: String {
+ switch self {
+ case .basalLock:
+ return LocalizedString("The value you have entered is higher than what is typically recommended.", comment: "Descriptive text for guardrail high value warning for basal lock")
+ }
+ }
+
+ var guardrailCaptionForOutsideValues: String {
+ return LocalizedString("The value you have entered for Basal Lock is outside of the recommended range.", comment: "Descriptive text for guardrail outside value warning for basal lock")
+ }
+
+ var guardrailSaveWarningCaption: String {
+ return LocalizedString("Please note that this value is outside of the recommended range.", comment: "Descriptive text for saving settings outside the recommended range for basal lock")
+ }
+}
diff --git a/LoopKit/LoopKitUI/ViewModels/PreferencesViewModel.swift b/LoopKit/LoopKitUI/ViewModels/PreferencesViewModel.swift
new file mode 100644
index 00000000..785a23f1
--- /dev/null
+++ b/LoopKit/LoopKitUI/ViewModels/PreferencesViewModel.swift
@@ -0,0 +1,34 @@
+//
+// PreferencesViewModel.swift
+// LoopKitUI
+//
+// Created by Jonas Björkert on 2024-02-25.
+// Copyright © 2024 LoopKit Authors. All rights reserved.
+//
+
+import Foundation
+import LoopKit
+import HealthKit
+
+public class PreferencesViewModel: ObservableObject {
+ @Published var basalLockThreshold: HKQuantity
+ @Published var isBasalLockEnabled: Bool
+
+ private var preferencesProvider: PreferencesProvider
+
+ public init(preferencesProvider: PreferencesProvider) {
+ self.preferencesProvider = preferencesProvider
+ self.basalLockThreshold = preferencesProvider.basalLockThreshold
+ self.isBasalLockEnabled = preferencesProvider.isBasalLockEnabled
+ }
+
+ func updateBasalLockThreshold(_ newValue: HKQuantity) {
+ preferencesProvider.basalLockThreshold = newValue
+ self.basalLockThreshold = newValue
+ }
+
+ func updateBasalLockEnabled(_ newValue: Bool) {
+ preferencesProvider.isBasalLockEnabled = newValue
+ self.isBasalLockEnabled = newValue
+ }
+}
diff --git a/LoopKit/LoopKitUI/Views/GuardrailConstrainedTimeIntervalView.swift b/LoopKit/LoopKitUI/Views/GuardrailConstrainedTimeIntervalView.swift
new file mode 100644
index 00000000..c473fd51
--- /dev/null
+++ b/LoopKit/LoopKitUI/Views/GuardrailConstrainedTimeIntervalView.swift
@@ -0,0 +1,122 @@
+//
+// GuardrailConstrainedTimeIntervalView.swift
+// LoopKitUI
+//
+// Created by Jonas Björkert on 2024-02-25.
+// Copyright © 2024 LoopKit Authors. All rights reserved.
+//
+
+import SwiftUI
+import HealthKit
+import LoopKit
+
+
+public struct GuardrailConstrainedTimeIntervalView: View {
+ @Environment(\.guidanceColors) var guidanceColors
+ var value: TimeInterval?
+ var guardrail: Guardrail<TimeInterval>
+ var isEditing: Bool
+ var isSupportedValue: Bool
+ var formatter: DateComponentsFormatter
+ var iconSpacing: CGFloat
+ var isUnitLabelVisible: Bool
+ var forceDisableAnimations: Bool
+
+ @State private var hasAppeared = false
+
+ public init(
+ value: TimeInterval,
+ guardrail: Guardrail<TimeInterval>,
+ isEditing: Bool,
+ isSupportedValue: Bool = true,
+ iconSpacing: CGFloat = 8,
+ isUnitLabelVisible: Bool = true,
+ forceDisableAnimations: Bool = false
+ ) {
+ self.value = value
+ self.guardrail = guardrail
+ self.isEditing = isEditing
+ self.isSupportedValue = isSupportedValue
+ self.iconSpacing = iconSpacing
+ self.isUnitLabelVisible = isUnitLabelVisible
+ self.forceDisableAnimations = forceDisableAnimations
+
+
+ self.formatter = {
+ let formatter = DateComponentsFormatter()
+ formatter.allowedUnits = [.hour, .minute]
+ formatter.unitsStyle = .short
+ formatter.zeroFormattingBehavior = [.dropLeading, .dropTrailing]
+
+ return formatter
+ }()
+ }
+
+ public var body: some View {
+ HStack {
+ HStack(spacing: iconSpacing) {
+ if value != nil {
+ if guardrail.classification(for: value!) != .withinRecommendedRange {
+ Image(systemName: "exclamationmark.triangle.fill")
+ .foregroundColor(warningColor)
+ .transition(.springInDisappear)
+ }
+
+ Text(formatter.string(from: value!) ?? "")
+ .foregroundColor(warningColor)
+ .fixedSize(horizontal: true, vertical: false)
+ } else {
+ Text("–")
+ .foregroundColor(.secondary)
+ }
+ }
+
+/* if isUnitLabelVisible {
+ Text(unit.shortLocalizedUnitString())
+ .foregroundColor(Color(.secondaryLabel))
+ }*/
+ }
+ .accessibilityElement(children: .combine)
+ .onAppear { self.hasAppeared = true }
+ .animation(animation)
+ }
+
+ private var animation: Animation? {
+ // A conditional implicit animation seems to behave funky on first appearance.
+ // Disable animations until the view has appeared.
+ if forceDisableAnimations || !hasAppeared {
+ return nil
+ }
+
+ // While editing, the text width is liable to change, which can cause a slow-feeling animation
+ // of the guardrail warning icon. Disable animations while editing.
+ return isEditing ? nil : .default
+ }
+
+ private var warningColor: Color {
+ guard let value = value else {
+ return .primary
+ }
+
+ guard isSupportedValue else { return guidanceColors.critical }
+
+ switch guardrail.classification(for: value) {
+ case .withinRecommendedRange:
+ return isEditing ? .accentColor : guidanceColors.acceptable
+ case .outsideRecommendedRange(let threshold):
+ switch threshold {
+ case .minimum, .maximum:
+ return guidanceColors.critical
+ case .belowRecommended, .aboveRecommended:
+ return guidanceColors.warning
+ }
+ }
+ }
+}
+
+fileprivate extension AnyTransition {
+ static let springInDisappear = asymmetric(
+ insertion: AnyTransition.scale.animation(.spring(dampingFraction: 0.5)),
+ removal: .identity
+ )
+}
diff --git a/LoopKit/LoopKitUI/Views/Preferences Editors/BasalLockEditor.swift b/LoopKit/LoopKitUI/Views/Preferences Editors/BasalLockEditor.swift
new file mode 100644
index 00000000..dc313a9c
--- /dev/null
+++ b/LoopKit/LoopKitUI/Views/Preferences Editors/BasalLockEditor.swift
@@ -0,0 +1,209 @@
+//
+// BasalLockEditor.swift
+// LoopKitUI
+//
+// Created by Jonas Björkert on 2024-02-25.
+// Copyright © 2024 LoopKit Authors. All rights reserved.
+//
+
+import SwiftUI
+import HealthKit
+import LoopKit
+
+public struct BasalLockEditor: View {
+ @EnvironmentObject private var displayGlucosePreference: DisplayGlucosePreference
+
+ @Environment(\.dismissAction) var dismiss
+ @Environment(\.authenticate) var authenticate
+ @Environment(\.appName) private var appName
+
+ let viewModel: PreferencesViewModel
+ let didSave: (() -> Void)?
+
+ @State private var userDidTap: Bool = false
+ @State private var isBasalLockEnabled: Bool
+ @State private var threshold: HKQuantity
+ @State private var isEditing = false
+ @State private var showingConfirmationAlert = false
+
+ private var initialValue: HKQuantity {
+ viewModel.basalLockThreshold
+ }
+
+ public init(preferencesViewModel: PreferencesViewModel, didSave: (() -> Void)? = nil) {
+ self.viewModel = preferencesViewModel
+ self._isBasalLockEnabled = State(initialValue: preferencesViewModel.isBasalLockEnabled)
+ self._threshold = State(initialValue: preferencesViewModel.basalLockThreshold)
+ self.didSave = didSave
+ }
+
+ public var body: some View {
+ contentWithCancel
+ .navigationBarTitle("", displayMode: .inline)
+ }
+
+ private var contentWithCancel: some View {
+ content
+ .navigationBarBackButtonHidden(isBasalLockEnabled != viewModel.isBasalLockEnabled || threshold != viewModel.basalLockThreshold)
+ .toolbar {
+ ToolbarItem(placement: .navigationBarLeading) {
+ leadingNavigationBarItem
+ }
+ }
+ }
+
+ @ViewBuilder
+ private var leadingNavigationBarItem: some View {
+ if isBasalLockEnabled != viewModel.isBasalLockEnabled || threshold != viewModel.basalLockThreshold {
+ cancelButton
+ } else {
+ EmptyView()
+ }
+ }
+
+ private var cancelButton: some View {
+ Button(action: { self.dismiss() }) {
+ Text(LocalizedString("Cancel", comment: "Cancel editing settings button title"))
+ }
+ }
+
+ private var picker: GlucoseValuePicker {
+ GlucoseValuePicker(
+ value: self.$threshold.animation(),
+ unit: displayGlucosePreference.unit,
+ guardrail: .basalLockThreshold,
+ bounds: Guardrail.basalLockThreshold.absoluteBounds.lowerBound...Guardrail.basalLockThreshold.absoluteBounds.upperBound
+ )
+ }
+
+ private var content: some View {
+ ConfigurationPage(
+ title: Text(LocalizedString("Basal Lock Threshold", comment: "Title for basal lock threshold setting")),
+ actionButtonTitle: Text(LocalizedString("Save", comment: "Save button title")),
+ actionButtonState: saveButtonState,
+ cards: {
+ Card {
+ description
+ .font(.callout)
+ .foregroundColor(Color(.secondaryLabel))
+ .fixedSize(horizontal: false, vertical: true)
+
+ Toggle(isOn: $isBasalLockEnabled) {
+ Text("Enable Basal Lock")
+ }
+ .animation(.default, value: isBasalLockEnabled)
+
+ ExpandableSetting(
+ isEditing: $isEditing,
+ valueContent: {
+ GuardrailConstrainedQuantityView(
+ value: threshold,
+ unit: displayGlucosePreference.unit,
+ guardrail: .basalLockThreshold,
+ isEditing: isEditing,
+ // Workaround for strange animation behavior on appearance
+ forceDisableAnimations: true
+ )
+ },
+ expandedContent: {
+ // Prevent the picker from expanding the card's width on small devices
+ picker.frame(maxWidth: 200)
+ }
+ )
+ .transition(.slide)
+ }
+ },
+ actionAreaContent: {
+ instructionalContentIfNecessary
+ if isBasalLockEnabled && warningThreshold != nil && userDidTap {
+ PreferencesGuardrailWarning(preferencesSetting: .basalLock, title: basalLockTitle, threshold: warningThreshold!)
+ .transition(.slide)
+ }
+ },
+ action: {
+ if self.warningThreshold == nil {
+ self.startSaving()
+ } else {
+ self.showingConfirmationAlert = true
+ }
+ }
+ )
+ .alert(isPresented: $showingConfirmationAlert, content: confirmationAlert)
+ .simultaneousGesture(TapGesture().onEnded {
+ withAnimation {
+ self.userDidTap = true
+ }
+ })
+ }
+
+ private var basalLockTitle: Text {
+ Text(LocalizedString("Basal Lock Threshold", comment: "Title for basal lock threshold setting"))
+ }
+
+ private var description: Text {
+ Text(LocalizedString("Basal Lock prevents the basal rate from being throttled if the blood glucose is above a certain level.", comment: "Description for basal lock threshold setting"))
+ }
+
+ private var instructionalContentIfNecessary: some View {
+ Group {
+ if !userDidTap {
+ instructionalContent
+ }
+ }
+ }
+
+ private var instructionalContent: some View {
+ HStack {
+ Text(LocalizedString("You can edit the setting by tapping into the line item.", comment: "Description of how to edit setting"))
+ .foregroundColor(.secondary)
+ .font(.subheadline)
+ Spacer()
+ }
+ }
+
+ private var saveButtonState: ConfigurationPageActionButtonState {
+ let selectableValues = picker.selectableValues
+ let adjustedBounds = (selectableValues.first!)...(selectableValues.last!)
+ guard adjustedBounds.contains(threshold.doubleValue(for: displayGlucosePreference.unit)) else {
+ return .disabled
+ }
+ return (isBasalLockEnabled != viewModel.isBasalLockEnabled || threshold != viewModel.basalLockThreshold) ? .enabled : .disabled
+ }
+
+ private var warningThreshold: SafetyClassification.Threshold? {
+ switch Guardrail.basalLockThreshold.classification(for: threshold) {
+ case .withinRecommendedRange:
+ return nil
+ case .outsideRecommendedRange(let threshold):
+ return threshold
+ }
+ }
+
+ private func confirmationAlert() -> SwiftUI.Alert {
+ SwiftUI.Alert(
+ title: Text(LocalizedString("Save Basal Lock Threshold?", comment: "Alert title for confirming a basal lock threshold outside the recommended range")),
+ message: Text(LocalizedString("Your setting for basal lock is outside the recommended range.", comment: "Descriptive text for saving settings outside the recommended range for basal lock")),
+ primaryButton: .cancel(Text(LocalizedString("Go Back", comment: "Text for go back action on confirmation alert"))),
+ secondaryButton: .default(
+ Text(LocalizedString("Continue", comment: "Text for continue action on confirmation alert")),
+ action: startSaving
+ )
+ )
+ }
+
+ private func startSaving() {
+ authenticate(LocalizedString("Authentication is required to save this setting.", comment: "Authentication challenge description for basal lock threshold")) {
+ switch $0 {
+ case .success: self.continueSaving()
+ case .failure: break
+ }
+ }
+ }
+
+ private func continueSaving() {
+ viewModel.updateBasalLockEnabled(self.isBasalLockEnabled)
+ viewModel.updateBasalLockThreshold(self.threshold)
+ didSave?()
+ self.dismiss()
+ }
+}
diff --git a/LoopKit/LoopKitUI/Views/Preferences Editors/PreferencesGuardrailWarning.swift b/LoopKit/LoopKitUI/Views/Preferences Editors/PreferencesGuardrailWarning.swift
new file mode 100644
index 00000000..3a7dd0ad
--- /dev/null
+++ b/LoopKit/LoopKitUI/Views/Preferences Editors/PreferencesGuardrailWarning.swift
@@ -0,0 +1,101 @@
+//
+// PreferencesGuardrailWarning.swift
+// LoopKitUI
+//
+// Created by Jonas Björkert on 2024-02-25.
+// Copyright © 2024 LoopKit Authors. All rights reserved.
+//
+
+import SwiftUI
+import LoopKit
+
+public struct PreferencesGuardrailWarning: View {
+ private enum CrossedThresholds {
+ case one(SafetyClassification.Threshold)
+ case oneOrMore([SafetyClassification.Threshold])
+ }
+
+ private var title: Text
+ private var crossedThresholds: CrossedThresholds
+ private var captionOverride: Text?
+ private var preferencesSetting: PreferencesSetting
+
+ public init(
+ preferencesSetting: PreferencesSetting,
+ title: Text,
+ threshold: SafetyClassification.Threshold,
+ caption: Text? = nil
+ ) {
+ self.preferencesSetting = preferencesSetting
+ self.title = title
+ self.crossedThresholds = .one(threshold)
+ self.captionOverride = caption
+ }
+
+ public init(
+ preferencesSetting: PreferencesSetting,
+ title: Text,
+ thresholds: [SafetyClassification.Threshold],
+ caption: Text? = nil
+ ) {
+ precondition(!thresholds.isEmpty)
+ self.preferencesSetting = preferencesSetting
+ self.title = title
+ self.crossedThresholds = .oneOrMore(thresholds)
+ self.captionOverride = caption
+ }
+
+ public var body: some View {
+ WarningView(title: title, caption: caption, severity: severity)
+ }
+
+ private var severity: WarningSeverity {
+ switch crossedThresholds {
+ case .one(let threshold):
+ return threshold.severity