ToastUI
ToastUI copied to clipboard
Showing ToastUI on app load
Pre-requisites:
- [x] Yes, I looked through both open and closed issues looking for what I needed.
- [x] 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
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 😞
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.
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