elai950 / AlertToast

Create Apple-like alerts & toasts using SwiftUI
https://elai950.github.io/AlertToast/
MIT License
2.12k stars 191 forks source link

Alert can never be dismissed #3

Closed A320Peter closed 3 years ago

A320Peter commented 3 years ago

This is a really cool framework but I am having problem dismissing the alert if I set duration greater than 2.0. Default setting works as expected.

This works perfectly (view diasappears after 2 seconds):

.toast(isPresenting: RM.$pendingNotifications) {
                AlertToast(displayMode: .hud, type: .systemImage("exclamationmark.triangle.fill", Color.orange), title: "alert alert alert")
            }

The code below does not work. With any duration above 2.0 the view appears and never disappears. It stays on top of the screen. Tap to dismiss also does not work. I also cannot influence anything with tapToDismiss:

.toast(isPresenting: RM.$pendingNotifications, duration: 3.0) {
                AlertToast(displayMode: .hud, type: .systemImage("exclamationmark.triangle.fill", Color.orange), title: "alert alert alert")
            }

RM is an observable object exposed to the environment and pendingNotifications is @Published variable.

A320Peter commented 3 years ago

I did some experiment in a short trial and I have no idea why, but in the first 0-2.4 seconds it's possible to dismiss the alert however later it is not. I tested and it is receiving the onTapGesture event and the isPresenting changes to false but the alert stays on the screen no matter what regardless isPresenting false or true. If I tap or set isPresenting withing 2.4 seconds it works as expected.

A320Peter commented 3 years ago

In a new project I can not reproduce this behaviour so I am looking deeper into it.

elai950 commented 3 years ago

It seems to works great on my side. Can you please submit the ObservableObject you're using? I think that is where the problem. Also, if RM is an ObservableObject, so the implementation should look like this:

Read this article about how to implement Alert Toast with ObservableObject correctly (the Bonus section): How to toast an alert in SwiftUI

//Notice the dollar sign
.toast(isPresenting: $RM.pendingNotifications, duration: 3.0) {
   AlertToast(displayMode: .hud, type: .systemImage("exclamationmark.triangle.fill", Color.orange), title: "alert alert alert")
}
A320Peter commented 3 years ago

Thanks I have it now. Nothing is wrong with your code. In my case it's a complicated project and the observable object @Published variable was indirectly connected to a custom property wrapper. For some reason this value is not updating if the new value source is outside of the object.

I could reproduce it in a more simple way with an @AppStorage wrapper in iOS14. I don't have much experience with @AppStorageso probably it's a normal behaviour but modifying this from outside of the observable object also preventing the alert to disappear. Probably the change is not pushed down the hierarchy.

Thanks again for this framework, it's awesome.

elai950 commented 3 years ago

Glad you found a solution 😊