@@ -27,6 +27,8 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
2727
2828 private let updateSubject = PassthroughSubject < Void , Never > ( )
2929
30+ private let settingsProvider = PickerSettingsProvider . shared
31+
3032 var updatePublisher : AnyPublisher < Void , Never > {
3133 updateSubject. eraseToAnyPublisher ( )
3234 }
@@ -111,7 +113,7 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
111113
112114 - Returns: The computed duration in hours.
113115 */
114- private func calculateComputedDuration( fpus: Decimal , timeCap: Int ) -> Int {
116+ private func calculateComputedDuration( fpus: Decimal , timeCap: Decimal ) -> Decimal {
115117 switch fpus {
116118 case ..< 2 :
117119 return 3
@@ -145,22 +147,25 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
145147 createdAt: Date ,
146148 actualDate: Date ?
147149 ) -> ( [ CarbsEntry ] , Decimal ) {
148- let interval = settings. settings. minuteInterval
149- let timeCap = settings. settings. timeCap
150- let adjustment = settings. settings. individualAdjustmentFactor
151- let delay = settings. settings. delay
150+ let trioSettings = settings. settings
151+ let providerSettings = settingsProvider. settings
152+
153+ let interval = trioSettings. minuteInterval. clamp ( to: providerSettings. minuteInterval)
154+ let timeCap = trioSettings. timeCap. clamp ( to: providerSettings. timeCap)
155+ let adjustment = trioSettings. individualAdjustmentFactor. clamp ( to: providerSettings. individualAdjustmentFactor)
156+ let delay = trioSettings. delay. clamp ( to: providerSettings. delay)
152157
153158 let kcal = protein * 4 + fat * 9
154159 let carbEquivalents = ( kcal / 10 ) * adjustment
155160 let fpus = carbEquivalents / 10
156161 var computedDuration = calculateComputedDuration ( fpus: fpus, timeCap: timeCap)
157162
158- var carbEquivalentSize : Decimal = carbEquivalents / Decimal ( computedDuration)
159- carbEquivalentSize /= Decimal ( 60 / interval)
163+ var carbEquivalentSize : Decimal = carbEquivalents / computedDuration
164+ carbEquivalentSize /= Decimal ( 60 ) / interval
160165
161166 if carbEquivalentSize < 1.0 {
162167 carbEquivalentSize = 1.0
163- computedDuration = Int ( carbEquivalents / carbEquivalentSize)
168+ computedDuration = min ( carbEquivalents / carbEquivalentSize, timeCap )
164169 }
165170
166171 let roundedEquivalent : Double = round ( Double ( carbEquivalentSize * 10 ) ) / 10
@@ -172,9 +177,12 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
172177 var futureCarbArray = [ CarbsEntry] ( )
173178 var firstIndex = true
174179
180+ // convert Decimal minutes to TimeInterval in seconds
181+ let delayTimeInterval = TimeInterval ( delay * 60 )
182+ let intervalTimeInterval = TimeInterval ( interval * 60 )
175183 while carbEquivalents > 0 , numberOfEquivalents > 0 {
176- useDate = firstIndex ? useDate. addingTimeInterval ( delay . minutes . timeInterval ) : useDate
177- . addingTimeInterval ( interval . minutes . timeInterval )
184+ useDate = firstIndex ? useDate. addingTimeInterval ( delayTimeInterval ) : useDate
185+ . addingTimeInterval ( intervalTimeInterval )
178186 firstIndex = false
179187
180188 let eachCarbEntry = CarbsEntry (
0 commit comments