Skip to content

Commit

Permalink
[DuckPlayer] 7- Open Settings (#3110)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/1204099484721401/1207821841500245/f

Description:

Tapping on the "Settings" button, open app settings in DuckPlayer
Tapping on the "Info" button, opens DuckPlayer info sheet
  • Loading branch information
afterxleep authored Jul 19, 2024
1 parent 01a1c38 commit bc098bb
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
32 changes: 32 additions & 0 deletions DuckDuckGo/DuckPlayer/DuckPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,20 @@ public enum DuckPlayerReferrer {
protocol DuckPlayerProtocol {

var settings: DuckPlayerSettingsProtocol { get }
var hostView: UIViewController? { get }

init(settings: DuckPlayerSettingsProtocol)

func setUserValues(params: Any, message: WKScriptMessage) -> Encodable?
func getUserValues(params: Any, message: WKScriptMessage) -> Encodable?
func openVideoInDuckPlayer(url: URL, webView: WKWebView)
func openDuckPlayerSettings(params: Any, message: WKScriptMessage) async -> Encodable?
func openDuckPlayerInfo(params: Any, message: WKScriptMessage) async -> Encodable?

func initialSetupPlayer(params: Any, message: WKScriptMessage) async -> Encodable?
func initialSetupOverlay(params: Any, message: WKScriptMessage) async -> Encodable?

func setHostViewController(_ vc: UIViewController)
}

final class DuckPlayer: DuckPlayerProtocol {
Expand All @@ -98,11 +103,18 @@ final class DuckPlayer: DuckPlayerProtocol {
static let commonName = "Duck Player"

private(set) var settings: DuckPlayerSettingsProtocol
private(set) var hostView: UIViewController?

init(settings: DuckPlayerSettingsProtocol = DuckPlayerSettings()) {
self.settings = settings
}

// Sets a presenting VC, so DuckPlayer can present the
// info sheet directly
public func setHostViewController(_ vc: UIViewController) {
hostView = vc
}

// MARK: - Common Message Handlers

public func setUserValues(params: Any, message: WKScriptMessage) -> Encodable? {
Expand Down Expand Up @@ -135,6 +147,26 @@ final class DuckPlayer: DuckPlayerProtocol {
let webView = message.webView
return await self.encodedPlayerSettings(with: webView)
}

public func openDuckPlayerSettings(params: Any, message: WKScriptMessage) async -> Encodable? {
NotificationCenter.default.post(
name: .settingsDeepLinkNotification,
object: SettingsViewModel.SettingsDeepLinkSection.duckPlayer,
userInfo: nil
)
return nil
}

@MainActor
public func presentDuckPlayerInfo() {
guard let hostView else { return }
DuckPlayerModalPresenter().presentDuckPlayerFeatureModal(on: hostView)
}

public func openDuckPlayerInfo(params: Any, message: WKScriptMessage) async -> Encodable? {
await presentDuckPlayerInfo()
return nil
}

private func encodeUserValues() -> UserValues {
UserValues(
Expand Down
6 changes: 6 additions & 0 deletions DuckDuckGo/DuckPlayer/YoutubePlayerUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ final class YoutubePlayerUserScript: NSObject, Subfeature {
static let setUserValues = "setUserValues"
static let getUserValues = "getUserValues"
static let initialSetup = "initialSetup"
static let openSettings = "openSettings"
static let openInfo = "openInfo"
}

init(duckPlayer: DuckPlayerProtocol) {
Expand Down Expand Up @@ -73,6 +75,10 @@ final class YoutubePlayerUserScript: NSObject, Subfeature {
return duckPlayer.setUserValues
case Handlers.initialSetup:
return duckPlayer.initialSetupPlayer
case Handlers.openSettings:
return duckPlayer.openDuckPlayerSettings
case Handlers.openInfo:
return duckPlayer.openDuckPlayerInfo
default:
assertionFailure("YoutubePlayerUserScript: Failed to parse User Script message: \(methodName)")
return nil
Expand Down
19 changes: 19 additions & 0 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class MainViewController: UIViewController {
private var favoritesDisplayModeCancellable: AnyCancellable?
private var emailCancellables = Set<AnyCancellable>()
private var urlInterceptorCancellables = Set<AnyCancellable>()
private var settingsDeepLinkcancellables = Set<AnyCancellable>()

#if NETWORK_PROTECTION
private let tunnelDefaults = UserDefaults.networkProtectionGroupDefaults
Expand Down Expand Up @@ -261,6 +262,7 @@ class MainViewController: UIViewController {
addLaunchTabNotificationObserver()
subscribeToEmailProtectionStatusNotifications()
subscribeToURLInterceptorNotifications()
subscribeToSettingsDeeplinkNotifications()

#if NETWORK_PROTECTION
subscribeToNetworkProtectionEvents()
Expand Down Expand Up @@ -1349,6 +1351,23 @@ class MainViewController: UIViewController {
}
.store(in: &urlInterceptorCancellables)
}

private func subscribeToSettingsDeeplinkNotifications() {
NotificationCenter.default.publisher(for: .settingsDeepLinkNotification)
.receive(on: DispatchQueue.main)
.sink { [weak self] notification in
switch notification.object as? SettingsViewModel.SettingsDeepLinkSection {

case .duckPlayer:
let deepLinkTarget: SettingsViewModel.SettingsDeepLinkSection
deepLinkTarget = .duckPlayer
self?.launchSettings(deepLinkTarget: deepLinkTarget)
default:
return
}
}
.store(in: &settingsDeepLinkcancellables)
}

#if NETWORK_PROTECTION
private func subscribeToNetworkProtectionEvents() {
Expand Down
2 changes: 2 additions & 0 deletions DuckDuckGo/SettingsRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ struct SettingsRootView: View {
SubscriptionContainerViewFactory.makeSubscribeFlow(origin: origin,
navigationCoordinator: subscriptionNavigationCoordinator,
subscriptionManager: AppDependencyProvider.shared.subscriptionManager)
case .duckPlayer:
SettingsDuckPlayerView().environmentObject(viewModel)
default:
EmptyView()
}
Expand Down
7 changes: 7 additions & 0 deletions DuckDuckGo/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ extension SettingsViewModel {
case dbp
case itr
case subscriptionFlow(origin: String? = nil)
case duckPlayer
// Add other cases as needed

var id: String {
Expand All @@ -642,6 +643,7 @@ extension SettingsViewModel {
case .dbp: return "dbp"
case .itr: return "itr"
case .subscriptionFlow: return "subscriptionFlow"
case .duckPlayer: return "duckPlayer"
// Ensure all cases are covered
}
}
Expand Down Expand Up @@ -791,3 +793,8 @@ extension SettingsViewModel {
}

}

// Deeplink notification handling
extension NSNotification.Name {
static let settingsDeepLinkNotification: NSNotification.Name = Notification.Name(rawValue: "com.duckduckgo.notification.settingsDeepLink")
}
12 changes: 12 additions & 0 deletions DuckDuckGoTests/DuckPlayerMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ final class MockDuckPlayerSettings: DuckPlayerSettingsProtocol {
}

final class MockDuckPlayer: DuckPlayerProtocol {
var hostView: UIViewController?

func openDuckPlayerSettings(params: Any, message: WKScriptMessage) async -> (any Encodable)? {
nil
}

func openDuckPlayerInfo(params: Any, message: WKScriptMessage) async -> (any Encodable)? {
nil
}

func setHostViewController(_ vc: UIViewController) {}

func initialSetupPlayer(params: Any, message: WKScriptMessage) async -> (any Encodable)? {
nil
}
Expand Down

0 comments on commit bc098bb

Please sign in to comment.