Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
IAMTHEBURT opened this issue Sep 18, 2023 · 1 comment

Comments

@IAMTHEBURT
Copy link

IAMTHEBURT commented Sep 18, 2023

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants