Replies: 2 comments 3 replies
-
Follow up, managed to get a demo app up to test this. Doing this in the initializer does not actually work, I get a no-live-implementation error: struct AuthenticationHostingView: View {
let store: StoreOf<AuthenticationHostingFeature>
public init(store: StoreOf<AuthenticationHostingFeature>) {
@Environment(\.webAuthenticationSession) var session
// Does not work 👇
self.store = withDependencies {
$0.authenticate = AuthenticateEffect { try await session.authenticate(using: $0, callbackURLScheme: $1, preferredBrowserSession: $2) }
} operation: {
store
}
}
// ...
} If there is a way to override a dependency from a view initializer as the store passes through, I am not aware of it. |
Beta Was this translation helpful? Give feedback.
-
Ah, thank you for the reminder. In hindsight that does make sense. I was not remembering that initialization in the operation block is the important part, not just passing through it.
Ah, okay. That sounds closer to the signaling approach explored in #1683. Thanks for the pointer, I will go back and explore that avenue more. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Apple has added a few useful side effects to SwiftUI's
EnvironmentValues
. In particular, I am interested in@Environment(\.webAuthenticationSession)
, added in iOS 16.4, a super-clean SwiftUI, async wrapper aroundASWebAuthenticationSession
.My issue is I want to invoke this environment value not directly from UI event, but after executing some logic in the reducer. I've previously explored wrapping
ASWebAuthenticationSession
as a dependency directly, signaling out to the view (#1683) and of courseswift-dependencies-additions
.But I'm always looking to slim down and reduce dependencies and maintained code, and I had a small lightbulb that maybe I could do this with
swift-dependencies
directly, and I just wanted to see if there's anything inadvisable about the following.Step 1, declare an unimplemented dependency that matches the Environment value's API:
Step 2, override the dependency from the Environment value in the view's initializer:
Step 3, use the dependency value in the reducer:
The idea/questionable bit is setting the dependency from the Environment value in the view's initializer. I know view initializers can be called repeatedly and certain kinds of work should be avoided there, and I am unsure if this is okay or not.
What do people think?
Beta Was this translation helpful? Give feedback.
All reactions