How to pass a varible to AppIntent in Swift

55 Views Asked by At

This is a view which contains 'data' variable which is passed to trignext AppIntent. Kindly help me pass it to AppIntent and update its value with the data coming from method channel.

import SwiftUI

struct widget_testEntryView: View {
    @State var data: String = "Leja Re"
    var entry: Provider.Entry

    var body: some View {
        ZStack {
            Color(UIColor(red: 0x73, green: 0x46, blue: 0x56))
            VStack(alignment: .leading) {
                Spacer().frame(height: 40)
                HStack {
                    Spacer().frame(width: 20)
                    Image("Image")
                        .resizable()
                        .frame(width: 60, height: 60)
                        .cornerRadius(10)
                    VStack {
                        HStack {
                            Spacer()
                            Image(systemName: "backward.end.fill")
                                .foregroundColor(.white)
                                .frame(width: 20, height: 20)
                            Spacer().frame(width: 20)
                        }
                        Spacer()
                    }
                }
                HStack {
                    Spacer().frame(width: 20)
                    VStack {
                        Spacer().frame(width: 0, height: 2)
                        Text(data)
                            .font(.system(size: 15))
                            .foregroundColor(.white)
                        Spacer().frame(width: 0, height: 2)
                        Text("Dhvani")
                            .font(.system(size: 15))
                            .foregroundColor(.gray)
                        Spacer().frame(width: 0, height: 5)
                    }
                }
                HStack {
                    Spacer().frame(width: 25)
                    Button(action: {
                        // Handle Previous button action
                    }) {
                        Label("Previous", systemImage: "backward.fill")
                            .padding()
                            .foregroundColor(.white)
                            .frame(width: 10, height: 10)
                    }
                    Spacer()
                    Button(action: {
                        // Handle Pause button action
                    }) {
                        Label("Pause", systemImage: "pause.fill")
                            .padding()
                            .foregroundColor(.white)
                            .frame(width: 10, height: 10)
                    }
                    Spacer()
                    Button(action: {
                        handleNext()
                    }) {
                        Label("Next", systemImage: "forward.fill")
                            .padding()
                            .foregroundColor(.white)
                            .frame(width: 10, height: 10)
                    }
                    Spacer().frame(width: 25)
                }
                Spacer().frame(height: 20)
            }
        }
    }

    func handleNext() {
        Task {
            do {
                let result = try await trignext(data: $data).perform()
            } catch {
                print("Error handling next: \(error)")
            }
        }
    }
}

This is the AppIntent which updates the value of data variable, here its giving error when @binding is used, it does not allow to pass the data variable.

import Intents

struct trignext: AppIntent {
    static var title: LocalizedStringResource = "Next Song"
    static var description = IntentDescription("Orders a soup from your favorite restaurant.")
    
    @Binding var data: String

    init(data: Binding<String>) {
        _data = data
    }

    func perform() async throws -> some IntentResult {
        let updatedData = try await NativeMethodChannel.triggerNext()
        
        DispatchQueue.main.async {
            data = updatedData ?? "Default Data"
        }
        
        return .result(value: data)
    }
}

This is the AppIntent which updates the value of data variable, here its giving error when @binding is used, it does not allow to pass the data variable.

0

There are 0 best solutions below