@@ -23,32 +23,24 @@ private func signalHandler(signal: Int32) {
2323}
2424
2525#if os(Linux) || os(Windows)
26- // If the Linux library expects LoopQuantity but doesn't provide it,
27- // we define a compatible version here.
28- public struct LoopQuantity : Codable {
29- public let unit : LoopUnit
30- public let doubleValue : Double
31-
32- public init ( unit: LoopUnit , doubleValue: Double ) {
33- self . unit = unit
34- self . doubleValue = doubleValue
35- }
26+ public typealias AlgorithmValue = Double
27+ #else
28+ public typealias AlgorithmValue = LoopQuantity
29+ #endif
3630
37- public func doubleValue( for unit: LoopUnit ) -> Double {
38- return doubleValue
31+ struct AlgorithmFactory {
32+ static func createScheduleValue( startDate: Date , endDate: Date , value: Double , unit: String ) -> AbsoluteScheduleValue < AlgorithmValue > {
33+ #if os(Linux) || os(Windows)
34+ // Linux wants the raw Double
35+ return AbsoluteScheduleValue < Double > ( startDate: startDate, endDate: endDate, value: value)
36+ #else
37+ // Mac wants the LoopQuantity object
38+ let quantity = LoopQuantity ( unit: LoopUnit ( from: unit) , doubleValue: value)
39+ return AbsoluteScheduleValue < LoopQuantity > ( startDate: startDate, endDate: endDate, value: quantity)
40+ #endif
3941 }
4042}
4143
42- public struct LoopUnit : Codable {
43- public let unitString : String
44- public init ( from string: String ) { self . unitString = string }
45- public static let gram = LoopUnit ( from: " g " )
46- }
47- #endif
48-
49- // This makes AlgorithmValue always LoopQuantity on ALL platforms
50- public typealias AlgorithmValue = LoopQuantity
51-
5244@_cdecl ( " initializeExceptionHandler " )
5345public func initializeExceptionHandler( ) {
5446 #if os(macOS) || os(iOS)
@@ -446,26 +438,23 @@ public func getDynamicCarbsOnBoard(jsonData: UnsafePointer<Int8>?) -> Double {
446438 let startDate = dateFormatter. date ( from: input. inputICE [ 0 ] . startAt) !
447439 let endDate = dateFormatter. date ( from: input. inputICE. last!. startAt) !
448440
449- // This uses the compatibility layer to work on all platforms
450- let carbRatio = [ AbsoluteScheduleValue (
451- startDate: startDate,
452- endDate: endDate,
453- value: LoopQuantity ( unit: LoopUnit ( from: " g/U " ) , doubleValue: input. carbRatio)
454- ) ]
441+ // Use the factory to get the right type for the current OS
442+ let carbRatio : [ AbsoluteScheduleValue < AlgorithmValue > ] = [
443+ AlgorithmFactory . createScheduleValue ( startDate: startDate, endDate: endDate, value: input. carbRatio, unit: " g/U " )
444+ ]
455445
456- let isf = [ AbsoluteScheduleValue (
457- startDate: startDate,
458- endDate: endDate,
459- value: LoopQuantity ( unit: LoopUnit ( from: " mg/dL/U " ) , doubleValue: input. sensitivity)
460- ) ]
446+ let isf : [ AbsoluteScheduleValue < AlgorithmValue > ] = [
447+ AlgorithmFactory . createScheduleValue ( startDate: startDate, endDate: endDate, value: input. sensitivity, unit: " mg/dL/U " )
448+ ]
461449
462450 let statuses = [ carbEntries [ 0 ] ] . map (
463451 to: inputICE,
464452 carbRatio: carbRatio,
465453 insulinSensitivity: isf,
466454 initialAbsorptionTimeOverrun: 2.0 ,
467455 absorptionModel: PiecewiseLinearAbsorption ( )
468- ) // TODO: Add a function for different absorbrionmodels
456+ )
457+ // TODO: Add a function for different absorbrionmodels
469458
470459 // The output here is a list of CarbValues with startDate, endDate (equal to startDate), and value (ICE?)
471460 let carbsOnBoard = statuses. dynamicCarbsOnBoard (
@@ -593,7 +582,7 @@ private func carbEntriesFromFixture(_ fixture: [JSONDictionary]) -> [FixtureCarb
593582public struct FixtureCarbEntry : CarbEntry , SampleValue {
594583 public var absorptionTime : TimeInterval ?
595584 public var startDate : Date
596- public var quantity : LoopQuantity
585+ public var quantity : LoopQuantity // This will now correctly map to the library's type
597586 public var foodType : String ?
598587
599588 // Explicit initializer
0 commit comments