-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/Issue URL: https://app.asana.com/0/72649045549333/1205043809052693/f Description: Implements ITP for subscriptions Implements navigating back in WebViews Shows/Hide navigation bar on WebViews depending on scroll position Subscription WebViews are now shown in a sheet for better navigation/usability Includes several improvements to the subscription flow for memory management and etc. Allows navigating to tel: FaceTime: and etc schemas Properly manages to navigate to new windows
- Loading branch information
1 parent
d7f7111
commit 18663c3
Showing
34 changed files
with
1,366 additions
and
246 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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
64 changes: 64 additions & 0 deletions
64
DuckDuckGo/Subscription/AsyncHeadlessWebview/AsyncHeadlessWebView.swift
This file contains 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,64 @@ | ||
// | ||
// AsyncHeadlessWebView.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import WebKit | ||
import UserScript | ||
import SwiftUI | ||
import DesignResourcesKit | ||
import Core | ||
|
||
struct AsyncHeadlessWebViewSettings { | ||
let bounces: Bool | ||
|
||
init(bounces: Bool = false) { | ||
self.bounces = bounces | ||
} | ||
} | ||
|
||
struct AsyncHeadlessWebView: View { | ||
@StateObject var viewModel: AsyncHeadlessWebViewViewModel | ||
|
||
var body: some View { | ||
GeometryReader { geometry in | ||
HeadlessWebView( | ||
userScript: viewModel.userScript, | ||
subFeature: viewModel.subFeature, | ||
settings: viewModel.settings, | ||
onScroll: { newPosition in | ||
viewModel.updateScrollPosition(newPosition) | ||
}, | ||
onURLChange: { newURL in | ||
viewModel.url = newURL | ||
}, | ||
onCanGoBack: { value in | ||
viewModel.canGoBack = value | ||
}, | ||
onCanGoForward: { value in | ||
viewModel.canGoForward = value | ||
}, | ||
onContentType: { value in | ||
viewModel.contentType = value | ||
}, | ||
navigationCoordinator: viewModel.navigationCoordinator | ||
) | ||
.frame(width: geometry.size.width, height: geometry.size.height) | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
DuckDuckGo/Subscription/AsyncHeadlessWebview/AsyncHeadlessWebViewModel.swift
This file contains 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,66 @@ | ||
// | ||
// AsyncHeadlessWebViewModel.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import UserScript | ||
import Core | ||
import Combine | ||
|
||
final class AsyncHeadlessWebViewViewModel: ObservableObject { | ||
let userScript: UserScriptMessaging? | ||
let subFeature: Subfeature? | ||
let settings: AsyncHeadlessWebViewSettings | ||
|
||
private var initialScrollPositionSubject = PassthroughSubject<CGPoint, Never>() | ||
private var subsequentScrollPositionSubject = PassthroughSubject<CGPoint, Never>() | ||
private var cancellables = Set<AnyCancellable>() | ||
private var isFirstUpdate = true | ||
private var initialDelay = 1 | ||
|
||
@Published var scrollPosition: CGPoint = .zero | ||
@Published var url: URL? | ||
@Published var canGoBack: Bool = false | ||
@Published var canGoForward: Bool = false | ||
@Published var contentType: String = "" | ||
|
||
var navigationCoordinator = HeadlessWebViewNavCoordinator(webView: nil) | ||
|
||
init(userScript: UserScriptMessaging?, subFeature: Subfeature?, settings: AsyncHeadlessWebViewSettings) { | ||
self.userScript = userScript | ||
self.subFeature = subFeature | ||
self.settings = settings | ||
|
||
// Delayed publishing first update for scrollPosition | ||
// To avoid publishing events on view updates | ||
initialScrollPositionSubject | ||
.delay(for: .seconds(initialDelay), scheduler: RunLoop.main) | ||
.merge(with: subsequentScrollPositionSubject) | ||
.assign(to: &$scrollPosition) | ||
} | ||
|
||
func updateScrollPosition(_ newPosition: CGPoint) { | ||
if isFirstUpdate { | ||
initialScrollPositionSubject.send(newPosition) | ||
isFirstUpdate = false | ||
} else { | ||
subsequentScrollPositionSubject.send(newPosition) | ||
} | ||
} | ||
|
||
|
||
} |
81 changes: 81 additions & 0 deletions
81
DuckDuckGo/Subscription/AsyncHeadlessWebview/HeadlessWebView.swift
This file contains 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,81 @@ | ||
// | ||
// HeadlessWebView.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
import WebKit | ||
import UserScript | ||
|
||
struct HeadlessWebView: UIViewRepresentable { | ||
let userScript: UserScriptMessaging? | ||
let subFeature: Subfeature? | ||
let settings: AsyncHeadlessWebViewSettings | ||
var onScroll: ((CGPoint) -> Void)? | ||
var onURLChange: ((URL) -> Void)? | ||
var onCanGoBack: ((Bool) -> Void)? | ||
var onCanGoForward: ((Bool) -> Void)? | ||
var onContentType: ((String) -> Void)? | ||
var navigationCoordinator: HeadlessWebViewNavCoordinator | ||
|
||
|
||
func makeUIView(context: Context) -> WKWebView { | ||
let configuration = WKWebViewConfiguration() | ||
configuration.userContentController = makeUserContentController() | ||
|
||
let webView = WKWebView(frame: .zero, configuration: configuration) | ||
|
||
navigationCoordinator.webView = webView | ||
webView.uiDelegate = context.coordinator | ||
webView.scrollView.delegate = context.coordinator | ||
webView.scrollView.bounces = settings.bounces | ||
webView.navigationDelegate = context.coordinator | ||
|
||
#if DEBUG | ||
if #available(iOS 16.4, *) { | ||
webView.isInspectable = true | ||
} | ||
#endif | ||
|
||
context.coordinator.setupWebViewObservation(webView) | ||
return webView | ||
} | ||
|
||
func updateUIView(_ uiView: WKWebView, context: Context) {} | ||
|
||
func makeCoordinator() -> HeadlessWebViewCoordinator { | ||
HeadlessWebViewCoordinator(self, | ||
onScroll: onScroll, | ||
onURLChange: onURLChange, | ||
onCanGoBack: onCanGoBack, | ||
onCanGoForward: onCanGoForward, | ||
onContentType: onContentType) | ||
} | ||
|
||
@MainActor | ||
private func makeUserContentController() -> WKUserContentController { | ||
let userContentController = WKUserContentController() | ||
if let userScript, let subFeature { | ||
userContentController.addUserScript(userScript.makeWKUserScriptSync()) | ||
userContentController.addHandler(userScript) | ||
userScript.registerSubfeature(delegate: subFeature) | ||
} | ||
return userContentController | ||
} | ||
|
||
} |
Oops, something went wrong.