Преглед изворни кода

Add/fix 'Add Pump' and 'Add CGM' functionality

Deniz Cengiz пре 1 година
родитељ
комит
f4a3ba9f61

+ 3 - 0
FreeAPS/Sources/APS/DeviceDataManager.swift

@@ -95,6 +95,9 @@ final class BaseDeviceDataManager: DeviceDataManager, Injectable {
                     }
                     pumpExpiresAtDate.send(endTime)
                 }
+                if let simulatorPump = pumpManager as? MockPumpManager {
+                    pumpDisplayState.value = PumpDisplayState(name: simulatorPump.localizedTitle, image: simulatorPump.smallImage)
+                }
             } else {
                 pumpDisplayState.value = nil
                 pumpExpiresAtDate.send(nil)

+ 6 - 0
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -79,6 +79,7 @@ extension Home {
         @Published var isOverrideCancelled: Bool = false
         @Published var preprocessedData: [(id: UUID, forecast: Forecast, forecastValue: ForecastValue)] = []
         @Published var pumpStatusHighlightMessage: String? = nil
+        @Published var cgmAvailable: Bool = false
 
         let context = CoreDataStack.shared.newTaskContext()
         let viewContext = CoreDataStack.shared.persistentContainer.viewContext
@@ -122,6 +123,7 @@ extension Home {
             displayYgridLines = settingsManager.settings.yGridLines
             thresholdLines = settingsManager.settings.rulerMarks
             tins = settingsManager.settings.tins
+            cgmAvailable = fetchGlucoseManager.cgmGlucoseSourceType != CGMType.none
 
             broadcaster.register(GlucoseObserver.self, observer: self)
             broadcaster.register(DeterminationObserver.self, observer: self)
@@ -387,6 +389,7 @@ extension Home.StateModel:
     PumpTimeZoneObserver,
     PumpDeactivatedObserver
 {
+    // TODO: still needed?
     func glucoseDidUpdate(_: [BloodGlucose]) {
 //        setupGlucose()
     }
@@ -409,8 +412,11 @@ extension Home.StateModel:
         displayYgridLines = settingsManager.settings.yGridLines
         thresholdLines = settingsManager.settings.rulerMarks
         tins = settingsManager.settings.tins
+        cgmAvailable = (fetchGlucoseManager.cgmGlucoseSourceType != CGMType.none)
+        displayPumpStatusHighlightMessage()
     }
 
+    // TODO: is this ever really triggered? react to MOC changes?
     func pumpHistoryDidUpdate(_: [PumpHistoryEvent]) {
         displayPumpStatusHighlightMessage()
     }

+ 71 - 56
FreeAPS/Sources/Modules/Home/View/Header/CurrentGlucoseView.swift

@@ -7,6 +7,8 @@ struct CurrentGlucoseView: View {
     @Binding var alarm: GlucoseAlarm?
     @Binding var lowGlucose: Decimal
     @Binding var highGlucose: Decimal
+    @Binding var cgmAvailable: Bool
+
     var glucose: [GlucoseStored]
     var manualGlucose: [GlucoseStored]
 
@@ -74,69 +76,82 @@ struct CurrentGlucoseView: View {
     var body: some View {
         let triangleColor = Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
 
-        ZStack {
-            TrendShape(gradient: angularGradient, color: triangleColor)
-                .rotationEffect(.degrees(rotationDegrees))
+        if cgmAvailable {
+            ZStack {
+                TrendShape(gradient: angularGradient, color: triangleColor)
+                    .rotationEffect(.degrees(rotationDegrees))
+
+                VStack(alignment: .center) {
+                    HStack {
+                        if let glucoseValue = combinedGlucoseValues.first?.glucose {
+                            let displayGlucose = convertGlucose(glucoseValue, to: units)
+                            Text(
+                                glucoseValue == 400 ? "HIGH" :
+                                    glucoseFormatter.string(from: NSNumber(value: displayGlucose)) ?? "--"
+                            )
+                            .font(.system(size: 40, weight: .bold, design: .rounded))
+                            .foregroundColor(alarm == nil ? colourGlucoseText : .loopRed)
+                        } else {
+                            Text("--")
+                                .font(.system(size: 40, weight: .bold, design: .rounded))
+                                .foregroundColor(.secondary)
+                        }
+                    }
+                    HStack {
+                        let minutesAgo = -1 * (combinedGlucoseValues.first?.date?.timeIntervalSinceNow ?? 0) / 60
+                        let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
+                        Text(
+                            minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
+                                text + " " +
+                                    NSLocalizedString("min", comment: "Short form for minutes") + " "
+                            )
+                        )
+                        .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
 
-            VStack(alignment: .center) {
-                HStack {
-                    if let glucoseValue = combinedGlucoseValues.first?.glucose {
-                        let displayGlucose = convertGlucose(glucoseValue, to: units)
                         Text(
-                            glucoseValue == 400 ? "HIGH" :
-                                glucoseFormatter.string(from: NSNumber(value: displayGlucose)) ?? "--"
+                            delta
                         )
-                        .font(.system(size: 40, weight: .bold, design: .rounded))
-                        .foregroundColor(alarm == nil ? colourGlucoseText : .loopRed)
-                    } else {
-                        Text("--")
-                            .font(.system(size: 40, weight: .bold, design: .rounded))
-                            .foregroundColor(.secondary)
-                    }
+                        .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
+                    }.frame(alignment: .top)
                 }
-                HStack {
-                    let minutesAgo = -1 * (combinedGlucoseValues.first?.date?.timeIntervalSinceNow ?? 0) / 60
-                    let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
-                    Text(
-                        minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
-                            text + " " +
-                                NSLocalizedString("min", comment: "Short form for minutes") + " "
-                        )
-                    )
-                    .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
-
-                    Text(
-                        delta
-                    )
-                    .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
-                }.frame(alignment: .top)
             }
-        }
-        .onChange(of: combinedGlucoseValues.first?.directionEnum) { newDirection in
-            withAnimation {
-                switch newDirection {
-                case .doubleUp,
-                     .singleUp,
-                     .tripleUp:
-                    rotationDegrees = -90
-                case .fortyFiveUp:
-                    rotationDegrees = -45
-                case .flat:
-                    rotationDegrees = 0
-                case .fortyFiveDown:
-                    rotationDegrees = 45
-                case .doubleDown,
-                     .singleDown,
-                     .tripleDown:
-                    rotationDegrees = 90
-                case nil,
-                     .notComputable,
-                     .rateOutOfRange:
-                    rotationDegrees = 0
-                default:
-                    rotationDegrees = 0
+            .onChange(of: combinedGlucoseValues.first?.directionEnum) { newDirection in
+                withAnimation {
+                    switch newDirection {
+                    case .doubleUp,
+                         .singleUp,
+                         .tripleUp:
+                        rotationDegrees = -90
+                    case .fortyFiveUp:
+                        rotationDegrees = -45
+                    case .flat:
+                        rotationDegrees = 0
+                    case .fortyFiveDown:
+                        rotationDegrees = 45
+                    case .doubleDown,
+                         .singleDown,
+                         .tripleDown:
+                        rotationDegrees = 90
+                    case nil,
+                         .notComputable,
+                         .rateOutOfRange:
+                        rotationDegrees = 0
+                    default:
+                        rotationDegrees = 0
+                    }
                 }
             }
+        } else {
+            VStack(alignment: .center, spacing: 12) {
+                HStack
+                    {
+                        // no cgm defined so display a generic CGM
+                        Image(systemName: "sensor.tag.radiowaves.forward.fill").font(.body).imageScale(.large)
+                    }
+                HStack {
+                    Text("Add CGM").font(.caption).bold()
+                }
+            }.frame(alignment: .top)
         }
     }
 

+ 4 - 13
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -160,24 +160,17 @@ extension Home {
                 alarm: $state.alarm,
                 lowGlucose: $state.lowGlucose,
                 highGlucose: $state.highGlucose,
+                cgmAvailable: $state.cgmAvailable,
                 glucose: state.glucoseFromPersistence,
                 manualGlucose: state.manualGlucoseFromPersistence
             ).scaleEffect(0.9)
                 .onTapGesture {
-                    if state.alarm == nil {
-                        state.openCGM()
-                    } else {
-                        state.showModal(for: .snooze)
-                    }
+                    state.openCGM()
                 }
                 .onLongPressGesture {
                     let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
                     impactHeavy.impactOccurred()
-                    if state.alarm == nil {
-                        state.showModal(for: .snooze)
-                    } else {
-                        state.openCGM()
-                    }
+                    state.showModal(for: .snooze)
                 }
         }
 
@@ -191,9 +184,7 @@ extension Home {
                 pumpStatusHighlightMessage: $state.pumpStatusHighlightMessage,
                 battery: state.batteryFromPersistence
             ).onTapGesture {
-                if state.pumpDisplayState != nil {
-                    state.setupPump = true
-                }
+                state.setupPump = true
             }
         }