Skip to content

Commit 42aeeb4

Browse files
Refactor RetrospectiveCorrection Initialization (#2014)
* evaluate state of irc * rename variables --------- Co-authored-by: marionbarker <[email protected]>
1 parent ed1a00f commit 42aeeb4

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

Loop/Managers/LoopDataManager.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ final class LoopDataManager {
123123

124124
self.trustedTimeOffset = trustedTimeOffset
125125

126-
retrospectiveCorrection = settings.enabledRetrospectiveCorrectionAlgorithm
127-
128126
overrideIntentObserver = UserDefaults.appGroup?.observe(\.intentExtensionOverrideToSet, options: [.new], changeHandler: {[weak self] (defaults, change) in
129127
guard let name = change.newValue??.lowercased(), let appGroup = UserDefaults.appGroup else {
130128
return
@@ -435,7 +433,23 @@ final class LoopDataManager {
435433
}
436434

437435
// Confined to dataAccessQueue
438-
private var retrospectiveCorrection: RetrospectiveCorrection
436+
private var lastIntegralRetrospectiveCorrectionEnabled: Bool?
437+
private var cachedRetrospectiveCorrection: RetrospectiveCorrection?
438+
439+
var retrospectiveCorrection: RetrospectiveCorrection {
440+
let currentIntegralRetrospectiveCorrectionEnabled = UserDefaults.standard.integralRetrospectiveCorrectionEnabled
441+
442+
if lastIntegralRetrospectiveCorrectionEnabled != currentIntegralRetrospectiveCorrectionEnabled || cachedRetrospectiveCorrection == nil {
443+
lastIntegralRetrospectiveCorrectionEnabled = currentIntegralRetrospectiveCorrectionEnabled
444+
if currentIntegralRetrospectiveCorrectionEnabled {
445+
cachedRetrospectiveCorrection = IntegralRetrospectiveCorrection(effectDuration: LoopSettings.retrospectiveCorrectionEffectDuration)
446+
} else {
447+
cachedRetrospectiveCorrection = StandardRetrospectiveCorrection(effectDuration: LoopSettings.retrospectiveCorrectionEffectDuration)
448+
}
449+
}
450+
451+
return cachedRetrospectiveCorrection!
452+
}
439453

440454
// MARK: - Background task management
441455

Loop/Models/LoopSettings+Loop.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,4 @@ extension LoopSettings {
2020

2121
static let retrospectiveCorrectionEffectDuration = TimeInterval(hours: 1)
2222

23-
/// Creates an instance of the enabled retrospective correction implementation
24-
var enabledRetrospectiveCorrectionAlgorithm: RetrospectiveCorrection {
25-
var enabledRetrospectiveCorrectionAlgorithm: RetrospectiveCorrection
26-
27-
let isIntegralRetrospectiveCorrectionEnabled = UserDefaults.standard.integralRetrospectiveCorrectionEnabled
28-
29-
if isIntegralRetrospectiveCorrectionEnabled {
30-
enabledRetrospectiveCorrectionAlgorithm = IntegralRetrospectiveCorrection(effectDuration: LoopSettings.retrospectiveCorrectionEffectDuration)
31-
} else {
32-
enabledRetrospectiveCorrectionAlgorithm = StandardRetrospectiveCorrection(effectDuration: LoopSettings.retrospectiveCorrectionEffectDuration)
33-
}
34-
35-
return enabledRetrospectiveCorrectionAlgorithm
36-
}
37-
3823
}

0 commit comments

Comments
 (0)