Closed mywristbands closed 1 year ago
@mywristbands, sorry, I do not have any recommendations for your scenario. PopupView is just a tool, you can use it however you see fit. Or fork it and improve to your liking.
@f3dm76 That's ok, I just wanted to see if anyone had already figured this out before I implemented a solution myself. Thanks for letting me know.
In my app, I would like to display popups from views that are deeply nested in my app. When displaying built-in SwiftUI alerts, confirmation dialogs, sheets, etc., this is very easy to do. However, with this PopupView library, it is much more difficult. This is because, as you explain in your
Note
:However, if I attach a popup to the root of my screen, but want to define the actual popup view from within a deeply nested view (so that I can more dynamically create the popup view for any situation), this is challenging to implement. To understand what I mean:
To resolve this problem, I had an idea to create an EnvironmentObject that any sub-view could use to change the popup presented, but it's only possible if I define the
view
parameter as type AnyView. Here's the EnvironmentObject I thought of:And then I define the following view modifier to attach the environment object (PopupRootModel) to the root view:
Then, any sub-view could access the PopupRootModel, and edit the
params
field to set the popup to whatever it wants.While I believe the above will work, I'd rather avoid using AnyView since it can have performance costs.
Another option would be to have a separate
params
property for every popup I need in my app. So I might have anerrorParams
,successParams
,filterSheetParams
, etc. However, this could get pretty unwieldy as I develop my app and may have dozens of different popup views, so it doesn't seem like an ideal option either.How do you recommend going about presenting a popup from a deeply nested view?