Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

FeatureRootView.swift

105 lines3.5 KB

1import SwiftUI
2​
3public struct FeatureRootView: View {
4 @State private var model: FeatureRootModel
5 private let navigationRequest: FeatureWorkspaceNavigationRequest?
6 private let onNavigationRequestConsumed: @MainActor (UUID) -> Void
7​
8 public init(client: any FeatureClient) {
9 _model = State(initialValue: FeatureRootModel(client: client))
10 navigationRequest = nil
11 onNavigationRequestConsumed = { _ in }
12 }
13​
14 init(
15 model: FeatureRootModel,
16 navigationRequest: FeatureWorkspaceNavigationRequest? = nil,
17 onNavigationRequestConsumed: @escaping @MainActor (UUID) -> Void = { _ in }
18 ) {
19 _model = State(initialValue: model)
20 self.navigationRequest = navigationRequest
21 self.onNavigationRequestConsumed = onNavigationRequestConsumed
22 }
23​
24 public var body: some View {
25 Group {
26 if model.isLoading {
27 FeatureLoadingView()
28 } else if shouldShowWorkspace {
29 WorkspaceView(
30 model: model,
31 navigationRequest: navigationRequest,
32 onNavigationRequestConsumed: onNavigationRequestConsumed
33 )
34 } else {
35 ConnectionOnboardingView(model: model)
36 }
37 }
38 .preferredColorScheme(preferredColorScheme)
39 .t3AppTextSize(steps: model.snapshot.settings.textSize.steps)
40 .t3CodeSizing(steps: model.snapshot.settings.codeSize.steps)
41 .tint(T3Colors.accent)
42 .background(T3Colors.background.ignoresSafeArea())
43 .task { await model.start() }
44 .alert(
45 "Something went wrong",
46 isPresented: Binding(
47 get: { model.errorMessage != nil },
48 set: { if !$0 { model.errorMessage = nil } }
49 ),
50 actions: {
51 Button("OK") { model.errorMessage = nil }
52 },
53 message: {
54 Text(model.errorMessage ?? "Unknown error")
55 }
56 )
57 }
58​
59 /// Keep the last-known workspace visible through a degraded connection.
60 /// Connection management also stays mounted while saved servers are being
61 /// removed, so a disconnected fallback cannot destroy its own Settings sheet.
62 private var shouldShowWorkspace: Bool {
63 FeatureRootPresentation.showsWorkspace(
64 snapshot: model.snapshot,
65 isManagingConnections: model.isManagingConnections
66 )
67 }
68​
69 private var preferredColorScheme: ColorScheme? {
70 switch model.snapshot.settings.appearance {
71 case .system: nil
72 case .light: .light
73 case .dark: .dark
74 }
75 }
77​
78enum FeatureRootPresentation {
79 static func showsWorkspace(
80 snapshot: FeatureSnapshot,
81 isManagingConnections: Bool
82 ) -> Bool {
83 isManagingConnections
84 || !snapshot.environments.isEmpty
85 || !snapshot.projects.isEmpty
86 || !snapshot.threads.isEmpty
87 }
89​
90private struct FeatureLoadingView: View {
91 var body: some View {
92 VStack(spacing: 14) {
93 Image(systemName: "chevron.left.forwardslash.chevron.right")
94 .font(.system(size: 30, weight: .semibold))
95 ProgressView()
96 .controlSize(.small)
97 Text("Connecting to T3 Code")
98 .font(T3Typography.supporting)
99 .foregroundStyle(T3Colors.textSecondary)
100 }
101 .frame(maxWidth: .infinity, maxHeight: .infinity)
102 .background(T3Colors.background)
103 .accessibilityElement(children: .combine)
104 }