struct ChatView: View {
@State private var input = ""
@State private var messages: [(String, Bool)] = [] // (text, isUser)
@State private var isLoading = false
var body: some View {
VStack {
// Messages
ScrollView {
ForEach(Array(messages.enumerated()), id: \.offset) { _, message in
HStack {
if message.1 { Spacer() }
Text(message.0)
.padding()
.background(message.1 ? Color.blue : Color.gray.opacity(0.2))
.foregroundColor(message.1 ? .white : .primary)
.cornerRadius(12)
if !message.1 { Spacer() }
}
}
}
// Input
HStack {
TextField("Message...", text: $input)
.textFieldStyle(.roundedBorder)
Button(action: sendMessage) {
Image(systemName: "arrow.up.circle.fill")
.font(.title)
}
.disabled(input.isEmpty || isLoading)
}
.padding()
}
}
func sendMessage() {
let userMessage = input
messages.append((userMessage, true))
input = ""
isLoading = true
Task {
do {
let response = try await RunAnywhere.chat(userMessage)
await MainActor.run {
messages.append((response, false))
isLoading = false
}
} catch {
await MainActor.run {
messages.append(("Error: \(error.localizedDescription)", false))
isLoading = false
}
}
}
}
}