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

Add minutes ago to Header and change color on text depending on how many minutes ago latest CGM reading was

(cherry picked from commit 84dd3ea5274e4aefdcfaed46576bedadfe804d1b)
Jon Mårtensson пре 5 година
родитељ
комит
2a0d2f033d
1 измењених фајлова са 27 додато и 6 уклоњено
  1. 27 6
      FreeAPS/Sources/Modules/Home/View/Header/CurrentGlucoseView.swift

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

@@ -32,11 +32,11 @@ struct CurrentGlucoseView: View {
 
     var colorOfGlucose: Color {
         let glucoseString =
-            " \(recentGlucose?.glucose.map { glucoseFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! })"
-        let glucoseStringWithoutSuffix = String(glucoseString.dropFirst(11)) // Drop first 11 characters "Conditional"
-        let glucoseStringTrimmed = String(glucoseStringWithoutSuffix.dropLast(3)) // Drop last 3 characters "x")"
+            recentGlucose?.glucose
+                .map { glucoseFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! } ?? "--"
+        let glucoseStringFirstTwoCharacters = String(glucoseString.dropLast(1))
 
-        switch glucoseStringTrimmed {
+        switch glucoseStringFirstTwoCharacters {
         case "4,",
              "5,",
              "6,",
@@ -50,6 +50,27 @@ struct CurrentGlucoseView: View {
         }
     }
 
+    var minutesAgo: Int {
+        let lastGlucoseDate = recentGlucose.map { dateFormatter.string(from: $0.dateString) } ?? "--"
+        let glucoseDate = Date(lastGlucoseDate) ?? Date()
+        let now = Date()
+        let diff = Int(glucoseDate.timeIntervalSince1970 - now.timeIntervalSince1970)
+        let hours = diff / 3600
+        var minutes = (diff - hours * 3600) / 60
+        return minutes
+    }
+
+    func colorOfMinutes(_ minutes: Int) -> Color {
+        switch minutes {
+        case 6 ... 9:
+            return .loopYellow
+        case 0 ... 5:
+            return .loopGray
+        default:
+            return .loopRed
+        }
+    }
+
     var body: some View {
         VStack(alignment: .center, spacing: 6) {
             HStack(spacing: 8) {
@@ -68,8 +89,8 @@ struct CurrentGlucoseView: View {
             }.padding(.leading, 4)
             HStack(alignment: .lastTextBaseline, spacing: 2) {
                 Text(
-                    recentGlucose.map { dateFormatter.string(from: $0.dateString) } ?? "--"
-                ).font(.caption2).foregroundColor(.secondary)
+                    "\(minutesAgo) min "
+                ).font(.caption2).foregroundColor(colorOfMinutes(minutesAgo))
                 Text(
                     delta
                         .map { deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!