This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #3872: Adds Brave News Display Ads
- Loading branch information
1 parent
9730006
commit 61f681e
Showing
12 changed files
with
474 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2021 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
|
||
class AdCardView: FeedCardBackgroundButton, FeedCardContent { | ||
var actionHandler: ((Int, FeedItemAction) -> Void)? | ||
var contextMenu: FeedItemMenu? | ||
|
||
let feedView = FeedItemView(layout: .ad).then { | ||
$0.thumbnailImageView.contentMode = .scaleAspectFit | ||
} | ||
|
||
private let adCalloutView = BraveAdCalloutView() | ||
private var contextMenuDelegate: NSObject? | ||
|
||
required init() { | ||
super.init(frame: .zero) | ||
|
||
addSubview(feedView) | ||
feedView.snp.makeConstraints { | ||
$0.edges.equalToSuperview() | ||
} | ||
|
||
addTarget(self, action: #selector(tappedSelf), for: .touchUpInside) | ||
feedView.callToActionButton.addTarget(self, action: #selector(tappedSelf), for: .touchUpInside) | ||
|
||
let contextMenuDelegate = FeedContextMenuDelegate( | ||
performedPreviewAction: { [weak self] in | ||
self?.actionHandler?(0, .opened()) | ||
}, | ||
menu: { [weak self] in | ||
return self?.contextMenu?.menu?(0) | ||
} | ||
) | ||
addInteraction(UIContextMenuInteraction(delegate: contextMenuDelegate)) | ||
self.contextMenuDelegate = contextMenuDelegate | ||
|
||
isAccessibilityElement = false | ||
accessibilityElements = [feedView, feedView.callToActionButton] | ||
feedView.accessibilityTraits.insert(.button) | ||
shouldGroupAccessibilityChildren = true | ||
|
||
addSubview(adCalloutView) | ||
adCalloutView.snp.makeConstraints { | ||
$0.top.trailing.equalToSuperview().inset(8) | ||
$0.leading.greaterThanOrEqualToSuperview().inset(8) | ||
$0.bottom.lessThanOrEqualToSuperview().inset(8) | ||
} | ||
} | ||
|
||
override var accessibilityLabel: String? { | ||
get { feedView.accessibilityLabel } | ||
set { assertionFailure("Accessibility label is inherited from a subview: \(String(describing: newValue)) ignored") } | ||
} | ||
|
||
@objc private func tappedSelf() { | ||
actionHandler?(0, .opened()) | ||
} | ||
} | ||
|
||
private class BraveAdCalloutView: UIView { | ||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
|
||
backgroundColor = .white | ||
layer.cornerRadius = 4 | ||
layer.cornerCurve = .continuous | ||
layer.borderColor = UIColor.braveLighterBlurple.cgColor | ||
layer.borderWidth = 1 | ||
layer.masksToBounds = true | ||
|
||
let stackView = UIStackView() | ||
stackView.spacing = 3 | ||
stackView.alignment = .center | ||
stackView.layoutMargins = .init(equalInset: 5) | ||
stackView.isLayoutMarginsRelativeArrangement = true | ||
stackView.addStackViewItems( | ||
.view(UIImageView(image: UIImage(imageLiteralResourceName: "bat-small")).then { | ||
$0.contentMode = .scaleAspectFit | ||
$0.snp.makeConstraints { | ||
$0.size.equalTo(14) | ||
} | ||
}), | ||
.view(UILabel().then { | ||
$0.text = "Ad" | ||
$0.textColor = .braveBlurple | ||
$0.font = { | ||
let metrics = UIFontMetrics(forTextStyle: .footnote) | ||
let desc = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .footnote) | ||
let font = UIFont.systemFont(ofSize: desc.pointSize, weight: .semibold) | ||
return metrics.scaledFont(for: font) | ||
}() | ||
$0.adjustsFontForContentSizeCategory = true | ||
}) | ||
) | ||
addSubview(stackView) | ||
stackView.snp.makeConstraints { | ||
$0.edges.equalToSuperview() | ||
} | ||
} | ||
@available(*, unavailable) | ||
required init(coder: NSCoder) { | ||
fatalError() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.