|
| 1 | +// Copyright 2025 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 | +import SwiftUI |
| 16 | +import GenerativeAIUIComponents |
| 17 | +#if canImport(FirebaseAILogic) |
| 18 | + import FirebaseAILogic |
| 19 | +#else |
| 20 | + import FirebaseAI |
| 21 | +#endif |
| 22 | + |
| 23 | +struct ImagenFromTemplateScreen: View { |
| 24 | + let firebaseService: FirebaseAI |
| 25 | + @StateObject var viewModel: ImagenFromTemplateViewModel |
| 26 | + |
| 27 | + init(firebaseService: FirebaseAI) { |
| 28 | + self.firebaseService = firebaseService |
| 29 | + _viewModel = StateObject(wrappedValue: ImagenFromTemplateViewModel(firebaseService: firebaseService)) |
| 30 | + } |
| 31 | + |
| 32 | + enum FocusedField: Hashable { |
| 33 | + case message |
| 34 | + } |
| 35 | + |
| 36 | + @FocusState |
| 37 | + var focusedField: FocusedField? |
| 38 | + |
| 39 | + var body: some View { |
| 40 | + ZStack { |
| 41 | + ScrollView { |
| 42 | + VStack { |
| 43 | + InputField("Enter a prompt to generate an image from template", text: $viewModel.userInput) { |
| 44 | + Image( |
| 45 | + systemName: viewModel.inProgress ? "stop.circle.fill" : "paperplane.circle.fill" |
| 46 | + ) |
| 47 | + .font(.title) |
| 48 | + } |
| 49 | + .focused($focusedField, equals: .message) |
| 50 | + .onSubmit { sendOrStop() } |
| 51 | + |
| 52 | + let spacing: CGFloat = 10 |
| 53 | + LazyVGrid(columns: [ |
| 54 | + GridItem(.flexible(), spacing: spacing), |
| 55 | + GridItem(.flexible(), spacing: spacing), |
| 56 | + ], spacing: spacing) { |
| 57 | + ForEach(viewModel.images, id: \.self) { image in |
| 58 | + Image(uiImage: image) |
| 59 | + .resizable() |
| 60 | + .aspectRatio(1, contentMode: .fill) |
| 61 | + .cornerRadius(12) |
| 62 | + .clipped() |
| 63 | + } |
| 64 | + } |
| 65 | + .padding(.horizontal, spacing) |
| 66 | + } |
| 67 | + } |
| 68 | + if viewModel.inProgress { |
| 69 | + ProgressOverlay() |
| 70 | + } |
| 71 | + } |
| 72 | + .onTapGesture { |
| 73 | + focusedField = nil |
| 74 | + } |
| 75 | + .navigationTitle("Imagen Template") |
| 76 | + .onAppear { |
| 77 | + focusedField = .message |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private func sendMessage() { |
| 82 | + Task { |
| 83 | + await viewModel.generateImageFromTemplate(prompt: viewModel.userInput) |
| 84 | + focusedField = .message |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private func sendOrStop() { |
| 89 | + if viewModel.inProgress { |
| 90 | + viewModel.stop() |
| 91 | + } else { |
| 92 | + sendMessage() |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#Preview { |
| 98 | + ImagenFromTemplateScreen(firebaseService: FirebaseAI.firebaseAI()) |
| 99 | +} |
0 commit comments