|
|
@@ -42,17 +42,12 @@ extension Home {
|
|
|
sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
|
|
|
) var fetchedPercent: FetchedResults<Override>
|
|
|
|
|
|
- @FetchRequest(
|
|
|
- entity: OrefDetermination.entity(),
|
|
|
- sortDescriptors: [NSSortDescriptor(key: "deliverAt", ascending: false)],
|
|
|
- predicate: NSPredicate.predicateFor30MinAgoForDetermination,
|
|
|
- animation: Animation.bouncy
|
|
|
- ) var determination: FetchedResults<OrefDetermination>
|
|
|
-
|
|
|
- @FetchRequest(
|
|
|
- fetchRequest: OrefDetermination.fetch(NSPredicate.enactedDetermination),
|
|
|
- animation: Animation.bouncy
|
|
|
- ) var enactedDeterminations: FetchedResults<OrefDetermination>
|
|
|
+// @FetchRequest(
|
|
|
+// entity: OrefDetermination.entity(),
|
|
|
+// sortDescriptors: [NSSortDescriptor(key: "deliverAt", ascending: false)],
|
|
|
+// predicate: NSPredicate.predicateFor30MinAgoForDetermination,
|
|
|
+// animation: Animation.bouncy
|
|
|
+// ) var determination: FetchedResults<OrefDetermination>
|
|
|
|
|
|
@FetchRequest(
|
|
|
entity: OverridePresets.entity(),
|
|
|
@@ -71,13 +66,6 @@ extension Home {
|
|
|
sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
|
|
|
) var enactedSliderTT: FetchedResults<TempTargetsSlider>
|
|
|
|
|
|
- @FetchRequest(
|
|
|
- entity: GlucoseStored.entity(),
|
|
|
- sortDescriptors: [NSSortDescriptor(keyPath: \GlucoseStored.date, ascending: true)],
|
|
|
- predicate: NSPredicate.predicateFor30MinAgo,
|
|
|
- animation: Animation.bouncy
|
|
|
- ) var glucoseFromPersistence: FetchedResults<GlucoseStored>
|
|
|
-
|
|
|
var bolusProgressFormatter: NumberFormatter {
|
|
|
let formatter = NumberFormatter()
|
|
|
formatter.numberStyle = .decimal
|
|
|
@@ -157,13 +145,32 @@ extension Home {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private var determination: OrefDetermination? {
|
|
|
+ guard let determinationObjectID = state.determinationsFromPersistence.first else {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return CoreDataStack.shared.backgroundContext.object(with: determinationObjectID) as? OrefDetermination
|
|
|
+ }
|
|
|
+
|
|
|
+ private var determinationAsBinding: Binding<OrefDetermination?> {
|
|
|
+ Binding<OrefDetermination?>(
|
|
|
+ get: {
|
|
|
+ guard let determinationObjectID = state.determinationsFromPersistence.first else {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return CoreDataStack.shared.backgroundContext.object(with: determinationObjectID) as? OrefDetermination
|
|
|
+ }, set: { _ in }
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
var glucoseView: some View {
|
|
|
CurrentGlucoseView(
|
|
|
timerDate: $state.timerDate,
|
|
|
units: $state.units,
|
|
|
alarm: $state.alarm,
|
|
|
lowGlucose: $state.lowGlucose,
|
|
|
- highGlucose: $state.highGlucose
|
|
|
+ highGlucose: $state.highGlucose,
|
|
|
+ glucoseFromPersistence: $state.glucoseFromPersistence
|
|
|
).scaleEffect(0.9)
|
|
|
.onTapGesture {
|
|
|
if state.alarm == nil {
|
|
|
@@ -431,6 +438,7 @@ extension Home {
|
|
|
VStack(alignment: .leading, spacing: 20) {
|
|
|
/// Loop view at bottomLeading
|
|
|
LoopView(
|
|
|
+ determination: determinationAsBinding,
|
|
|
closedLoop: $state.closedLoop,
|
|
|
timerDate: $state.timerDate,
|
|
|
isLooping: $state.isLooping,
|
|
|
@@ -446,7 +454,7 @@ extension Home {
|
|
|
}
|
|
|
/// eventualBG string at bottomTrailing
|
|
|
|
|
|
- if let eventualBG = determination.first?.eventualBG {
|
|
|
+ if let eventualBG = determination?.eventualBG {
|
|
|
let bg = eventualBG as Decimal
|
|
|
HStack {
|
|
|
Image(systemName: "arrow.right.circle")
|
|
|
@@ -494,7 +502,7 @@ extension Home {
|
|
|
.font(.system(size: 16))
|
|
|
.foregroundColor(Color.insulin)
|
|
|
Text(
|
|
|
- (numberFormatter.string(from: (determination.first?.iob ?? 0) as NSNumber) ?? "0") +
|
|
|
+ (numberFormatter.string(from: (determination?.iob ?? 0) as NSNumber) ?? "0") +
|
|
|
NSLocalizedString(" U", comment: "Insulin unit")
|
|
|
)
|
|
|
.font(.system(size: 16, weight: .bold, design: .rounded))
|
|
|
@@ -507,7 +515,7 @@ extension Home {
|
|
|
.font(.system(size: 16))
|
|
|
.foregroundColor(.loopYellow)
|
|
|
Text(
|
|
|
- (numberFormatter.string(from: (determination.first?.cob ?? 0) as NSNumber) ?? "0") +
|
|
|
+ (numberFormatter.string(from: (determination?.cob ?? 0) as NSNumber) ?? "0") +
|
|
|
NSLocalizedString(" g", comment: "gram of carbs")
|
|
|
)
|
|
|
.font(.system(size: 16, weight: .bold, design: .rounded))
|
|
|
@@ -530,7 +538,7 @@ extension Home {
|
|
|
if !state.tins {
|
|
|
Spacer()
|
|
|
Text(
|
|
|
- "TDD: " + (numberFormatter.string(from: (determination.first?.totalDailyDose ?? 0) as NSNumber) ?? "0") +
|
|
|
+ "TDD: " + (numberFormatter.string(from: (determination?.totalDailyDose ?? 0) as NSNumber) ?? "0") +
|
|
|
NSLocalizedString(" U", comment: "Insulin unit")
|
|
|
)
|
|
|
.font(.system(size: 16, weight: .bold, design: .rounded))
|
|
|
@@ -814,7 +822,7 @@ extension Home {
|
|
|
ZStack(alignment: .bottom) {
|
|
|
TabView {
|
|
|
let carbsRequiredBadge: String? = {
|
|
|
- guard let carbsRequired = determination.first?.carbsRequired as? Decimal else { return nil }
|
|
|
+ guard let carbsRequired = determination?.carbsRequired as? Decimal else { return nil }
|
|
|
if carbsRequired > state.settingsManager.settings.carbsRequiredThreshold {
|
|
|
let numberAsNSNumber = NSDecimalNumber(decimal: carbsRequired)
|
|
|
let formattedNumber = numberFormatter.string(from: numberAsNSNumber) ?? ""
|
|
|
@@ -877,13 +885,21 @@ extension Home {
|
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
|
Text(statusTitle).font(.headline).foregroundColor(.white)
|
|
|
.padding(.bottom, 4)
|
|
|
- if let determination = enactedDeterminations.first {
|
|
|
- TagCloudView(tags: determination.reasonParts).animation(.none, value: false)
|
|
|
-
|
|
|
- Text(determination.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
|
|
|
+ if let determinationObjectID = state.determinationsFromPersistence.first {
|
|
|
+ if let determination = CoreDataStack.shared.backgroundContext
|
|
|
+ .object(with: determinationObjectID) as? OrefDetermination
|
|
|
+ {
|
|
|
+ if determination.glucose == 400 {
|
|
|
+ Text("Invalid CGM reading (HIGH).").font(.callout).bold().foregroundColor(.loopRed).padding(.top, 8)
|
|
|
+ Text("SMBs and High Temps Disabled.").font(.caption).foregroundColor(.white).padding(.bottom, 4)
|
|
|
+ } else {
|
|
|
+ TagCloudView(tags: determination.reasonParts).animation(.none, value: false)
|
|
|
|
|
|
- } else {
|
|
|
- Text("No determination found").font(.body).foregroundColor(.white)
|
|
|
+ Text(determination.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Text("No determination found").font(.body).foregroundColor(.white)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if let errorMessage = state.errorMessage, let date = state.errorDate {
|
|
|
@@ -893,24 +909,26 @@ extension Home {
|
|
|
.padding(.bottom, 4)
|
|
|
.padding(.top, 8)
|
|
|
Text(errorMessage).font(.caption).foregroundColor(.loopRed)
|
|
|
- } else if let determination = determination.first, (determination.glucose ?? 100) == 400 {
|
|
|
- Text("Invalid CGM reading (HIGH).").font(.callout).bold().foregroundColor(.loopRed).padding(.top, 8)
|
|
|
- Text("SMBs and High Temps Disabled.").font(.caption).foregroundColor(.white).padding(.bottom, 4)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private func setStatusTitle() {
|
|
|
- guard let determination = determination.first else {
|
|
|
+ if let determinationObjectID = state.determinationsFromPersistence.first {
|
|
|
+ if let determination = CoreDataStack.shared.backgroundContext
|
|
|
+ .object(with: determinationObjectID) as? OrefDetermination
|
|
|
+ {
|
|
|
+ let dateFormatter = DateFormatter()
|
|
|
+ dateFormatter.timeStyle = .short
|
|
|
+ statusTitle = NSLocalizedString("Oref Determination enacted at", comment: "Headline in enacted pop up") +
|
|
|
+ " " +
|
|
|
+ dateFormatter
|
|
|
+ .string(from: determination.deliverAt ?? Date())
|
|
|
+ }
|
|
|
+ } else {
|
|
|
statusTitle = "No Oref determination"
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
- let dateFormatter = DateFormatter()
|
|
|
- dateFormatter.timeStyle = .short
|
|
|
- statusTitle = NSLocalizedString("Oref Determination enacted at", comment: "Headline in enacted pop up") + " " +
|
|
|
- dateFormatter
|
|
|
- .string(from: determination.deliverAt ?? Date())
|
|
|
}
|
|
|
}
|
|
|
}
|