@MainActor
@Observable
class CompilerManager {
var isAuthenticated = false
var errorMessage: String?
var isCheckingAuth = true
let client: CompilerClient
init() {
// Initialize with Google Gemini Flash
client = CompilerClient(
appID: UUID(uuidString: "YOUR_COMPILER_APP_ID")!,
configuration: CompilerClient.Configuration(
streamingChat: .google(.flash)
)
)
// Check auth state on init
Task { @MainActor in
do {
isAuthenticated = try await client.attemptAutoLogin()
} catch {
errorMessage = error.localizedDescription
}
isCheckingAuth = false
}
}
}
struct ContentView: View {
@State private var compiler = CompilerManager()
@State private var currentNonce: String?
var body: some View {
VStack(spacing: 20) {
if compiler.isCheckingAuth {
ProgressView("Checking authentication...")
.progressViewStyle(.circular)
} else if compiler.isAuthenticated {
VStack(spacing: 20) {
Text("Chat")
.font(.title)
.fontWeight(.bold)
// The ChatView automatically uses the model configured in the client
ChatView(client: compiler.client, inputType: .combined)
.userBubbleStyle(backgroundColor: .blue, textColor: .white)
.assistantBubbleStyle(backgroundColor: .clear, textColor: .black)
.inputFieldStyle(
backgroundColor: .gray.opacity(0.1),
textColor: .primary,
placeholder: "Message"
)
.inputButtonStyle(
size: 32,
sendTint: .blue
)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
} else {
SignInWithAppleButton(
onRequest: { request in
let nonce = CompilerClient.randomNonceString()
currentNonce = nonce
request.nonce = CompilerClient.sha256(nonce)
request.requestedScopes = [.fullName, .email]
},
onCompletion: { result in
Task {
do {
compiler.isAuthenticated = try await compiler.client.handleSignInWithApple(result, nonce: currentNonce)
} catch {
compiler.errorMessage = error.localizedDescription
}
}
}
)
.frame(width: 280, height: 45)
.signInWithAppleButtonStyle(.black)
}
if let errorMessage = compiler.errorMessage {
Text(errorMessage)
.foregroundColor(.red)
.padding()
}
}
.padding()
.frame(minWidth: 400, minHeight: 600)
.frame(maxWidth: 800, maxHeight: .infinity)
}
}