Parcourir la source

Move bottom and top threshold into enum for reuse

Deniz Cengiz il y a 1 an
Parent
commit
fb0a98ccd0

+ 17 - 0
Trio/Sources/Models/TimeInRangeType.swift

@@ -14,4 +14,21 @@ enum TimeInRangeType: String, JSON, CaseIterable, Identifiable, Codable, Hashabl
             return String(localized: "Time in Normoglycemia (TING)", comment: "")
         }
     }
+
+    var bottomThreshold: Int {
+        switch self {
+        case .timeInTightRange:
+            return 70
+        case .timeInNormoglycemia:
+            return 63
+        }
+    }
+
+    var topThreshold: Int {
+        switch self {
+        case .timeInNormoglycemia,
+             .timeInTightRange:
+            return 140
+        }
+    }
 }

+ 6 - 20
Trio/Sources/Modules/Stat/StatStateModel+Setup/StackedChartSetup.swift

@@ -64,23 +64,6 @@ extension Stat.StateModel {
     func calculateGlucoseRangeStatsForStackedChart(from ids: [NSManagedObjectID]) async {
         let taskContext = CoreDataStack.shared.newTaskContext()
 
-        var bottomThreshold: Int {
-            switch timeInRangeType {
-            case .timeInTightRange:
-                return 70
-            case .timeInNormoglycemia:
-                return 63
-            }
-        }
-
-        var topThreshold: Int {
-            switch timeInRangeType {
-            case .timeInNormoglycemia,
-                 .timeInTightRange:
-                return 140
-            }
-        }
-
         let calendar = Calendar.current
 
         let stats = await taskContext.perform {
@@ -110,9 +93,12 @@ extension Stat.StateModel {
             // Ranges are processed from bottom to top in the stacked chart
             let ranges: [(name: String, condition: (Int) -> Bool)] = [
                 ("<54", { g in g <= 54 }),
-                ("54-\(bottomThreshold)", { g in g > 54 && g < bottomThreshold }),
-                ("\(bottomThreshold)-\(topThreshold)", { g in g >= bottomThreshold && g <= topThreshold }),
-                ("\(topThreshold)-180", { g in g > topThreshold && g <= 180 }),
+                ("54-\(self.timeInRangeType.bottomThreshold)", { g in g > 54 && g < self.timeInRangeType.bottomThreshold }),
+                (
+                    "\(self.timeInRangeType.bottomThreshold)-\(self.timeInRangeType.topThreshold)",
+                    { g in g >= self.timeInRangeType.bottomThreshold && g <= self.timeInRangeType.topThreshold }
+                ),
+                ("\(self.timeInRangeType.topThreshold)-180", { g in g > self.timeInRangeType.topThreshold && g <= 180 }),
                 ("180-200", { g in g > 180 && g <= 200 }),
                 ("200-220", { g in g > 200 && g <= 220 }),
                 (">220", { g in g > 220 })

+ 6 - 23
Trio/Sources/Modules/Stat/View/ViewElements/Glucose/GlucoseDistributionChart.swift

@@ -9,23 +9,6 @@ struct GlucoseDistributionChart: View {
     let glucoseRangeStats: [GlucoseRangeStats]
     let timeInRangeType: TimeInRangeType
 
-    var bottomThreshold: Int {
-        switch timeInRangeType {
-        case .timeInTightRange:
-            return 70
-        case .timeInNormoglycemia:
-            return 63
-        }
-    }
-
-    var topThreshold: Int {
-        switch timeInRangeType {
-        case .timeInNormoglycemia,
-             .timeInTightRange:
-            return 140
-        }
-    }
-
     var body: some View {
         VStack(alignment: .leading, spacing: 8) {
             Text("Glucose Distribution")
@@ -43,9 +26,9 @@ struct GlucoseDistributionChart: View {
             }
             .chartForegroundStyleScale([
                 "<54": .purple.opacity(0.7),
-                "54-\(bottomThreshold)": .red.opacity(0.7),
-                "\(bottomThreshold)-\(topThreshold)": .green,
-                "\(topThreshold)-180": .green.opacity(0.7),
+                "54-\(timeInRangeType.bottomThreshold)": .red.opacity(0.7),
+                "\(timeInRangeType.bottomThreshold)-\(timeInRangeType.topThreshold)": .green,
+                "\(timeInRangeType.topThreshold)-180": .green.opacity(0.7),
                 "180-200": .yellow.opacity(0.7),
                 "200-220": .orange.opacity(0.7),
                 ">220": .orange.opacity(0.8)
@@ -54,15 +37,15 @@ struct GlucoseDistributionChart: View {
                 let legendItems: [(String, Color)] = [
                     ("<\(units == .mgdL ? Decimal(54) : 54.asMmolL)", .purple.opacity(0.7)),
                     (
-                        "\(units == .mgdL ? Decimal(54) : 54.asMmolL)-\(units == .mgdL ? Decimal(bottomThreshold) : bottomThreshold.asMmolL)",
+                        "\(units == .mgdL ? Decimal(54) : 54.asMmolL)-\(units == .mgdL ? Decimal(timeInRangeType.bottomThreshold) : timeInRangeType.bottomThreshold.asMmolL)",
                         .red.opacity(0.7)
                     ),
                     (
-                        "\(units == .mgdL ? Decimal(bottomThreshold) : bottomThreshold.asMmolL)-\(units == .mgdL ? Decimal(topThreshold) : topThreshold.asMmolL)",
+                        "\(units == .mgdL ? Decimal(timeInRangeType.bottomThreshold) : timeInRangeType.bottomThreshold.asMmolL)-\(units == .mgdL ? Decimal(timeInRangeType.topThreshold) : timeInRangeType.topThreshold.asMmolL)",
                         .green
                     ),
                     (
-                        "\(units == .mgdL ? Decimal(topThreshold) : topThreshold.asMmolL)-\(units == .mgdL ? Decimal(180) : 180.asMmolL)",
+                        "\(units == .mgdL ? Decimal(timeInRangeType.topThreshold) : timeInRangeType.topThreshold.asMmolL)-\(units == .mgdL ? Decimal(180) : 180.asMmolL)",
                         .green.opacity(0.7)
                     ),
                     (

+ 10 - 22
Trio/Sources/Modules/Stat/View/ViewElements/Glucose/GlucoseSectorChart.swift

@@ -23,23 +23,6 @@ struct GlucoseSectorChart: View {
         case low = "Low"
     }
 
-    var tightBottomThreshold: Int {
-        switch timeInRangeType {
-        case .timeInTightRange:
-            return 70
-        case .timeInNormoglycemia:
-            return 63
-        }
-    }
-
-    var topThreshold: Int {
-        switch timeInRangeType {
-        case .timeInNormoglycemia,
-             .timeInTightRange:
-            return 140
-        }
-    }
-
     var body: some View {
         HStack(alignment: .center, spacing: 20) {
             // Calculate total number of glucose readings
@@ -47,7 +30,8 @@ struct GlucoseSectorChart: View {
             // Count readings between high limit and 250 mg/dL (high)
             let high = glucose.filter { $0.glucose > Int(highLimit) }.count
             // Count readings between low limit (TITR: 70 mg/dL, TING 63 mg/dL) and 140 mg/dL (tight control)
-            let tight = glucose.filter { $0.glucose >= tightBottomThreshold && $0.glucose <= topThreshold }.count
+            let tight = glucose
+                .filter { $0.glucose >= timeInRangeType.bottomThreshold && $0.glucose <= timeInRangeType.topThreshold }.count
             // Count readings between 140 and high limit (normal range)
             let normal = glucose.filter { $0.glucose >= Int(lowLimit) && $0.glucose <= Int(highLimit) }.count
             // Count readings between 54 and low limit (low)
@@ -72,8 +56,11 @@ struct GlucoseSectorChart: View {
                 }
 
                 VStack(alignment: .leading, spacing: 5) {
-                    Text("\(formatValue(Decimal(tightBottomThreshold)))-\(formatValue(Decimal(topThreshold)))").font(.subheadline)
-                        .foregroundStyle(Color.secondary)
+                    Text(
+                        "\(formatValue(Decimal(timeInRangeType.bottomThreshold)))-\(formatValue(Decimal(timeInRangeType.topThreshold)))"
+                    )
+                    .font(.subheadline)
+                    .foregroundStyle(Color.secondary)
                     Text(tightPercentage.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1))) + "%")
                         .foregroundStyle(Color.green)
                 }
@@ -238,7 +225,8 @@ struct GlucoseSectorChart: View {
             )
 
         case .inRange:
-            let tight = glucose.filter { $0.glucose >= Int(tightBottomThreshold) && $0.glucose <= topThreshold }.count
+            let tight = glucose
+                .filter { $0.glucose >= Int(timeInRangeType.bottomThreshold) && $0.glucose <= timeInRangeType.topThreshold }.count
             let glucoseValues = glucose.filter { $0.glucose >= Int(lowLimit) && $0.glucose <= Int(highLimit) }
             let glucoseValuesAsInt = glucoseValues.map { Int($0.glucose) }
             let (average, median, standardDeviation) = calculateDetailedStatistics(for: glucoseValuesAsInt)
@@ -253,7 +241,7 @@ struct GlucoseSectorChart: View {
                     ),
                     (
                         String(
-                            localized: "\(timeInRangeType == .timeInTightRange ? "TITR" : "TING") (\(formatValue(Decimal(tightBottomThreshold)))-\(formatValue(Decimal(topThreshold))))"
+                            localized: "\(timeInRangeType == .timeInTightRange ? "TITR" : "TING") (\(formatValue(Decimal(timeInRangeType.bottomThreshold)))-\(formatValue(Decimal(timeInRangeType.topThreshold))))"
                         ),
                         formatPercentage(Decimal(tight) / total * 100)
                     ),