Bläddra i källkod

Merge pull request #495 from nightscout/plist-flag

Migrate Time-Sensitive Persistent Flags to Plist-Backed Storage
Sam King 1 år sedan
förälder
incheckning
1a0eae7a11

+ 4 - 0
Trio.xcodeproj/project.pbxproj

@@ -591,6 +591,7 @@
 		DD6F63CC2D27F615007D94CF /* TreatmentMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6F63CB2D27F606007D94CF /* TreatmentMenuView.swift */; };
 		DD73FA0F2D74F58E00D19D1E /* BackgroundTask+Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD73FA0E2D74F57300D19D1E /* BackgroundTask+Helper.swift */; };
 		DD8262CB2D289297009F6F62 /* BolusConfirmationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8262CA2D289297009F6F62 /* BolusConfirmationView.swift */; };
+		DD82D4B82DCAB2BA00BAFC77 /* PropertyPersistentFlags.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD82D4B72DCAB2BA00BAFC77 /* PropertyPersistentFlags.swift */; };
 		DD88C8E22C50420800F2D558 /* DefinitionRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD88C8E12C50420800F2D558 /* DefinitionRow.swift */; };
 		DD940BAA2CA7585D000830A5 /* GlucoseColorScheme.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD940BA92CA7585D000830A5 /* GlucoseColorScheme.swift */; };
 		DD940BAC2CA75889000830A5 /* DynamicGlucoseColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD940BAB2CA75889000830A5 /* DynamicGlucoseColor.swift */; };
