From 2d03f51208f52a8b5079210670a35522353bed10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Tue, 27 Jun 2023 19:59:58 +0200 Subject: [PATCH 1/2] evaluate state of irc --- Loop/Managers/LoopDataManager.swift | 20 +++++++++++++++++--- Loop/Models/LoopSettings+Loop.swift | 15 --------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Loop/Managers/LoopDataManager.swift b/Loop/Managers/LoopDataManager.swift index e4d6dadeb5..485847dc40 100644 --- a/Loop/Managers/LoopDataManager.swift +++ b/Loop/Managers/LoopDataManager.swift @@ -123,8 +123,6 @@ final class LoopDataManager { self.trustedTimeOffset = trustedTimeOffset - retrospectiveCorrection = settings.enabledRetrospectiveCorrectionAlgorithm - overrideIntentObserver = UserDefaults.appGroup?.observe(\.intentExtensionOverrideToSet, options: [.new], changeHandler: {[weak self] (defaults, change) in guard let name = change.newValue??.lowercased(), let appGroup = UserDefaults.appGroup else { return @@ -435,7 +433,23 @@ final class LoopDataManager { } // Confined to dataAccessQueue - private var retrospectiveCorrection: RetrospectiveCorrection + private var lastRetrospectiveCorrectionEnabled: Bool? + private var cachedRetrospectiveCorrection: RetrospectiveCorrection? + + var retrospectiveCorrection: RetrospectiveCorrection { + let currentRetrospectiveCorrectionEnabled = UserDefaults.standard.integralRetrospectiveCorrectionEnabled + + if lastRetrospectiveCorrectionEnabled != currentRetrospectiveCorrectionEnabled || cachedRetrospectiveCorrection == nil { + lastRetrospectiveCorrectionEnabled = currentRetrospectiveCorrectionEnabled + if currentRetrospectiveCorrectionEnabled { + cachedRetrospectiveCorrection = IntegralRetrospectiveCorrection(effectDuration: LoopSettings.retrospectiveCorrectionEffectDuration) + } else { + cachedRetrospectiveCorrection = StandardRetrospectiveCorrection(effectDuration: LoopSettings.retrospectiveCorrectionEffectDuration) + } + } + + return cachedRetrospectiveCorrection! + } // MARK: - Background task management diff --git a/Loop/Models/LoopSettings+Loop.swift b/Loop/Models/LoopSettings+Loop.swift index ff6f4d2f74..fd35b6416b 100644 --- a/Loop/Models/LoopSettings+Loop.swift +++ b/Loop/Models/LoopSettings+Loop.swift @@ -20,19 +20,4 @@ extension LoopSettings { static let retrospectiveCorrectionEffectDuration = TimeInterval(hours: 1) - /// Creates an instance of the enabled retrospective correction implementation - var enabledRetrospectiveCorrectionAlgorithm: RetrospectiveCorrection { - var enabledRetrospectiveCorrectionAlgorithm: RetrospectiveCorrection - - let isIntegralRetrospectiveCorrectionEnabled = UserDefaults.standard.integralRetrospectiveCorrectionEnabled - - if isIntegralRetrospectiveCorrectionEnabled { - enabledRetrospectiveCorrectionAlgorithm = IntegralRetrospectiveCorrection(effectDuration: LoopSettings.retrospectiveCorrectionEffectDuration) - } else { - enabledRetrospectiveCorrectionAlgorithm = StandardRetrospectiveCorrection(effectDuration: LoopSettings.retrospectiveCorrectionEffectDuration) - } - - return enabledRetrospectiveCorrectionAlgorithm - } - } From 65ec7003827c129eac2fd03ebdb561c6a3554f54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Tue, 27 Jun 2023 20:25:58 +0200 Subject: [PATCH 2/2] rename variables --- Loop/Managers/LoopDataManager.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Loop/Managers/LoopDataManager.swift b/Loop/Managers/LoopDataManager.swift index 485847dc40..d1c66f134c 100644 --- a/Loop/Managers/LoopDataManager.swift +++ b/Loop/Managers/LoopDataManager.swift @@ -433,15 +433,15 @@ final class LoopDataManager { } // Confined to dataAccessQueue - private var lastRetrospectiveCorrectionEnabled: Bool? + private var lastIntegralRetrospectiveCorrectionEnabled: Bool? private var cachedRetrospectiveCorrection: RetrospectiveCorrection? - + var retrospectiveCorrection: RetrospectiveCorrection { - let currentRetrospectiveCorrectionEnabled = UserDefaults.standard.integralRetrospectiveCorrectionEnabled + let currentIntegralRetrospectiveCorrectionEnabled = UserDefaults.standard.integralRetrospectiveCorrectionEnabled - if lastRetrospectiveCorrectionEnabled != currentRetrospectiveCorrectionEnabled || cachedRetrospectiveCorrection == nil { - lastRetrospectiveCorrectionEnabled = currentRetrospectiveCorrectionEnabled - if currentRetrospectiveCorrectionEnabled { + if lastIntegralRetrospectiveCorrectionEnabled != currentIntegralRetrospectiveCorrectionEnabled || cachedRetrospectiveCorrection == nil { + lastIntegralRetrospectiveCorrectionEnabled = currentIntegralRetrospectiveCorrectionEnabled + if currentIntegralRetrospectiveCorrectionEnabled { cachedRetrospectiveCorrection = IntegralRetrospectiveCorrection(effectDuration: LoopSettings.retrospectiveCorrectionEffectDuration) } else { cachedRetrospectiveCorrection = StandardRetrospectiveCorrection(effectDuration: LoopSettings.retrospectiveCorrectionEffectDuration)