Skip to content

Can't react to the state of a class instance? #48

@iwa

Description

@iwa

hello,

first let me prephrase that i'm a Swift newbie so it might just be a misunderstanding and skill issue

i'm trying to centralize my app state into its own class, so i can provide methods to mutate the data all over my app

tried to do something like below, but the app does not respond to the buttons input

am i doing something wrong or is it not possible / intended cause bad practice?

    @State var appState = AppState()

    var body: some View {
        content
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
        .border(.rounded)
    }

    private var content: some View {
        VStack {
            ForEach(appState.getTodoList(), id: \.id) { todo in
                HStack {
                    Text(" [\(todo.completed ? "x" : " ")] \(todo.id) - \(todo.title)")
                    Spacer()
                    Button("Complete") {
                        if let index = appState.getTodoList().firstIndex(where: { $0.id == todo.id }) {
                            appState.toggleTodoCompletion(at: index)
                        }
                    }
                    Button("Remove") {
                        if let index = appState.getTodoList().firstIndex(where: { $0.id == todo.id }) {
                            appState.removeTodo(at: index)
                        }
                    }
                }
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions