Ver código fonte

Extend LA widgets by glucose large and tdd items, refactor accordingly

Deniz Cengiz 1 ano atrás
pai
commit
179006879d

+ 35 - 4
FreeAPS/Sources/Modules/LiveActivitySettings/View/LiveActivityWidgetConfiguration.swift

@@ -199,6 +199,8 @@ struct LiveActivityWidgetConfiguration: BaseView {
 
     private func getItemPreview(for item: LiveActivityItem) -> some View {
         switch item {
+        case .currentGlucoseLarge:
+            return AnyView(currentGlucoseLargePreview)
         case .currentGlucose:
             return AnyView(currentGlucosePreview)
         case .cob:
@@ -207,6 +209,8 @@ struct LiveActivityWidgetConfiguration: BaseView {
             return AnyView(iobPreview)
         case .updatedLabel:
             return AnyView(updatedLabelPreview)
+        case .totalDailyDose:
+            return AnyView(totalDailyDosePreview)
         }
     }
 
@@ -253,6 +257,16 @@ struct LiveActivityWidgetConfiguration: BaseView {
         .frame(height: 100)
     }
 
+    private var currentGlucoseLargePreview: some View {
+        HStack(alignment: .center) {
+            Text("123")
+                + Text("\u{2192}")
+        }
+        .foregroundStyle(Color.loopGreen)
+        .fontWeight(.bold)
+        .font(.subheadline)
+    }
+
     private var currentGlucosePreview: some View {
         VStack {
             HStack(alignment: .center) {
@@ -294,6 +308,17 @@ struct LiveActivityWidgetConfiguration: BaseView {
         }
     }
 
+    private var totalDailyDosePreview: some View {
+        VStack {
+            Text("43.21 U")
+                .fontWeight(.bold)
+                .font(.caption)
+                .foregroundStyle(.primary)
+
+            Text("TDD").font(.caption2).foregroundStyle(.primary)
+        }
+    }
+
     private func loadOrder() {
         if let savedItems = UserDefaults.standard.loadLiveActivityOrder() {
             selectedItems = savedItems.count == 4 ? savedItems : savedItems + Array(repeating: nil, count: 4 - savedItems.count)
@@ -342,27 +367,33 @@ extension UserDefaults {
 
 // Enum to represent each live activity item
 enum LiveActivityItem: String, CaseIterable, Identifiable {
+    case currentGlucoseLarge
     case currentGlucose
     case iob
     case cob
     case updatedLabel
+    case totalDailyDose
 
     var id: String { rawValue }
 
     static var defaultItems: [LiveActivityItem] {
-        [.currentGlucose, .iob, .cob, .updatedLabel]
+        [.currentGlucoseLarge, .iob, .cob, .updatedLabel]
     }
 
     var displayName: String {
         switch self {
+        case .currentGlucoseLarge:
+            return "Glucose and Trend, no Delta"
         case .currentGlucose:
-            return "Current Glucose"
+            return "Glucose, Trend, Delta"
         case .iob:
-            return "IOB"
+            return "Insulin on Board (IOB)"
         case .cob:
-            return "COB"
+            return "Carbs on Board (IOB)"
         case .updatedLabel:
             return "Last Updated"
+        case .totalDailyDose:
+            return "Total Daily Dose"
         }
     }
 }

+ 2 - 1
FreeAPS/Sources/Services/LiveActivity/Data/DataManager.swift

@@ -33,7 +33,7 @@ extension LiveActivityBridge {
             key: "deliverAt",
             ascending: false,
             fetchLimit: 1,
-            propertiesToFetch: ["iob", "cob", "currentTarget", "deliverAt"]
+            propertiesToFetch: ["iob", "cob", "totalDailyDose", "currentTarget", "deliverAt"]
         )
 
         return await context.perform {
@@ -45,6 +45,7 @@ extension LiveActivityBridge {
                 DeterminationData(
                     cob: ($0["cob"] as? Int) ?? 0,
                     iob: ($0["iob"] as? NSDecimalNumber)?.decimalValue ?? 0,
+                    tdd: ($0["totalDailyDose"] as? NSDecimalNumber)?.decimalValue ?? 0,
                     target: ($0["currentTarget"] as? NSDecimalNumber)?.decimalValue ?? 0,
                     date: $0["deliverAt"] as? Date ?? nil
                 )

+ 1 - 0
FreeAPS/Sources/Services/LiveActivity/Data/DeterminationData.swift

@@ -3,6 +3,7 @@ import Foundation
 struct DeterminationData {
     let cob: Int
     let iob: Decimal
+    let tdd: Decimal
     let target: Decimal
     let date: Date?
 }

+ 4 - 1
FreeAPS/Sources/Services/LiveActivity/LiveActitiyAttributes.swift

@@ -3,13 +3,15 @@ import Foundation
 
 struct LiveActivityAttributes: ActivityAttributes {
     enum LiveActivityItem: String, Hashable, Codable, Equatable {
+        case currentGlucoseLarge
         case currentGlucose
         case iob
         case cob
         case updatedLabel
+        case totalDailyDose
         case empty
 
-        static let defaultItems: [Self] = [.currentGlucose, .iob, .cob, .updatedLabel]
+        static let defaultItems: [Self] = [.currentGlucoseLarge, .iob, .cob, .updatedLabel]
     }
 
     struct ContentState: Codable, Hashable {
@@ -34,6 +36,7 @@ struct LiveActivityAttributes: ActivityAttributes {
         let rotationDegrees: Double
         let cob: Decimal
         let iob: Decimal
+        let tdd: Decimal
         let isOverrideActive: Bool
         let overrideName: String
         let overrideDate: Date

+ 1 - 0
FreeAPS/Sources/Services/LiveActivity/LiveActivityAttributes+Helper.swift

@@ -109,6 +109,7 @@ extension LiveActivityAttributes.ContentState {
                 rotationDegrees: rotationDegrees,
                 cob: Decimal(determination?.cob ?? 0),
                 iob: determination?.iob ?? 0 as Decimal,
+                tdd: determination?.tdd ?? 0 as Decimal,
                 isOverrideActive: override?.isActive ?? false,
                 overrideName: override?.overrideName ?? "Override",
                 overrideDate: override?.date ?? Date(),

+ 1 - 0
LiveActivity/LiveActivity.swift

@@ -84,6 +84,7 @@ private extension LiveActivityAttributes.ContentState {
         rotationDegrees: 0,
         cob: 20,
         iob: 1.5,
+        tdd: 43.21,
         isOverrideActive: false,
         overrideName: "Exercise",
         overrideDate: Date().addingTimeInterval(-3600),

+ 0 - 1
LiveActivity/Views/LiveActivityGlucoseDeltaLabelView.swift

@@ -11,7 +11,6 @@ import WidgetKit
 struct LiveActivityGlucoseDeltaLabelView: View {
     var context: ActivityViewContext<LiveActivityAttributes>
     var glucoseColor: Color
-    var isDetailed: Bool = false
 
     var body: some View {
         if !context.state.change.isEmpty {

+ 14 - 6
LiveActivity/Views/LiveActivityView.swift

@@ -63,23 +63,32 @@ struct LiveActivityView: View {
                             case .currentGlucose:
                                 VStack {
                                     LiveActivityBGLabelView(context: context, additionalState: detailedViewState)
+                                        .foregroundStyle(glucoseColor)
+
                                     HStack {
                                         LiveActivityGlucoseDeltaLabelView(
                                             context: context,
-                                            glucoseColor: .primary,
-                                            isDetailed: true
+                                            glucoseColor: .primary
                                         )
                                         if !context.isStale, let direction = context.state.direction {
                                             Text(direction).font(.headline)
                                         }
                                     }
                                 }
+                            case .currentGlucoseLarge:
+                                LiveActivityBGLabelLargeView(
+                                    context: context,
+                                    additionalState: detailedViewState,
+                                    glucoseColor: glucoseColor
+                                )
                             case .iob:
                                 LiveActivityIOBLabelView(context: context, additionalState: detailedViewState)
                             case .cob:
                                 LiveActivityCOBLabelView(context: context, additionalState: detailedViewState)
                             case .updatedLabel:
                                 LiveActivityUpdatedLabelView(context: context, isDetailedLayout: true)
+                            case .totalDailyDose:
+                                LiveActivityTotalDailyDoseView(context: context, additionalState: detailedViewState)
                             case .empty:
                                 Text("").frame(width: 50, height: 50)
                             }
@@ -120,8 +129,7 @@ struct LiveActivityView: View {
                         VStack(alignment: .trailing, spacing: 5) {
                             LiveActivityGlucoseDeltaLabelView(
                                 context: context,
-                                glucoseColor: hasStaticColorScheme ? .primary : glucoseColor,
-                                isDetailed: false
+                                glucoseColor: hasStaticColorScheme ? .primary : glucoseColor
                             ).font(.title3)
                             LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false).font(.caption)
                                 .foregroundStyle(.primary.opacity(0.7))
@@ -158,7 +166,7 @@ struct LiveActivityExpandedTrailingView: View {
     var glucoseColor: Color
 
     var body: some View {
-        LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor, isDetailed: false).font(.title2)
+        LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor).font(.title2)
             .padding(.trailing, 5)
     }
 }
@@ -198,7 +206,7 @@ struct LiveActivityCompactTrailingView: View {
     var glucoseColor: Color
 
     var body: some View {
-        LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor, isDetailed: false).padding(.trailing, 4)
+        LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor).padding(.trailing, 4)
     }
 }
 

+ 29 - 0
LiveActivity/Views/WidgetItems/LiveActivityBGLabelLargeView.swift

@@ -0,0 +1,29 @@
+import Foundation
+import SwiftUI
+import WidgetKit
+
+struct LiveActivityBGLabelLargeView: View {
+    var context: ActivityViewContext<LiveActivityAttributes>
+    var additionalState: LiveActivityAttributes.ContentAdditionalState
+    var glucoseColor: Color
+
+    var body: some View {
+        HStack {
+            if let trendArrow = context.state.direction {
+                Text(context.state.bg)
+                    .fontWeight(.heavy)
+                    .font(.title)
+                    .foregroundStyle(context.isStale ? .secondary : glucoseColor)
+                    .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
+                    +
+                    Text(trendArrow).foregroundStyle(context.isStale ? .secondary : glucoseColor)
+            } else {
+                Text(context.state.bg)
+                    .fontWeight(.heavy)
+                    .font(.title)
+                    .foregroundStyle(context.isStale ? .secondary : glucoseColor)
+                    .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
+            }
+        }
+    }
+}

+ 35 - 0
LiveActivity/Views/WidgetItems/LiveActivityTotalDailyDoseView.swift

@@ -0,0 +1,35 @@
+import Foundation
+import SwiftUI
+import WidgetKit
+
+struct LiveActivityTotalDailyDoseView: View {
+    var context: ActivityViewContext<LiveActivityAttributes>
+    var additionalState: LiveActivityAttributes.ContentAdditionalState
+
+    private var bolusFormatter: NumberFormatter {
+        let formatter = NumberFormatter()
+        formatter.numberStyle = .decimal
+        formatter.maximumFractionDigits = 1
+        return formatter
+    }
+
+    var body: some View {
+        VStack(spacing: 2) {
+            HStack {
+                Text(
+                    bolusFormatter.string(from: additionalState.tdd as NSNumber) ?? "--"
+                )
+                .fontWeight(.bold)
+                .font(.title3)
+                .foregroundStyle(context.isStale ? .secondary : .primary)
+                .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
+
+                Text("U")
+                    .font(.headline).fontWeight(.bold)
+                    .foregroundStyle(context.isStale ? .secondary : .primary)
+                    .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
+            }
+            Text("TDD").font(.subheadline).foregroundStyle(.primary)
+        }
+    }
+}