Skip to content

Commit

Permalink
[DuckPlayer] 26. Fix saved favs when DuckPlayer is disabled (#3285)
Browse files Browse the repository at this point in the history
  • Loading branch information
afterxleep authored Aug 29, 2024
1 parent e6d4806 commit 4649c38
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
33 changes: 23 additions & 10 deletions DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,13 @@ extension DuckPlayerNavigationHandler: DuckNavigationHandling {
return
}


// Daily Unique View Pixel
if url.isDuckPlayer,
duckPlayer.settings.mode != .disabled {
let setting = duckPlayer.settings.mode == .enabled ? Constants.duckPlayerAlwaysString : Constants.duckPlayerDefaultString
DailyPixel.fire(pixel: Pixel.Event.duckPlayerDailyUniqueView, withAdditionalParameters: [Constants.settingsKey: setting])

}

// Pixel for Views From Youtube
Expand All @@ -237,16 +239,27 @@ extension DuckPlayerNavigationHandler: DuckNavigationHandling {
Pixel.fire(pixel: Pixel.Event.duckPlayerViewFromYoutubeAutomatic)
}

// If DuckPlayer is Enabled or in ask mode, render the video
if url.isDuckURLScheme,
duckPlayer.settings.mode == .enabled || duckPlayer.settings.mode == .alwaysAsk,
!url.hasWatchInYoutubeQueryParameter {
let newRequest = Self.makeDuckPlayerRequest(from: URLRequest(url: url))

Logger.duckPlayer.debug("DP: Loading Simulated Request for \(navigationAction.request.url?.absoluteString ?? "")")

DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.performRequest(request: newRequest, webView: webView)

if url.isDuckURLScheme {

// If DuckPlayer is Enabled or in ask mode, render the video
if duckPlayer.settings.mode == .enabled || duckPlayer.settings.mode == .alwaysAsk,
!url.hasWatchInYoutubeQueryParameter {
let newRequest = Self.makeDuckPlayerRequest(from: URLRequest(url: url))

Logger.duckPlayer.debug("DP: Loading Simulated Request for \(navigationAction.request.url?.absoluteString ?? "")")

DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.performRequest(request: newRequest, webView: webView)
}

// Otherwise, just redirect to YouTube
} else {
if let (videoID, timestamp) = url.youtubeVideoParams {
let youtubeURL = URL.youtube(videoID, timestamp: timestamp)
let request = URLRequest(url: youtubeURL)
webView.load(request)
}
}
return
}
Expand Down
24 changes: 24 additions & 0 deletions DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,30 @@ class DuckPlayerNavigationHandlerTests: XCTestCase {

}

@MainActor
func testHandleNavigationWithDuckPlayerDisabledRedirectsToYoutube() {

let link = URL(string: "duck://player/12345")!
let navigationAction = MockNavigationAction(request: URLRequest(url: link))
let playerSettings = MockDuckPlayerSettings(appSettings: mockAppSettings, privacyConfigManager: mockPrivacyConfig)
playerSettings.mode = .disabled
let player = MockDuckPlayer(settings: playerSettings)
let handler = DuckPlayerNavigationHandler(duckPlayer: player, featureFlagger: featureFlagger, appSettings: mockAppSettings)

handler.handleNavigation(navigationAction, webView: webView)

let expectation = self.expectation(description: "Youtube URL request")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {


XCTAssertEqual(self.webView.url?.absoluteString, "https://m.youtube.com/watch?v=12345")
expectation.fulfill()
}

waitForExpectations(timeout: 1.0, handler: nil)

}

@MainActor
func testHandleReloadForDuckPlayerVideo() {
let duckPlayerURL = URL(string: "https://www.youtube-nocookie.com/embed/abc123?t=10s")!
Expand Down

0 comments on commit 4649c38

Please sign in to comment.