Sharing Environment dependencies with UIViewRepresentable views #549
Replies: 2 comments 2 replies
-
Maybe you can use the If it's not possible, and without touching TCA inner bits, I would steal it from the reducer and store it in the state, in an optional class wrapper. It shouldn't affect automatic Equatable conformance (you can rely on the nil/non-nil state of the optional to trigger the You should setup your representable so it can handle a nil environment to begin with, as you're not guaranteed to have one when the I would recommend the first approach with direct injection though, as you can bypass optionals and count on a non-nil environment at any time. The setup is also way simpler and doesn't have 10 reasons to go wrong. You're most likely instantiating a I think it will not break any TCA principles if you're only using the environment as an environment. |
Beta Was this translation helpful? Give feedback.
-
I updated the topic on the swift forum https://forums.swift.org/t/local-shared-environment/39465/3 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, we have a situation where we need to share my
Environment
dependency with aUIViewRepresentable
view. Unfortunately, environments are only available to the reducers, and theStore
does not seem to allow sharing them in any way.In a related discussion @mbrandonw suggested that (paraphrasing) "
UIViewRepresentable
views should be treated as vanilla SwiftUI views" (at least for simple use cases that permit it). Our case, however, is more complex.We use
Mapbox
that we wrap in aMapView
in combination with Composable Core Location("CCL")LocationManager
to drive location updates. The wrappedMGLMapView
has alocationManager
property, which can be overridden to gain control of map location updates.The first problem is somehow proxying the CCL's
LocationManager
calls and running it's effects. To replace thelocationManager
one has to adopt theMGLLocationManager
(link) protocol.The second problem, is actually sharing the
LocationManager
from theEnvironment
with theUIViewRepresentable
.From my search I only found one related issue on the TCA Swift forums by @maximkrouk, and from my understanding of his solution:
he is using some globally shared environment dependency that is injected twice, first in the reducer, and second time in the
UIViewRepresentable
view.What are some ways that this could be achieved without violating the TCA architectural patterns? Is there a way to run ad-hoc effects without the use of a
Store
? We're super-duper blocked by this at the moment, and would really appreciate any solutions or workarounds that the community can share 🙏Beta Was this translation helpful? Give feedback.
All reactions