How to Toggle a var Bool in a @EnvironmentObject var thanks to a Toggle

Good morning everyone,

I'm trying to create an application for a connected home. So I have a light class that has the property of having a @Published var allLights: [Light] as follows:

struct Light: Identifiable {
    var id: Int
    var deviceName: String
    var intensity: Int
    //I don't know if I have to remove the @State wrapper because I'll use Toggle to toggle this var...
     @State var mode: Bool
}
class Lights: ObservableObject {
  @Published var allLights = [Light]()

  init() // I initialize this array using an API (thanks to SwiftyJSON)
}


Thus, in a View I would like to be able to change the different values of my lights (only the mode var for the moment). However the Toggle does not change the value of the @State var mode of each light... Here is the code for the view:


struct LightsCollection: View {
  @EnvironmentObject var lightStore : Lights

  var body: some View {
       VStack {
            ForEach(lightStore.allLights) { light in
                 HStack {
                      Text("\(light.id)")
                      Text("\(light.deviceName)")
                      Text("- \(light.intensity)")
                      Text("- " + String(light.mode))
                      Toggle(isOn: light.$mode) {
                           Text(light.mode ? "Switch off" : "Switch on")
                      }
                 }
            }
       }
  }
}


I've already read a lot of forums but nothing to do... Any help will be appreciated :-)

How to Toggle a var Bool in a @EnvironmentObject var thanks to a Toggle
 
 
Q