How can i make an AppIntent modify a variable declared in ContentView?

39 views Asked by At

I want to modify this list named texts from my AppIntents page but it gives me the error "Instance member 'model' of type 'ContentView' cannot be used on instance of nested type 'ContentView.startcanvas'".

Here is my code:

import SwiftUI
import AppIntents

class TextsModel: ObservableObject {
    @Published var texts: [String] = []
}



struct ContentView: View {
    @StateObject private var model = TextsModel()

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            ForEach(model.texts, id: \.self) { text in
                Text(text)
            }
            Button("metro") {
                model.texts.append("New Text \(model.texts.count + 1)")
            }
        }
        .padding()
    }
    
    struct startcanvas: AppIntent {
        static var title: LocalizedStringResource = "Start Canvas"

        @MainActor
        func perform() async throws -> some IntentResult {
            model.texts.append("New Text \(model.texts.count + 1)")
            return .result()
        }
      
        static var openAppWhenRun: Bool = true
    }
}

#Preview {
    ContentView()
}

I have tried to make it a public var declared out of any struct but that wouldn't allow anything to modify the variable, only read it.

0

There are 0 answers