Skip to content

Commit

Permalink
Add settings store logging, ensure storeSettings called from main queue.
Browse files Browse the repository at this point in the history
  • Loading branch information
ps2 committed Jul 29, 2022
1 parent 01e8aa3 commit 2005769
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
25 changes: 6 additions & 19 deletions Loop.xcodeproj/xcshareddata/xcschemes/WatchApp.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,16 @@
debugServiceExtension = "internal"
allowLocationSimulation = "YES"
notificationPayloadFile = "WatchApp Extension/PushNotificationPayload.apns">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/$(MAIN_APP_DISPLAY_NAME)">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "43A943711B926B7B0051FA24"
BuildableName = "WatchApp.app"
BlueprintName = "WatchApp"
ReferencedContainer = "container:Loop.xcodeproj">
</BuildableReference>
</RemoteRunnable>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand All @@ -108,27 +106,16 @@
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES"
notificationPayloadFile = "WatchApp Extension/PushNotificationPayload.apns">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/$(MAIN_APP_DISPLAY_NAME)">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "43A943711B926B7B0051FA24"
BuildableName = "WatchApp.app"
BlueprintName = "WatchApp"
ReferencedContainer = "container:Loop.xcodeproj">
</BuildableReference>
</RemoteRunnable>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "43A943711B926B7B0051FA24"
BuildableName = "WatchApp.app"
BlueprintName = "WatchApp"
ReferencedContainer = "container:Loop.xcodeproj">
</BuildableReference>
</MacroExpansion>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
Expand Down
27 changes: 19 additions & 8 deletions Loop/Managers/SettingsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import HealthKit
import Combine
import LoopCore
import LoopKitUI
import os.log


protocol DeviceStatusProvider {
var pumpManagerStatus: PumpManagerStatus? { get }
Expand All @@ -37,6 +39,7 @@ class SettingsManager {

private var cancellables: Set<AnyCancellable> = []

private let log = OSLog(category: "SettingsManager")

init(cacheStore: PersistenceController, expireAfter: TimeInterval)
{
Expand All @@ -45,13 +48,15 @@ class SettingsManager {
if let storedSettings = settingsStore.latestSettings {
latestSettings = storedSettings
} else {
log.default("SettingsStore has no latestSettings: initializing empty StoredSettings.")
latestSettings = StoredSettings()
}

settingsStore.delegate = self

// Migrate old settings from UserDefaults
if var legacyLoopSettings = UserDefaults.appGroup?.legacyLoopSettings {
log.default("Migrating settings from UserDefaults")
legacyLoopSettings.insulinSensitivitySchedule = UserDefaults.appGroup?.legacyInsulinSensitivitySchedule
legacyLoopSettings.basalRateSchedule = UserDefaults.appGroup?.legacyBasalRateSchedule
legacyLoopSettings.carbRatioSchedule = UserDefaults.appGroup?.legacyCarbRatioSchedule
Expand Down Expand Up @@ -95,7 +100,7 @@ class SettingsManager {
}
}

func mergeSettings(newLoopSettings: LoopSettings? = nil, notificationSettings: NotificationSettings? = nil) -> StoredSettings
private func mergeSettings(newLoopSettings: LoopSettings? = nil, notificationSettings: NotificationSettings? = nil) -> StoredSettings
{

#if targetEnvironment(simulator)
Expand Down Expand Up @@ -142,6 +147,10 @@ class SettingsManager {

latestSettings = mergedSettings

if latestSettings.insulinSensitivitySchedule == nil {
log.default("Saving settings with no ISF schedule.")
}

#if !targetEnvironment(simulator)
// Only store settings once we have a device token
guard deviceToken != nil else {
Expand All @@ -153,15 +162,17 @@ class SettingsManager {

func storeSettingsCheckingNotificationPermissions() {
UNUserNotificationCenter.current().getNotificationSettings() { notificationSettings in
guard let latestSettings = self.settingsStore.latestSettings else {
return
}
DispatchQueue.main.async {
guard let latestSettings = self.settingsStore.latestSettings else {
return
}

let notificationSettings = NotificationSettings(notificationSettings)
let notificationSettings = NotificationSettings(notificationSettings)

if notificationSettings != latestSettings.notificationSettings
{
self.storeSettings(notificationSettings: notificationSettings)
if notificationSettings != latestSettings.notificationSettings
{
self.storeSettings(notificationSettings: notificationSettings)
}
}
}
}
Expand Down

0 comments on commit 2005769

Please sign in to comment.