AndreaMiotto / PartialSheet

A SwiftUI Partial Sheet fully customizable with dynamic height

Home Page:https://github.com/AndreaMiotto/PartialSheet/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Partial Sheet Not Triggering When Using @Published Variable in ViewModel

IAMTHEBURT opened this issue · comments

I'm having difficulty triggering a partial sheet from a @published variable within my ViewModel. However, the same partial sheet triggers as expected when using a @State variable within the View.
Here is the code snippet that is not working as expected:

class MainViewModel: ObservableObject {
    @Published var isPresented: Bool = false
}

struct ContentView: View {
    @StateObject var vm: MainViewModel = MainViewModel()
    
    var body: some View {
        ZStack{
            VStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)
                    .onTapGesture {
                        vm.isPresented.toggle()
                    }
                
                Text("Hello, world!")
            }
            .padding()
            .partialSheet(isPresented: $vm.isPresented) {
                Text("Some content")
            }
        }
        .attachPartialSheetToRoot()
    }
}

When tapping the "globe" image, the partial sheet is not triggered, despite vm.isPresented being toggled.

Expected Behavior:

I expect the partial sheet to be presented when the @published variable isPresented is toggled in the ViewModel.

Actual Behavior:

The partial sheet is not being presented when the @published variable isPresented is toggled in the ViewModel.

Could you please help me understand why this is happening? Is this a bug or am I missing something?

I am also experiencing the exact same issue.

I confirmed that the @Published variable was getting updated by adding a didSet {} property observer.

Also confirmed that using a local @State variable works as @IAMTHEBURT mentioned.

This seems like such a typical use case that I'm also wondering if I'm missing something here.