Skip to content

Refactor the UI layout of ImagenScreen to solve the navigation flicker #1719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 28, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions firebaseai/ImagenScreen/ImagenScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct ImagenScreen: View {
var focusedField: FocusedField?

var body: some View {
ZStack {
ScrollView {
VStack {
InputField("Enter a prompt to generate an image", text: $viewModel.userInput) {
Image(
Expand All @@ -44,28 +44,24 @@ struct ImagenScreen: View {
.focused($focusedField, equals: .message)
.onSubmit { sendOrStop() }

ScrollView {
let spacing: CGFloat = 10
LazyVGrid(columns: [
GridItem(.fixed(UIScreen.main.bounds.width / 2 - spacing), spacing: spacing),
GridItem(.fixed(UIScreen.main.bounds.width / 2 - spacing), spacing: spacing),
], spacing: spacing) {
ForEach(viewModel.images, id: \.self) { image in
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: UIScreen.main.bounds.width / 2 - spacing,
height: UIScreen.main.bounds.width / 2 - spacing)
.cornerRadius(12)
.clipped()
}
let spacing: CGFloat = 10
LazyVGrid(columns: [
GridItem(.fixed(UIScreen.main.bounds.width / 2 - spacing), spacing: spacing),
GridItem(.fixed(UIScreen.main.bounds.width / 2 - spacing), spacing: spacing),
], spacing: spacing) {
ForEach(viewModel.images, id: \.self) { image in

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using id: \.self with UIImage can cause performance issues because UIImage is a class. When images are regenerated, new UIImage instances cause SwiftUI to re-render the entire grid. Consider wrapping UIImage in a custom struct that conforms to Identifiable (e.g., using a UUID for the id) to improve UI performance.

Copy link
Contributor Author

@YoungHypo YoungHypo Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve tried to generate images multiple times in imagenscreen. The UI remained smooth without noticeable lag when re-rendering the image grid. If you think adjustments are necessary, I'll do that. @paulb777

Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: UIScreen.main.bounds.width / 2 - spacing,
height: UIScreen.main.bounds.width / 2 - spacing)
.cornerRadius(12)
.clipped()
}
.padding(.horizontal, spacing)
}
.padding(.horizontal, spacing)
}
if viewModel.inProgress {
ProgressOverlay()
}
.overlay(viewModel.inProgress ? ProgressOverlay() : nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make sure the progress spinner is centered on screen?
RocketSim_Screenshot_iPhone_16_Pro_6 3_2025-07-25_16 24 42

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also be good to refactor the ProgressOverlay into a separate file to encourage reuse.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve changed the layout of ProgressOverlay so that it covers the entire screen. Regarding the refactoring of ProgressOverlay, I’ll submit a new PR to address that. Thanks, Peter! @peterfriese
Simulator Screenshot - iPhone 16 Pro - 2025-07-27 at 23 27 05

}
.navigationTitle("Imagen example")
.onAppear {
Expand Down