Skip to content

Commit

Permalink
Handoff prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
quanganhdo committed Dec 6, 2024
1 parent 7b2a893 commit e4d8c4a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions DuckDuckGo/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
Task { @MainActor in
await subscriptionCookieManager.refreshSubscriptionCookie()
}

WindowControllersManager.shared.lastKeyMainWindowController?.mainViewController.browserTabViewController.becomeCurrentActivity()
}

private func initializeSync() {
Expand Down Expand Up @@ -627,6 +629,17 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
urlEventHandler.handleFiles(files)
}

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

restorationHandler([mainWindowController.browserTabViewController])

return true
}

// MARK: - PixelKit

static func configurePixelKit() {
Expand Down
4 changes: 4 additions & 0 deletions DuckDuckGo/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@
<false/>
<key>NSSupportsSuddenTermination</key>
<false/>
<key>NSUserActivityTypes</key>
<array>
<string>com.duckduckgo.mobile.ios.web-browsing</string>
</array>
<key>SUBSCRIPTION_APP_GROUP</key>
<string>$(SUBSCRIPTION_APP_GROUP)</string>
<key>SUEnableAutomaticChecks</key>
Expand Down
46 changes: 46 additions & 0 deletions DuckDuckGo/Tab/View/BrowserTabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ final class BrowserTabViewController: NSViewController {

self.adjustFirstResponder(force: true)
removeExistingDialog()

self.updateCurrentActivity(url: selectedTabViewModel?.tab.content.urlForWebView)
}
.store(in: &cancellables)
}
Expand Down Expand Up @@ -615,6 +617,13 @@ final class BrowserTabViewController: NSViewController {
tabViewModel?.tab.webViewDidFinishNavigationPublisher.sink { [weak self] in
self?.updateStateAndPresentContextualOnboarding()
}.store(in: &tabViewModelCancellables)

tabViewModel?.tab.webView.publisher(for: \.url)
.dropFirst()
.receive(on: DispatchQueue.main)
.sink { [weak self] url in
self?.updateCurrentActivity(url: url)
}.store(in: &tabViewModelCancellables)
}

private func subscribeToUserDialogs(of tabViewModel: TabViewModel?) {
Expand Down Expand Up @@ -1510,3 +1519,40 @@ private extension NSViewController {
}

}

extension BrowserTabViewController {
override func restoreUserActivityState(_ userActivity: NSUserActivity) {
guard userActivity.activityType == "com.duckduckgo.mobile.ios.web-browsing", let url = userActivity.webpageURL else {
return
}
openNewTab(with: .url(url, credential: nil, source: .appOpenUrl))
}

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

userActivity?.becomeCurrent()
}

private func updateCurrentActivity(url: URL?) {
let newURL: URL? = {
guard let url, let scheme = url.scheme, ["http", "https"].contains(scheme) else { return nil }
return 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()
}
}

0 comments on commit e4d8c4a

Please sign in to comment.