Просмотр исходного кода

Fix pump model pre-selection if returning user; skip returning step if new user

Deniz Cengiz 1 год назад
Родитель
Сommit
b1cfa61578

+ 58 - 11
Trio/Sources/Modules/Onboarding/OnboardingStateModel.swift

@@ -28,6 +28,47 @@ extension Onboarding {
         var diagnosticsSharingOption: DiagnosticsSharingOption = .enabled
         var hasAcceptedPrivacyPolicy: Bool = false
 
+        // MARK: - Determine Initial Build State
+
+        var isFreshTrioInstall: Bool {
+            let fileManager = FileManager.default
+            guard let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
+                return false
+            }
+
+            debug(.default, "Checking for fresh install in \(documentsURL.path)...")
+
+            let expectedLogsFolder = "logs"
+            let expectedPreferencesFile = OpenAPS.Settings.preferences
+
+            do {
+                let contents = try fileManager.contentsOfDirectory(atPath: documentsURL.path)
+
+                debug(.default, "Found \(contents) in \(documentsURL.path)...")
+
+                // Expect exactly 2 entries: "logs" and the preferences file
+                guard contents.count == 2 else {
+                    debug(.default, "Trio install is not fresh; returning user.")
+                    return false
+                }
+
+                // Ensure they match exactly
+                let expectedSet = Set([expectedLogsFolder, expectedPreferencesFile])
+                let actualSet = Set(contents)
+
+                debug(.default, "Expected: \(expectedSet), Actual: \(actualSet)")
+
+                let isFreshInstall = expectedSet == actualSet
+                debug(.default, "Trio install is fresh; new user.")
+
+                return isFreshInstall
+
+            } catch {
+                debug(.default, "Cannot determine Initial Build State. Failed to read documents directory: \(error)")
+                return false
+            }
+        }
+
         // MARK: - Nightscout Setup
 
         var nightscoutSetupOption: NightscoutSetupOption = .noSelection
@@ -47,29 +88,35 @@ extension Onboarding {
         private var selectedPumpOption: PumpOptionForOnboardingUnits?
         var pumpOptionForOnboardingUnits: PumpOptionForOnboardingUnits {
             get {
-                // If the user has made a selection, use that
-                if let userSelected = _selectedPumpOption {
-                    return userSelected
+                // let user edit selection and return user-selection, if present
+                if let selected = selectedPumpOption {
+                    return selected
                 }
 
-                // Otherwise, reflect current pumpManager type
+                let defaultOption: PumpOptionForOnboardingUnits
                 if let pumpManager = apsManager?.pumpManager {
                     if pumpManager is OmniBLEPumpManager {
-                        return .omnipodDash
+                        defaultOption = .omnipodDash
                     } else if pumpManager is OmnipodPumpManager {
-                        return .omnipodEros
+                        defaultOption = .omnipodEros
                     } else if pumpManager is DanaKitPumpManager {
-                        return .dana
+                        defaultOption = .dana
                     } else if pumpManager is MinimedPumpManager {
-                        return .minimed
+                        defaultOption = .minimed
+                    } else {
+                        defaultOption = .omnipodDash
                     }
+                } else {
+                    defaultOption = .omnipodDash
                 }
 
-                // Default fallback
-                return .omnipodDash
+                // cache it so picker can stay in sync
+                selectedPumpOption = defaultOption
+
+                return defaultOption
             }
             set {
-                _selectedPumpOption = newValue
+                selectedPumpOption = newValue
             }
         }
 

+ 22 - 2
Trio/Sources/Modules/Onboarding/View/OnboardingRootView.swift

@@ -1,3 +1,4 @@
+import Foundation
 import SwiftUI
 import Swinject
 
@@ -152,6 +153,7 @@ extension Onboarding {
                             currentSMBSubstep: $currentSMBSubstep,
                             currentTargetBehaviorSubstep: $currentTargetBehaviorSubstep,
                             onboardingManager: onboardingManager,
+                            isFreshTrioInstall: state.isFreshTrioInstall,
                             state: state,
                             shouldDisableNextButton: shouldDisableNextButton,
                             navigationDirectionChanged: { navigationDirection = $0 }
@@ -454,6 +456,7 @@ struct OnboardingNavigationButtons: View {
     @Binding var currentTargetBehaviorSubstep: TargetBehaviorSubstep
 
     let onboardingManager: OnboardingManager
+    let isFreshTrioInstall: Bool
     @Bindable var state: Onboarding.StateModel
     var shouldDisableNextButton: Bool
     var navigationDirectionChanged: (OnboardingNavigationDirection) -> Void
@@ -508,7 +511,14 @@ struct OnboardingNavigationButtons: View {
 
         switch currentStep {
         case .startupInfo:
-            if let previousSub = StartupSubstep(rawValue: currentStartupSubstep.rawValue - 1) {
+            var previous = StartupSubstep(rawValue: currentStartupSubstep.rawValue - 1)
+
+            /// Skip `.returningUser` if this is a fresh install
+            if previous == .returningUser, isFreshTrioInstall == true {
+                previous = StartupSubstep(rawValue: previous!.rawValue - 1)
+            }
+
+            if let previousSub = previous {
                 currentStartupSubstep = previousSub
             } else if let previous = currentStep.previous {
                 currentStep = previous
@@ -631,7 +641,17 @@ struct OnboardingNavigationButtons: View {
 
         switch currentStep {
         case .startupInfo:
-            if let next = StartupSubstep(rawValue: currentStartupSubstep.rawValue + 1) {
+            let nextSubstepRaw = currentStartupSubstep.rawValue + 1
+
+            if isFreshTrioInstall, StartupSubstep(rawValue: nextSubstepRaw) == .returningUser {
+                /// Skip `.returningUser` if it's a fresh install
+                if let nextAfterSkip = StartupSubstep(rawValue: nextSubstepRaw + 1) {
+                    currentStartupSubstep = nextAfterSkip
+                } else if let nextStep = currentStep.next {
+                    currentStep = nextStep
+                    currentStartupSubstep = .startupGuide
+                }
+            } else if let next = StartupSubstep(rawValue: nextSubstepRaw) {
                 currentStartupSubstep = next
             } else if let nextStep = currentStep.next {
                 currentStep = nextStep