SummarizeScreen.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. VStack(alignment: .leading) {
  27. Text("Enter some text, then tap on _Go_ to summarize it.")
  28. .padding(.horizontal, 6)
  29. HStack(alignment: .top) {
  30. TextField("Enter text summarize", text: $userInput, axis: .vertical)
  31. .focused($focusedField, equals: .message)
  32. .textFieldStyle(.roundedBorder)
  33. .onSubmit {
  34. onSummarizeTapped()
  35. }
  36. Button("Go") {
  37. onSummarizeTapped()
  38. }
  39. .padding(.top, 4)
  40. }
  41. }
  42. .padding(.horizontal, 16)
  43. List {
  44. HStack(alignment: .top) {
  45. if viewModel.inProgress {
  46. ProgressView()
  47. } else {
  48. Image(systemName: "cloud.circle.fill")
  49. .font(.title2)
  50. }
  51. Markdown("\(viewModel.outputText)")
  52. }
  53. .listRowSeparator(.hidden)
  54. }
  55. .listStyle(.plain)
  56. }
  57. .navigationTitle("Text sample")
  58. }
  59. private func onSummarizeTapped() {
  60. focusedField = nil
  61. Task {
  62. await viewModel.summarize(inputText: userInput)
  63. }
  64. }
  65. }
  66. #Preview {
  67. NavigationStack {
  68. SummarizeScreen()
  69. }
  70. }