Преглед на файлове

Decimal precision for sensitive values

Ivan Valkou преди 4 години
родител
ревизия
f6e2ff297a

+ 10 - 0
FreeAPS/Sources/Helpers/String+Extensions.swift

@@ -1,3 +1,5 @@
+import Foundation
+
 extension String {
     func capitalizingFirstLetter() -> String {
         prefix(1).capitalized + dropFirst()
@@ -7,3 +9,11 @@ extension String {
         self = capitalizingFirstLetter()
     }
 }
+
+extension LosslessStringConvertible {
+    var string: String { .init(self) }
+}
+
+extension FloatingPoint where Self: LosslessStringConvertible {
+    var decimal: Decimal? { Decimal(string: string) }
+}

+ 17 - 0
FreeAPS/Sources/Models/BasalProfileEntry.swift

@@ -9,3 +9,20 @@ struct BasalProfileEntry: JSON, Equatable {
 protocol BasalProfileObserver {
     func basalProfileDidChange(_ basalProfile: [BasalProfileEntry])
 }
+
+extension BasalProfileEntry {
+    private enum CodingKeys: String, CodingKey {
+        case start
+        case minutes
+        case rate
+    }
+
+    init(from decoder: Decoder) throws {
+        let container = try decoder.container(keyedBy: CodingKeys.self)
+        let start = try container.decode(String.self, forKey: .start)
+        let minutes = try container.decode(Int.self, forKey: .minutes)
+        let rate = try container.decode(Double.self, forKey: .rate).decimal ?? .zero
+
+        self = BasalProfileEntry(start: start, minutes: minutes, rate: rate)
+    }
+}

+ 17 - 0
FreeAPS/Sources/Models/CarbRatios.swift

@@ -15,3 +15,20 @@ enum CarbUnit: String, JSON {
     case grams
     case exchanges
 }
+
+extension CarbRatioEntry {
+    private enum CodingKeys: String, CodingKey {
+        case start
+        case offset
+        case ratio
+    }
+
+    init(from decoder: Decoder) throws {
+        let container = try decoder.container(keyedBy: CodingKeys.self)
+        let start = try container.decode(String.self, forKey: .start)
+        let offset = try container.decode(Int.self, forKey: .offset)
+        let ratio = try container.decode(Double.self, forKey: .ratio).decimal ?? .zero
+
+        self = CarbRatioEntry(start: start, offset: offset, ratio: ratio)
+    }
+}

+ 17 - 0
FreeAPS/Sources/Models/InsulinSensitivities.swift

@@ -19,3 +19,20 @@ struct InsulinSensitivityEntry: JSON {
     let offset: Int
     let start: String
 }
+
+extension InsulinSensitivityEntry {
+    private enum CodingKeys: String, CodingKey {
+        case sensitivity
+        case offset
+        case start
+    }
+
+    init(from decoder: Decoder) throws {
+        let container = try decoder.container(keyedBy: CodingKeys.self)
+        let sensitivity = try container.decode(Double.self, forKey: .sensitivity).decimal ?? .zero
+        let start = try container.decode(String.self, forKey: .start)
+        let offset = try container.decode(Int.self, forKey: .offset)
+
+        self = InsulinSensitivityEntry(sensitivity: sensitivity, offset: offset, start: start)
+    }
+}

+ 2 - 1
FreeAPS/Sources/Modules/BasalProfileEditor/BasalProfileEditorStateModel.swift

@@ -15,7 +15,8 @@ extension BasalProfileEditor {
         }
 
         override func subscribe() {
-            rateValues = provider.supportedBasalRates ?? stride(from: Decimal(0.05), to: 10.01, by: 0.05).map { $0 }
+            rateValues = provider.supportedBasalRates ?? stride(from: 5.0, to: 1001.0, by: 5.0)
+                .map { ($0.decimal ?? .zero) / 100 }
             items = provider.profile.map { value in
                 let timeIndex = timeValues.firstIndex(of: Double(value.minutes * 60)) ?? 0
                 let rateIndex = rateValues.firstIndex(of: value.rate) ?? 0

+ 1 - 1
FreeAPS/Sources/Modules/CREditor/CREditorStateModel.swift

@@ -7,7 +7,7 @@ extension CREditor {
 
         let timeValues = stride(from: 0.0, to: 1.days.timeInterval, by: 30.minutes.timeInterval).map { $0 }
 
-        let rateValues = stride(from: 3, to: 50.01, by: 0.1).map { Decimal($0) }
+        let rateValues = stride(from: 30.0, to: 501.0, by: 1.0).map { ($0.decimal ?? .zero) / 10 }
 
         var canAdd: Bool {
             guard let lastItem = items.last else { return true }

+ 1 - 1
FreeAPS/Sources/Modules/ISFEditor/ISFEditorStateModel.swift

@@ -14,7 +14,7 @@ extension ISFEditor {
             case .mgdL:
                 return stride(from: 9, to: 540.01, by: 1.0).map { Decimal($0) }
             case .mmolL:
-                return stride(from: 0.1, to: 30.01, by: 0.1).map { Decimal($0) }
+                return stride(from: 1.0, to: 301.0, by: 1.0).map { ($0.decimal ?? .zero) / 10 }
             }
         }