diff --git a/apps/swift-ios/Features/Root/FeatureRootModel.swift b/apps/swift-ios/Features/Root/FeatureRootModel.swift index 1e739a04af..26fbbac3ce 100644 --- a/apps/swift-ios/Features/Root/FeatureRootModel.swift +++ b/apps/swift-ios/Features/Root/FeatureRootModel.swift @@ -8,6 +8,7 @@ public final class FeatureRootModel { public private(set) var details: [String: FeatureThreadDetail] = [:] public private(set) var isLoading = true public private(set) var isPerformingAction = false + public private(set) var isManagingConnections = false public var errorMessage: String? let client: any FeatureClient @@ -66,6 +67,7 @@ public final class FeatureRootModel { } public func disconnect() async { + isManagingConnections = false await client.disconnect() snapshot = FeatureSnapshot( environments: snapshot.environments, @@ -74,6 +76,10 @@ public final class FeatureRootModel { details.removeAll() } + public func setConnectionManagementPresented(_ isPresented: Bool) { + isManagingConnections = isPresented + } + public func addProject(path: String) async -> Bool { await perform { try await client.addProject(path: path) diff --git a/apps/swift-ios/Features/Root/FeatureRootView.swift b/apps/swift-ios/Features/Root/FeatureRootView.swift index 8496c5dc6c..87e69607b4 100644 --- a/apps/swift-ios/Features/Root/FeatureRootView.swift +++ b/apps/swift-ios/Features/Root/FeatureRootView.swift @@ -45,11 +45,25 @@ public struct FeatureRootView: View { } /// Keep the last-known workspace visible through a degraded connection. - /// A deliberate disconnect clears workspace data and returns to onboarding. + /// Connection management also stays mounted while saved servers are being + /// removed, so a disconnected fallback cannot destroy its own Settings sheet. private var shouldShowWorkspace: Bool { - model.snapshot.connection.state != .disconnected - || !model.snapshot.projects.isEmpty - || !model.snapshot.threads.isEmpty + FeatureRootPresentation.showsWorkspace( + snapshot: model.snapshot, + isManagingConnections: model.isManagingConnections + ) + } +} + +enum FeatureRootPresentation { + static func showsWorkspace( + snapshot: FeatureSnapshot, + isManagingConnections: Bool + ) -> Bool { + snapshot.connection.state != .disconnected + || !snapshot.projects.isEmpty + || !snapshot.threads.isEmpty + || (isManagingConnections && !snapshot.environments.isEmpty) } } diff --git a/apps/swift-ios/Features/Settings/SettingsView.swift b/apps/swift-ios/Features/Settings/SettingsView.swift index ba13dc045e..f96894cd61 100644 --- a/apps/swift-ios/Features/Settings/SettingsView.swift +++ b/apps/swift-ios/Features/Settings/SettingsView.swift @@ -81,6 +81,12 @@ public struct SettingsView: View { } ) } + .onAppear { + model.setConnectionManagementPresented(true) + } + .onDisappear { + model.setConnectionManagementPresented(false) + } } } diff --git a/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift b/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift index d31d2fe423..34ee6f9f35 100644 --- a/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift @@ -5,6 +5,50 @@ import Testing @MainActor @Suite("Feature root model") struct FeatureRootModelTests { + @Test + func connectionManagementStaysMountedForDisconnectedSavedServers() { + let savedEnvironment = FeatureEnvironment( + id: "offline-demo", + name: "Offline demo", + endpoint: "https://offline.example", + connectionState: .disconnected + ) + let snapshot = FeatureSnapshot( + connection: .init(state: .disconnected), + environments: [savedEnvironment] + ) + + #expect( + FeatureRootPresentation.showsWorkspace( + snapshot: snapshot, + isManagingConnections: true + ) + ) + #expect( + !FeatureRootPresentation.showsWorkspace( + snapshot: snapshot, + isManagingConnections: false + ) + ) + #expect( + !FeatureRootPresentation.showsWorkspace( + snapshot: FeatureSnapshot(connection: .init(state: .disconnected)), + isManagingConnections: true + ) + ) + } + + @Test + func disconnectEndsConnectionManagement() async { + let client = FeatureClientStub() + let model = FeatureRootModel(client: client) + model.setConnectionManagementPresented(true) + + await model.disconnect() + + #expect(!model.isManagingConnections) + } + @Test func testPairReloadsConnectedSnapshot() async { let client = FeatureClientStub()