0

I have a simple sheet:

.sheet(isPresented: $newContactSheetTrigger) {
 NewContactSheet()
 .presentationDetents([.large])
}

with the following code:

import SwiftUI
import SwiftData
import WidgetKit
struct NewContactSheet: View {
 
 @Environment(\.dismiss) private var dismiss
 @State private var contactName = ""
 @State private var newDaysDue: Set<String> = []
 @State private var favorite = false
 private let templatesHeight = UIScreen.main.bounds.height * 0.035
 private let dayWidth = UIScreen.main.bounds.width * 0.1
 private let weekdays: [String] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
 private let buttonBackground = Color(uiColor: .systemGroupedBackground)
 private let green85 = Color.green.opacity(0.85)
 private let green30 = Color.green.opacity(0.3)
 private let adaptiveBlack = Color("AdaptiveBlack")
 
 var body: some View {
 NavigationStack {
 Form {
 Section {
 TextField("Contact name", text: $contactName)
 HStack {
 Text("Templates:")
 .font(.footnote)
 .foregroundStyle(.secondary)
 
 ScrollView(.horizontal, showsIndicators: false) {
 LazyHStack {
 ForEach(NewContactTemplate.predefinedTemplates) { template in
 Button {
 if contactName == template.name {
 clearTemplate()
 } else {
 applyTemplate(template: template)
 }
 } label: {
 Text("\(template.name)")
 .padding(.horizontal)
 .font(.footnote)
 .frame(height: templatesHeight)
 .foregroundStyle(adaptiveBlack)
 }
 .background(buttonBackground, in: RoundedRectangle(cornerRadius: 10))
 .buttonStyle(.borderless)
 }
 }
 }
 .contentMargins(.horizontal, 0)
 }
 } header: {
 Text("Name")
 }
 Section {
 HStack (alignment: .center) {
 Spacer()
 ForEach (weekdays, id: \.self) { day in
 let containsCheck = newDaysDue.contains(day)
 Button {
 if favorite {
 // activeAlert = .correctDaysSelector
 // showAlert = true
 } else {
 if containsCheck {
 newDaysDue.remove(day)
 } else {
 newDaysDue.insert(day)
 }
 }
 } label: {
 Text(day)
 .font(.caption)
 .frame(width: dayWidth, height: templatesHeight)
 .background(
 containsCheck ?
 RoundedRectangle(cornerRadius: 10)
 .fill(green85)
 .overlay(
 RoundedRectangle(cornerRadius: 10)
 .stroke(.clear, lineWidth: 2)
 )
 
 :
 RoundedRectangle(cornerRadius: 10)
 .fill(.clear)
 .overlay(
 RoundedRectangle(cornerRadius: 10)
 .stroke(green30, lineWidth: 2)
 )
 )
 .foregroundStyle(favorite ? .gray : containsCheck ? .white : green85)
 }
 .buttonStyle(.plain)
 }
 Spacer()
 }
 
 HStack {
 Text("Presets:")
 .font(.footnote)
 .foregroundStyle(.secondary)
 
 ScrollView(.horizontal, showsIndicators: false) {
 LazyHStack {
 ForEach(NewContactDaysDue.predefinedTemplates) { template in
 Button {
 if newDaysDue.count == template.daycount {
 newDaysDue = []
 } else {
 newDaysDue = template.daysDue
 }
 } label: {
 Text("\(template.name)")
 .padding(.horizontal)
 .font(.footnote)
 .frame(height: templatesHeight)
 .foregroundStyle(adaptiveBlack)
 }
 .buttonStyle(.borderless)
 .background(buttonBackground, in: RoundedRectangle(cornerRadius: 10))
 }
 }
 }
 .contentMargins(.horizontal, 0)
 }
 } header: {
 Text("Meet")
 }
 Section {
 
 } header: {
 Text("xxx")
 }
 Section {
 
 } header: {
 Text("xxx")
 }
 Section {
 
 } header: {
 Text("xxx")
 }
 Section {
 
 } header: {
 Text("xxx")
 }
 Section {
 
 } header: {
 Text("xxx")
 }
 Section {
 
 } header: {
 Text("xxx")
 }
 Section {
 
 } header: {
 Text("xxx")
 }
 Section {
 
 } header: {
 Text("xxx")
 }
 }
 .scrollIndicators(.hidden)
 .toolbar {
 ToolbarItem(placement: .topBarLeading) {
 Button {
 dismiss()
 } label: {
 Text("Cancel")
 .foregroundStyle(.red)
 }
 }
 ToolbarItem(placement: .topBarTrailing) {
 Button {
 //implement save logic
 WidgetCenter.shared.reloadAllTimelines()
 dismiss()
 } label: {
 Text("Save")
 .foregroundStyle(.green)
 }
 }
 }
 .navigationTitle("New Contact")
 .navigationBarTitleDisplayMode(.inline)
 .navigationBarHidden(false)
 }
 
 }
 
 func applyTemplate(template: NewContactTemplate) {
 contactName = template.name
 }
 
 func clearTemplate() {
 contactName = ""
 }
}
#Preview {
 NewContactSheet()
}
struct NewContactTemplate: Identifiable {
 let id = UUID()
 let name: String
 let daysDue: Set<String>
}
extension NewContactTemplate {
 static let predefinedTemplates: [NewContactTemplate] = [
 NewContactTemplate(name: "Test1",
 daysDue: ["Mon", "Tue", "Wed"]),
 NewContactTemplate(name: "Test2",
 daysDue: ["Tue", "Wed", "Fri"]),
 NewContactTemplate(name: "Test3",
 daysDue: ["Sat", "Sun", "Mon"])
 ]
}
struct NewContactDaysDue: Identifiable {
 let id = UUID()
 let name: String
 let daysDue: Set<String>
 let daycount: Int
}
extension NewContactDaysDue {
 static let predefinedTemplates: [NewContactDaysDue] = [
 NewContactDaysDue(name: "Daily", daysDue: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], daycount: 7),
 NewContactDaysDue(name: "Weekdays", daysDue: ["Mon", "Tue", "Wed", "Thu", "Fri"], daycount: 5),
 NewContactDaysDue(name: "Weekend", daysDue: ["Sat", "Sun"], daycount: 2)
 ]
}

However when I tap on the button that triggers it I get a microhang in my profiler (testing on an actual device not simulator). enter image description here

No matter how much I try to optimise the code I can't get rid of it, any suggestions on how to avoid these microhangs?

I'm targeting iOS 17.0+

Any help would be very much appreciated

asked Aug 28 at 21:59
2
  • 3
    Well how about showing a minimal reproducible code that produces your issue, see: minimal code, not just some bits of a NavigationStack code. Commented Aug 28 at 22:40
  • Sure! I edited the question and added enough code to fully reproduce the issue Commented Aug 29 at 13:00

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.