Skip to content

Commit

Permalink
[ads] Fix NTT video background may be blank after changing NTP media …
Browse files Browse the repository at this point in the history
…type.
  • Loading branch information
aseren committed Aug 22, 2024
1 parent 71ea507 commit e20246f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class NewTabPageBackgroundButtonsView: UIView, PreferencesObserver {
private let playButton = PlayButton().then {
$0.isHidden = true
}
private var shouldShowPlayButton = false
private var isVideoBackground = false
private var playButtonGestureRecognizer: UITapGestureRecognizer?

/// The parent safe area insets (since UICollectionView doesn't feed down
Expand Down Expand Up @@ -175,6 +175,11 @@ class NewTabPageBackgroundButtonsView: UIView, PreferencesObserver {
tappedBackgroundDuringAutoplay?()
}

func resetVideoBackgroundButtons() {
isVideoBackground = false
updatePlayButtonVisibility()
}

func videoAutoplayStarted() {
let tapGesture = UITapGestureRecognizer(
target: self,
Expand All @@ -185,7 +190,7 @@ class NewTabPageBackgroundButtonsView: UIView, PreferencesObserver {
}

func videoAutoplayFinished() {
shouldShowPlayButton = true
isVideoBackground = true
updatePlayButtonVisibility()

if let playButtonGestureRecognizer = playButtonGestureRecognizer {
Expand All @@ -201,7 +206,7 @@ class NewTabPageBackgroundButtonsView: UIView, PreferencesObserver {
if isLandscape && UIDevice.isPhone {
playButton.isHidden = true
} else {
playButton.isHidden = !shouldShowPlayButton
playButton.isHidden = !isVideoBackground
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class NewTabPageBackgroundView: UIView {
}
}

func resetPlayerLayer() {
playerLayer.player = nil
playerLayer.removeFromSuperlayer()
}

override init(frame: CGRect) {
super.init(frame: frame)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class NewTabPageVideoPlayer {

init(_ backgroundVideoPath: URL) {
self.backgroundVideoPath = backgroundVideoPath
createPlayer()
}

deinit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,18 @@ class NewTabPageViewController: UIViewController {
}

background.changed = { [weak self] in
self?.setupBackgroundImage()
guard let self else { return }
setupBackgroundImage()

let isTabVisible = viewIfLoaded?.window != nil
setupBackgroundVideoIfNeeded(shouldCreatePlayer: isTabVisible)
// Load the video asset here, as viewDidAppear is not called when the view
// is already visible
if isTabVisible {
videoPlayer?.loadAndAutoplayVideoAssetIfNeeded(
shouldAutoplay: false
)
}
}

Preferences.BraveNews.isEnabled.observe(from: self)
Expand Down Expand Up @@ -330,7 +341,7 @@ class NewTabPageViewController: UIViewController {
}

setupBackgroundImage()
setupBackgroundVideoIfNeeded()
setupBackgroundVideoIfNeeded(shouldCreatePlayer: true)
backgroundView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
Expand Down Expand Up @@ -394,7 +405,6 @@ class NewTabPageViewController: UIViewController {
self?.reportSponsoredBackgroundEvent(.viewedImpression)
}

setupBackgroundVideoIfNeeded()
videoPlayer?.loadAndAutoplayVideoAssetIfNeeded(
shouldAutoplay: shouldShowBackgroundVideo()
)
Expand Down Expand Up @@ -487,20 +497,22 @@ class NewTabPageViewController: UIViewController {
)
}

func setupBackgroundVideoIfNeeded() {
func setupBackgroundVideoIfNeeded(shouldCreatePlayer: Bool) {
videoButtonsView.isHidden = true

// Setup the background video only if:
// - it hasn't been setup before
// - the current NTP background is a video
guard videoPlayer == nil,
let backgroundVideoPath = background.backgroundVideoPath
else {
guard let backgroundVideoPath = background.backgroundVideoPath else {
videoPlayer = nil
backgroundView.resetPlayerLayer()
backgroundButtonsView.resetVideoBackgroundButtons()
return
}

gradientView.isHidden = false
videoPlayer = NewTabPageVideoPlayer(backgroundVideoPath)
if shouldCreatePlayer {
videoPlayer?.createPlayer()
}

backgroundView.setupPlayerLayer(backgroundVideoPath, player: videoPlayer?.player)

videoButtonsView.tappedBackgroundVideo = { [weak videoPlayer] in
Expand Down

0 comments on commit e20246f

Please sign in to comment.