Просмотр исходного кода

wip: Start rework time warning

bastiaan.verhaar 1 год назад
Родитель
Сommit
89e92649d2

+ 25 - 14
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -64,7 +64,6 @@ extension Home {
         var displayXgridLines: Bool = false
         var displayYgridLines: Bool = false
         var thresholdLines: Bool = false
-        var timeZone: TimeZone?
         var hours: Int16 = 6
         var totalBolus: Decimal = 0
         var isLoopStatusPresented: Bool = false
@@ -91,6 +90,8 @@ extension Home {
         var isOverrideCancelled: Bool = false
         var preprocessedData: [(id: UUID, forecast: Forecast, forecastValue: ForecastValue)] = []
         var pumpStatusHighlightMessage: String?
+        var pumpStatusBadgeImage: UIImage?
+        var pumpStatusBadgeColor: Color?
         var cgmAvailable: Bool = false
         var showCarbsRequiredBadge: Bool = true
         private(set) var setupPumpType: PumpConfig.PumpType = .minimed
@@ -175,9 +176,6 @@ extension Home {
                         self.setupReservoir()
                     }
                     group.addTask {
-                        self.setupCurrentPumpTimezone()
-                    }
-                    group.addTask {
                         self.setupOverrides()
                     }
                     group.addTask {
@@ -239,6 +237,7 @@ extension Home {
                 self.setupInsulinArray()
                 self.setupLastBolus()
                 self.displayPumpStatusHighlightMessage()
+                self.displayPumpStatusBadge()
             }.store(in: &subscriptions)
 
             coreDataPublisher?.filterByEntityName("OpenAPS_Battery").sink { [weak self] _ in
@@ -335,6 +334,7 @@ extension Home {
                     } else {
                         self.setupReservoir()
                         self.displayPumpStatusHighlightMessage()
+                        self.displayPumpStatusBadge()
                         self.setupBatteryArray()
                     }
                 }
@@ -396,6 +396,23 @@ extension Home {
             }
         }
 
+        private func displayPumpStatusBadge(_ didDeactivate: Bool = false) {
+            DispatchQueue.main.async { [weak self] in
+                guard let self = self else { return }
+                if let statusBadge = self.provider.deviceManager.pumpManager?.pumpStatusBadge,
+                   let image = statusBadge.image, !didDeactivate
+                {
+                    pumpStatusBadgeImage = image
+                    pumpStatusBadgeColor = statusBadge.state == .critical ? .critical : .warning
+                    let x = 0
+
+                } else {
+                    pumpStatusBadgeImage = nil
+                    pumpStatusBadgeColor = nil
+                }
+            }
+        }
+
         func runLoop() {
             provider.heartbeatNow()
         }
@@ -495,13 +512,6 @@ extension Home {
             }
         }
 
-        private func setupCurrentPumpTimezone() {
-            DispatchQueue.main.async { [weak self] in
-                guard let self = self else { return }
-                self.timeZone = self.provider.pumpTimeZone()
-            }
-        }
-
         private func getCurrentGlucoseTarget() async {
             let now = Date()
             let calendar = Calendar.current
@@ -591,6 +601,7 @@ extension Home.StateModel:
         forecastDisplayType = settingsManager.settings.forecastDisplayType
         cgmAvailable = (fetchGlucoseManager.cgmGlucoseSourceType != CGMType.none)
         displayPumpStatusHighlightMessage()
+        displayPumpStatusBadge()
         setupBatteryArray()
     }
 
@@ -624,16 +635,16 @@ extension Home.StateModel:
     func pumpReservoirDidChange(_: Decimal) {
         setupReservoir()
         displayPumpStatusHighlightMessage()
+        displayPumpStatusBadge()
     }
 
     func pumpDeactivatedDidChange() {
         displayPumpStatusHighlightMessage(true)
+        displayPumpStatusBadge(true)
         batteryFromPersistence = []
     }
 
-    func pumpTimeZoneDidChange(_: TimeZone) {
-        setupCurrentPumpTimezone()
-    }
+    func pumpTimeZoneDidChange(_: TimeZone) {}
 }
 
 extension Home.StateModel: CompletionDelegate {

+ 71 - 73
FreeAPS/Sources/Modules/Home/View/Header/PumpView.swift

@@ -6,7 +6,8 @@ struct PumpView: View {
     let name: String
     let expiresAtDate: Date?
     let timerDate: Date
-    let timeZone: TimeZone?
+    let pumpStatusBadge: UIImage?
+    let pumpStatusBadgeColor: Color?
     let pumpStatusHighlightMessage: String?
     let battery: [OpenAPS_Battery]
 
@@ -19,99 +20,96 @@ struct PumpView: View {
     }
 
     var body: some View {
-        if let pumpStatusHighlightMessage = pumpStatusHighlightMessage { // display message instead pump info
-            VStack(alignment: .center) {
-                Text(pumpStatusHighlightMessage).font(.footnote).fontWeight(.bold)
-                    .multilineTextAlignment(.center).frame(maxWidth: /*@START_MENU_TOKEN@*/ .infinity/*@END_MENU_TOKEN@*/)
-            }.frame(width: 100)
-        } else {
-            VStack(alignment: .leading, spacing: 20) {
-                if reservoir == nil && battery.isEmpty {
-                    VStack(alignment: .center, spacing: 12) {
-                        HStack {
-                            Image(systemName: "keyboard.onehanded.left")
-                                .font(.body)
-                                .imageScale(.large)
-                        }
-                        HStack {
-                            Text("Add pump")
-                                .font(.caption)
-                                .bold()
+        ZStack(alignment: .topLeading) {
+            if let pumpStatusHighlightMessage = pumpStatusHighlightMessage { // display message instead pump info
+                VStack(alignment: .center) {
+                    Text(pumpStatusHighlightMessage).font(.footnote).fontWeight(.bold)
+                        .multilineTextAlignment(.center).frame(maxWidth: /*@START_MENU_TOKEN@*/ .infinity/*@END_MENU_TOKEN@*/)
+                }.frame(width: 100)
+            } else {
+                VStack(alignment: .leading, spacing: 20) {
+                    if reservoir == nil && battery.isEmpty {
+                        VStack(alignment: .center, spacing: 12) {
+                            HStack {
+                                Image(systemName: "keyboard.onehanded.left")
+                                    .font(.body)
+                                    .imageScale(.large)
+                            }
+                            HStack {
+                                Text("Add pump")
+                                    .font(.caption)
+                                    .bold()
+                            }
                         }
+                        .frame(alignment: .top)
                     }
-                    .frame(alignment: .top)
-                }
-                if let reservoir = reservoir {
-                    HStack {
-                        Image(systemName: "cross.vial.fill")
-                            .font(.callout)
+                    if let reservoir = reservoir {
+                        HStack {
+                            Image(systemName: "cross.vial.fill")
+                                .font(.callout)
 
-                        if reservoir == 0xDEAD_BEEF {
-                            Text("50+ " + NSLocalizedString("U", comment: "Insulin unit"))
+                            if reservoir == 0xDEAD_BEEF {
+                                Text("50+ " + NSLocalizedString("U", comment: "Insulin unit"))
+                                    .font(.callout)
+                                    .fontWeight(.bold)
+                                    .fontDesign(.rounded)
+                            } else {
+                                Text(
+                                    Formatter.integerFormatter
+                                        .string(from: reservoir as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
+                                )
                                 .font(.callout)
                                 .fontWeight(.bold)
                                 .fontDesign(.rounded)
-                        } else {
-                            Text(
-                                Formatter.integerFormatter
-                                    .string(from: reservoir as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
-                            )
-                            .font(.callout)
-                            .fontWeight(.bold)
-                            .fontDesign(.rounded)
+                            }
                         }
+                        .padding(.vertical, 5)
+                        .padding(.horizontal, 10)
+                        .foregroundStyle(reservoirColor)
+                        .overlay(
+                            Capsule()
+                                .stroke(reservoirColor.opacity(0.4), lineWidth: 2)
+                        )
                     }
-                    .padding(.vertical, 5)
-                    .padding(.horizontal, 10)
-                    .foregroundStyle(reservoirColor)
-                    .overlay(
-                        Capsule()
-                            .stroke(reservoirColor.opacity(0.4), lineWidth: 2)
-                    )
-
-                    if let timeZone = timeZone, timeZone.secondsFromGMT() != TimeZone.current.secondsFromGMT() {
+
+                    if (battery.first?.display) != nil, let shouldBatteryDisplay = battery.first?.display, shouldBatteryDisplay {
                         HStack {
-                            Image(systemName: "clock.badge.exclamationmark.fill")
+                            Image(systemName: "battery.100")
                                 .font(.callout)
-                                .symbolRenderingMode(.palette)
-                                .foregroundStyle(.red, Color(.warning))
+                                .foregroundStyle(batteryColor)
+                            Text("\(Int(battery.first?.percent ?? 100)) %")
+                                .font(.callout).fontWeight(.bold).fontDesign(.rounded)
+                        }
+                    }
 
-                            Text("Timezone")
+                    if let date = expiresAtDate {
+                        HStack {
+                            Image(systemName: "stopwatch.fill")
                                 .font(.callout)
+                                .foregroundStyle(timerColor)
+
+                            Text(remainingTimeString(time: date.timeIntervalSince(timerDate)))
+                                .font(!(date.timeIntervalSince(timerDate) > 0) ? .subheadline : .callout)
                                 .fontWeight(.bold)
                                 .fontDesign(.rounded)
-                                .foregroundStyle(.red)
                         }
+                        // aligns the stopwatch icon exactly with the first pixel of the reservoir icon
                         .padding(.leading, 12)
                     }
                 }
-
-                if (battery.first?.display) != nil, let shouldBatteryDisplay = battery.first?.display, shouldBatteryDisplay {
-                    HStack {
-                        Image(systemName: "battery.100")
-                            .font(.callout)
-                            .foregroundStyle(batteryColor)
-                        Text("\(Int(battery.first?.percent ?? 100)) %")
-                            .font(.callout).fontWeight(.bold).fontDesign(.rounded)
-                    }
-                }
-
-                if let date = expiresAtDate {
-                    HStack {
-                        Image(systemName: "stopwatch.fill")
-                            .font(.callout)
-                            .foregroundStyle(timerColor)
-
-                        Text(remainingTimeString(time: date.timeIntervalSince(timerDate)))
-                            .font(!(date.timeIntervalSince(timerDate) > 0) ? .subheadline : .callout)
-                            .fontWeight(.bold)
-                            .fontDesign(.rounded)
-                    }
-                    // aligns the stopwatch icon exactly with the first pixel of the reservoir icon
-                    .padding(.leading, 12)
+            }
+            if let badgeImage = pumpStatusBadge, let badgeColor = pumpStatusBadgeColor {
+                HStack {
+                    Spacer()
+                    Image(uiImage: badgeImage.withRenderingMode(.alwaysTemplate))
+                        .colorMultiply(badgeColor)
                 }
+                .padding(.trailing, -2)
+                .padding(.top, -10)
+                .zIndex(1)
             }
         }
+        .frame(width: 100)
     }
 
     private func remainingTimeString(time: TimeInterval) -> String {

+ 2 - 1
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -108,7 +108,8 @@ extension Home {
                 name: state.pumpName,
                 expiresAtDate: state.pumpExpiresAtDate,
                 timerDate: state.timerDate,
-                timeZone: state.timeZone,
+                pumpStatusBadge: state.pumpStatusBadgeImage,
+                pumpStatusBadgeColor: state.pumpStatusBadgeColor,
                 pumpStatusHighlightMessage: state.pumpStatusHighlightMessage,
                 battery: state.batteryFromPersistence
             )