瀏覽代碼

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

Jon Mårtensson 5 年之前
父節點
當前提交
84dd3ea527
共有 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 {
     var colorOfGlucose: Color {
         let glucoseString =
         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,",
         case "4,",
              "5,",
              "5,",
              "6,",
              "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 {
     var body: some View {
         VStack(alignment: .center, spacing: 6) {
         VStack(alignment: .center, spacing: 6) {
             HStack(spacing: 8) {
             HStack(spacing: 8) {
@@ -68,8 +89,8 @@ struct CurrentGlucoseView: View {
             }.padding(.leading, 4)
             }.padding(.leading, 4)
             HStack(spacing: 2) {
             HStack(spacing: 2) {
                 Text(
                 Text(
-                    recentGlucose.map { dateFormatter.string(from: $0.dateString) } ?? "--"
-                ).font(.caption2).foregroundColor(.secondary)
+                    "\(minutesAgo) min "
+                ).font(.caption2).foregroundColor(colorOfMinutes(minutesAgo))
                 Text(
                 Text(
                     delta
                     delta
                         .map { deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
                         .map { deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!