MultimodalOutputScreen.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 GenerativeAIUIComponents
  15. import MarkdownUI
  16. import PhotosUI
  17. import SwiftUI
  18. struct MultimodalOutputScreen: View {
  19. @StateObject var viewModel = MultimodalOutputViewModel()
  20. enum FocusedField: Hashable {
  21. case message
  22. }
  23. @FocusState
  24. var focusedField: FocusedField?
  25. var body: some View {
  26. VStack {
  27. InputField(text: $viewModel.userInput)
  28. .focused($focusedField, equals: .message)
  29. .onSubmit {
  30. onSendTapped()
  31. }
  32. ScrollViewReader { scrollViewProxy in
  33. List {
  34. if let outputText = viewModel.outputText {
  35. HStack(alignment: .top) {
  36. if viewModel.inProgress {
  37. ProgressView()
  38. } else {
  39. Image(systemName: "cloud.circle.fill")
  40. .font(.title2)
  41. }
  42. Markdown("\(outputText)")
  43. }
  44. .listRowSeparator(.hidden)
  45. }
  46. }
  47. .listStyle(.plain)
  48. }
  49. }
  50. .navigationTitle("Multimodal Output Sample")
  51. .onAppear {
  52. focusedField = .message
  53. }
  54. }
  55. // MARK: - Actions
  56. private func onSendTapped() {
  57. focusedField = nil
  58. Task {
  59. await viewModel.generate()
  60. }
  61. }
  62. }
  63. #Preview {
  64. NavigationStack {
  65. MultimodalOutputScreen()
  66. }
  67. }