ConversationScreen.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseVertexAI
  15. import GenerativeAIUIComponents
  16. import SwiftUI
  17. struct ConversationScreen: View {
  18. @EnvironmentObject
  19. var viewModel: ConversationViewModel
  20. @State
  21. private var userPrompt = ""
  22. enum FocusedField: Hashable {
  23. case message
  24. }
  25. @FocusState
  26. var focusedField: FocusedField?
  27. var body: some View {
  28. VStack {
  29. ScrollViewReader { scrollViewProxy in
  30. List {
  31. ForEach(viewModel.messages) { message in
  32. MessageView(message: message)
  33. }
  34. if let error = viewModel.error {
  35. ErrorView(error: error)
  36. .tag("errorView")
  37. }
  38. }
  39. .listStyle(.plain)
  40. .onChange(of: viewModel.messages, perform: { newValue in
  41. if viewModel.hasError {
  42. // wait for a short moment to make sure we can actually scroll to the bottom
  43. DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
  44. withAnimation {
  45. scrollViewProxy.scrollTo("errorView", anchor: .bottom)
  46. }
  47. focusedField = .message
  48. }
  49. } else {
  50. guard let lastMessage = viewModel.messages.last else { return }
  51. // wait for a short moment to make sure we can actually scroll to the bottom
  52. DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
  53. withAnimation {
  54. scrollViewProxy.scrollTo(lastMessage.id, anchor: .bottom)
  55. }
  56. focusedField = .message
  57. }
  58. }
  59. })
  60. }
  61. InputField("Message...", text: $userPrompt) {
  62. Image(systemName: viewModel.busy ? "stop.circle.fill" : "arrow.up.circle.fill")
  63. .font(.title)
  64. }
  65. .focused($focusedField, equals: .message)
  66. .onSubmit { sendOrStop() }
  67. }
  68. .toolbar {
  69. ToolbarItem(placement: .primaryAction) {
  70. Button(action: newChat) {
  71. Image(systemName: "square.and.pencil")
  72. }
  73. }
  74. }
  75. .navigationTitle("Chat sample")
  76. .onAppear {
  77. focusedField = .message
  78. }
  79. }
  80. private func sendMessage() {
  81. Task {
  82. let prompt = userPrompt
  83. userPrompt = ""
  84. await viewModel.sendMessage(prompt, streaming: true)
  85. }
  86. }
  87. private func sendOrStop() {
  88. focusedField = nil
  89. if viewModel.busy {
  90. viewModel.stop()
  91. } else {
  92. sendMessage()
  93. }
  94. }
  95. private func newChat() {
  96. viewModel.startNewChat()
  97. }
  98. }
  99. struct ConversationScreen_Previews: PreviewProvider {
  100. struct ContainerView: View {
  101. @StateObject var viewModel = ConversationViewModel()
  102. var body: some View {
  103. ConversationScreen()
  104. .environmentObject(viewModel)
  105. .onAppear {
  106. viewModel.messages = ChatMessage.samples
  107. }
  108. }
  109. }
  110. static var previews: some View {
  111. NavigationStack {
  112. ConversationScreen()
  113. }
  114. }
  115. }