1

enter image description here

Let me begin by saying that I am quite new to programming, I have little to no experience. I need to make a to-do list for a school project and really need help.

I am trying to make a to-do list with a more aesthetic layout, so rather than make it a list view, I made it a LazyVStack embedded into a ScrollView. Now I am trying to come up with a way to:

a. delete different to-dos; I had tried to do this using an EditButton(), but because it isn't a list view I was using, I can't use the simple: .onDelete modifier.

b. check items off of the to-do list. Basically the same thing as above, just when pressing the little check button for each to-do.

I tried to use a Boolean state variable: @State private var showingGroupBox: Bool = true, and then toggling this when the button is pressed, but for some reason, that didn't work.

I also tried a deleteTodo function (see the second block of code), to no avail.

Therefore, I would really appreciate it if somebody could help me with the following things:

a. when pressing the EditButton() in the navigation bar, that there is a way to delete the todos.

b. that when pressing the little check box in each todo, the todo is deleted/ no longer stored with CoreData.

Here is the code for my to-do list:

import SwiftUI
import CoreData

struct ContentView: View {
    
    let persistenceController = PersistenceController.shared
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(entity: Todo.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Todo.name, ascending: true)])
    var todos: FetchedResults<Todo>
    
    @State private var showingNewToDoPanel: Bool = false
  //  @State private var showingGroupBox: Bool = true
    
    var body: some View {
        NavigationView {
            ScrollView {
                LazyVStack {
                    ForEach(self.todos, id: \.self) { todo in
                        
//                        if isShowingGroupBox { could be used for a "Clear all" functionality, where when the user clicks on a button, all of the todos disappear.
                            GroupBox {
                            HStack {
                                
                                CheckBoxView()
                                Text(todo.name ?? "Untitled").foregroundColor(.black)
                                Spacer()
                                Text(todo.priority ?? "No priority")
                            }
                        }//: GROUP BOX
                        .clipShape(Capsule())
                        .shadow(color: Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.25)), radius:4, x:0, y:4)
                            
//                        }//:CONDITIONAL
                    }
                }//: LAZYVSTACK
                .padding(.horizontal, 20)
                .padding(.top, 20)
                .navigationBarTitle("To-Dos", displayMode: .automatic)
                .navigationBarItems(leading:
                                      EditButton(),
                    trailing:
                
                                    Button(action: {
                                        
                                        self.showingNewToDoPanel.toggle()
                                        
                                    }, label: {
                                            Image(systemName: "plus")
                                        }) //:BUTTON
                                        .sheet(isPresented: $showingToDoView, content: {
                                            ToDoView().environment(\.managedObjectContext, self.managedObjectContext)
                                        })
                
                
            )
            }//: SCROLLVIEW
        }//: NAVIGATIONVIEW
    }
    
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
    }
}

And here is my code for the button.

import SwiftUI
import CoreData

struct CheckBoxView: View {
    
//    @State private var isShowingToDo: Bool = true "CLEAR ALL"-esque FUNCTIONALITY

    let persistenceController = PersistenceController.shared
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(entity: Todo.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Todo.name, ascending: true)])
    var todos: FetchedResults<Todo>
    
    func deleteTodo(at offsets: IndexSet) {
      for index in offsets {
        let todo = todos[index]
        managedObjectContext.delete(todo)

        do {
          try managedObjectContext.save()
        } catch {
          print(error)
        }
      }
    }
    
    
    var body: some View {
        
        GeometryReader() { geometry in
        Button(action: {
            
//            self.isShowingToDo.toggle()
            
           deleteTodo()
            
        }, label: {
                RoundedRectangle(cornerRadius: 4)
                    .fill(Color(#colorLiteral(red: 0.978395164, green: 0.9725784659, blue: 0.9828661084, alpha: 1)))
                    .frame(width: geometry.size.height / 0.8, height: geometry.size.height / 0.8)
                    .shadow(color: Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.25)), radius:1, x:0, y:1)
        })
        }
    }
}
  • Does this answer your question https://stackoverflow.com/a/60893462/12299030? or https://stackoverflow.com/a/63833510/12299030? – Asperi Jan 06 '21 at 04:54
  • Hey, @Asperi, thank you for your reply. That is more or less what I need, but I am really struggling to make that work for a todo when it is stored in CoreData and all... Do you could think you could help with that? – William March-Kramer Jan 07 '21 at 12:14

0 Answers0