AndreaMiotto / PartialSheet

A SwiftUI Partial Sheet fully customizable with dynamic height
https://github.com/AndreaMiotto/PartialSheet/wiki
MIT License
1.73k stars 194 forks source link

Partial Sheet Not Triggering When Using @Published Variable in ViewModel #178

Open IAMTHEBURT opened 1 year ago

IAMTHEBURT commented 1 year ago

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?

sebastinto commented 8 months ago

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.