SummarizeScreen.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 MarkdownUI
  15. import SwiftUI
  16. struct SummarizeScreen: View {
  17. @StateObject var viewModel = SummarizeViewModel()
  18. @State var userInput = ""
  19. enum FocusedField: Hashable {
  20. case message
  21. }
  22. @FocusState
  23. var focusedField: FocusedField?
  24. var body: some View {
  25. VStack {
  26. Text("Enter some text, then tap on _Go_ to summarize it.")
  27. HStack(alignment: .top) {
  28. TextField("Enter text summarize", text: $userInput, axis: .vertical)
  29. .textFieldStyle(.roundedBorder)
  30. .onSubmit {
  31. onSummarizeTapped()
  32. }
  33. Button("Go") {
  34. onSummarizeTapped()
  35. }
  36. .padding(.top, 4)
  37. }
  38. .padding([.horizontal, .bottom])
  39. List {
  40. HStack(alignment: .top) {
  41. if viewModel.inProgress {
  42. ProgressView()
  43. } else {
  44. Image(systemName: "cloud.circle.fill")
  45. .font(.title2)
  46. }
  47. Markdown("\(viewModel.outputText)")
  48. }
  49. .listRowSeparator(.hidden)
  50. }
  51. .listStyle(.plain)
  52. }
  53. .navigationTitle("Text sample")
  54. }
  55. private func onSummarizeTapped() {
  56. Task {
  57. await viewModel.summarize(inputText: userInput)
  58. }
  59. }
  60. }
  61. #Preview {
  62. NavigationStack {
  63. SummarizeScreen()
  64. }
  65. }