-
Notifications
You must be signed in to change notification settings - Fork 121
[Just In Time Messages] Open WebView for JITM Call to Action #7973
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
272b7c7
7855 Extract WebView creation from existing cards
joshheald 9a07cc7
7855 Show webview when JITM CTA is tapped
joshheald c319e23
StackNavigationViewStyle avoids split view on iPad
joshheald 89d238e
Merge branch 'trunk' into issue/7855-open-webview-for-jitm-cta
joshheald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
WooCommerce/Classes/ViewRelated/ReusableViews/SwiftUI Components/WebView.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import SwiftUI | ||
| import WebKit | ||
| import Alamofire | ||
| import class Networking.UserAgent | ||
|
|
||
| /// Mirror of AuthenticatedWebView, for equivalent display of URLs in `WKWebView` that do not need authentication on WPCom. | ||
| struct WebView: UIViewRepresentable { | ||
| @Environment(\.presentationMode) var presentation | ||
| @Binding var isPresented: Bool { | ||
| didSet { | ||
| if !isPresented { | ||
| presentation.wrappedValue.dismiss() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let url: URL | ||
|
|
||
| /// Optional URL or part of URL to trigger exit | ||
| /// | ||
| var urlToTriggerExit: String? | ||
|
|
||
| /// Callback that will be triggered if the destination url containts the `urlToTriggerExit` | ||
| /// | ||
| var exitTrigger: (() -> Void)? | ||
|
|
||
| private let credentials = ServiceLocator.stores.sessionManager.defaultCredentials | ||
|
|
||
| func makeCoordinator() -> WebViewCoordinator { | ||
| WebViewCoordinator(self) | ||
| } | ||
|
|
||
| func makeUIView(context: Context) -> WKWebView { | ||
| let webview = WKWebView() | ||
| webview.customUserAgent = UserAgent.defaultUserAgent | ||
| webview.navigationDelegate = context.coordinator | ||
|
|
||
| webview.load(URLRequest(url: url)) | ||
| return webview | ||
| } | ||
|
|
||
| func updateUIView(_ uiView: WKWebView, context: Context) { | ||
|
|
||
| } | ||
|
|
||
| class WebViewCoordinator: NSObject, WKNavigationDelegate { | ||
| private var parent: WebView | ||
|
|
||
| init(_ uiWebView: WebView) { | ||
| parent = uiWebView | ||
| } | ||
|
|
||
| func webView(_ webView: WKWebView, decidePolicyFor | ||
| navigationAction: WKNavigationAction, | ||
| decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { | ||
| if let url = webView.url?.absoluteString, let urlTrigger = parent.urlToTriggerExit, url.contains(urlTrigger) { | ||
| parent.exitTrigger?() | ||
| decisionHandler(.cancel) | ||
| webView.navigationDelegate = nil | ||
| return | ||
| } | ||
| decisionHandler(.allow) | ||
| } | ||
| } | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
WooCommerce/Classes/ViewRelated/ReusableViews/SwiftUI Components/WebViewSheet.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import SwiftUI | ||
|
|
||
| struct WebViewSheetViewModel { | ||
| let url: URL | ||
| let navigationTitle: String | ||
| let wpComAuthenticated: Bool | ||
| } | ||
|
|
||
| struct WebViewSheet: View { | ||
| let viewModel: WebViewSheetViewModel | ||
|
|
||
| let done: () -> Void | ||
|
|
||
| var body: some View { | ||
| WooNavigationSheet(viewModel: .init(navigationTitle: viewModel.navigationTitle, | ||
| done: done)) { | ||
| switch viewModel.wpComAuthenticated { | ||
| case true: | ||
| AuthenticatedWebView(isPresented: .constant(true), | ||
| url: viewModel.url) | ||
| case false: | ||
| WebView(isPresented: .constant(true), | ||
| url: viewModel.url) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct WebViewSheet_Previews: PreviewProvider { | ||
| static var previews: some View { | ||
| WebViewSheet( | ||
| viewModel: WebViewSheetViewModel.init( | ||
| url: URL(string: "https://woocommerce.com")!, | ||
| navigationTitle: "WooCommerce.com", | ||
| wpComAuthenticated: true), | ||
| done: { }) | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
WooCommerce/Classes/ViewRelated/ReusableViews/SwiftUI Components/WooNavigationSheet.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import SwiftUI | ||
|
|
||
| struct WooNavigationSheetViewModel { | ||
| let navigationTitle: String | ||
| let done: () -> Void | ||
| let doneButtonTitle = NSLocalizedString( | ||
| "Done", | ||
| comment: "Title for the Done button on a WebView modal sheet") | ||
| } | ||
|
|
||
| struct WooNavigationSheet<Content: View>: View { | ||
| let content: Content | ||
|
|
||
| let viewModel: WooNavigationSheetViewModel | ||
|
|
||
| init(viewModel: WooNavigationSheetViewModel, | ||
| @ViewBuilder content: () -> Content) { | ||
| self.content = content() | ||
| self.viewModel = viewModel | ||
| } | ||
|
|
||
| var body: some View { | ||
| NavigationView { | ||
| content | ||
| .navigationTitle(viewModel.navigationTitle) | ||
| .navigationBarTitleDisplayMode(.inline) | ||
| .toolbar { | ||
| ToolbarItem(placement: .confirmationAction) { | ||
| Button(action: viewModel.done, | ||
| label: { | ||
| Text(viewModel.doneButtonTitle) | ||
| }) | ||
| } | ||
| } | ||
| .wooNavigationBarStyle() | ||
| } | ||
| .navigationViewStyle(StackNavigationViewStyle()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering, since most of this code is duplicated with
AuthenticatedWebView, is there a way to collapse both structs into one with some sort of configuration to authenticate when necessary?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@toupper Probably... I'll take a look towards the end of the week in another PR, as I'm trying to de-risk delivery in 11.1 as much as possible. Other than that, would you be happy to approve?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's great! sure thing 👍