Fun experiment: Controlled Store #1176
tgrapperon
started this conversation in
Show and tell
Replies: 1 comment
-
After a second thought, the delays are a little awkward in this position. We could probably replace them by some Send(action1)
Wait(seconds:2)
Send(action2, animation: .default) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
As I was working on a project of mine, I wanted to add some nice animations to a placeholder view, and I didn't want to clutter my app domain with this superfluous and high-frequency domain.
So I made this fun SwiftUI
View
calledWithControlledStore
where you can schedule some store's actions to be executed using a builder. The view holds a reference to the store you pass, so it can be a scoped store, or a whole new detached/autonomous store.It works like this:
You send
Start()
to make the program start automatically when the view appears. Otherwise, you can insert acontrollerState: { }
closure before the view where you can return.run
,.pause
ornil
as you see fit.In the builder,
Start
starts the program. You don't need it if you control with the closure.Stop
pauses the program. You don't need it if you control with the closure. You don't need it either to end your scheduled program. When there are no more actions, it stops.Loop
restarts the scheduled program from the beginning.Here is an example using
CaseStudies
:The code used is:
There is one notable issue for the moment: the list of actions is captured only once when the view appears. This could cause issues if some values used are not constant. For example, we can't use a
ViewStore
to take decisions in the builder. It's probably possible to make it work, but I didn't go farther as it already fills my needs.The API can probably be improved too.
There are probably some interesting uses when debugging, but I didn't push farther.
Here is the code for
WithControlledStore
:https://gist.github.com/tgrapperon/388bbfd43e725bcf0b77ee918abb26ca
Beta Was this translation helpful? Give feedback.
All reactions