feat(swift-ios): unify environment management
This commit is contained in:
7 files changed
+452
-142
No files matched your search
@@ -335,8 +335,9 @@ public final class T3ConnectController: T3ConnectDeviceManaging {
|
||||
return credential
|
||||
}
|
||||
|
||||
public func unlink(_ environment: T3ConnectRelayEnvironment) async {
|
||||
guard let auth, let relay else { return }
|
||||
@discardableResult
|
||||
public func unlink(_ environment: T3ConnectRelayEnvironment) async -> Bool {
|
||||
guard let auth, let relay else { return false }
|
||||
busyEnvironmentID = environment.environmentId
|
||||
defer { busyEnvironmentID = nil }
|
||||
do {
|
||||
@@ -348,10 +349,12 @@ public final class T3ConnectController: T3ConnectDeviceManaging {
|
||||
environmentID: environment.environmentId,
|
||||
clerkToken: token
|
||||
)
|
||||
guard isAuthorizationCurrent(generation) else { return }
|
||||
guard isAuthorizationCurrent(generation) else { return false }
|
||||
environments.removeAll { $0.id == environment.environmentId }
|
||||
return true
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ public struct ConnectionOnboardingView: View {
|
||||
private let readinessChecker: any ConnectionReadinessChecking
|
||||
private let onConnected: @MainActor () -> Void
|
||||
private let onCancel: (@MainActor () -> Void)?
|
||||
private let showsT3ConnectOption: Bool
|
||||
|
||||
@State private var stage = ConnectionStage.welcome
|
||||
@State private var endpoint = ""
|
||||
@@ -22,11 +23,13 @@ public struct ConnectionOnboardingView: View {
|
||||
|
||||
public init(
|
||||
model: FeatureRootModel,
|
||||
showsT3ConnectOption: Bool = true,
|
||||
onConnected: @escaping @MainActor () -> Void = {},
|
||||
onCancel: (@MainActor () -> Void)? = nil
|
||||
) {
|
||||
self.model = model
|
||||
readinessChecker = LocalNetworkAccessChecker()
|
||||
self.showsT3ConnectOption = showsT3ConnectOption
|
||||
self.onConnected = onConnected
|
||||
self.onCancel = onCancel
|
||||
}
|
||||
@@ -34,11 +37,13 @@ public struct ConnectionOnboardingView: View {
|
||||
init(
|
||||
model: FeatureRootModel,
|
||||
readinessChecker: any ConnectionReadinessChecking,
|
||||
showsT3ConnectOption: Bool = true,
|
||||
onConnected: @escaping @MainActor () -> Void = {},
|
||||
onCancel: (@MainActor () -> Void)? = nil
|
||||
) {
|
||||
self.model = model
|
||||
self.readinessChecker = readinessChecker
|
||||
self.showsT3ConnectOption = showsT3ConnectOption
|
||||
self.onConnected = onConnected
|
||||
self.onCancel = onCancel
|
||||
}
|
||||
@@ -130,7 +135,8 @@ public struct ConnectionOnboardingView: View {
|
||||
.foregroundStyle(T3Colors.textSecondary)
|
||||
.padding(.top, 12)
|
||||
|
||||
if let capability = model.client as? any T3ConnectCapable,
|
||||
if showsT3ConnectOption,
|
||||
let capability = model.client as? any T3ConnectCapable,
|
||||
capability.t3ConnectController.unavailableReason == nil {
|
||||
NavigationLink {
|
||||
T3ConnectView(capability: capability) {
|
||||
@@ -191,7 +197,7 @@ public struct ConnectionOnboardingView: View {
|
||||
|
||||
@ViewBuilder
|
||||
private var knownEnvironments: some View {
|
||||
if !model.snapshot.environments.isEmpty {
|
||||
if !knownEnvironmentValues.isEmpty {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
Text("KNOWN SERVERS")
|
||||
.font(T3Typography.eyebrow)
|
||||
@@ -199,7 +205,7 @@ public struct ConnectionOnboardingView: View {
|
||||
.padding(.top, 34)
|
||||
.padding(.bottom, 8)
|
||||
|
||||
ForEach(model.snapshot.environments) { environment in
|
||||
ForEach(knownEnvironmentValues) { environment in
|
||||
Button {
|
||||
connect(
|
||||
.activate(
|
||||
@@ -233,7 +239,7 @@ public struct ConnectionOnboardingView: View {
|
||||
.buttonStyle(.plain)
|
||||
.accessibilityHint("Reconnects to this server")
|
||||
|
||||
if environment.id != model.snapshot.environments.last?.id {
|
||||
if environment.id != knownEnvironmentValues.last?.id {
|
||||
Divider().overlay(T3Colors.border)
|
||||
}
|
||||
}
|
||||
@@ -241,6 +247,11 @@ public struct ConnectionOnboardingView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private var knownEnvironmentValues: [FeatureEnvironment] {
|
||||
guard !showsT3ConnectOption else { return model.snapshot.environments }
|
||||
return model.snapshot.environments.filter { $0.source == .direct }
|
||||
}
|
||||
|
||||
private var detailsView: some View {
|
||||
ScrollView {
|
||||
VStack(alignment: .leading, spacing: 24) {
|
||||
|
||||
@@ -3,6 +3,11 @@ import ClerkKitUI
|
||||
import SwiftUI
|
||||
|
||||
public struct T3ConnectView: View {
|
||||
public enum Purpose: Sendable {
|
||||
case connect
|
||||
case manage
|
||||
}
|
||||
|
||||
@SwiftUI.Environment(\.dismiss) private var dismiss
|
||||
@Bindable private var controller: T3ConnectController
|
||||
@State private var isAuthPresented = false
|
||||
@@ -13,15 +18,21 @@ public struct T3ConnectView: View {
|
||||
@MainActor (T3ConnectManagedEnvironmentCredential) async throws -> Void
|
||||
private let signOut: @MainActor () async -> Void
|
||||
private let onConnected: @MainActor () async -> Void
|
||||
private let onUnlinked: @MainActor (String) async -> Void
|
||||
private let purpose: Purpose
|
||||
|
||||
public init(
|
||||
capability: any T3ConnectCapable,
|
||||
onConnected: @escaping @MainActor () async -> Void = {}
|
||||
purpose: Purpose = .connect,
|
||||
onConnected: @escaping @MainActor () async -> Void = {},
|
||||
onUnlinked: @escaping @MainActor (String) async -> Void = { _ in }
|
||||
) {
|
||||
controller = capability.t3ConnectController
|
||||
connectEnvironment = capability.connectT3Environment
|
||||
signOut = capability.signOutT3Connect
|
||||
self.purpose = purpose
|
||||
self.onConnected = onConnected
|
||||
self.onUnlinked = onUnlinked
|
||||
}
|
||||
|
||||
public var body: some View {
|
||||
@@ -183,10 +194,10 @@ public struct T3ConnectView: View {
|
||||
}
|
||||
|
||||
private var environmentSection: some View {
|
||||
Section("Cloud environments") {
|
||||
Section(purpose == .manage ? "Linked machines" : "Cloud environments") {
|
||||
if controller.environments.isEmpty, !controller.isRefreshing {
|
||||
VStack(alignment: .leading, spacing: 6) {
|
||||
Text("No linked environments")
|
||||
Text("No linked machines")
|
||||
.font(T3Typography.homeTitle)
|
||||
Text("Link an environment from T3 Code on desktop, then pull to refresh.")
|
||||
.font(T3Typography.supporting)
|
||||
@@ -201,7 +212,11 @@ public struct T3ConnectView: View {
|
||||
.listRowBackground(T3Colors.background)
|
||||
.swipeActions {
|
||||
Button(role: .destructive) {
|
||||
Task { await controller.unlink(item.environment) }
|
||||
Task {
|
||||
if await controller.unlink(item.environment) {
|
||||
await onUnlinked(item.id)
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Label("Unlink", systemImage: "link.badge.minus")
|
||||
}
|
||||
@@ -240,24 +255,26 @@ public struct T3ConnectView: View {
|
||||
|
||||
Spacer(minLength: 8)
|
||||
|
||||
Button {
|
||||
Task { await handleConnect(item.environment) }
|
||||
} label: {
|
||||
if controller.busyEnvironmentID == item.id
|
||||
|| connectingEnvironmentID == item.id {
|
||||
ProgressView()
|
||||
.frame(width: 54)
|
||||
} else {
|
||||
Text("Connect")
|
||||
.font(T3Typography.supportingStrong)
|
||||
if purpose == .connect {
|
||||
Button {
|
||||
Task { await handleConnect(item.environment) }
|
||||
} label: {
|
||||
if controller.busyEnvironmentID == item.id
|
||||
|| connectingEnvironmentID == item.id {
|
||||
ProgressView()
|
||||
.frame(width: 54)
|
||||
} else {
|
||||
Text("Connect")
|
||||
.font(T3Typography.supportingStrong)
|
||||
}
|
||||
}
|
||||
.buttonStyle(.borderless)
|
||||
.disabled(
|
||||
controller.busyEnvironmentID != nil
|
||||
|| connectingEnvironmentID != nil
|
||||
|| item.status?.status == .offline
|
||||
)
|
||||
}
|
||||
.buttonStyle(.borderless)
|
||||
.disabled(
|
||||
controller.busyEnvironmentID != nil
|
||||
|| connectingEnvironmentID != nil
|
||||
|| item.status?.status == .offline
|
||||
)
|
||||
}
|
||||
.padding(.vertical, 5)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import Foundation
|
||||
|
||||
struct T3ConnectEnvironmentPresentation: Identifiable, Equatable {
|
||||
let linkedEnvironment: T3ConnectCloudEnvironment?
|
||||
let savedEnvironment: FeatureEnvironment?
|
||||
|
||||
var id: String {
|
||||
linkedEnvironment?.id ?? savedEnvironment?.id ?? ""
|
||||
}
|
||||
|
||||
var name: String {
|
||||
linkedEnvironment?.environment.label ?? savedEnvironment?.name ?? "T3 environment"
|
||||
}
|
||||
|
||||
var isEnabled: Bool {
|
||||
savedEnvironment?.isEnabled ?? false
|
||||
}
|
||||
|
||||
var isOnline: Bool {
|
||||
if savedEnvironment?.connectionState == .connected {
|
||||
return true
|
||||
}
|
||||
return linkedEnvironment?.status?.status == .online
|
||||
}
|
||||
}
|
||||
|
||||
enum ConnectionHubPresentation {
|
||||
static func directEnvironments(
|
||||
in environments: [FeatureEnvironment]
|
||||
) -> [FeatureEnvironment] {
|
||||
environments.filter { $0.source == .direct }
|
||||
}
|
||||
|
||||
/// T3 Connect owns the account catalog while Core owns the environments
|
||||
/// already saved on this iPhone. Join them by server identity so the hub
|
||||
/// has one row and one switch for each machine.
|
||||
static func t3ConnectEnvironments(
|
||||
saved: [FeatureEnvironment],
|
||||
linked: [T3ConnectCloudEnvironment]
|
||||
) -> [T3ConnectEnvironmentPresentation] {
|
||||
let savedByID = Dictionary(
|
||||
uniqueKeysWithValues: saved
|
||||
.filter { $0.source == .t3Connect }
|
||||
.map { ($0.id, $0) }
|
||||
)
|
||||
let linkedIDs = Set(linked.map(\.id))
|
||||
let linkedRows = linked.map {
|
||||
T3ConnectEnvironmentPresentation(
|
||||
linkedEnvironment: $0,
|
||||
savedEnvironment: savedByID[$0.id]
|
||||
)
|
||||
}
|
||||
let savedOnlyRows: [T3ConnectEnvironmentPresentation] = saved.compactMap { environment in
|
||||
guard environment.source == .t3Connect,
|
||||
!linkedIDs.contains(environment.id) else { return nil }
|
||||
return T3ConnectEnvironmentPresentation(
|
||||
linkedEnvironment: nil,
|
||||
savedEnvironment: environment
|
||||
)
|
||||
}
|
||||
return linkedRows + savedOnlyRows
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ struct ConnectionsView: View {
|
||||
@State private var showingT3Connect = false
|
||||
@State private var detailEnvironmentID: String?
|
||||
@State private var removalTarget: FeatureEnvironment?
|
||||
@State private var connectingEnvironmentID: String?
|
||||
@State private var connectionErrorMessage: String?
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
@@ -18,8 +20,8 @@ struct ConnectionsView: View {
|
||||
|
||||
ScrollView {
|
||||
LazyVStack(alignment: .leading, spacing: 28) {
|
||||
connectionList
|
||||
accountSection
|
||||
directConnectionsSection
|
||||
t3ConnectSection
|
||||
accessSection
|
||||
}
|
||||
.padding(.horizontal, 20)
|
||||
@@ -29,9 +31,13 @@ struct ConnectionsView: View {
|
||||
}
|
||||
.background(T3Colors.background)
|
||||
.toolbar(.hidden, for: .navigationBar)
|
||||
.task {
|
||||
await t3ConnectController?.refresh()
|
||||
}
|
||||
.sheet(isPresented: $showingAddConnection) {
|
||||
ConnectionOnboardingView(
|
||||
model: model,
|
||||
showsT3ConnectOption: false,
|
||||
onConnected: {
|
||||
showingAddConnection = false
|
||||
Task { await model.reloadAfterConnection() }
|
||||
@@ -55,7 +61,10 @@ struct ConnectionsView: View {
|
||||
NavigationStack {
|
||||
T3ConnectView(
|
||||
capability: capability,
|
||||
onConnected: { await model.reloadAfterConnection() }
|
||||
purpose: .manage,
|
||||
onUnlinked: { id in
|
||||
await model.removeEnvironment(id)
|
||||
}
|
||||
)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
@@ -103,7 +112,18 @@ struct ConnectionsView: View {
|
||||
}
|
||||
Button("Cancel", role: .cancel) {}
|
||||
} message: { environment in
|
||||
Text("\(environment.name) will need a new pairing code to be added again.")
|
||||
Text(removalMessage(for: environment))
|
||||
}
|
||||
.alert(
|
||||
"T3 Connect",
|
||||
isPresented: Binding(
|
||||
get: { connectionErrorMessage != nil },
|
||||
set: { if !$0 { connectionErrorMessage = nil } }
|
||||
)
|
||||
) {
|
||||
Button("OK") { connectionErrorMessage = nil }
|
||||
} message: {
|
||||
Text(connectionErrorMessage ?? "Something went wrong.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,15 +139,14 @@ struct ConnectionsView: View {
|
||||
|
||||
Spacer(minLength: 0)
|
||||
|
||||
Text("Connections")
|
||||
Text("Environments")
|
||||
.font(T3Typography.navigationTitle)
|
||||
.foregroundStyle(T3Colors.textPrimary)
|
||||
|
||||
Spacer(minLength: 0)
|
||||
|
||||
Button("Add") { showingAddConnection = true }
|
||||
.frame(width: 92, alignment: .trailing)
|
||||
.accessibilityIdentifier("connections-add-button")
|
||||
Color.clear
|
||||
.frame(width: 92)
|
||||
}
|
||||
.font(T3Typography.control)
|
||||
.foregroundStyle(T3Colors.accent)
|
||||
@@ -135,74 +154,47 @@ struct ConnectionsView: View {
|
||||
.frame(minHeight: 54)
|
||||
}
|
||||
|
||||
private var connectionList: some View {
|
||||
private var directConnectionsSection: some View {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Text(connectionSummary.uppercased())
|
||||
.font(T3Typography.eyebrow)
|
||||
.foregroundStyle(T3Colors.textSecondary)
|
||||
HStack(spacing: 12) {
|
||||
Text("DIRECT CONNECTIONS")
|
||||
.font(T3Typography.eyebrow)
|
||||
.foregroundStyle(T3Colors.textSecondary)
|
||||
Spacer(minLength: 0)
|
||||
Button("Add") { showingAddConnection = true }
|
||||
.font(T3Typography.control)
|
||||
.accessibilityIdentifier("connections-add-button")
|
||||
}
|
||||
|
||||
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) }
|
||||
if directEnvironments.isEmpty {
|
||||
emptyRow("No direct connections")
|
||||
} else {
|
||||
VStack(spacing: 0) {
|
||||
Divider().overlay(T3Colors.separator)
|
||||
ForEach(model.snapshot.environments) { environment in
|
||||
connectionRow(environment)
|
||||
ForEach(directEnvironments) { environment in
|
||||
directConnectionRow(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 {
|
||||
private func directConnectionRow(_ environment: FeatureEnvironment) -> some View {
|
||||
HStack(spacing: 12) {
|
||||
Button {
|
||||
detailEnvironmentID = environment.id
|
||||
} label: {
|
||||
HStack(spacing: 12) {
|
||||
Image(systemName: environment.source.systemImage)
|
||||
Image(systemName: "desktopcomputer")
|
||||
.font(.system(size: 18, weight: .medium))
|
||||
.foregroundStyle(environment.source == .t3Connect
|
||||
? T3Colors.accent
|
||||
: T3Colors.textSecondary)
|
||||
.foregroundStyle(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)
|
||||
}
|
||||
environmentLabel(
|
||||
name: environment.name,
|
||||
isOnline: environment.connectionState == .connected
|
||||
)
|
||||
|
||||
Spacer(minLength: 8)
|
||||
}
|
||||
@@ -226,41 +218,121 @@ struct ConnectionsView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private var accountSection: some View {
|
||||
private var t3ConnectSection: some View {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Text("T3 CONNECT ACCOUNT")
|
||||
.font(T3Typography.eyebrow)
|
||||
.foregroundStyle(T3Colors.textSecondary)
|
||||
HStack(spacing: 12) {
|
||||
Text("T3 CONNECT")
|
||||
.font(T3Typography.eyebrow)
|
||||
.foregroundStyle(T3Colors.textSecondary)
|
||||
Spacer(minLength: 0)
|
||||
Button("Manage") { showingT3Connect = true }
|
||||
.font(T3Typography.control)
|
||||
.disabled(t3ConnectCapability == nil)
|
||||
.accessibilityIdentifier("connections-manage-t3-connect-button")
|
||||
}
|
||||
|
||||
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)
|
||||
if t3ConnectRows.isEmpty {
|
||||
if t3ConnectController?.isRefreshing == true {
|
||||
HStack(spacing: 10) {
|
||||
ProgressView()
|
||||
.controlSize(.small)
|
||||
Text("Checking T3 Connect")
|
||||
.font(T3Typography.supporting)
|
||||
.foregroundStyle(T3Colors.textSecondary)
|
||||
}
|
||||
Spacer(minLength: 8)
|
||||
Image(systemName: "chevron.right")
|
||||
.font(T3Typography.supportingStrong)
|
||||
.foregroundStyle(T3Colors.textTertiary)
|
||||
.frame(minHeight: 58)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.overlay(alignment: .top) { Divider().overlay(T3Colors.separator) }
|
||||
.overlay(alignment: .bottom) { Divider().overlay(T3Colors.separator) }
|
||||
} else if t3ConnectController?.account == nil {
|
||||
emptyRow("Sign in to access your T3 Connect machines")
|
||||
} else {
|
||||
emptyRow("No linked machines")
|
||||
}
|
||||
} else {
|
||||
VStack(spacing: 0) {
|
||||
Divider().overlay(T3Colors.separator)
|
||||
ForEach(t3ConnectRows) { item in
|
||||
t3ConnectConnectionRow(item)
|
||||
Divider().overlay(T3Colors.separator)
|
||||
}
|
||||
}
|
||||
.frame(minHeight: 58)
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.disabled(model.client is any T3ConnectCapable == false)
|
||||
|
||||
Text("Turn on any online machine to use it on this iPhone.")
|
||||
.font(T3Typography.supporting)
|
||||
.foregroundStyle(T3Colors.textTertiary)
|
||||
.padding(.top, 2)
|
||||
}
|
||||
}
|
||||
|
||||
private func t3ConnectConnectionRow(
|
||||
_ item: T3ConnectEnvironmentPresentation
|
||||
) -> some View {
|
||||
HStack(spacing: 12) {
|
||||
Group {
|
||||
if let environment = item.savedEnvironment {
|
||||
Button {
|
||||
detailEnvironmentID = environment.id
|
||||
} label: {
|
||||
t3ConnectEnvironmentLabel(item)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
} else {
|
||||
t3ConnectEnvironmentLabel(item)
|
||||
}
|
||||
}
|
||||
|
||||
Toggle("Enabled", isOn: t3ConnectEnabledBinding(for: item))
|
||||
.labelsHidden()
|
||||
.tint(T3Colors.success)
|
||||
.disabled(isT3ConnectToggleDisabled(item))
|
||||
.accessibilityLabel("Enable \(item.name)")
|
||||
}
|
||||
.frame(minHeight: 70)
|
||||
}
|
||||
|
||||
private func t3ConnectEnvironmentLabel(
|
||||
_ item: T3ConnectEnvironmentPresentation
|
||||
) -> some View {
|
||||
HStack(spacing: 12) {
|
||||
Image(systemName: "desktopcomputer")
|
||||
.font(.system(size: 18, weight: .medium))
|
||||
.foregroundStyle(T3Colors.textSecondary)
|
||||
.frame(width: 25)
|
||||
|
||||
environmentLabel(name: item.name, isOnline: item.isOnline)
|
||||
Spacer(minLength: 8)
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
|
||||
private func environmentLabel(name: String, isOnline: Bool) -> some View {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(name)
|
||||
.font(T3Typography.homeTitle)
|
||||
.foregroundStyle(T3Colors.textPrimary)
|
||||
.lineLimit(1)
|
||||
|
||||
HStack(spacing: 6) {
|
||||
Circle()
|
||||
.fill(isOnline ? T3Colors.success : T3Colors.danger)
|
||||
.frame(width: 7, height: 7)
|
||||
Text(isOnline ? "Online" : "Offline")
|
||||
}
|
||||
.font(T3Typography.supporting)
|
||||
.foregroundStyle(T3Colors.textSecondary)
|
||||
}
|
||||
}
|
||||
|
||||
private func emptyRow(_ message: String) -> some View {
|
||||
Text(message)
|
||||
.font(T3Typography.threadBody)
|
||||
.foregroundStyle(T3Colors.textSecondary)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.frame(minHeight: 58)
|
||||
.overlay(alignment: .top) { Divider().overlay(T3Colors.separator) }
|
||||
.overlay(alignment: .bottom) { Divider().overlay(T3Colors.separator) }
|
||||
}
|
||||
}
|
||||
|
||||
private var accessSection: some View {
|
||||
@@ -306,27 +378,84 @@ struct ConnectionsView: View {
|
||||
)
|
||||
}
|
||||
|
||||
private var connectionSummary: String {
|
||||
let environments = model.snapshot.environments
|
||||
let online = environments.count {
|
||||
$0.isEnabled && $0.connectionState == .connected
|
||||
private func t3ConnectEnabledBinding(
|
||||
for item: T3ConnectEnvironmentPresentation
|
||||
) -> Binding<Bool> {
|
||||
Binding(
|
||||
get: { pendingEnabledValues[item.id] ?? item.isEnabled },
|
||||
set: { enabled in
|
||||
pendingEnabledValues[item.id] = enabled
|
||||
Task { await setT3ConnectEnvironment(item, enabled: enabled) }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private func setT3ConnectEnvironment(
|
||||
_ item: T3ConnectEnvironmentPresentation,
|
||||
enabled: Bool
|
||||
) async {
|
||||
defer {
|
||||
pendingEnabledValues[item.id] = nil
|
||||
if connectingEnvironmentID == item.id {
|
||||
connectingEnvironmentID = nil
|
||||
}
|
||||
}
|
||||
return "\(environments.count) saved · \(online) online"
|
||||
|
||||
if let savedEnvironment = item.savedEnvironment {
|
||||
_ = await model.setEnvironmentEnabled(savedEnvironment.id, enabled: enabled)
|
||||
return
|
||||
}
|
||||
|
||||
guard enabled,
|
||||
let linkedEnvironment = item.linkedEnvironment,
|
||||
let capability = t3ConnectCapability else { return }
|
||||
|
||||
connectingEnvironmentID = item.id
|
||||
do {
|
||||
let credential = try await capability.t3ConnectController.credential(
|
||||
for: linkedEnvironment.environment
|
||||
)
|
||||
try await capability.connectT3Environment(credential)
|
||||
await model.reloadAfterConnection()
|
||||
} catch {
|
||||
connectionErrorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
private func isT3ConnectToggleDisabled(
|
||||
_ item: T3ConnectEnvironmentPresentation
|
||||
) -> Bool {
|
||||
pendingEnabledValues[item.id] != nil
|
||||
|| (connectingEnvironmentID != nil && connectingEnvironmentID != item.id)
|
||||
|| t3ConnectController?.busyEnvironmentID != nil
|
||||
}
|
||||
|
||||
private var directEnvironments: [FeatureEnvironment] {
|
||||
ConnectionHubPresentation.directEnvironments(in: model.snapshot.environments)
|
||||
}
|
||||
|
||||
private var t3ConnectRows: [T3ConnectEnvironmentPresentation] {
|
||||
ConnectionHubPresentation.t3ConnectEnvironments(
|
||||
saved: model.snapshot.environments,
|
||||
linked: t3ConnectController?.environments ?? []
|
||||
)
|
||||
}
|
||||
|
||||
private var t3ConnectCapability: (any T3ConnectCapable)? {
|
||||
model.client as? any T3ConnectCapable
|
||||
}
|
||||
|
||||
private var t3ConnectController: T3ConnectController? {
|
||||
(model.client as? any T3ConnectCapable)?.t3ConnectController
|
||||
t3ConnectCapability?.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 func removalMessage(for environment: FeatureEnvironment) -> String {
|
||||
switch environment.source {
|
||||
case .direct:
|
||||
"\(environment.name) will need a new pairing code to be added again."
|
||||
case .t3Connect:
|
||||
"\(environment.name) will remain linked to your T3 Connect account."
|
||||
}
|
||||
}
|
||||
|
||||
private var deviceManager: any FeatureDeviceManaging {
|
||||
@@ -386,7 +515,7 @@ private struct ConnectionDetailView: View {
|
||||
Button("Remove", role: .destructive, action: onRemove)
|
||||
Button("Cancel", role: .cancel) {}
|
||||
} message: {
|
||||
Text("A new pairing code will be required to add it again.")
|
||||
Text(removalMessage)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -398,6 +527,13 @@ private struct ConnectionDetailView: View {
|
||||
model.snapshot.projects.count { $0.environmentID == environmentID }
|
||||
}
|
||||
|
||||
private var removalMessage: String {
|
||||
guard environment?.source == .t3Connect else {
|
||||
return "A new pairing code will be required to add it again."
|
||||
}
|
||||
return "It will remain linked to your T3 Connect account."
|
||||
}
|
||||
|
||||
private func enabledBinding(for environment: FeatureEnvironment) -> Binding<Bool> {
|
||||
Binding(
|
||||
get: { pendingEnabledValues[environment.id] ?? environment.isEnabled },
|
||||
@@ -420,12 +556,6 @@ private extension FeatureEnvironment.Source {
|
||||
}
|
||||
}
|
||||
|
||||
var systemImage: String {
|
||||
switch self {
|
||||
case .direct: "desktopcomputer"
|
||||
case .t3Connect: "cloud"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension FeatureEnvironment {
|
||||
|
||||
@@ -92,13 +92,12 @@ public struct SettingsView: View {
|
||||
}
|
||||
|
||||
private var connectionSection: some View {
|
||||
SettingsSection(title: "Connections") {
|
||||
SettingsSection(title: "Environments") {
|
||||
NavigationLink {
|
||||
ConnectionsView(model: model)
|
||||
} label: {
|
||||
SettingsNavigationRow(
|
||||
title: "Connections",
|
||||
value: connectionSummary,
|
||||
title: "Environments",
|
||||
systemImage: "server.rack"
|
||||
)
|
||||
}
|
||||
@@ -198,14 +197,6 @@ public struct SettingsView: View {
|
||||
!isSaving && settings != model.snapshot.settings
|
||||
}
|
||||
|
||||
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
|
||||
private func save() {
|
||||
isSaving = true
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
import Testing
|
||||
@testable import T3Code
|
||||
|
||||
@Suite("Environment management presentation")
|
||||
struct ConnectionHubPresentationTests {
|
||||
@Test
|
||||
func directSectionContainsOnlyDirectConnections() {
|
||||
let direct = environment(id: "direct", source: .direct)
|
||||
let managed = environment(id: "managed", source: .t3Connect)
|
||||
|
||||
#expect(
|
||||
ConnectionHubPresentation.directEnvironments(in: [managed, direct]) == [direct]
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
func t3ConnectSectionJoinsSavedAndAccountMachinesWithoutDuplicates() {
|
||||
let saved = [
|
||||
environment(
|
||||
id: "shared",
|
||||
name: "Old saved label",
|
||||
source: .t3Connect,
|
||||
isEnabled: true,
|
||||
connectionState: .connected
|
||||
),
|
||||
environment(id: "saved-only", source: .t3Connect, isEnabled: false),
|
||||
environment(id: "direct", source: .direct),
|
||||
]
|
||||
let linked = [
|
||||
cloudEnvironment(id: "linked-only", name: "Travel Mac", isOnline: true),
|
||||
cloudEnvironment(id: "shared", name: "Big O", isOnline: false),
|
||||
]
|
||||
|
||||
let rows = ConnectionHubPresentation.t3ConnectEnvironments(
|
||||
saved: saved,
|
||||
linked: linked
|
||||
)
|
||||
|
||||
#expect(rows.map(\.id) == ["linked-only", "shared", "saved-only"])
|
||||
#expect(rows[0].name == "Travel Mac")
|
||||
#expect(!rows[0].isEnabled)
|
||||
#expect(rows[0].isOnline)
|
||||
#expect(rows[1].name == "Big O")
|
||||
#expect(rows[1].isEnabled)
|
||||
#expect(rows[1].isOnline)
|
||||
#expect(rows[2].savedEnvironment?.id == "saved-only")
|
||||
#expect(rows[2].linkedEnvironment == nil)
|
||||
}
|
||||
|
||||
private func environment(
|
||||
id: String,
|
||||
name: String? = nil,
|
||||
source: FeatureEnvironment.Source,
|
||||
isEnabled: Bool = true,
|
||||
connectionState: FeatureConnection.State? = nil
|
||||
) -> FeatureEnvironment {
|
||||
FeatureEnvironment(
|
||||
id: id,
|
||||
name: name ?? id,
|
||||
endpoint: "https://\(id).example",
|
||||
isEnabled: isEnabled,
|
||||
source: source,
|
||||
connectionState: connectionState
|
||||
)
|
||||
}
|
||||
|
||||
private func cloudEnvironment(
|
||||
id: String,
|
||||
name: String,
|
||||
isOnline: Bool
|
||||
) -> T3ConnectCloudEnvironment {
|
||||
let endpoint = T3ConnectManagedEndpoint(
|
||||
httpBaseUrl: "https://\(id).example",
|
||||
wsBaseUrl: "wss://\(id).example",
|
||||
providerKind: .t3Relay
|
||||
)
|
||||
return T3ConnectCloudEnvironment(
|
||||
environment: T3ConnectRelayEnvironment(
|
||||
environmentId: id,
|
||||
label: name,
|
||||
endpoint: endpoint,
|
||||
linkedAt: "2026-08-14T00:00:00.000Z"
|
||||
),
|
||||
status: T3ConnectRelayEnvironmentStatus(
|
||||
environmentId: id,
|
||||
endpoint: endpoint,
|
||||
status: isOnline ? .online : .offline,
|
||||
checkedAt: "2026-08-14T00:00:00.000Z",
|
||||
descriptor: nil,
|
||||
error: nil,
|
||||
traceId: nil
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user