13.9 C
Canberra
Friday, January 23, 2026

Choice shouldn’t be updating when consumer selects row in multi-selected record


I’ve a Checklist that modifications a row’s colour to blue when the consumer selects it however would not use Checklist(choice:) for causes undisclosed. When the consumer pushes a button on the toolbar, it selects all the gadgets on the record in a grey colour, with the primary chosen merchandise nonetheless in blue (that is so this primary chosen merchandise can nonetheless have issues added to it whereas all the opposite gadgets are chosen):

Choice shouldn’t be updating when consumer selects row in multi-selected record

That is the code I am utilizing to spotlight the row to the proper colour:

            HStack
            {
                Button
                {
                    if (!merchandise.isSelected)

                    {
                        self.queriedItems.forEach { $0.isSelected = false }
                        merchandise.isSelected = true
                    }
                    
                    merchandise.lastSelectedDate = .now
                }
                label:
                {
                    Textual content(merchandise.identify)
                    .body(maxWidth: .infinity, alignment: .main)
                    .contentShape(Rectangle())
                }
                .buttonStyle(.plain)
            }
            .listRowBackground(merchandise.isSelected ?  ((self.itemIsLastSelected(merchandise: merchandise)) ? Colour.blue : Colour.grey) : nil)

Nevertheless, I am having points when all of the gadgets are chosen and I attempt to change the primary chosen merchandise. The code is working however the .listRowBackground line is not updating the brand new primary chosen merchandise row colour to blue. The one method to repair that is so as to add .id(merchandise.lastSelectedDate) to the HStack, that is how I’ve gotten it to work within the animated instance above. Merchandise is a mannequin object in SwiftData and must be observable, so I do not perceive why a change to an gadgets lastSelectedData is not triggering the replace. I’ve tried utilizing a computed property or doing the logic in a separate closure / perform, but it surely would not work.

Does anybody know the right way to repair this with out utilizing .id (which is inflicting a bit of little bit of a glitchy animation in my primary app)? Full code beneath:

import SwiftUI
import SwiftData

@Mannequin
remaining class Merchandise
{
    var identify: String
    
    var createdDate: Date = Date.now
    var lastSelectedDate: Date = Date.now
    var isSelected: Bool = false
    
    init(identify: String)
    {
        self.identify = identify
    }
}

struct ContentView: View
{
    @Surroundings(.modelContext) personal var modelContext
    @Question personal var queriedItems: [Item]

    var physique: some View
    {
        Checklist
        {
            Part
            {
                ForEach(self.queriedItems.sorted { $0.createdDate < $1.createdDate })
                {
                    merchandise in
                    
                    HStack
                    {
                        Button
                        {
                            if (!merchandise.isSelected)

                            {
                                self.queriedItems.forEach { $0.isSelected = false }
                                merchandise.isSelected = true
                            }
                            
                            merchandise.lastSelectedDate = .now
                        }
                        label:
                        {
                            Textual content(merchandise.identify)
                            .body(maxWidth: .infinity, alignment: .main)
                            .contentShape(Rectangle())
                        }
                        .buttonStyle(.plain)
                    }
                    .listRowBackground(merchandise.isSelected ?  ((self.itemIsLastSelected(merchandise: merchandise)) ? Colour.blue : Colour.grey) : nil)
                    //.id(merchandise.lastSelectedDate)
                }
            }
            header:
            {
                Button(motion:
                {
                    self.queriedItems.forEach { $0.isSelected = true }
                },
                label:
                {
                    Picture(systemName: "individual.2")
                })
                .buttonStyle(.plain)
            }
        }
        .onAppear
        {
            self.seedItems()
        }
    }
    
    personal func itemIsLastSelected(merchandise: Merchandise) -> Bool
    {
        if let lastSelectedItem = self.queriedItems.max(by: { $0.lastSelectedDate < $1.lastSelectedDate })
        {
            return lastSelectedItem == merchandise
        }
        else
        {
            return false
        }
    }
    
    personal func seedItems()
    {
        for merchandise in queriedItems
        {
            modelContext.delete(merchandise)
        }

        let names = [
            "Item 1",
            "Item 2",
            "Item 3",
            "Item 4",
            "Item 5"
        ]

        for identify in names
        {
            modelContext.insert(Merchandise(identify: identify))
        }
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles