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")
}
}
}
}