HomeiOS Developmentios - deal with choice and replace in MVVM?

ios – deal with choice and replace in MVVM?


I am enjoying round with MVVM and have hassle wrapping my head round methods to work with deciding on a single factor.

Following on-line examples I’ve written one thing fundamental that fetches an inventory of information and shows it.

struct NoteListView: View {
    @State non-public var mannequin = NoteModel()
    
    var physique: some View {
        NavigationStack {
            Listing(mannequin.notes) { be aware in
                NavigationLink {
                    NoteDetailView(be aware: be aware)
                } label: {
                    Textual content(be aware.title)
                }
            }
            .process {
                mannequin.fetchNotes()
            }
        }
    }
}

struct NoteDetailView: View {
    let be aware: Observe
    
    var physique: some View {
        Textual content(be aware.title)
            .font(.title)
        Textual content(be aware.content material)
    }
}

@Observable
class NoteModel {
    var notes: [Note] = []
    
    func fetchNotes() {
        self.notes = [
            Note(title: "First note", content: "Test note"),
            Note(title: "Reminder", content: "Don't forget to water the plants!"),
            Note(title: "Shopping list", content: "Eggs, milk, hat, bread")
        ]
    }
}

struct Observe: Identifiable, Hashable {
    let id = UUID()
    var title: String
    var content material: String
}

Now I need to replace a be aware on the element view. Updating includes a PUT request to the server with the server calculating knowledge for the replace, so after the request is profitable the brand new model of the be aware must be fetched. I can not appear to determine methods to write this. I feel the mannequin would look one thing like this.

@Observable
class NoteModel {
    var notes: [Note] = []
    var selectedNote: Observe?
    
    func fetchNotes() {
        self.notes = [
            Note(title: "First note", content: "Test note"),
            Note(title: "Reminder", content: "Don't forget to water the plants!"),
            Note(title: "Shopping list", content: "Eggs, milk, hat, bread")
        ]
    }

    func fetchNote(title: String) {
        // fetch a single be aware, in all probability used after updating a be aware to show the up to date be aware on the small print view
        self.selectedNote = APIClient.fetchNote()
    }
    
    func updateNote(title: String, content material: String) {
        self.selectedNote?.title = title
        self.selectedNote?.content material = content material
        // makes a PUT request to replace the be aware, after which the be aware must be fetched
    }
}

However I do not know methods to move the chosen be aware to the small print view and methods to replace the be aware displayed on the small print view and on the checklist view. I think about that it is a pretty fundamental situation for MVVM, however I could not discover any examples illustrating the fundamental conventions on how to do that.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments