create a note taking app using swiftui and store notes using userdefaults in swift, cancel notes with drag gesture and allow to open the note to edit it in swift

To create a note taking app using SwiftUI, you can start by creating a struct for the note object which will store the title, content, and other necessary properties.

Next, you can use UserDefaults to store the notes. To do this, you can create a class that handles the saving and retrieval of the notes using the Codable protocol to encode and decode the notes.

To enable the canceling of a note using a drag gesture, you can use the onDelete method of List view which invokes when the user swipes left on an item in the list.

To allow the user to edit a note, you can create a new view that shows the details of the note and allows for editing. You can then pass the note object to this view and update it when changes are made.

Here is a sample implementation of a note taking app in SwiftUI:

main.swift
// Note struct to store the properties of the note
struct Note: Codable, Identifiable {
    let id = UUID()
    var title: String
    var content: String
}

// Note Manager class to handle the saving and retrieval of the notes from UserDefaults
class NoteManager {
    static let shared = NoteManager()
    
    let key = "notes"
    var notes: [Note] {
        get {
            guard let data = UserDefaults.standard.data(forKey: key) else { return [] }
            
            if let decoded = try? JSONDecoder().decode([Note].self, from: data) {
                return decoded
            }
            
            return []
        }
        set {
            if let encoded = try? JSONEncoder().encode(newValue) {
                UserDefaults.standard.set(encoded, forKey: key)
            }
        }
    }
    
    // Method to add a new note to the list
    func add(note: Note) {
        var existingNotes = notes
        existingNotes.append(note)
        notes = existingNotes
    }
    
    // Method to remove a note from the list
    func remove(note: Note) {
        var existingNotes = notes
        existingNotes.removeAll { $0.id == note.id }
        notes = existingNotes
    }
}

// View that shows the list of notes
struct NoteListView: View {
    @State private var notes = NoteManager.shared.notes
    
    var body: some View {
        NavigationView {
            List {
                ForEach(notes) { note in
                    NavigationLink(
                        destination: NoteDetailView(note: note),
                        label: { Text(note.title) }
                    )
                }
                .onDelete(perform: deleteNotes)
            }
            .navigationBarTitle("Notes")
            .navigationBarItems(trailing: Button(action: addNote) {
                Image(systemName: "plus")
            })
        }
    }
    
    // Method to delete notes with the swipe gesture
    func deleteNotes(at offsets: IndexSet) {
        offsets.forEach { index in
            let note = notes[index]
            NoteManager.shared.remove(note: note)
        }
        notes = NoteManager.shared.notes
    }
    
    // Method to add a new note to the list
    func addNote() {
        let note = Note(title: "New Note", content: "")
        NoteManager.shared.add(note: note)
        notes = NoteManager.shared.notes
    }
}

// View that shows the details of the note and allows editing
struct NoteDetailView: View {
    @State private var note: Note
    
    var body: some View {
        Form {
            Section(header: Text("Title")) {
                TextField("", text: $note.title)
            }
            
            Section(header: Text("Content")) {
                TextEditor(text: $note.content)
            }
        }
        .navigationBarTitle(note.id.uuidString)
        .onDisappear(perform: updateNote)
    }
    
    // Method to update the note when changes are made
    func updateNote() {
        NoteManager.shared.remove(note: note)
        NoteManager.shared.add(note: note)
    }
}
3066 chars
108 lines

gistlibby LogSnag