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

Showing ToastUI on app load #30

Open
2 tasks done
rursache opened this issue May 22, 2022 · 3 comments
Open
2 tasks done

Showing ToastUI on app load #30

rursache opened this issue May 22, 2022 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@rursache
Copy link

Pre-requisites:

  • Yes, I looked through both open and closed issues looking for what I needed.
  • No, I did not find what I was looking for.

Expected Behavior

Be able to show the ToastUI at app load. Useful when doing initial network requests like auth or other early tasks

Current Behavior

Currently the ToastUI does not appear

Steps to Reproduce

struct TestView: View {
    @State private var isLoading: Bool = false
	
    var body: some View {
        VStack {
            Text(isLoading ? "Please wait..." : "Got auth token")
                .padding()
            Spacer()
        }
        .toast(isPresented: $isLoading) {
            ToastView("Loading").toastViewStyle(.indeterminate)
        }
        .onAppear {
            self.isLoading = true
        }
    }
}

Context

I basically want to open the app and the ToastUI to be visible until some network requests are done in the background
Disclaimer: I'm still new to SwiftUI so i might try to achieve this the wrong way

Your Environment

  • Swift Version: 5.5
  • Xcode Version: 13.4
  • Operating System Version: macOS Monteray 12.4
  • Device or Simulator: Irrelevant
@rursache rursache added bug Something isn't working help wanted Extra attention is needed labels May 22, 2022
@rursache
Copy link
Author

rursache commented May 22, 2022

UPDATE: It works if i do:

.onAppear {
  DispatchQueue.main.async {
    self.isLoading = true	
  }
}

Seems like it's not ToastUI's fault but by own lack of SwiftUI understanding 😞

@quanshousio quanshousio reopened this May 23, 2022
@quanshousio
Copy link
Owner

This is actually an issue, not because of your lack of SwiftUI understanding. SwiftUI presentations such as .sheet and .alert can be presented in onAppear, so I do expect ToastUI to follow the same behavior. I have found the culprit but not sure how to implement a fix yet.

I also found another bug where if you initialize the @State variable with true then ToastUI will not present the toast. For now, you can safely use DispatchQueue.main.async in onAppear as the workaround. Thanks for the report.

@rursache
Copy link
Author

rursache commented May 23, 2022

I actually got it working as i want it to work!

@State var isLoading: Bool = false

then on my view, i have this:

.toast(isPresented: $isLoading) {
	ToastView("Loading").toastViewStyle(.indeterminate)
}
.task {
	self.isLoading = true
	await apiCall()
	self.isLoading = false
}

works fine and feels like the SwiftUI way of doing it

EDIT

I also found another bug where if you initialize the @State variable with true then ToastUI will not present the toast

I can confirm this is also true. That's why I start with the @State property as false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants