0

I'm try to make a ToDoApp using TCA and CoreData. When the user tap on a cell I would to change the complete attribute and change the value of the checkbox of the task cell.

struct MainViewModel: ReducerProtocol {

let loadElements: () -> [Tasks]

struct State: Equatable {
    var showForm: Bool = false
    var buttonIcon: String {
        showForm ? "minus" : "plus"
    }
    var tasks: [Tasks] = []
}

enum Action: Equatable {
    case showHideForm
    case loadElement
    case addElement(String)
    case toggleCompleted(Tasks)
}

func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
    switch action {
    case .showHideForm:
        state.showForm.toggle()
        return .none
    case .loadElement:
        state.tasks = loadElements()
        return .none
    case .addElement(let name):
        StorageContainer.shared.addElement(event: name)
        let effects: [EffectTask<MainViewModel.Action>] = [EffectTask(value: .showHideForm), EffectTask(value: .loadElement)]
        return .concatenate(effects)
    case .toggleCompleted(let task):
        StorageContainer.shared.update(task: task)
        return EffectTask(value: .loadElement)
    }
}

}

In the view I call the update action when the user tap on the cell

ForEach(vm.tasks, id: \.self) { task in
                            TaskCell(task: task)
                                .onTapGesture {
                                    vm.send(.toggleCompleted(task), animation: .easeInOut)
                                }
                                .listRowBackground(Color.clear)
                                .listRowSeparator(.hidden)
                        }

Here you can see how I update the entity in core data

func update(task: Tasks) {
    task.completed.toggle()
    save()
}

func save() {
    try? container.viewContext.save()
}
Fabio Cirruto
  • 71
  • 1
  • 7
  • 2
    Copy/paste code, not screenshots. – Larme Jun 19 '23 at 08:08
  • And describe your issue in more detail, “nothing change” isn’t very helpful for us. – Joakim Danielson Jun 19 '23 at 08:58
  • https://github.com/FabioCirruto/ToDoTCA here you can find all the project. When I tap on a task I would like to toggle the complete attribute and change the checkbox value of the cell. Thx for the help – Fabio Cirruto Jun 19 '23 at 09:08
  • No, you should post relevant code as text in your question. No images, no links to external pages. – Joakim Danielson Jun 19 '23 at 09:40
  • sorry, now I have changed my question – Fabio Cirruto Jun 19 '23 at 09:53
  • In SwiftUI you don't really need TCA because it has a build in reducer called `PreferenceKey` which is used by most of the modifiers. Also you don't need view model objects because that is the job of the `View` struct and property wrappers. – malhal Jun 19 '23 at 10:41

0 Answers0