SwiftUI: Using a Published attribute to displaying a sheet

610 views Asked by At

Good evening!

I’m working on a simple project, where I have a view model, which has published boolean value. I would like to show a sheet when this value is set. However, the sheet function uses the Binding type, meanwhile I have the Published type.

Would it be possible to somehow deal with such a case? In case if I will try to deal with it with some callbacks and a State variable in the view object, it will make the architecture more dirty so, I would like to avoid it.


final class ContentViewModel: ObservableObject {
    @Published private(set) var isVisable = false
    func toggleVisability() {
        isVisable.toggle()
    }
} 

struct ContentView: View {
    @StateObject var model = ContentViewModel()

    var body: some View {
        VStack {
            Button("Show") {
                model.toggleVisability()                
            }
            .sheet(isPresented: model.$isVisable, // ERROR: Cannot convert value of type 'Published<Bool>.Publisher' to expected argument type 'Binding<Bool>'
                   onDismiss: { model.toggleVisability() }) {
                Text("Placeholder")
            }
        }
    }
}
1

There are 1 answers

0
Kirill Rud On

The published variable should be public to set, and binding should be provided for the model (observable object) itself.

final class ContentViewModel: ObservableObject {
    @Published var isVisable = false // CHANGE HERE
    func toggleVisability() {
        isVisable.toggle()
    }
} 

struct ContentView: View {
    @StateObject var model = ContentViewModel()

    var body: some View {
        VStack {
            Button("Show") {
                model.toggleVisability()                
            }
            .sheet(isPresented: $model.isVisable, // CHANGE HERE
                   onDismiss: { model.toggleVisability() }) {
                Text("Placeholder")
            }
        }
    }
}

Many thanks to Joakim Danielson and lorem ipsum.