Wednesday, April 10, 2024
HomeiOS Developmentios - SwiftUI Boolean not updating in view however updating in struct

ios – SwiftUI Boolean not updating in view however updating in struct


I am fairly new to SwiftUI and I’m constructing an utility that breaks down duties into subtasks utilizing AI, however I am having points displaying mentioned Subtasks. The present performance I would like is for the consumer to have the ability to tick each subtask that they full and it persists all through the applying, I’m utilizing SwiftData for this. I’ve the fundamental UI for the guidelines carried out however the isCompleted property simply doesn’t need to toggle.

THe toggle() perform produces the identical outcomes however together with the mutating perform helps me to debug this

I included debug prints within the code to debug the adjustments to the variable.

Right here is the code for the duty and subtask class/struct:

@Mannequin
class BlendedTask: Codable, Identifiable {
    var id = UUID()
    let identify: String
    var subtasks: [Subtask]
    
    var job: UserTask {
        return UserTask(id: id, identify: identify, period: 3, startTime: nil, precedence: .medium, imageURL: "twister", particulars: nil, pomodoro: true, pomodoroCounter: 0, blended: true)
    }
    
    non-public enum CodingKeys: String, CodingKey {
        case id, identify, subtasks
    }
    
    //  Initializers
    
    init(identify: String, subtasks: [Subtask]) {
        self.identify = identify
        self.subtasks = subtasks
    }
    
    init() {
        self.id = UUID()
        self.identify = ""
        self.subtasks = []
    }
    
    required init(from decoder: Decoder) throws {
        let container = strive decoder.container(keyedBy: CodingKeys.self)
        identify = strive container.decode(String.self, forKey: .identify)
        subtasks = strive container.decode([Subtask].self, forKey: .subtasks)
    }
    
    
}
// Subtask
struct Subtask: Codable, Identifiable, Hashable{
    var id = UUID()
    let identify: String
    var particulars: [Detail]

    non-public enum CodingKeys: String, CodingKey {
        case id, identify, particulars
    }
}
// Subtask particulars
struct Element: Codable, Identifiable, Hashable {
    var id = UUID()
    var description: String
    var isCompleted: Bool
    
    init(description: String) {
        self.description = description
        self.isCompleted = false
    }
    
    mutating func toggleCompleted() {
        print("-struct OLD: (self.isCompleted)")
        self.isCompleted.toggle()
        print("-struct NEW: (self.isCompleted)")
        print("toggled for element: (self.description)")
    }
}

And right here is the implementation within the view to indicate the small print:

struct BlendedTaskDetailView: View {
    @Surroundings(.dismiss) non-public var dismiss

    @State non-public var isAnimated = false
    @State var blendedTask: BlendedTask
    
    var physique: some View {
        
        ZStack {
            Colour.BG.ignoresSafeArea()
            VStack {
                Textual content("Activity: " + blendedTask.identify)
                    //modifiers
                
                Listing {
                    ForEach(Array($blendedTask.subtasks.enumerated()), id: .1.id) { index, $subtask  in
                        SubtaskCell(subtask: $subtask, taskNo: index + 1)
                    }
                    .listRowBackground(Colour.faPurple)
                }
                // Modifiers
            }
            .padding(.prime, 30)
            
        }
    }
    
        
}
struct SubtaskCell: View {
    @Binding var subtask: Subtask
    let taskNo: Int
    
    @State non-public var isAnimated = false
    
    var physique: some View {
        VStack(alignment: .middle) {
            Textual content("Activity (taskNo): (subtask.identify)")
                // modifiers
            
            ForEach(subtask.particulars.indices, id: .self) { j in
                DetailRow(element: $subtask.particulars[j])
            }
            
        }
        //modifiers
    }
}
struct DetailRow: View {
    @Binding var element: Element
    @State non-public var isAnimated = false
    var physique: some View {
        HStack {
            VStack(alignment: .main) {
                Textual content(element.description)
            }
            Spacer()
            
            Button(motion: {
                withAnimation {
                    print("-view OLD: (element.isCompleted)")
                    element.toggleCompleted()
                    print("-view NEW: (element.isCompleted)")
                }
            }, label: {
                Picture(systemName: element.isCompleted ? "checkmark.circle.fill" : "circle")
                    .contentTransition(.symbolEffect(.change))
                
            })
            .body(width: 35, peak: 35)
            .foregroundStyle(element.isCompleted ? .inexperienced : .white)
            
        }
       //modifiers
    }
}

Here’s what the UI seems to be like:

That is what’s printed when a button is pressed:



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments