|
| 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 | + |
| 15 | +#if canImport(FirebaseAILogic) |
| 16 | + import FirebaseAILogic |
| 17 | +#else |
| 18 | + import FirebaseAI |
| 19 | +#endif |
| 20 | +import GenerativeAIUIComponents |
| 21 | +import SwiftUI |
| 22 | + |
| 23 | +struct ConversationFromTemplateScreen: View { |
| 24 | + let firebaseService: FirebaseAI |
| 25 | + let title: String |
| 26 | + @StateObject var viewModel: ConversationFromTemplateViewModel |
| 27 | + |
| 28 | + @State |
| 29 | + private var userPrompt = "" |
| 30 | + |
| 31 | + init(firebaseService: FirebaseAI, title: String) { |
| 32 | + self.title = title |
| 33 | + self.firebaseService = firebaseService |
| 34 | + _viewModel = |
| 35 | + StateObject(wrappedValue: ConversationFromTemplateViewModel(firebaseService: firebaseService)) |
| 36 | + } |
| 37 | + |
| 38 | + enum FocusedField: Hashable { |
| 39 | + case message |
| 40 | + } |
| 41 | + |
| 42 | + @FocusState |
| 43 | + var focusedField: FocusedField? |
| 44 | + |
| 45 | + var body: some View { |
| 46 | + VStack { |
| 47 | + ScrollViewReader { scrollViewProxy in |
| 48 | + List { |
| 49 | + ForEach(viewModel.messages) { message in |
| 50 | + MessageView(message: message) |
| 51 | + } |
| 52 | + if let error = viewModel.error { |
| 53 | + ErrorView(error: error) |
| 54 | + .tag("errorView") |
| 55 | + } |
| 56 | + } |
| 57 | + .listStyle(.plain) |
| 58 | + .onChange(of: viewModel.messages, perform: { newValue in |
| 59 | + if viewModel.hasError { |
| 60 | + // wait for a short moment to make sure we can actually scroll to the bottom |
| 61 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { |
| 62 | + withAnimation { |
| 63 | + scrollViewProxy.scrollTo("errorView", anchor: .bottom) |
| 64 | + } |
| 65 | + focusedField = .message |
| 66 | + } |
| 67 | + } else { |
| 68 | + guard let lastMessage = viewModel.messages.last else { return } |
| 69 | + |
| 70 | + // wait for a short moment to make sure we can actually scroll to the bottom |
| 71 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { |
| 72 | + withAnimation { |
| 73 | + scrollViewProxy.scrollTo(lastMessage.id, anchor: .bottom) |
| 74 | + } |
| 75 | + focusedField = .message |
| 76 | + } |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | + InputField("Message...", text: $userPrompt) { |
| 81 | + Image(systemName: viewModel.busy ? "stop.circle.fill" : "arrow.up.circle.fill") |
| 82 | + .font(.title) |
| 83 | + } |
| 84 | + .focused($focusedField, equals: .message) |
| 85 | + .onSubmit { sendOrStop() } |
| 86 | + } |
| 87 | + .onTapGesture { |
| 88 | + focusedField = nil |
| 89 | + } |
| 90 | + .toolbar { |
| 91 | + ToolbarItem(placement: .primaryAction) { |
| 92 | + Button(action: newChat) { |
| 93 | + Image(systemName: "square.and.pencil") |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + .navigationTitle(title) |
| 98 | + .onAppear { |
| 99 | + focusedField = .message |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private func sendMessage() { |
| 104 | + Task { |
| 105 | + let prompt = userPrompt |
| 106 | + userPrompt = "" |
| 107 | + await viewModel.sendMessage(prompt) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private func sendOrStop() { |
| 112 | + focusedField = nil |
| 113 | + |
| 114 | + if viewModel.busy { |
| 115 | + viewModel.stop() |
| 116 | + } else { |
| 117 | + sendMessage() |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private func newChat() { |
| 122 | + viewModel.startNewChat() |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +struct ConversationFromTemplateScreen_Previews: PreviewProvider { |
| 127 | + struct ContainerView: View { |
| 128 | + @StateObject var viewModel = ConversationFromTemplateViewModel(firebaseService: FirebaseAI |
| 129 | + .firebaseAI()) // Example service init |
| 130 | + |
| 131 | + var body: some View { |
| 132 | + ConversationFromTemplateScreen( |
| 133 | + firebaseService: FirebaseAI.firebaseAI(), |
| 134 | + title: "Chat from Template sample" |
| 135 | + ) |
| 136 | + .onAppear { |
| 137 | + viewModel.messages = ChatMessage.samples |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + static var previews: some View { |
| 143 | + NavigationStack { |
| 144 | + ConversationFromTemplateScreen( |
| 145 | + firebaseService: FirebaseAI.firebaseAI(), |
| 146 | + title: "Chat from Template sample" |
| 147 | + ) |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments