This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MainActor | |
struct ChatView: View { | |
@Bindable private var viewModel: ChatViewModel = ChatViewModel() | |
var body: some View { | |
NavigationStack { | |
VStack { | |
ChatMessagesList(viewModel: viewModel) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SendMessageButton { | |
Task { | |
await viewModel.sendMessage() | |
} | |
} | |
.disabled(!viewModel.canSendMessage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MainActor | |
@Observable | |
class ChatViewModel { | |
// Private | |
private let client: GeminiClient = GeminiClient() | |
// Public | |
var error: Error? | |
var waitingForResponse: Bool = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ChatMessagesList: View { | |
@Bindable var viewModel: ChatViewModel | |
@State private var loadingViewId: UUID? = UUID() | |
var body: some View { | |
ScrollView { | |
ForEach(viewModel.messages) { message in | |
MessageCell(message: message) | |
.padding(message.role.padding) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MainActor | |
@Observable | |
class ChatViewModel { | |
// ... | |
var scrollPosition: UUID? = nil | |
func sendMessage() { | |
guard canSendMessage else { return } | |
// In a real chat app this would make a server call to send the message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MainActor | |
struct ChatView: View { | |
@Bindable private var viewModel: ChatViewModel = ChatViewModel() | |
var body: some View { | |
NavigationStack { | |
VStack { | |
ChatMessagesList(viewModel: viewModel) | |
HStack { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Observation | |
@MainActor | |
@Observable | |
class ChatViewModel { | |
var draftMessage: String = "" | |
private(set) var messages: [Message] = [ | |
Message( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ErrorView: View { | |
var body: some View { | |
Text("Something went wrong, please try sending your message again.") | |
.foregroundStyle(Color.red) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct LoadingView: View { | |
var body: some View { | |
Image(systemName: "ellipsis.rectangle.fill") | |
.symbolRenderingMode(.hierarchical) | |
.foregroundStyle(Color.gray) | |
.font(.largeTitle) | |
.symbolEffect(.variableColor, options: .repeating, isActive: true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MainActor | |
@Observable | |
class ChatViewModel { | |
// Private | |
private let client: GeminiClient = GeminiClient() | |
// Public | |
var error: Error? | |
var waitingForResponse: Bool = false |
NewerOlder