Jonas Björkert пре 2 година
родитељ
комит
6670b2fa6e

+ 1 - 1
LoopFollow/Controllers/Nightscout/DeviceStatus.swift

@@ -83,7 +83,7 @@ extension MainViewController {
         
     // NS Device Status Response Processor
     func updateDeviceStatusDisplay(jsonDeviceStatus: [[String:AnyObject]]) {
-        infoManager.clearInfoData(types: [.iob, .cob, .override, .battery, .pump, .target, .isf, .carbRatio, .updated, .recBolus])
+        infoManager.clearInfoData(types: [.iob, .cob, .override, .battery, .pump, .target, .isf, .carbRatio, .updated, .recBolus, .tdd])
 
         if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Process: device status") }
         if jsonDeviceStatus.count == 0 {

+ 3 - 8
LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift

@@ -9,6 +9,7 @@
 import Foundation
 import UIKit
 import Charts
+import HealthKit
 
 extension MainViewController {
     func DeviceStatusLoop(formatter: ISO8601DateFormatter, lastLoopRecord: [String: AnyObject]) {
@@ -51,16 +52,11 @@ extension MainViewController {
                  */
                 let profileTargetLow = profileManager.currentTargetLow()
                 let profileTargetHigh = profileManager.currentTargetHigh()
-                var profileTarget: String?
 
                 if let profileTargetLow = profileTargetLow, let profileTargetHigh = profileTargetHigh, profileTargetLow != profileTargetHigh {
-                    profileTarget = "\(profileTargetLow) - \(profileTargetHigh)"
+                    infoManager.updateInfoData(type: .target, firstValue: profileTargetLow, secondValue: profileTargetHigh, separator: .dash)
                 } else if let profileTargetLow = profileTargetLow {
-                    profileTarget = profileTargetLow
-                }
-
-                if let profileTarget = profileTarget {
-                    infoManager.updateInfoData(type: .target, value: profileTarget)
+                    infoManager.updateInfoData(type: .target, value: profileTargetLow)
                 }
 
                 /*
@@ -71,7 +67,6 @@ extension MainViewController {
                     latestIOB = insulinMetric
                 }
 
-
                 /*
                  COB
                  */

+ 22 - 7
LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift

@@ -123,17 +123,32 @@ extension MainViewController {
                  Target
                  */
                 let profileTargetHigh = profileManager.currentTargetHigh()
-                var enactedTarget: String?
+                var enactedTarget: HKQuantity?
                 if let enactedTargetValue = enacted["current_target"] as? Double {
-                    enactedTarget = Localizer.toDisplayUnits(String(enactedTargetValue))
+                    enactedTarget = HKQuantity(unit: .milligramsPerDeciliter, doubleValue: enactedTargetValue)
                 }
-                if let profileTargetHigh = profileTargetHigh, let enactedTarget = enactedTarget, profileTargetHigh != enactedTarget {
-                    infoManager.updateInfoData(type: .target, value: "\(profileTargetHigh) → \(enactedTarget)")
-                } else if let profileTargetHigh = profileTargetHigh {
-                    infoManager.updateInfoData(type: .target, value: profileTargetHigh)
+
+                if let profileTargetHigh = profileTargetHigh, let enactedTarget = enactedTarget {
+                    let profileTargetHighFormatted = Localizer.formatQuantity(profileTargetHigh)
+                    let enactedTargetFormatted = Localizer.formatQuantity(enactedTarget)
+
+                    // Compare formatted values to avoid issues with minor floating-point differences
+                    // Profile target could be in another unit than enacted target
+                    if profileTargetHighFormatted != enactedTargetFormatted {
+                        infoManager.updateInfoData(type: .target, firstValue: profileTargetHigh, secondValue: enactedTarget, separator: .arrow)
+                    } else {
+                        infoManager.updateInfoData(type: .target, value: profileTargetHigh)
+                    }
+                }
+
+                /*
+                 TDD
+                 */
+                if let tddMetric = InsulinMetric(from: enacted, key: "TDD") {
+                    infoManager.updateInfoData(type: .tdd, value: tddMetric)
                 }
 
-                var predictioncolor = UIColor.systemGray
+                let predictioncolor = UIColor.systemGray
                 PredictionLabel.textColor = predictioncolor
                 topPredictionBG = UserDefaultsRepository.minBGScale.value
                 if let predbgdata = enacted["predBGs"] as? [String: AnyObject] {

+ 4 - 10
LoopFollow/Controllers/Nightscout/ProfileManager.swift

@@ -78,18 +78,12 @@ struct ProfileManager {
         return getCurrentValue(from: carbRatioSchedule)
     }
 
-    func currentTargetLow() -> String? {
-        if let targetLow = getCurrentValue(from: targetLowSchedule) {
-            return Localizer.formatQuantity(targetLow)
-        }
-        return nil
+    func currentTargetLow() -> HKQuantity? {
+        return getCurrentValue(from: targetLowSchedule)
     }
 
-    func currentTargetHigh() -> String? {
-        if let targetHigh = getCurrentValue(from: targetHighSchedule) {
-            return Localizer.formatQuantity(targetHigh)
-        }
-        return nil
+    func currentTargetHigh() -> HKQuantity? {
+        return getCurrentValue(from: targetHighSchedule)
     }
 
     private func getCurrentValue<T>(from schedule: [TimeValue<T>]) -> T? {

+ 2 - 1
LoopFollow/InfoTable/InfoType.swift

@@ -9,7 +9,7 @@
 import Foundation
 
 enum InfoType: Int, CaseIterable {
-    case iob, cob, basal, override, battery, pump, sage, cage, recBolus, minMax, carbsToday, autosens, profile, target, isf, carbRatio, updated
+    case iob, cob, basal, override, battery, pump, sage, cage, recBolus, minMax, carbsToday, autosens, profile, target, isf, carbRatio, updated, tdd
 
     var name: String {
         switch self {
@@ -30,6 +30,7 @@ enum InfoType: Int, CaseIterable {
         case .isf: return "ISF"
         case .carbRatio: return "CR"
         case .updated: return "Updated"
+        case .tdd: return "TDD"
         }
     }