Skip to content
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

Handoff prototype #3687

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Core/FeatureFlag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum FeatureFlag: String {
case autcompleteTabs
case textZoom
case adAttributionReporting
case handoff

/// https://app.asana.com/0/72649045549333/1208231259093710/f
case networkProtectionUserTips
Expand Down Expand Up @@ -127,6 +128,8 @@ extension FeatureFlag: FeatureFlagDescribing {
return .remoteReleasable(.feature(.autofillSurveys))
case .autcompleteTabs:
return .remoteReleasable(.feature(.autocompleteTabs))
case .handoff:
return .internalOnly()
case .networkProtectionUserTips:
return .remoteReleasable(.subfeature(NetworkProtectionSubfeature.userTips))
case .textZoom:
Expand Down
13 changes: 13 additions & 0 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,8 @@ import os.log
}

AppDependencyProvider.shared.persistentPixel.sendQueuedPixels { _ in }

mainViewController?.currentTab?.becomeCurrentActivity()
}

private func stopAndRemoveVPNIfNotAuthenticated() async {
Expand Down Expand Up @@ -921,6 +923,17 @@ import os.log
return true
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([any UIUserActivityRestoring]?) -> Void) -> Bool {
guard userActivity.activityType == "com.duckduckgo.mobile.ios.web-browsing",
let mainViewController else {
return false
}

restorationHandler([mainViewController])

return true
}

// MARK: private

private func sendAppLaunchPostback() {
Expand Down
1 change: 1 addition & 0 deletions DuckDuckGo/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
<key>NSUserActivityTypes</key>
<array>
<string>ConfigurationIntent</string>
<string>com.duckduckgo.mobile.ios.web-browsing</string>
</array>
<key>UIApplicationShortcutItems</key>
<array>
Expand Down
30 changes: 29 additions & 1 deletion DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ class MainViewController: UIViewController {
hideNotificationBarIfBrokenSitePromptShown()
if tab.link == nil {
attachHomeScreen()
invalidateCurrentActivity()
} else {
attachTab(tab: tab)
refreshControls()
Expand All @@ -1072,14 +1073,15 @@ class MainViewController: UIViewController {
hideNotificationBarIfBrokenSitePromptShown()
currentTab?.progressWorker.progressBar = nil
currentTab?.chromeDelegate = nil

addToContentContainer(controller: tab)

viewCoordinator.logoContainer.isHidden = true

tab.progressWorker.progressBar = viewCoordinator.progress
chromeManager.attach(to: tab.webView.scrollView)
tab.chromeDelegate = self
tab.becomeCurrentActivity()
}

private func addToContentContainer(controller: UIViewController) {
Expand Down Expand Up @@ -1464,6 +1466,8 @@ class MainViewController: UIViewController {
tabsBarController?.refresh(tabsModel: tabManager.model)
swipeTabsCoordinator?.refresh(tabsModel: tabManager.model, scrollToSelected: true)
newTabPageViewController?.openedAsNewTab(allowingKeyboard: allowingKeyboard)

invalidateCurrentActivity()
}

func updateFindInPage() {
Expand Down Expand Up @@ -2962,3 +2966,27 @@ extension MainViewController: AIChatViewControllerDelegate {
loadUrlInNewTab(url, inheritedAttribution: nil)
}
}

// NSUserActivity-related
extension MainViewController {
private func invalidateCurrentActivity() {
guard supportsHandoff() else { return }

userActivity?.invalidate()
userActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb)
userActivity?.webpageURL = nil
userActivity?.becomeCurrent()
}

override func restoreUserActivityState(_ activity: NSUserActivity) {
guard supportsHandoff(), activity.activityType == "com.duckduckgo.mobile.ios.web-browsing", let url = activity.webpageURL else {
return
}

loadUrlInNewTab(url, reuseExisting: true, inheritedAttribution: nil)
}

private func supportsHandoff() -> Bool {
featureFlagger.isFeatureOn(.handoff)
}
}
42 changes: 41 additions & 1 deletion DuckDuckGo/TabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class TabViewController: UIViewController {
updateTabModel()
delegate?.tabLoadingStateDidChange(tab: self)
checkLoginDetectionAfterNavigation()
updateCurrentActivity(url: url)
}
}

Expand Down Expand Up @@ -274,7 +275,7 @@ class TabViewController: UIViewController {
manager.delegate = self
return manager
}()

private static let debugEvents = EventMapping<AMPProtectionDebugEvents> { event, _, _, onComplete in
let domainEvent: Pixel.Event
switch event {
Expand Down Expand Up @@ -3182,3 +3183,42 @@ extension TabViewController: DuckPlayerTabNavigationHandling {
}

}

// NSUserActivity-related
extension TabViewController {
func becomeCurrentActivity() {
guard supportsHandoff() else { return }

if userActivity?.webpageURL == nil {
userActivity?.invalidate()
userActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb)
userActivity?.webpageURL = nil
}

userActivity?.becomeCurrent()
}

private func updateCurrentActivity(url: URL?) {
guard supportsHandoff() else { return }

let newURL: URL? = {
guard let url, let scheme = url.scheme, ["http", "https"].contains(scheme) else { return nil }
return url.isDuckDuckGo ? url.removingInternalSearchParameters() : url
}()
guard newURL != userActivity?.webpageURL else { return }

userActivity?.invalidate()
if newURL != nil {
userActivity = NSUserActivity(activityType: "com.duckduckgo.mobile.ios.web-browsing")
} else {
userActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb)
}
userActivity?.webpageURL = newURL

userActivity?.becomeCurrent()
}

private func supportsHandoff() -> Bool {
featureFlagger.isFeatureOn(.handoff)
}
}
Loading