Array unchangable on event call

Viewed 41

I'm trying to create a Floating action button in my application, that when pressed adds actors to a panel. I basically just add an actor to an array which is rendered to the view. When adding this functionality directly to a button everything works.

However, to keep things decoupled I tried to implement my own basic event system. So I have a class with a static function that loops over all the objects that are listening for the event and when the sub button of the floating action button is pressed, I am calling that static function. I know that the function by the listener is called since I've tried printing in the same function. But somehow there is not any new actor added to the array.

The class that sends out events to listeners:

class IndquestHandler{

static var actorEventListeners: Array<ActorEventListener> = Array()

public static func addListener(listener : ActorEventListener){
    actorEventListeners.append(listener)
}

public static func addActor(){
    for listener in actorEventListeners {
        listener.invoke(inquestEvent: InquestEvent())
    }
}}

Interface for the listener:

public protocol ActorEventListener{

   func invoke(inquestEvent : InquestEvent<Void>)
}

The floating action button:

 Button(action: {}){
            Image(systemName: "person.crop.circle.badge.plus")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 35, height: 35, alignment: .bottomTrailing)
                .padding(EdgeInsets.init(top: 0, leading: 0, bottom: 20, trailing: 0))            
                .scaleEffect(animating ? 1 : 0, anchor: .bottom)
                .animation(.interpolatingSpring(stiffness: stiffness, damping: damping).delay(0), value: animating).onDisappear()
                .onTapGesture {
                    IndquestHandler.addActor()
                }     
        }

The view where the actors should appear:

@State private var actors: [ActorPrefab] = []

func invoke(inquestEvent: InquestEvent<Void>) {
    
    actors.append(ActorPrefab(actorType: .Misstänkt))

}

I'm new to Swift so there's probably something obvious that I'm missing, but I've tried this approach 1000 times in java and c# and it works fine.

0 Answers
Related