Explorar el Código

Make function cleaner

Jon Mårtensson hace 5 años
padre
commit
08e050da70
Se han modificado 1 ficheros con 14 adiciones y 15 borrados
  1. 14 15
      FreeAPS/Sources/Modules/Home/View/Header/CurrentGlucoseView.swift

+ 14 - 15
FreeAPS/Sources/Modules/Home/View/Header/CurrentGlucoseView.swift

@@ -31,19 +31,19 @@ struct CurrentGlucoseView: View {
     }
     }
 
 
     var colorOfGlucose: Color {
     var colorOfGlucose: Color {
-        let glucoseString =
-            recentGlucose?.glucose
-                .map { glucoseFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! } ?? "--"
-        let glucoseStringFirstTwoCharacters = String(glucoseString.dropLast(1))
+        guard var recentBG = recentGlucose?.glucose
+        else { return .loopYellow }
 
 
-        switch glucoseStringFirstTwoCharacters {
-        case "4,",
-             "5,",
-             "6,",
-             "7,":
+        recentBG /= 18 // convert to mmol/l for calculation
+
+        switch recentBG {
+        case 4,
+             5,
+             6,
+             7:
             return .loopGreen
             return .loopGreen
-        case "8,",
-             "9,":
+        case 8,
+             9:
             return .loopYellow
             return .loopYellow
         default:
         default:
             return .loopRed
             return .loopRed
@@ -52,12 +52,11 @@ struct CurrentGlucoseView: View {
 
 
     var minutesAgo: Int {
     var minutesAgo: Int {
         let lastGlucoseDateString = recentGlucose.map { dateFormatter.string(from: $0.dateString) } ?? "--"
         let lastGlucoseDateString = recentGlucose.map { dateFormatter.string(from: $0.dateString) } ?? "--"
-        let glucoseDate = Date(lastGlucoseDateString) ?? Date()
+        let LastGlucoseDate = Date(lastGlucoseDateString) ?? Date()
         let now = Date()
         let now = Date()
-        let diff = Int(glucoseDate.timeIntervalSince1970 - now.timeIntervalSince1970)
+        let diff = Int(now.timeIntervalSince1970 - LastGlucoseDate.timeIntervalSince1970)
         let hoursDiff = diff / 3600
         let hoursDiff = diff / 3600
-        var minutesDiff = (diff - hoursDiff * 3600) / 60
-        minutesDiff.negate() // Remove "-" sign
+        let minutesDiff = (diff - hoursDiff * 3600) / 60
         return minutesDiff
         return minutesDiff
     }
     }