SwiftUI - Trigger a Modal Sheet from an ObservableObject?

718 views Asked by At

I've created a random number generator that should present a new sheet if the number 3 appears.

The logic is in a separate class but when I use .sheet or .fullScreenCover on the ContentView it doesn't work.

Is it possible to trigger a modal sheet from an ObservableObject in Xcode12 / iOS 14 SwiftUI?

Minimal reproducible example below:

    import SwiftUI
    
    struct ContentView: View {
        
        @StateObject var mathLogic = MathLogic()
        
        var body: some View {
            
            VStack{
                
            Text(String(mathLogic.newNumber))
                .padding(.bottom, 40)
        
            Text("Tap for a number")
                             .onTapGesture{
                                 mathLogic.generateRandomNumber()
                             }
      }
        .fullScreenCover(isPresented: mathLogic.$isLucky3, content: NewModalView.init)
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct NewModalView: View {
    
    var body: some View {
        Text("You hit lucky number 3!")
    }
}

class MathLogic: ObservableObject {
    
    @Published var newNumber = 0
    
    @State var isLucky3 = false
    
    func generateRandomNumber() {
        
        newNumber = Int.random(in: 1..<5)
        
        guard self.newNumber != 3 else {
            // trigger modal
            self.isLucky3.toggle()        
            return
        }       
    }  
}
1

There are 1 answers

0
Asperi On BEST ANSWER

The @State is intended to be in View, in ObservableObject we use @Published, so it should be

class MathLogic: ObservableObject {
    
    @Published var newNumber = 0
    
    @Published var isLucky3 = false    // << here !!

// .. other code

and to bind it via ObservedObject, because .$isLucky3 gives publisher

    // ... other code
  }
  .fullScreenCover(isPresented: $mathLogic.isLucky3, content: NewModalView.init)

Tested with Xcode 12 / iOS 14