SwiftUI Fix Multi Value update

In the next code I have tow views “SelectSymbolView” which contains “SymbolCategorySection” , the issue is in “SymbolCategorySection” which update “selectedSymbol” four times when user tap on a symbol to select it even when I ensure to not update “selectedSymbol” until its necessary.

The SelectSymbolView:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>struct SelectSymbolView: View {
// MARK: - Properties
@Binding var selectedSymbol: String
@State private var selectedCategory: SymbolCategory = .cloths
@State private var dismissView = false
let categories = SymbolCategory.allCases
let symbolGridColumns = [GridItem(.adaptive(minimum: 48, maximum: 48), spacing: 11, alignment: .center)]
@Environment(.dismiss) private var dismiss
// MARK: - Body
var body: some View {
NavigationStack {
ScrollViewReader { scrollProxy in
ScrollView {
LazyVStack(spacing: 18) {
ForEach(categories, id: .self) { category in
SymbolCategorySection(selectedSymbol: $selectedSymbol, category: category)
.id(category.title)
}
.padding(.horizontal, 16)
}
}
.offset(y: 16)
.overlay(alignment: .bottom) {
CategoryTapBar(selectedCategory: $selectedCategory, categories: categories)
.offset(y: -22)
}
.onChange(of: selectedCategory, initial: false) { _, _ in
withAnimation(.smooth(duration: 0.3)) {
scrollProxy.scrollTo(selectedCategory.title, anchor: .top)
}
}
.onChange(of: selectedSymbol, initial: false, { _, _ in
dismissView = true
})
.task(id: selectedSymbol, priority: .userInitiated) {
guard dismissView else { return }
try? await Task.sleep(seconds: 0.7)
dismiss()
}
}
.navigationTitle("Select Symbol")
.navigationBarTitleDisplayMode(.inline)
.background(Color(.systemGroupedBackground))
.interactiveDismissDisabled(true)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
CancelButton(isToolbarItem: true) { dismiss() }
}
}
}
}
}
</code>
<code>struct SelectSymbolView: View { // MARK: - Properties @Binding var selectedSymbol: String @State private var selectedCategory: SymbolCategory = .cloths @State private var dismissView = false let categories = SymbolCategory.allCases let symbolGridColumns = [GridItem(.adaptive(minimum: 48, maximum: 48), spacing: 11, alignment: .center)] @Environment(.dismiss) private var dismiss // MARK: - Body var body: some View { NavigationStack { ScrollViewReader { scrollProxy in ScrollView { LazyVStack(spacing: 18) { ForEach(categories, id: .self) { category in SymbolCategorySection(selectedSymbol: $selectedSymbol, category: category) .id(category.title) } .padding(.horizontal, 16) } } .offset(y: 16) .overlay(alignment: .bottom) { CategoryTapBar(selectedCategory: $selectedCategory, categories: categories) .offset(y: -22) } .onChange(of: selectedCategory, initial: false) { _, _ in withAnimation(.smooth(duration: 0.3)) { scrollProxy.scrollTo(selectedCategory.title, anchor: .top) } } .onChange(of: selectedSymbol, initial: false, { _, _ in dismissView = true }) .task(id: selectedSymbol, priority: .userInitiated) { guard dismissView else { return } try? await Task.sleep(seconds: 0.7) dismiss() } } .navigationTitle("Select Symbol") .navigationBarTitleDisplayMode(.inline) .background(Color(.systemGroupedBackground)) .interactiveDismissDisabled(true) .toolbar { ToolbarItem(placement: .topBarLeading) { CancelButton(isToolbarItem: true) { dismiss() } } } } } } </code>
struct SelectSymbolView: View {
    
    // MARK: - Properties
    @Binding var selectedSymbol: String
    @State private var selectedCategory: SymbolCategory = .cloths
    @State private var dismissView = false
    let categories = SymbolCategory.allCases
    let symbolGridColumns = [GridItem(.adaptive(minimum: 48, maximum: 48), spacing: 11, alignment: .center)]
    @Environment(.dismiss) private var dismiss
    
    // MARK: - Body
    var body: some View {
        NavigationStack {
            ScrollViewReader { scrollProxy in
                ScrollView {
                    LazyVStack(spacing: 18) {
                        ForEach(categories, id: .self) { category in
                            SymbolCategorySection(selectedSymbol: $selectedSymbol, category: category)
                                .id(category.title)
                        }
                        .padding(.horizontal, 16)
                    }
                }
                .offset(y: 16)
                .overlay(alignment: .bottom) {
                    CategoryTapBar(selectedCategory: $selectedCategory, categories: categories)
                        .offset(y: -22)
                }
                .onChange(of: selectedCategory, initial: false) { _, _ in
                    withAnimation(.smooth(duration: 0.3)) {
                        scrollProxy.scrollTo(selectedCategory.title, anchor: .top)
                    }
                }
                .onChange(of: selectedSymbol, initial: false, { _, _ in
                    dismissView = true
                })
                .task(id: selectedSymbol, priority: .userInitiated) {
                    guard dismissView else { return }
                    try? await Task.sleep(seconds: 0.7)
                    dismiss()
                }
            }
            .navigationTitle("Select Symbol")
            .navigationBarTitleDisplayMode(.inline)
            .background(Color(.systemGroupedBackground))
            .interactiveDismissDisabled(true)
            .toolbar {
                ToolbarItem(placement: .topBarLeading) {
                    CancelButton(isToolbarItem: true) { dismiss() }
                }
            }
        }
    }
    
}

The SymbolCategorySection view

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>extension SelectSymbolView {
struct SymbolCategorySection: View {
// MARK: - Properties
@Binding var selectedSymbol: String
let category: SymbolCategory
let symbolGridColumns = [GridItem(.adaptive(minimum: 48, maximum: 48), spacing: 11, alignment: .center)]
// MARK: - Body
var body: some View {
LazyVStack(alignment: .leading, spacing: 10) {
// Section Title
HStack(alignment: .center, spacing: 12) {
Image(systemName: category.symbolName)
.font(.title3)
.fontWeight(.semibold)
.frame(width: 24, height: 24)
Text(category.title)
.font(.title2)
.fontWeight(.bold)
}
.foregroundStyle(Color(.secondaryLabel))
.padding(.horizontal, 8)
// Section Symbols
LazyVGrid(columns: symbolGridColumns, alignment: .center, spacing: 11) {
ForEach(category.symbols, id: .self) { symbolData in
TaskSymbol(systemName: symbolData.systemName,
style: .selection,
color: symbolColor(for: symbolData))
.overlay(alignment: .bottomTrailing, content: {
if symbolData.isPlus {
AppPlus(type: .symbol)
}
})
.onTapGesture {
select(symbolData.systemName)
}
}
}
.padding(.vertical, 9)
.background(SettingItemBackground())
}
.sensoryFeedback(.selection, trigger: selectedSymbol)
.onChange(of: selectedSymbol, initial: false) { oldValue, newValue in
print("🔶 selected Symbol changed") <<< Use it to indicate update times
}
}
// MARK: - Actions
private func select(_ symbol: String) {
if selectedSymbol != symbol {
withAnimation(.smooth(duration: 0.3)) {
selectedSymbol = symbol
}
}
}
private func symbolColor(for symbolData: SymbolData) -> Color {
if selectedSymbol == symbolData.systemName {
return Color(.systemBlue)
}
if symbolData.isPlus {
return Color(.secondaryLabel)
}
return Color(.charcoalGray)
}
}
}
</code>
<code>extension SelectSymbolView { struct SymbolCategorySection: View { // MARK: - Properties @Binding var selectedSymbol: String let category: SymbolCategory let symbolGridColumns = [GridItem(.adaptive(minimum: 48, maximum: 48), spacing: 11, alignment: .center)] // MARK: - Body var body: some View { LazyVStack(alignment: .leading, spacing: 10) { // Section Title HStack(alignment: .center, spacing: 12) { Image(systemName: category.symbolName) .font(.title3) .fontWeight(.semibold) .frame(width: 24, height: 24) Text(category.title) .font(.title2) .fontWeight(.bold) } .foregroundStyle(Color(.secondaryLabel)) .padding(.horizontal, 8) // Section Symbols LazyVGrid(columns: symbolGridColumns, alignment: .center, spacing: 11) { ForEach(category.symbols, id: .self) { symbolData in TaskSymbol(systemName: symbolData.systemName, style: .selection, color: symbolColor(for: symbolData)) .overlay(alignment: .bottomTrailing, content: { if symbolData.isPlus { AppPlus(type: .symbol) } }) .onTapGesture { select(symbolData.systemName) } } } .padding(.vertical, 9) .background(SettingItemBackground()) } .sensoryFeedback(.selection, trigger: selectedSymbol) .onChange(of: selectedSymbol, initial: false) { oldValue, newValue in print("🔶 selected Symbol changed") <<< Use it to indicate update times } } // MARK: - Actions private func select(_ symbol: String) { if selectedSymbol != symbol { withAnimation(.smooth(duration: 0.3)) { selectedSymbol = symbol } } } private func symbolColor(for symbolData: SymbolData) -> Color { if selectedSymbol == symbolData.systemName { return Color(.systemBlue) } if symbolData.isPlus { return Color(.secondaryLabel) } return Color(.charcoalGray) } } } </code>
extension SelectSymbolView {
    
    struct SymbolCategorySection: View { 
        
        // MARK: - Properties
        @Binding var selectedSymbol: String
        let category: SymbolCategory
        let symbolGridColumns = [GridItem(.adaptive(minimum: 48, maximum: 48), spacing: 11, alignment: .center)]
        
        // MARK: - Body
        var body: some View {
            LazyVStack(alignment: .leading, spacing: 10) {
                // Section Title
                HStack(alignment: .center, spacing: 12) {
                    Image(systemName: category.symbolName)
                        .font(.title3)
                        .fontWeight(.semibold)
                        .frame(width: 24, height: 24)
                    Text(category.title)
                        .font(.title2)
                        .fontWeight(.bold)
                }
                .foregroundStyle(Color(.secondaryLabel))
                .padding(.horizontal, 8)
                // Section Symbols
                LazyVGrid(columns: symbolGridColumns, alignment: .center, spacing: 11) {
                    ForEach(category.symbols, id: .self) { symbolData in
                        TaskSymbol(systemName: symbolData.systemName,
                                   style: .selection,
                                   color: symbolColor(for: symbolData))
                        .overlay(alignment: .bottomTrailing, content: {
                            if symbolData.isPlus {
                                AppPlus(type: .symbol)
                            }
                        })
                        .onTapGesture {
                            select(symbolData.systemName)
                        }
                    }
                }
                .padding(.vertical, 9)
                .background(SettingItemBackground())
            }
            .sensoryFeedback(.selection, trigger: selectedSymbol)
            .onChange(of: selectedSymbol, initial: false) { oldValue, newValue in
                print("🔶 selected Symbol changed") <<< Use it to indicate update times
            }
        }
        
        // MARK: - Actions
        private func select(_ symbol: String) {
            if selectedSymbol != symbol {
                withAnimation(.smooth(duration: 0.3)) {
                    selectedSymbol = symbol
                }
            }
        }
        
        private func symbolColor(for symbolData: SymbolData) -> Color {
            if selectedSymbol == symbolData.systemName {
                return Color(.systemBlue)
            }
            if symbolData.isPlus {
                return Color(.secondaryLabel)
            }
            return Color(.charcoalGray)
        }
        
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật