HomeiOS DevelopmentChoice is just not updating when consumer selects row in multi-selected listing

Choice is just not updating when consumer selects row in multi-selected listing


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

Choice is just not updating when consumer selects row in multi-selected listing

That is the code I am utilizing to focus on 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.title)
                    .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 objects are chosen and I attempt to change the principle 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 needs to be observable, so I do not perceive why a change to an objects lastSelectedData is not triggering the replace. I’ve tried utilizing a computed property or doing the logic in a separate closure / operate, however it does not work.

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

import SwiftUI
import SwiftData

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

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

    var physique: some View
    {
        Record
        {
            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.title)
                            .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: "particular person.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 title in names
        {
            modelContext.insert(Merchandise(title: title))
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments