Skip to content

Commit

Permalink
Fixes by review comments #3
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Apr 12, 2024
1 parent 2401e18 commit 287010a
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ enum class ConfirmationType {
// When the user converts on an ad.
kConversion,

// When a new tab page video ad started playing.
// When a new tab page video ad starts playing.
kMediaPlay,

// When played 25% of a new tab page video ad.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,15 @@ class NewTabPageBackgroundButtonsView: UIView, PreferencesObserver {
tappedPlayButton?()
}

@objc private func tappedVideDuringAutoplay() {
@objc private func tappedVideoDuringAutoplay() {
tappedBackgroundDuringAutoplay?()
}

func videoAutoplayStarted() {
let tapGesture = UITapGestureRecognizer(
target: self,
action: #selector(self.tappedVideDuringAutoplay)
action: #selector(self.tappedVideoDuringAutoplay)
)
tapGesture.numberOfTapsRequired = 1
addGestureRecognizer(tapGesture)
playButtonGestureRecognizer = tapGesture
}
Expand All @@ -196,7 +195,7 @@ class NewTabPageBackgroundButtonsView: UIView, PreferencesObserver {
}

private func updatePlayButtonVisibility() {
let isLandscape = frame.width > frame.height
let isLandscape = window?.windowScene?.interfaceOrientation.isLandscape == true

// Hide the play button if the video is in landscape mode on iPhone
if isLandscape && UIDevice.isPhone {
Expand Down Expand Up @@ -281,7 +280,9 @@ extension NewTabPageBackgroundButtonsView {
}
private class PlayButton: SpringButton {
let imageView = UIImageView(
image: UIImage(braveSystemNamed: "leo.play.circle", compatibleWith: nil)!
image: UIImage(braveSystemNamed: "leo.play.circle")!.applyingSymbolConfiguration(
.init(scale: .large)
)
).then {
$0.tintColor = .white
$0.contentMode = .scaleAspectFit
Expand All @@ -305,10 +306,9 @@ extension NewTabPageBackgroundButtonsView {

backgroundView.snp.makeConstraints {
$0.edges.equalToSuperview()
$0.width.equalTo(self.snp.height)
}
imageView.snp.makeConstraints {
$0.width.equalTo(40)
$0.height.equalTo(40)
$0.edges.equalToSuperview().inset(UIEdgeInsets(equalInset: 10))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class NewTabPageBackgroundView: UIView {
bounds = .init(x: -x, y: 0, width: bounds.width, height: bounds.height)
}

func setupPlayerLayer(_ backgroundVideoPath: URL) {
backgroundColor = parseColorFromFilename(
func setupPlayerLayer(_ backgroundVideoPath: URL, player: AVPlayer?) {
playerLayer.player = player
layer.addSublayer(playerLayer)

backgroundColor = parseBackgroundColorFromFilename(
filename: backgroundVideoPath.lastPathComponent
)

let resizeToFill = shouldResizeToFill(filename: backgroundVideoPath.lastPathComponent)
if resizeToFill {
if shouldResizePlayerToFill(filename: backgroundVideoPath.lastPathComponent) {
playerLayer.videoGravity = .resizeAspectFill
} else {
playerLayer.videoGravity = .resizeAspect
Expand All @@ -48,8 +50,6 @@ class NewTabPageBackgroundView: UIView {
return .init(rgb: 0x3b3e4f)
}

layer.addSublayer(playerLayer)

addSubview(imageView)
imageView.snp.makeConstraints {
$0.edges.equalToSuperview()
Expand All @@ -61,31 +61,21 @@ class NewTabPageBackgroundView: UIView {
fatalError()
}

private func parseColorFromFilename(filename: String) -> UIColor {
var color: String?
private func parseBackgroundColorFromFilename(filename: String) -> UIColor {
var colorHex: String?
if let range = filename.range(of: "\\.RGB[a-fA-F0-9]+\\.", options: .regularExpression) {
color = filename[range].replacingOccurrences(of: ".RGB", with: "")
colorHex = filename[range].replacingOccurrences(of: ".RGB", with: "")
.replacingOccurrences(of: ".", with: "")
}

guard let color = color,
color.count == 6
else {
guard let colorHex, colorHex.count == 6 else {
return UIColor.black
}

var rgbValue: UInt64 = 0
Scanner(string: color).scanHexInt64(&rgbValue)

return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
return UIColor(colorString: colorHex)
}

private func shouldResizeToFill(filename: String) -> Bool {
return filename.range(of: "\\.RTF\\.", options: .regularExpression) != nil
private func shouldResizePlayerToFill(filename: String) -> Bool {
return filename.contains(".RTF.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import UIKit
/// The foreground view of the New Tab Page video player. It contains the cancel
/// button and handles user tap gestures to play/pause the video.
class NewTabPageVideoButtonsView: UIView {
var tappedVideoBackground: (() -> Bool)?
var tappedBackgroundVideo: (() -> Bool)?
var tappedCancelButton: (() -> Void)?

private let playPauseButtonImage = BluredImageView().then {
Expand All @@ -33,9 +33,8 @@ class NewTabPageVideoButtonsView: UIView {

let tapGesture = UITapGestureRecognizer(
target: self,
action: #selector(self.videoBackgroundTapped(sender:))
action: #selector(self.backgroundVideoTapped(sender:))
)
tapGesture.numberOfTapsRequired = 1
addGestureRecognizer(tapGesture)

cancelButton.snp.makeConstraints {
Expand All @@ -61,24 +60,29 @@ class NewTabPageVideoButtonsView: UIView {
tappedCancelButton?()
}

@objc private func videoBackgroundTapped(sender: UITapGestureRecognizer) {
@objc private func backgroundVideoTapped(sender: UITapGestureRecognizer) {
let location = sender.location(in: self)
if let view = super.hitTest(location, with: nil), view == cancelButton {
tappedVideoCancelButton()
return
}

guard let playStarted = tappedVideoBackground?() else {
guard let playStarted = tappedBackgroundVideo?() else {
return
}

if playStarted {
playPauseButtonImage.setImage(imageName: "leo.play.circle")
showAndFadeOutImage(imageView: playPauseButtonImage)
playPauseButtonImage.imageView.image = UIImage(braveSystemNamed: "leo.play.circle")!
.applyingSymbolConfiguration(
.init(scale: .large)
)
} else {
playPauseButtonImage.setImage(imageName: "leo.pause.circle")
showAndFadeOutImage(imageView: playPauseButtonImage)
playPauseButtonImage.imageView.image = UIImage(braveSystemNamed: "leo.pause.circle")!
.applyingSymbolConfiguration(
.init(scale: .large)
)
}
showAndFadeOutImage(imageView: playPauseButtonImage)
}

private func showAndFadeOutImage(imageView: UIView) {
Expand Down Expand Up @@ -109,7 +113,9 @@ class NewTabPageVideoButtonsView: UIView {
extension NewTabPageVideoButtonsView {
private class CancelButton: SpringButton {
let imageView = UIImageView(
image: UIImage(braveSystemNamed: "leo.close", compatibleWith: nil)!
image: UIImage(braveSystemNamed: "leo.close")!.applyingSymbolConfiguration(
.init(scale: .small)
)
).then {
$0.tintColor = .white
$0.contentMode = .scaleAspectFit
Expand All @@ -126,17 +132,17 @@ extension NewTabPageVideoButtonsView {
super.init(frame: frame)

clipsToBounds = true
accessibilityLabel = Strings.CancelString

addSubview(backgroundView)

backgroundView.contentView.addSubview(imageView)

backgroundView.snp.makeConstraints {
$0.edges.equalToSuperview()
$0.width.equalTo(self.snp.height)
}
imageView.snp.makeConstraints {
$0.width.equalTo(16)
$0.height.equalTo(16)
$0.edges.equalToSuperview().inset(UIEdgeInsets(equalInset: 4))
}
}
Expand All @@ -155,7 +161,7 @@ extension NewTabPageVideoButtonsView {
$0.clipsToBounds = true
$0.isUserInteractionEnabled = false
}
private let imageView = UIImageView().then {
let imageView = UIImageView().then {
$0.tintColor = .white
$0.contentMode = .scaleAspectFit
}
Expand All @@ -171,10 +177,9 @@ extension NewTabPageVideoButtonsView {

backgroundView.snp.makeConstraints {
$0.edges.equalToSuperview()
$0.width.equalTo(self.snp.height)
}
imageView.snp.makeConstraints {
$0.width.equalTo(40)
$0.height.equalTo(40)
$0.edges.equalToSuperview().inset(UIEdgeInsets(equalInset: 10))
}
}
Expand All @@ -184,10 +189,6 @@ extension NewTabPageVideoButtonsView {
layer.cornerRadius = bounds.height / 2.0
}

func setImage(imageName: String) {
imageView.image = UIImage(braveSystemNamed: imageName, compatibleWith: nil)!
}

@available(*, unavailable)
required init(coder: NSCoder) {
fatalError()
Expand Down
Loading

0 comments on commit 287010a

Please sign in to comment.