Ivan Valkou 5 年 前
コミット
7ecc996c57

+ 17 - 0
FreeAPS/Sources/Modules/Home/HomeViewModel.swift

@@ -25,6 +25,7 @@ extension Home {
         @Published var isLooping = false
         @Published var statusTitle = ""
         @Published var lastLoopDate: Date = .distantPast
+        @Published var tempRate: Decimal?
 
         @Published var allowManualTemp = false
         private(set) var units: GlucoseUnits = .mmolL
@@ -122,6 +123,22 @@ extension Home {
                 self.tempBasals = self.provider.pumpHistory(hours: self.filteredHours).filter {
                     $0.type == .tempBasal || $0.type == .tempBasalDuration
                 }
+                let lastTempBasal = Array(self.tempBasals.suffix(2))
+                guard lastTempBasal.count == 2 else {
+                    self.tempRate = nil
+                    return
+                }
+
+                guard let lastRate = lastTempBasal[0].rate, let lastDuration = lastTempBasal[1].durationMin else {
+                    self.tempRate = nil
+                    return
+                }
+                let lastDate = lastTempBasal[0].timestamp
+                guard Date().timeIntervalSince(lastDate.addingTimeInterval(lastDuration.minutes.timeInterval)) < 0 else {
+                    self.tempRate = nil
+                    return
+                }
+                self.tempRate = lastRate
             }
         }
 

+ 1 - 23
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

@@ -68,13 +68,6 @@ struct MainChartView: View {
         return formatter
     }
 
-    private var basalFormatter: NumberFormatter {
-        let formatter = NumberFormatter()
-        formatter.numberStyle = .decimal
-        formatter.maximumFractionDigits = 2
-        return formatter
-    }
-
     private var bolusFormatter: NumberFormatter {
         let formatter = NumberFormatter()
         formatter.numberStyle = .decimal
@@ -163,10 +156,6 @@ struct MainChartView: View {
             tempBasalPath.fill(Color.blue)
             tempBasalPath.stroke(Color.blue, lineWidth: 1)
             regularBasalPath.stroke(Color.yellow, lineWidth: 1)
-            Text(lastBasalRateString())
-                .foregroundColor(.blue)
-                .font(.caption2)
-                .position(CGPoint(x: lastBasalPoint(fullSize: fullSize).x + 30, y: Config.basalHeight / 2))
         }
         .drawingGroup()
         .frame(width: fullGlucoseWidth(viewWidth: fullSize.width) + additionalWidth(viewWidth: fullSize.width))
@@ -459,11 +448,9 @@ extension MainChartView {
                 let rateCost = Config.basalHeight / CGFloat(maxBasal)
                 let x0 = timeToXCoordinate(timeBegin, fullSize: fullSize)
                 let y0 = Config.basalHeight - CGFloat(chunk[0].rate ?? 0) * rateCost
-                let x1 = timeToXCoordinate(timeEnd, fullSize: fullSize)
-                let y1 = Config.basalHeight
                 let regularPoints = findRegularBasalPoints(timeBegin: lastTimeEnd, timeEnd: timeBegin, fullSize: fullSize)
                 lastTimeEnd = timeEnd
-                return regularPoints + [CGPoint(x: x0, y: y0), CGPoint(x: x1, y: y1)]
+                return regularPoints + [CGPoint(x: x0, y: y0)]
             }.flatMap { $0 }
             let tempBasalPath = Path { path in
                 var yPoint: CGFloat = Config.basalHeight
@@ -608,15 +595,6 @@ extension MainChartView {
         return CGPoint(x: x, y: y)
     }
 
-    private func lastBasalRateString() -> String {
-        let lastBasal = Array(tempBasals.suffix(2))
-        guard lastBasal.count == 2 else {
-            return ""
-        }
-        let lastRate = lastBasal[0].rate ?? 0
-        return (basalFormatter.string(from: lastRate as NSNumber) ?? "0") + " U/hr"
-    }
-
     private func fullGlucoseWidth(viewWidth: CGFloat) -> CGFloat {
         viewWidth * CGFloat(hours) / CGFloat(Config.screenHours)
     }

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

@@ -31,7 +31,7 @@ struct CurrentGlucoseView: View {
     }
 
     var body: some View {
-        HStack {
+        HStack(spacing: 0) {
             VStack {
                 Text(
                     recentGlucose?.glucose

+ 29 - 15
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -15,17 +15,7 @@ extension Home {
         var header: some View {
             HStack {
                 VStack(alignment: .leading) {
-                    HStack {
-                        Text("IOB").font(.caption)
-                        Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
-                            .font(.caption2)
-                    }.padding(.top, 16)
-                    Spacer()
-                    HStack {
-                        Text("COB").font(.caption)
-                        Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
-                            .font(.caption2)
-                    }
+                    Text("PUMP").font(.caption)
                 }.frame(minWidth: 0, maxWidth: .infinity)
                 Spacer()
                 CurrentGlucoseView(
@@ -49,11 +39,34 @@ extension Home {
             }.frame(maxWidth: .infinity)
         }
 
+        var infoPanal: some View {
+            HStack(alignment: .firstTextBaseline) {
+                Text("IOB").font(.caption)
+                    .padding(.leading)
+                Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
+                    .font(.caption)
+
+                Text("COB").font(.caption)
+                Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
+                    .font(.caption)
+                if let tempRate = viewModel.tempRate {
+                    Text("Temp basal").font(.caption).foregroundColor(.blue)
+                    Text((numberFormatter.string(from: tempRate as NSNumber) ?? "0") + " U/hr")
+                        .font(.caption).foregroundColor(.blue)
+                }
+
+                Spacer()
+
+            }.frame(maxWidth: .infinity, maxHeight: 30)
+                .background(Rectangle().fill(Color.gray.opacity(0.2)))
+        }
+
         var body: some View {
             viewModel.setFilteredGlucoseHours(hours: 24)
             return GeometryReader { geo in
-                VStack {
+                VStack(spacing: 0) {
                     header.padding(.vertical).frame(maxHeight: 70)
+                    infoPanal
                     MainChartView(
                         glucose: $viewModel.glucose,
                         suggestion: $viewModel.suggestion,
@@ -66,6 +79,7 @@ extension Home {
                         carbs: $viewModel.carbs,
                         units: viewModel.units
                     )
+                    .padding(.bottom)
 
                     ZStack {
                         Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
@@ -77,7 +91,7 @@ extension Home {
                                     .renderingMode(.template)
                                     .resizable()
                                     .frame(width: 24, height: 24)
-                            }.foregroundColor(.green)
+                            }.foregroundColor(.orange)
                             Spacer()
                             Button { viewModel.showModal(for: .addTempTarget) }
                             label: {
@@ -85,7 +99,7 @@ extension Home {
                                     .renderingMode(.template)
                                     .resizable()
                                     .frame(width: 24, height: 24)
-                            }.foregroundColor(.green)
+                            }.foregroundColor(.primary)
                             Spacer()
                             Button { viewModel.showModal(for: .bolus) }
                             label: {
@@ -93,7 +107,7 @@ extension Home {
                                     .renderingMode(.template)
                                     .resizable()
                                     .frame(width: 24, height: 24)
-                            }.foregroundColor(.orange)
+                            }.foregroundColor(.blue)
                             Spacer()
                             if viewModel.allowManualTemp {
                                 Button { viewModel.showModal(for: .manualTempBasal) }