Skip to content

Introduce .viewEnvironment(_:_:) modifier #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

b1ackturtle
Copy link
Collaborator

No description provided.

var value: Value
}

extension View {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that attaching viewEnvironment(_:_:) to anything other than a View conforming to WebViewRepresentable results in a compilation error might make extending View feel a bit unnatural.

An alternative approach would be to implement modifiers separately for WebView and ModifiedViewEnvironment, but that would be redundant, so I chose this implementation instead.

I would appreciate any thoughts you may have on this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a less essential question, but why did you choose the name viewEnvironment instead of webViewEnvironment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might not have been a particularly strong intention behind naming it viewEnvironment. If I had to point out one reason, however, it would be that this modifier can change not just properties belonging to WKWebView, but also those of UIView and UIScrollView. That’s why I chose viewEnvironment instead of webViewEnvironment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially thought webViewEnvironment was appropriate since there’s an inheritance relationship, but I understand your reasoning as well. I’m now okay with either naming.

@@ -60,6 +60,7 @@ struct ContentView: View {
.allowsBackForwardNavigationGestures(true)
.allowsLinkPreview(false)
.refreshable()
.viewEnvironment(\.customUserAgent, "Customized User Agent")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some modifications to the example app to write UI tests for viewEnvironment(_:_). However, I feel that having deprecated modifiers in the example app might be confusing for readers.
What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b1ackturtle
How about commenting that it is a workaround example?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to test for deprecated API? (Of course it is better to have it.)
It might be good to have some explanation in the README that there is a temporary workaround.

Also, it was not the UserAgent that triggered the need for this modifier.
Since our original intention was to have the UserAgent specified in WKWebViewConfiguration, it may be more appropriate to present a different use case.

If you want additional specifications for webview-debugger, let me know. I will add it right away.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about commenting that it is a workaround example?

I think this workaround will function properly.

Do we need to test for deprecated API?

Since it’s deprecated, it might be acceptable not to write tests for it.

it was not the UserAgent that triggered the need for this modifier.

You made a valid point.
I prioritized something that would simply work and wrote a UI test using a custom user agent.
However, that’s not really optimal.


First, we need to decide whether we want to guarantee its behavior with tests or not.
Personally, since this modifier isn’t intended to be fully supported, I’m starting to think we might be able to do without it.
What do you think? @elmetal @Kyome22

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, this is a workaround rather than a deprecation, so I don’t think marking it as deprecated would be accurate. That said, we’re currently using deprecated to indicate that we don’t intend to provide full support, but I’m open to changing it if there’s a better way to express that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried removing deprecated and building the document, but the message no longer appears.

- @available(*, deprecated, message: "This ViewModifier ... GitHub repository.")
+ @available(*, message: "This ViewModifier ... GitHub repository.")

Is there another way to make it explicit that this API is a workaround? I don't know.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kyome22
I don’t know how to solve this either. We might have to accept the limitations in expressiveness and just mark it as deprecated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I, too, feel it can only be described as deprecated.
Given that it's marked as deprecated, do you think this API should be tested to ensure it functions correctly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it were literally deprecated, we wouldn’t need to test it—but since this is a matter of expressiveness, I think it’s worth keeping the tests.

@b1ackturtle b1ackturtle marked this pull request as ready for review March 20, 2025 14:59
@b1ackturtle b1ackturtle requested a review from Kyome22 as a code owner March 20, 2025 14:59
@b1ackturtle b1ackturtle requested a review from elmetal March 20, 2025 15:00
public func viewEnvironment<Value>(
_ keyPath: ReferenceWritableKeyPath<WKWebView, Value>,
_ value: Value
) -> ModifiedViewEnvironment<Self, Value> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type name of ViewModifier does not look like View, so there is a sense of discomfort when using it.

For example, ModifiedEnvironmentView or ModifiedEnvironmentWebView might be easier to understand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like some View for the declaration here would be more appropriate.

In case we decide not to use some View, ModifiedViewEnvironmentContent might be a good fit.
It aligns well with SwiftUI’s naming conventions like ModifiedContent, and clearly expresses that the content has been altered by changes to the environment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @elmetal also mentioned, I named ModifiedViewEnvironment with ModifiedContent in mind.
However, since it seems a bit unclear, I’m planning to rename it to ModifiedViewEnvironmentContent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants