Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #2636: Implement UI for Brave Ads state level targeting (#2637)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson committed Aug 5, 2020
1 parent 5ed5e0a commit a3eb69f
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 4 deletions.
Binary file modified BraveRewards/BraveRewards.framework/BraveRewards
Binary file not shown.
9 changes: 9 additions & 0 deletions BraveRewards/BraveRewards.framework/Headers/BATBraveAds.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ NS_SWIFT_NAME(BraveAds)
/// The max number of ads the user can see in a day
@property (nonatomic, assign) NSInteger numberOfAllowableAdsPerDay NS_SWIFT_NAME(adsPerDay);

/// Whether or not the user has opted out of subdivision ad targeting
@property (nonatomic, assign, getter=shouldAllowSubdivisionTargeting) BOOL allowSubdivisionTargeting;

/// Selected ads subdivision targeting option
@property (nonatomic, copy) NSString * subdivisionTargetingCode;

/// Automatically detected ads subdivision targeting code
@property (nonatomic, copy) NSString * automaticallyDetectedSubdivisionTargetingCode;

/// The user model locales Brave Ads supports currently
@property (nonatomic, readonly) NSArray<NSString *> *userModelLanguages;

Expand Down
Binary file modified BraveRewards/BraveRewards.framework/Info.plist
Binary file not shown.
1 change: 1 addition & 0 deletions BraveRewardsUI/Localized Strings/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ internal extension Strings {
static let noActivitiesYet = NSLocalizedString("BraveRewardsNoActivitiesYet", bundle: .rewardsUI, value: "No activities yet…", comment: "")
static let adsMaxPerHour = NSLocalizedString("BraveRewardsAdsMaxPerDay", bundle: .rewardsUI, value: "Maximum number of ads displayed", comment: "")
static let numberOfAdsPerHourOptionsTitle = NSLocalizedString("BraveRewardsNumberOfAdsPerHourOptionsTitle", bundle: .rewardsUI, value: "Ads per hour", comment: "")
static let adsSubdivisionTargeting = NSLocalizedString("BraveRewardsAdsSubdivisionTargeting", bundle: .rewardsUI, value: "State level ad targeting", comment: "")
static let adsEstimatedEarnings = NSLocalizedString("BraveRewardsAdsEstimatedEarnings", bundle: .rewardsUI, value: "Estimated pending rewards", comment: "")
static let nextPaymentDate = NSLocalizedString("BraveRewardsPaymentDate", bundle: .rewardsUI, value: "Next payment date", comment: "")
static let adNotificationsReceived = NSLocalizedString("BraveRewardsAdNotificationsReceived", bundle: .rewardsUI, value: "Ad notifications received this month", comment: "")
Expand Down
109 changes: 105 additions & 4 deletions BraveRewardsUI/Settings/Ads/Details/AdsDetailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,23 @@ class AdsDetailsViewController: UIViewController {

title = Strings.settingsAdsTitle

generateRows()

fetchAdsDetails()
}

func generateRows() {
rows.removeAll()

rows.append(.adsPerHour)
if state.ads.shouldAllowSubdivisionTargeting {
rows.append(.subdivisionTargeting)
}
rows.append(.currentEarnings)
rows.append(.nextPayment)
rows.append(.numberOfAdsReceived)
}

func fetchAdsDetails() {
state.ledger.adsDetailsForCurrentCycle { [weak self] adsReceived, estimatedEarnings, nextPaymentDate in
self?.updateAdsInfo(adsReceived: adsReceived, estimatedEarnings: estimatedEarnings, nextPaymentDate: nextPaymentDate)
Expand Down Expand Up @@ -82,14 +96,73 @@ class AdsDetailsViewController: UIViewController {
Strings.fourAdsPerHour,
Strings.fiveAdsPerHour
]


// TODO(https://github.com/brave/brave-browser/issues/10316) return subdivisions from ads lib
private let subdivisionTargetingOptions: KeyValuePairs = [
"AUTO": "Auto-detect",
"DISABLED": "Disable",
"US-AL": "Alabama",
"US-AK": "Alaska",
"US-AZ": "Arizona",
"US-AR": "Arkansas",
"US-CA": "California",
"US-CO": "Colorado",
"US-CT": "Connecticut",
"US-DE": "Delaware",
"US-FL": "Florida",
"US-GA": "Georgia",
"US-HI": "Hawaii",
"US-ID": "Idaho",
"US-IL": "Illinois",
"US-IN": "Indiana",
"US-IA": "Iowa",
"US-KS": "Kansas",
"US-KY": "Kentucky",
"US-LA": "Louisiana",
"US-ME": "Maine",
"US-MD": "Maryland",
"US-MA": "Massachusetts",
"US-MI": "Michigan",
"US-MN": "Minnesota",
"US-MS": "Mississippi",
"US-MO": "Missouri",
"US-MT": "Montana",
"US-NE": "Nebraska",
"US-NV": "Nevada",
"US-NH": "New Hampshire",
"US-NJ": "New Jersey",
"US-NM": "New Mexico",
"US-NY": "New York",
"US-NC": "North Carolina",
"US-ND": "North Dakota",
"US-OH": "Ohio",
"US-OK": "Oklahoma",
"US-OR": "Oregon",
"US-PA": "Pennsylvania",
"US-RI": "Rhode Island",
"US-SC": "South Carolina",
"US-SD": "South Dakota",
"US-TN": "Tennessee",
"US-TX": "Texas",
"US-UT": "Utah",
"US-VT": "Vermont",
"US-VA": "Virginia",
"US-WA": "Washington",
"US-WV": "West Virginia",
"US-WI": "Wisconsin",
"US-WY": "Wyoming"
]

private var adsReceived: Int = 0
private var estimatedEarnings: Double = 0.0

private var rows: [Row] = []
}

extension AdsDetailsViewController: UITableViewDelegate, UITableViewDataSource {
private enum Row: Int, CaseIterable {
case adsPerHour
case subdivisionTargeting
case currentEarnings
case nextPayment
case numberOfAdsReceived
Expand All @@ -98,7 +171,7 @@ extension AdsDetailsViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)

guard let row = Row(rawValue: indexPath.row) else { fatalError() }
guard let row = rows[safe: indexPath.row] else { fatalError() }

if row == .adsPerHour {
let choices = [Int](1..<adsPerHourOptions.count+1)
Expand All @@ -114,15 +187,27 @@ extension AdsDetailsViewController: UITableViewDelegate, UITableViewDataSource {
}
controller.title = Strings.numberOfAdsPerHourOptionsTitle
navigationController?.pushViewController(controller, animated: true)
} else if row == .subdivisionTargeting {
guard let selectedIndex = subdivisionTargetingOptions.firstIndex(where: { $0.0 == state.ads.subdivisionTargetingCode }) else { return }

let controller = OptionsSelectionViewController(
options: subdivisionTargetingOptions.map { $0.value },
selectedOptionIndex: selectedIndex) { [weak self] (selectedIndex) in
guard let self = self else { return }
self.state.ads.subdivisionTargetingCode = self.subdivisionTargetingOptions[selectedIndex].0
self.navigationController?.popViewController(animated: true)
}
controller.title = Strings.adsSubdivisionTargeting
navigationController?.pushViewController(controller, animated: true)
}
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Row.allCases.count
return rows.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let row = Row(rawValue: indexPath.row) else { fatalError() }
guard let row = rows[safe: indexPath.row] else { fatalError() }
let cell = tableView.dequeueReusableCell(for: indexPath) as Value1TableViewCell
cell.label.font = SettingsUX.bodyFont
cell.label.numberOfLines = 0
Expand All @@ -137,6 +222,22 @@ extension AdsDetailsViewController: UITableViewDelegate, UITableViewDataSource {
if adsPerHour - 1 < adsPerHourOptions.count {
cell.accessoryLabel?.text = adsPerHourOptions[adsPerHour - 1]
}
case .subdivisionTargeting:
cell.accessoryType = .disclosureIndicator

cell.label.text = Strings.adsSubdivisionTargeting

var adsSubdivisionTargetingCode: String
if state.ads.subdivisionTargetingCode == "DISABLED" {
adsSubdivisionTargetingCode = "Disabled"
} else if state.ads.subdivisionTargetingCode == "AUTO" {
adsSubdivisionTargetingCode = state.ads.automaticallyDetectedSubdivisionTargetingCode
} else {
adsSubdivisionTargetingCode = state.ads.subdivisionTargetingCode
}

guard let selectedIndex = subdivisionTargetingOptions.firstIndex(where: { $0.0 == adsSubdivisionTargetingCode }) else { fatalError() }
cell.accessoryLabel?.text = subdivisionTargetingOptions[selectedIndex].1
case .currentEarnings:
cell.label.text = Strings.adsEstimatedEarnings
cell.selectionStyle = .none
Expand Down

0 comments on commit a3eb69f

Please sign in to comment.