This file contains hidden or 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 ContentView: View { | |
| @State private var showAlert: Bool = false | |
| var body: some View { | |
| Button("Show alert") { | |
| showAlert = true | |
| } | |
| } | |
| } | 
  
    
      This file contains hidden or 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 ContentView: View { | |
| @State private var showAlert: Bool = false | |
| // ... | |
| } | 
  
    
      This file contains hidden or 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 hidden or 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 { | |
| viewModel.sendMessage() | |
| } | |
| .disabled(!viewModel.canSendMessage) | 
  
    
      This file contains hidden or 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 hidden or 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 ChatMessagesList: View { | |
| @Bindable var viewModel: ChatViewModel | |
| var body: some View { | |
| ScrollView { | |
| ForEach(viewModel.messages) { message in | |
| MessageCell(message: message) | 
  
    
      This file contains hidden or 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 ChatMessagesList: View { | |
| @Bindable var viewModel: ChatViewModel | |
| var body: some View { | |
| ScrollView { | |
| ForEach(viewModel.messages) { message in | |
| MessageCell(message: message) | 
  
    
      This file contains hidden or 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 MessageTextField: View { | |
| @Binding var draftMessage: String | |
| var body: some View { | |
| TextField("Message...", text: $draftMessage) | |
| .textFieldStyle(.plain) | |
| .padding(8) | 
  
    
      This file contains hidden or 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 MessageCell: View { | |
| let message: Message | |
| var body: some View { | |
| HStack { | |
| if message.role == .sender { Spacer() } | |
| MessageBubble(message: message) | |
| if message.role == .receiver { Spacer() } | |
| } |