feat(swift-ios): add unified connections hub

This commit is contained in:
Theo Browne committed 2026-08-10 15:46:34 -07:00
1 parent 6a0c3bc510
commit 9cc33eab68
2 files changed
+472 -349

No files matched your search

@@ -0,0 +1,452 @@
import SwiftUI
struct ConnectionsView: View {
@SwiftUI.Environment(\.dismiss) private var dismiss
@Bindable var model: FeatureRootModel
@State private var pendingEnabledValues: [String: Bool] = [:]
@State private var showingAddConnection = false
@State private var showingDevices = false
@State private var showingT3Connect = false
@State private var detailEnvironmentID: String?
@State private var removalTarget: FeatureEnvironment?
var body: some View {
VStack(spacing: 0) {
header
Divider().overlay(T3Colors.border)
ScrollView {
LazyVStack(alignment: .leading, spacing: 28) {
connectionList
accountSection
accessSection
}
.padding(.horizontal, 20)
.padding(.vertical, 20)
}
.scrollDismissesKeyboard(.interactively)
}
.background(T3Colors.background)
.toolbar(.hidden, for: .navigationBar)
.sheet(isPresented: $showingAddConnection) {
ConnectionOnboardingView(
model: model,
onConnected: {
showingAddConnection = false
Task { await model.reloadAfterConnection() }
},
onCancel: { showingAddConnection = false }
)
}
.sheet(isPresented: $showingDevices) {
NavigationStack {
DevicesView(manager: deviceManager)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Done") { showingDevices = false }
}
}
}
.presentationDragIndicator(.visible)
}
.sheet(isPresented: $showingT3Connect) {
if let capability = model.client as? any T3ConnectCapable {
NavigationStack {
T3ConnectView(
capability: capability,
onConnected: { await model.reloadAfterConnection() }
)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Done") { showingT3Connect = false }
}
}
}
.presentationDragIndicator(.visible)
}
}
.sheet(
isPresented: Binding(
get: { detailEnvironmentID != nil },
set: { if !$0 { detailEnvironmentID = nil } }
)
) {
if let id = detailEnvironmentID {
NavigationStack {
ConnectionDetailView(
model: model,
environmentID: id,
pendingEnabledValues: $pendingEnabledValues,
onRemove: {
detailEnvironmentID = nil
Task { await model.removeEnvironment(id) }
}
)
}
.presentationDragIndicator(.visible)
}
}
.alert(
"Remove connection?",
isPresented: Binding(
get: { removalTarget != nil },
set: { if !$0 { removalTarget = nil } }
),
presenting: removalTarget
) { environment in
Button("Remove", role: .destructive) {
Task {
await model.removeEnvironment(environment.id)
removalTarget = nil
}
}
Button("Cancel", role: .cancel) {}
} message: { environment in
Text("\(environment.name) will need a new pairing code to be added again.")
}
}
private var header: some View {
HStack(spacing: 12) {
Button {
dismiss()
} label: {
Label("Settings", systemImage: "chevron.left")
.labelStyle(.titleAndIcon)
}
.frame(width: 92, alignment: .leading)
Spacer(minLength: 0)
Text("Connections")
.font(T3Typography.navigationTitle)
.foregroundStyle(T3Colors.textPrimary)
Spacer(minLength: 0)
Button("Add") { showingAddConnection = true }
.frame(width: 92, alignment: .trailing)
.accessibilityIdentifier("connections-add-button")
}
.font(T3Typography.control)
.foregroundStyle(T3Colors.accent)
.padding(.horizontal, 20)
.frame(minHeight: 54)
}
private var connectionList: some View {
VStack(alignment: .leading, spacing: 8) {
Text(connectionSummary.uppercased())
.font(T3Typography.eyebrow)
.foregroundStyle(T3Colors.textSecondary)
if model.snapshot.environments.isEmpty {
Button {
showingAddConnection = true
} label: {
Label("Add your first connection", systemImage: "plus")
.font(T3Typography.threadBody)
.foregroundStyle(T3Colors.accent)
.frame(maxWidth: .infinity, alignment: .leading)
.frame(minHeight: 58)
}
.buttonStyle(.plain)
.overlay(alignment: .bottom) { Divider().overlay(T3Colors.separator) }
} else {
VStack(spacing: 0) {
Divider().overlay(T3Colors.separator)
ForEach(model.snapshot.environments) { environment in
connectionRow(environment)
Divider().overlay(T3Colors.separator)
}
}
}
Text("Disabled connections stay saved but stop network activity and disappear from new-task choices.")
.font(T3Typography.supporting)
.foregroundStyle(T3Colors.textTertiary)
.padding(.top, 2)
}
}
private func connectionRow(_ environment: FeatureEnvironment) -> some View {
HStack(spacing: 12) {
Button {
detailEnvironmentID = environment.id
} label: {
HStack(spacing: 12) {
Image(systemName: environment.source.systemImage)
.font(.system(size: 18, weight: .medium))
.foregroundStyle(environment.source == .t3Connect
? T3Colors.accent
: T3Colors.textSecondary)
.frame(width: 25)
VStack(alignment: .leading, spacing: 4) {
Text(environment.name)
.font(T3Typography.homeTitle)
.foregroundStyle(T3Colors.textPrimary)
.lineLimit(1)
HStack(spacing: 6) {
Circle()
.fill(environment.status.color)
.frame(width: 7, height: 7)
Text(environment.status.title)
Text("·")
Text(environment.source.title)
.foregroundStyle(environment.source == .t3Connect
? T3Colors.accent
: T3Colors.textSecondary)
}
.font(T3Typography.supporting)
.foregroundStyle(T3Colors.textSecondary)
}
Spacer(minLength: 8)
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
Toggle("Enabled", isOn: enabledBinding(for: environment))
.labelsHidden()
.tint(T3Colors.success)
.disabled(pendingEnabledValues[environment.id] != nil)
.accessibilityLabel("Enable \(environment.name)")
}
.frame(minHeight: 70)
.contextMenu {
Button(role: .destructive) {
removalTarget = environment
} label: {
Label("Remove connection", systemImage: "trash")
}
}
}
private var accountSection: some View {
VStack(alignment: .leading, spacing: 8) {
Text("T3 CONNECT ACCOUNT")
.font(T3Typography.eyebrow)
.foregroundStyle(T3Colors.textSecondary)
Button {
showingT3Connect = true
} label: {
HStack(spacing: 12) {
Image(systemName: "cloud")
.font(.system(size: 18, weight: .medium))
.foregroundStyle(T3Colors.accent)
.frame(width: 25)
VStack(alignment: .leading, spacing: 3) {
Text(t3ConnectAccountTitle)
.font(T3Typography.threadBody)
.foregroundStyle(T3Colors.textPrimary)
Text(t3ConnectAccountDetail)
.font(T3Typography.supporting)
.foregroundStyle(T3Colors.textSecondary)
}
Spacer(minLength: 8)
Image(systemName: "chevron.right")
.font(T3Typography.supportingStrong)
.foregroundStyle(T3Colors.textTertiary)
}
.frame(minHeight: 58)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.disabled(model.client is any T3ConnectCapable == false)
.overlay(alignment: .top) { Divider().overlay(T3Colors.separator) }
.overlay(alignment: .bottom) { Divider().overlay(T3Colors.separator) }
}
}
private var accessSection: some View {
VStack(alignment: .leading, spacing: 8) {
Text("ACCESS")
.font(T3Typography.eyebrow)
.foregroundStyle(T3Colors.textSecondary)
Button {
showingDevices = true
} label: {
HStack(spacing: 12) {
Image(systemName: "laptopcomputer.and.iphone")
.font(.system(size: 18, weight: .medium))
.foregroundStyle(T3Colors.accent)
.frame(width: 25)
Text("Devices and sessions")
.font(T3Typography.threadBody)
.foregroundStyle(T3Colors.textPrimary)
Spacer(minLength: 8)
Image(systemName: "chevron.right")
.font(T3Typography.supportingStrong)
.foregroundStyle(T3Colors.textTertiary)
}
.frame(minHeight: 54)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.overlay(alignment: .top) { Divider().overlay(T3Colors.separator) }
.overlay(alignment: .bottom) { Divider().overlay(T3Colors.separator) }
}
}
private func enabledBinding(for environment: FeatureEnvironment) -> Binding<Bool> {
Binding(
get: { pendingEnabledValues[environment.id] ?? environment.isEnabled },
set: { enabled in
pendingEnabledValues[environment.id] = enabled
Task {
_ = await model.setEnvironmentEnabled(environment.id, enabled: enabled)
pendingEnabledValues[environment.id] = nil
}
}
)
}
private var connectionSummary: String {
let environments = model.snapshot.environments
let online = environments.count {
$0.isEnabled && $0.connectionState == .connected
}
return "\(environments.count) saved · \(online) online"
}
private var t3ConnectController: T3ConnectController? {
(model.client as? any T3ConnectCapable)?.t3ConnectController
}
private var t3ConnectAccountTitle: String {
t3ConnectController?.account?.email ?? "T3 Connect"
}
private var t3ConnectAccountDetail: String {
guard let controller = t3ConnectController else { return "Unavailable in this build" }
guard controller.account != nil else { return "Sign in or manage cloud connections" }
let count = model.snapshot.environments.count { $0.source == .t3Connect }
return "Signed in · \(count) \(count == 1 ? "connection" : "connections")"
}
private var deviceManager: any FeatureDeviceManaging {
(model.client as? any FeatureDeviceManaging) ?? EmptyFeatureDeviceManager.shared
}
}
private struct ConnectionDetailView: View {
@SwiftUI.Environment(\.dismiss) private var dismiss
@Bindable var model: FeatureRootModel
let environmentID: String
@Binding var pendingEnabledValues: [String: Bool]
let onRemove: () -> Void
@State private var showingRemoval = false
var body: some View {
List {
if let environment {
Section {
Toggle("Enabled", isOn: enabledBinding(for: environment))
.tint(T3Colors.success)
.disabled(pendingEnabledValues[environment.id] != nil)
LabeledContent("Status", value: environment.status.title)
LabeledContent("Connection", value: environment.source.title)
}
Section("Server") {
Text(environment.endpoint)
.textSelection(.enabled)
LabeledContent("Projects", value: "\(projectCount)")
}
Section {
Button("Remove connection", role: .destructive) {
showingRemoval = true
}
}
} else {
ContentUnavailableView("Connection removed", systemImage: "network.slash")
}
}
.scrollContentBackground(.hidden)
.background(T3Colors.background)
.navigationTitle(environment?.name ?? "Connection")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") { dismiss() }
}
}
.confirmationDialog(
"Remove this connection?",
isPresented: $showingRemoval,
titleVisibility: .visible
) {
Button("Remove", role: .destructive, action: onRemove)
Button("Cancel", role: .cancel) {}
} message: {
Text("A new pairing code will be required to add it again.")
}
}
private var environment: FeatureEnvironment? {
model.snapshot.environments.first { $0.id == environmentID }
}
private var projectCount: Int {
model.snapshot.projects.count { $0.environmentID == environmentID }
}
private func enabledBinding(for environment: FeatureEnvironment) -> Binding<Bool> {
Binding(
get: { pendingEnabledValues[environment.id] ?? environment.isEnabled },
set: { enabled in
pendingEnabledValues[environment.id] = enabled
Task {
_ = await model.setEnvironmentEnabled(environment.id, enabled: enabled)
pendingEnabledValues[environment.id] = nil
}
}
)
}
}
private extension FeatureEnvironment.Source {
var title: String {
switch self {
case .direct: "Direct"
case .t3Connect: "T3 Connect"
}
}
var systemImage: String {
switch self {
case .direct: "desktopcomputer"
case .t3Connect: "cloud"
}
}
}
private extension FeatureEnvironment {
var status: ConnectionStatusPresentation {
guard isEnabled else {
return ConnectionStatusPresentation(title: "Off", color: T3Colors.textTertiary)
}
switch connectionState {
case .connected:
return ConnectionStatusPresentation(title: "Online", color: T3Colors.success)
case .connecting, .reconnecting:
return ConnectionStatusPresentation(title: "Connecting", color: T3Colors.warning)
case .disconnected:
return ConnectionStatusPresentation(title: "Unreachable", color: T3Colors.danger)
case nil:
return ConnectionStatusPresentation(title: "Checking", color: T3Colors.textTertiary)
}
}
}
private struct ConnectionStatusPresentation {
let title: String
let color: Color
}
@@ -5,11 +5,6 @@ public struct SettingsView: View {
@Bindable private var model: FeatureRootModel
@State private var settings: FeatureSettings
@State private var isSaving = false
@State private var showingDisconnect = false
@State private var showingAddEnvironment = false
@State private var showingDevices = false
@State private var showingT3Connect = false
@State private var removalTarget: FeatureEnvironment?
@State private var saveErrorMessage: String?
public init(model: FeatureRootModel) {
@@ -28,9 +23,7 @@ public struct SettingsView: View {
ScrollView {
LazyVStack(alignment: .leading, spacing: 28) {
connectionSection
t3ConnectSection
generalSection
agentSection
preferencesSection
aboutSection
}
@@ -40,39 +33,6 @@ public struct SettingsView: View {
}
.background(T3Colors.background)
.toolbar(.hidden, for: .navigationBar)
.confirmationDialog(
"Disconnect from this server?",
isPresented: $showingDisconnect,
titleVisibility: .visible
) {
Button("Disconnect", role: .destructive) {
Task {
await model.disconnect()
dismiss()
}
}
Button("Cancel", role: .cancel) {}
} message: {
Text("Your saved server and credentials will stay on this iPhone.")
}
.alert(
"Remove saved server?",
isPresented: Binding(
get: { removalTarget != nil },
set: { if !$0 { removalTarget = nil } }
),
presenting: removalTarget
) { environment in
Button("Remove", role: .destructive) {
Task {
await model.removeEnvironment(environment.id)
removalTarget = nil
}
}
Button("Cancel", role: .cancel) {}
} message: { environment in
Text("\(environment.name) will need a new pairing code to be added again.")
}
.alert(
"Couldn’t save settings",
isPresented: Binding(
@@ -84,41 +44,6 @@ public struct SettingsView: View {
} message: {
Text(saveErrorMessage ?? "Something went wrong.")
}
.sheet(isPresented: $showingAddEnvironment) {
ConnectionOnboardingView(
model: model,
onConnected: {
showingAddEnvironment = false
},
onCancel: {
showingAddEnvironment = false
}
)
}
.sheet(isPresented: $showingDevices) {
NavigationStack {
DevicesView(manager: deviceManager)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Done") { showingDevices = false }
}
}
}
.presentationDragIndicator(.visible)
}
.sheet(isPresented: $showingT3Connect) {
if let capability = model.client as? any T3ConnectCapable {
NavigationStack {
T3ConnectView(capability: capability)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Done") { showingT3Connect = false }
}
}
}
.presentationDragIndicator(.visible)
}
}
.onAppear {
model.setConnectionManagementPresented(true)
}
@@ -168,86 +93,16 @@ public struct SettingsView: View {
private var connectionSection: some View {
SettingsSection(title: "Connections") {
VStack(spacing: 0) {
if model.snapshot.environments.isEmpty {
connectionFallbackRow
} else {
ForEach(Array(model.snapshot.environments.enumerated()), id: \.element.id) {
index, environment in
if index > 0 {
settingsDivider
}
environmentRow(environment)
}
}
settingsDivider
Button {
showingDevices = true
} label: {
SettingsNavigationRow(
title: "Devices and sessions",
systemImage: "laptopcomputer.and.iphone"
)
}
.buttonStyle(.plain)
settingsDivider
Button {
showingAddEnvironment = true
} label: {
SettingsActionRow(
title: "Add server",
systemImage: "plus"
)
}
.buttonStyle(.plain)
settingsDivider
Button(role: .destructive) {
showingDisconnect = true
} label: {
SettingsActionRow(
title: "Disconnect current server",
systemImage: "rectangle.portrait.and.arrow.right",
color: T3Colors.danger
)
}
.buttonStyle(.plain)
}
}
}
private var agentSection: some View {
SettingsSection(title: "Default agent") {
VStack(spacing: 0) {
ProviderModelPicker(
providers: model.snapshot.providers,
selection: $settings.defaultSelection
NavigationLink {
ConnectionsView(model: model)
} label: {
SettingsNavigationRow(
title: "Connections",
value: connectionSummary,
systemImage: "server.rack"
)
.padding(.horizontal, 20)
.frame(minHeight: 58)
if let provider = selectedProvider {
settingsDivider
SettingsValueRow(title: "Provider", value: provider.name)
}
if let detail = selectedModel?.detail {
settingsDivider
Text(detail)
.font(T3Typography.supporting)
.foregroundStyle(T3Colors.textSecondary)
.padding(.horizontal, 20)
.padding(.vertical, 12)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.buttonStyle(.plain)
}
}
@@ -265,30 +120,6 @@ public struct SettingsView: View {
}
}
private var t3ConnectSection: some View {
SettingsSection(
title: "T3 Connect",
footer: "Optional account sync for relay-managed environments."
) {
if model.client is any T3ConnectCapable {
Button {
showingT3Connect = true
} label: {
SettingsNavigationRow(
title: "Cloud environments",
systemImage: "cloud"
)
}
.buttonStyle(.plain)
} else {
SettingsValueRow(
title: "Cloud environments",
value: "Unavailable",
systemImage: "cloud.slash"
)
}
}
}
private var preferencesSection: some View {
SettingsSection(title: "Preferences") {
@@ -351,100 +182,6 @@ public struct SettingsView: View {
}
}
private var connectionFallbackRow: some View {
HStack(spacing: 12) {
SettingsRowIcon(systemName: connectionSymbol, color: connectionColor)
VStack(alignment: .leading, spacing: 2) {
Text(model.snapshot.connection.environmentName ?? "T3 server")
.font(T3Typography.homeTitle)
.foregroundStyle(T3Colors.textPrimary)
Text(connectionDescription)
.font(T3Typography.supporting)
.foregroundStyle(T3Colors.textSecondary)
.lineLimit(1)
}
Spacer(minLength: 8)
Text(connectionStatus)
.font(T3Typography.supportingStrong)
.foregroundStyle(connectionColor)
}
.padding(.horizontal, 20)
.frame(minHeight: 58)
.accessibilityElement(children: .combine)
}
private func environmentRow(_ environment: FeatureEnvironment) -> some View {
Button {
guard !environment.isActive else { return }
Task { await model.activateEnvironment(environment.id) }
} label: {
HStack(spacing: 12) {
let status = environmentStatus(for: environment)
let activeIsConnected = environment.isActive
&& model.snapshot.connection.state == .connected
SettingsRowIcon(
systemName: activeIsConnected ? "checkmark.circle.fill" : "desktopcomputer",
color: activeIsConnected ? T3Colors.success : T3Colors.textTertiary
)
VStack(alignment: .leading, spacing: 2) {
Text(environment.name)
.font(T3Typography.homeTitle)
.foregroundStyle(T3Colors.textPrimary)
.lineLimit(1)
Text(environment.endpoint)
.font(T3Typography.supporting)
.foregroundStyle(T3Colors.textSecondary)
.lineLimit(1)
.truncationMode(.middle)
}
Spacer(minLength: 8)
Label(status.title, systemImage: status.symbol)
.labelStyle(SettingsStatusLabelStyle())
.font(T3Typography.supportingStrong)
.foregroundStyle(status.color)
}
.padding(.leading, 20)
.padding(.trailing, environment.isActive ? 20 : 56)
.frame(minHeight: 62)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityElement(children: .combine)
.accessibilityHint(environment.isActive ? "Current server" : "Switch to this server")
.overlay(alignment: .trailing) {
if !environment.isActive {
Menu {
Button(role: .destructive) {
removalTarget = environment
} label: {
Label("Remove saved server", systemImage: "trash")
}
} label: {
Image(systemName: "ellipsis")
.font(T3Typography.control)
.foregroundStyle(T3Colors.textTertiary)
.frame(width: T3Metrics.minimumTapTarget, height: 62)
.contentShape(Rectangle())
}
.padding(.trailing, 8)
.accessibilityLabel("Actions for \(environment.name)")
}
}
.contextMenu {
if !environment.isActive {
Button(role: .destructive) {
removalTarget = environment
} label: {
Label("Remove saved server", systemImage: "trash")
}
}
}
}
private var settingsDivider: some View {
Divider()
.overlay(T3Colors.separator)
@@ -461,79 +198,12 @@ public struct SettingsView: View {
!isSaving && settings != model.snapshot.settings
}
private var deviceManager: any FeatureDeviceManaging {
(model.client as? any FeatureDeviceManaging) ?? EmptyFeatureDeviceManager.shared
}
private var selectedProvider: FeatureProvider? {
guard let selection = settings.defaultSelection else { return nil }
return model.snapshot.providers.first { $0.id == selection.providerID }
}
private var selectedModel: FeatureModel? {
guard let selection = settings.defaultSelection else { return nil }
return selectedProvider?.models.first { $0.id == selection.modelID }
}
private var connectionStatus: String {
switch model.snapshot.connection.state {
case .connected: "Online"
case .connecting: "Connecting"
case .reconnecting: "Reconnecting"
case .disconnected: "Offline"
}
}
private var connectionSymbol: String {
model.snapshot.connection.state == .connected ? "checkmark.circle.fill" : "network.slash"
}
private var connectionColor: Color {
model.snapshot.connection.state == .connected ? .green : .secondary
}
private var connectionDescription: String {
model.snapshot.connection.endpoint ?? "No active server"
}
private func environmentStatus(
for environment: FeatureEnvironment
) -> EnvironmentStatusPresentation {
let state = environment.isActive
? model.snapshot.connection.state
: environment.connectionState
switch state {
case .connected where environment.isActive:
return EnvironmentStatusPresentation(
title: "Active",
symbol: "dot.radiowaves.left.and.right",
color: T3Colors.accent
)
case .connected:
return EnvironmentStatusPresentation(
title: "Ready",
symbol: "network",
color: T3Colors.textSecondary
)
case .connecting, .reconnecting:
return EnvironmentStatusPresentation(
title: "Checking",
symbol: "arrow.triangle.2.circlepath",
color: T3Colors.warning
)
case .disconnected:
return EnvironmentStatusPresentation(
title: "Offline",
symbol: "network.slash",
color: T3Colors.danger
)
case nil:
return EnvironmentStatusPresentation(
title: "Saved",
symbol: "bookmark",
color: T3Colors.textTertiary
)
private var connectionSummary: String {
let environments = model.snapshot.environments
let online = environments.count {
$0.isEnabled && $0.connectionState == .connected
}
return "\(online) online · \(environments.count) saved"
}
@MainActor
@@ -551,12 +221,6 @@ public struct SettingsView: View {
}
}
private struct EnvironmentStatusPresentation {
let title: String
let symbol: String
let color: Color
}
private struct SettingsSection<Content: View>: View {
let title: String
let footer: String?
@@ -607,6 +271,7 @@ private struct SettingsRowIcon: View {
private struct SettingsNavigationRow: View {
let title: String
var value: String? = nil
let systemImage: String
var trailingSystemImage = "chevron.right"
@@ -617,6 +282,12 @@ private struct SettingsNavigationRow: View {
.font(T3Typography.threadBody)
.foregroundStyle(T3Colors.textPrimary)
Spacer(minLength: 8)
if let value {
Text(value)
.font(T3Typography.supporting)
.foregroundStyle(T3Colors.textSecondary)
.lineLimit(1)
}
Image(systemName: trailingSystemImage)
.font(T3Typography.supportingStrong)
.foregroundStyle(T3Colors.textTertiary)