0

I have an Observable Object GlobalOO with a @Published value

In any View , I can observe changes with .onReceive(globalOO.$value){ newValue in ...}

Now I only want to receive those changes according to a condition in the view @State private var isActive = false

.onReceive(globalOO.$value.drop(while: { _ in isActive == false })){newValue in ...}

This view modifier works but seems "weird" . Is there any possibility to create a view modifier that does exactly the same but more "compact". Thank you

Code could be shorter and more compact

Gyh
  • 3
  • 1

1 Answers1

0

Try this!

struct ContentView: View {
    @ObservedObject var globalOO = GlobalOO()
    @State private var isActive = false

    var body: some View {
        Text("Hello, World!")
            .onReceive(globalOO.$value.drop(while: { _ in !isActive })) { newValue in
                // Handle newValue
            }
    }
}
john deere
  • 34
  • 4
  • Thanx but it is what i wrote ( == false is the same as !contidion) , I m looking to replace / change the whole drop(while: that makes code quite long and anyway seems "weird" – Gyh Jun 29 '23 at 18:18