ios – Learn how to replace computed property when Information Mannequin modifications

0
35


class TodoItem : Identifiable {
    
    var id: UUID
    
    var itemDescription: String
    var itemCreatedAt: Date
    
    var isComplete: Bool
    
    init(textual content: String){
        self.id = UUID()
        self.itemDescription = textual content
        self.itemCreatedAt = Date.now
        self.isComplete = false
    }
    
}

And a corresponding Repository Class

class TodosRepository : ObservableObject{
    
    @Printed var todos: [TodoItem] = []
    
    var completedTodos: [TodoItem] {
        
        let accomplished = todos.filter({
            $0.isComplete == true
        })
        return accomplished
    }
    
    init()
    {
    }
    
    func addTodo(textual content: String)
    {
        let todo = TodoItem(textual content: textual content)
        
        todos.append(todo)
    }
}

Then I’ve a view that has an ObservedObject and reveals the todos.

struct MainView: View {
    
    @ObservedObject var todosRepository: TodosRepository
    
    @State non-public var newTodoText: String = ""
    
    var physique: some View {
        
        
        ZStack {
            
            Shade(.darkGray)
                .ignoresSafeArea()
            
            VStack {
                
                HStack {
                    Textual content("Depend: (todosRepository.completedTodos.rely)")
                        .font(.title)
                        .foregroundColor(.white)
                    
                    Spacer()
                }
                .padding(.backside, 10)
                
                ForEach(todosRepository.todos){ todo in
                    TodoListItemView(todoItem: todo)
                        .foregroundColor(.white)
                        .padding(.backside, 10)
                    
                    Divider()
                        .background(Shade(.lightGray))
                }
                
                Spacer()
                
                HStack {
                    TextField("New Todo Entry", textual content: $newTodoText)
                        .padding(10)
                        .foregroundColor(.blue)
                        .background {
                            Shade(.white)
                                .cornerRadius(10)
                        }
                    
                    Button("Add", motion: {
                        todosRepository.addTodo(textual content: newTodoText)
                        print(todosRepository.todos)
                    })
                    .padding(10)
                    .foregroundColor(.white)
                    .background {
                        Shade(.blue)
                            .cornerRadius(10)
                    }
                }
                
            }
            .padding()
            
        }
        
        
        
        
        
    }
}

The Merchandise View seems to be like this with a Toggle Button

struct TodoListItemView: View {
    
    @State var todoItem: TodoItem
    
    var physique: some View {
        

        HStack {
            VStack(alignment: .main) {
                Textual content(todoItem.itemDescription)
                    .font(.headline)
                Textual content(todoItem.itemCreatedAt.debugDescription)
                    .fontWeight(.skinny)
                    .font(.subheadline)
            }
            Spacer()
            
            Toggle(isOn: $todoItem.isComplete) {}
                .body(width: 50)
                
        }
    }
}

I’ve a View (iOS App) that represents a TodoList which accommodates a Listing of Todos from a DataModel. The TodoItemListView has a Toggle Button linked to the isCompleted property of the Todoitem Class. I would like the MainView first Textbox that reveals what number of accomplished Objects I’ve to replace once I toggle the button. Proper now the Textual content within the MainView will not be up to date, I believe in some way the computed Property for the completedTodos will not be working correctly however I’m not positive, I’m a newbie in Swift and iOS Improvement. Nonetheless if I full just a few Todos after which add a brand new Todo to the Listing the Quantity is accurately up to date. It simply doesn’t replace once I press the Toggle. How can I replace the “Accomplished Todos Quantity” as quickly as I toggle among the Objects.