@@ -1403,6 +1404,7 @@
 		DD6F63CB2D27F606007D94CF /* TreatmentMenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TreatmentMenuView.swift; sourceTree = "<group>"; };
 		DD73FA0E2D74F57300D19D1E /* BackgroundTask+Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BackgroundTask+Helper.swift"; sourceTree = "<group>"; };
 		DD8262CA2D289297009F6F62 /* BolusConfirmationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BolusConfirmationView.swift; sourceTree = "<group>"; };
+		DD82D4B72DCAB2BA00BAFC77 /* PropertyPersistentFlags.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PropertyPersistentFlags.swift; sourceTree = "<group>"; };
 		DD88C8E12C50420800F2D558 /* DefinitionRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefinitionRow.swift; sourceTree = "<group>"; };
 		DD940BA92CA7585D000830A5 /* GlucoseColorScheme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlucoseColorScheme.swift; sourceTree = "<group>"; };
 		DD940BAB2CA75889000830A5 /* DynamicGlucoseColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicGlucoseColor.swift; sourceTree = "<group>"; };
@@ -2382,6 +2384,7 @@
 		388E5A5A25B6F05F0019842D /* Helpers */ = {
 			isa = PBXGroup;
 			children = (
+				DD82D4B72DCAB2BA00BAFC77 /* PropertyPersistentFlags.swift */,
 				DDCAE8322D78D49C00B1BB51 /* TherapySettingsUtil.swift */,
 				BD249DA62D42FE3800412DEB /* Calendar+GlucoseStatsChart.swift */,
 				DD73FA0E2D74F57300D19D1E /* BackgroundTask+Helper.swift */,
@@ -4160,6 +4163,7 @@
 				E013D872273AC6FE0014109C /* GlucoseSimulatorSource.swift in Sources */,
 				BD249D862D42FBEC00412DEB /* GlucoseMetricsView.swift in Sources */,
 				58645BA32CA2D325008AFCE7 /* BatterySetup.swift in Sources */,
+				DD82D4B82DCAB2BA00BAFC77 /* PropertyPersistentFlags.swift in Sources */,
 				388E5A5C25B6F0770019842D /* JSON.swift in Sources */,
 				3811DF0225CA9FEA00A708ED /* Credentials.swift in Sources */,
 				5837A5302BD2E3C700A5DC04 /* CarbEntryStored+helper.swift in Sources */,

+ 2 - 2
Trio/Sources/Application/TrioApp.swift

@@ -366,7 +366,7 @@ extension Notification.Name {
     }
 
     private func performCleanupIfNecessary() {
-        if let lastCleanupDate = UserDefaults.standard.object(forKey: "lastCleanupDate") as? Date {
+        if let lastCleanupDate = PropertyPersistentFlags.shared.lastCleanupDate {
             let sevenDaysAgo = Date().addingTimeInterval(-7 * 24 * 60 * 60)
             if lastCleanupDate < sevenDaysAgo {
                 cleanupOldData()
@@ -383,7 +383,7 @@ extension Notification.Name {
             try await purgeData
 
             // Update the last cleanup date
-            UserDefaults.standard.set(Date(), forKey: "lastCleanupDate")
+            PropertyPersistentFlags.shared.lastCleanupDate = Date()
         }
     }
 

+ 26 - 0
Trio/Sources/Helpers/PropertyPersistentFlags.swift

@@ -0,0 +1,26 @@
+//
+//  PropertyPersistentFlags.swift
+//  Trio
+//
+//  Created by Cengiz Deniz on 06.05.25.
+//
+import Foundation
+
+/// Centralized store for app-wide persistent flags backed by property list (.plist) files.
+///
+/// This class uses the `@PersistedProperty` wrapper to store simple state flags such as
+/// onboarding completion, diagnostics sharing preference, and the last cleanup timestamp.
+///
+/// All values are persisted independently in the app’s documents directory as `.plist` files,
+/// and survive app restarts and reinstallations (unless the sandbox is cleared).
+///
+/// Accessed as a singleton via `PropertyPersistentFlags.shared`.
+final class PropertyPersistentFlags {
+    static let shared = PropertyPersistentFlags()
+
+    @PersistedProperty(key: "onboardingCompleted")  var onboardingCompleted: Bool?
+
+    @PersistedProperty(key: "diagnosticsSharing")  var diagnosticsSharingEnabled: Bool?
+
+    @PersistedProperty(key: "lastCleanupDate")  var lastCleanupDate: Date?
+}

+ 7 - 3
Trio/Sources/Modules/Onboarding/OnboardingStateModel.swift

@@ -25,7 +25,11 @@ extension Onboarding {
 
         // MARK: - App Diagnostics
 
-        var diagnosticsSharingOption: DiagnosticsSharingOption = .enabled
+        var diagnosticsSharingOption: DiagnosticsSharingOption {
+            get { (PropertyPersistentFlags.shared.diagnosticsSharingEnabled ?? true) ? .enabled : .disabled }
+            set { PropertyPersistentFlags.shared.diagnosticsSharingEnabled = (newValue == .enabled) }
+        }
+
         var hasAcceptedPrivacyPolicy: Bool = false
 
         // MARK: - Determine Initial Build State
@@ -679,8 +683,8 @@ extension Onboarding {
 
         /// Persists the current diagnostics sharing option to UserDefaults as a boolean.
         func applyDiagnostics() {
-            let booleanValue: Bool = diagnosticsSharingOption == .enabled
-            UserDefaults.standard.set(booleanValue, forKey: "DiagnosticsSharing")
+            let booleanValue = diagnosticsSharingOption == .enabled
+            PropertyPersistentFlags.shared.diagnosticsSharingEnabled = booleanValue
             Crashlytics.crashlytics().setCrashlyticsCollectionEnabled(booleanValue)
         }
 

+ 3 - 6
Trio/Sources/Services/OnboardingManager/OnboardingManager.swift

@@ -18,21 +18,18 @@ import Swinject
 
     /// Checks if onboarding has been completed and updates the shouldShowOnboarding flag accordingly.
     private func checkOnboardingStatus() {
-        shouldShowOnboarding = !UserDefaults.standard.onboardingCompleted
-
-        // Only for Debugging purposes
-//        shouldShowOnboarding = true
+        shouldShowOnboarding = !(PropertyPersistentFlags.shared.onboardingCompleted ?? false)
     }
 
     /// Marks onboarding as completed and updates the shouldShowOnboarding flag.
     func completeOnboarding() {
-        UserDefaults.standard.onboardingCompleted = true
+        PropertyPersistentFlags.shared.onboardingCompleted = true
         shouldShowOnboarding = false
     }
 
     /// Resets the onboarding status for testing purposes.
     func resetOnboarding() {
-        UserDefaults.standard.onboardingCompleted = false
+        PropertyPersistentFlags.shared.onboardingCompleted = false
         shouldShowOnboarding = true
     }
 }