Tuesday, March 26, 2024
HomeiOS Developmentios - How do I modify ViewModel knowledge from button in swiftui

ios – How do I modify ViewModel knowledge from button in swiftui


It appears I’m solely loosing an idea of MVVM knowledge stream:
I’ve a easy instance view, displaying array of Doc objects, provided by ViewModel class that must be a singleton, accessed from a number of locations.
Every merchandise has identify and a button, that ought to change it is “isFav” worth, and mirror the change by way of colour change of the stated button.

I attempted altering it through vm operate, through struct mutating func, the direct approach, however each I provide you with provides me a distinct error.
What am I doing improper?

import SwiftUI

struct Doc: Identifiable
{
    let id: UUID
    var identify: String
    var isFav: Bool = false
    
    init(_ identify: String)
    {
        self.id = UUID()
        self.identify = identify
    }
    
    init()
    {
        self.id = UUID()
        self.identify = id.uuidString.dropLast(13).lowercased()
    }
    
    mutating func updateIsFav()
    {
        self.isFav.toggle()
    }
}

class ViewModel: ObservableObject
{
    static var shared = ViewModel()
    
    non-public init() {}
    @Printed var array: [Doc] = []

    func updateIsFav(for doc: Doc)
    {
        if let index = array.firstIndex(the place: { $0.id == doc.id} )
        {
            self.array[index].isFav.toggle()
        }
    }
}

struct ContentView: View
{    
    @ObservedObject var mannequin = ViewModel.shared
    var physique: some View
    {
        ZStack
        {
            Coloration.black.ignoresSafeArea()
            ScrollView
            {
                VStack
                {
                    ForEach( mannequin.array, id: .id )
                    {
                        doc in
                        HStack
                        {
                            Textual content( doc.identify )
                                .foregroundColor(.white)
                            
                            Button(motion:
                                    {
                                        doc.updateIsFav()           // Can't use mutating member on immutable worth: 'doc' is a 'let' fixed
                                        doc.isFav.toggle            // Can't reference 'mutating' methodology as operate worth
                                        mannequin.updateisFav(for: doc) // Can't name worth of non-function kind 'Binding<Topic>'
                                    },
                                   label:
                                    {
                                            Picture(systemName: "star")
                                        .foregroundColor(doc.isFav ? .yellow : .white)
                                    }
                            )
                           

                        }
                    }
                }
            }
        }
        .onAppear()
        {
            mannequin.array.append(Doc())
            mannequin.array.append(Doc())
            mannequin.array.append(Doc())
            mannequin.array.append(Doc())
            mannequin.array.append(Doc())
            mannequin.array.append(Doc())
            mannequin.array.append(Doc())
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View 
    {
        ContentView()
            .previewDevice("iPhone 13")
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments