Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Apple Intelligence Chat/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ struct ContentView: View {
// UI State
@State private var messages: [ChatMessage] = []
@State private var inputText: String = ""
@State private var isResponding = false
@State private var showSettings = false
@State private var showErrorAlert = false
@State private var errorMessage = ""
Expand Down Expand Up @@ -43,7 +42,7 @@ struct ContentView: View {
ScrollView {
VStack {
ForEach(messages) { message in
MessageView(message: message, isResponding: isResponding)
MessageView(message: message, isResponding: isModelResponding)
.id(message.id)
}
}
Expand Down Expand Up @@ -93,7 +92,7 @@ struct ContentView: View {
.textFieldStyle(.plain)
.lineLimit(1...5)
.frame(minHeight: 22)
.disabled(isResponding)
.disabled(isModelResponding)
.onSubmit {
if !inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
handleSendOrStop()
Expand All @@ -104,12 +103,12 @@ struct ContentView: View {
HStack {
Spacer()
Button(action: handleSendOrStop) {
Image(systemName: isResponding ? "stop.circle.fill" : "arrow.up.circle.fill")
Image(systemName: isModelResponding ? "stop.circle.fill" : "arrow.up.circle.fill")
.font(.system(size: 30, weight: .bold))
.foregroundStyle(isSendButtonDisabled ? Color.gray.opacity(0.6) : .primary)
}
.disabled(isSendButtonDisabled)
.animation(.easeInOut(duration: 0.2), value: isResponding)
.animation(.easeInOut(duration: 0.2), value: isModelResponding)
.animation(.easeInOut(duration: 0.2), value: isSendButtonDisabled)
.glassEffect(.regular.interactive())
.padding(.trailing, 8)
Expand All @@ -119,7 +118,7 @@ struct ContentView: View {
}

private var isSendButtonDisabled: Bool {
return inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty && !isResponding
return inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty && !isModelResponding
}

@ToolbarContentBuilder
Expand Down Expand Up @@ -152,7 +151,7 @@ struct ContentView: View {
// MARK: - Model Interaction

private func handleSendOrStop() {
if isResponding {
if isModelResponding {
stopStreaming()
} else {
guard model.isAvailable else {
Expand All @@ -164,7 +163,6 @@ struct ContentView: View {
}

private func sendMessage() {
isResponding = true
let userMessage = ChatMessage(role: .user, text: inputText)
messages.append(userMessage)
let prompt = inputText
Expand All @@ -179,7 +177,6 @@ struct ContentView: View {

guard let currentSession = session else {
showError(message: "Session could not be created.")
isResponding = false
return
}

Expand All @@ -203,7 +200,6 @@ struct ContentView: View {
showError(message: "An error occurred: \(error.localizedDescription)")
}

isResponding = false
streamingTask = nil
}
}
Expand Down Expand Up @@ -253,8 +249,12 @@ struct ContentView: View {
private func showError(message: String) {
self.errorMessage = message
self.showErrorAlert = true
self.isResponding = false
}

private var isModelResponding: Bool {
session?.isResponding ?? false
}

}

#Preview {
Expand Down