Skip to content

Commit

Permalink
Merge pull request brave/brave-ios#8702 from brave/issues/brave-core-…
Browse files Browse the repository at this point in the history
…35522-related-changes

[ads] Unify iOS kBraveNTPBrandedWallpaper
  • Loading branch information
aseren authored Feb 6, 2024
2 parents 59c4397 + f7228cd commit 4dc3370
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
19 changes: 3 additions & 16 deletions Sources/Brave/Frontend/Browser/HomePanel/NTPDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,6 @@ public class NTPDataSource {
/// This is reset on each launch, so `3` can be shown again if app is removed from memory.
/// This number _must_ be less than the number of backgrounds!
private static let numberOfDuplicateAvoidance = 6
/// The number of images in a sponsorship rotation.
/// i.e. This number will be repeated before a new sponsorship is shown.
/// If sposnored image is shown as Nth image, this number of normal images will be shown before Nth is reached again.
private static let sponsorshipShowRate = 4
/// On this value, a sponsored image will be shown.
private static let sponsorshipShowValue = 2

/// The counter that indicates what background should be shown, this is used to determine when a new
/// sponsored image should be shown. (`1` means, first image in cycle N, should be shown).
/// One example, if rotation is every 4 images, but sponsored image should be shown as 2nd image, then this will
/// be reset back to `1` after reaching `4`, and when the value is `2`, a sponsored image will be shown.
/// This can be easily converted to a preference to persist
private var backgroundRotationCounter = 1

let service: NTPBackgroundImagesService

Expand Down Expand Up @@ -133,7 +120,7 @@ public class NTPDataSource {
if let sponsor = service.sponsoredImageData {
let attemptSponsored =
Preferences.NewTabPage.backgroundSponsoredImages.value
&& backgroundRotationCounter == NTPDataSource.sponsorshipShowValue
&& Preferences.NewTabPage.backgroundRotationCounter.value == service.initialCountToBrandedWallpaper
&& !privateBrowsingManager.isPrivateBrowsing

if attemptSponsored {
Expand Down Expand Up @@ -186,9 +173,9 @@ public class NTPDataSource {
}()

// Force back to `0` if at end
backgroundRotationCounter %= NTPDataSource.sponsorshipShowRate
Preferences.NewTabPage.backgroundRotationCounter.value %= service.countToBrandedWallpaper
// Increment regardless, this is a counter, not an index, so smallest should be `1`
backgroundRotationCounter += 1
Preferences.NewTabPage.backgroundRotationCounter.value += 1

guard let bgWithIndex = backgroundSet[safe: backgroundIndex] else { return nil }
return bgWithIndex
Expand Down
6 changes: 6 additions & 0 deletions Sources/Brave/Frontend/ClientPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ extension Preferences {
/// Whether sponsored images are included into the background image rotation
static let backgroundSponsoredImages = Option<Bool>(key: "newtabpage.background-sponsored-images", default: true)

/// The counter that indicates what background should be shown, this is used to determine when a new
/// sponsored image should be shown. (`1` means, first image in cycle N, should be shown).
/// One example, if rotation is every 4 images, but sponsored image should be shown as 2nd image, then this will
/// be reset back to `1` after reaching `4`, and when the value is `2`, a sponsored image will be shown.
static let backgroundRotationCounter = Option<Int>(key: "newtabpage.background-rotation-count", default: 0)

/// At least one notification must show before we lock showing subsequent notifications.
static let atleastOneNTPNotificationWasShowed = Option<Bool>(
key: "newtabpage.one-notificaiton-showed",
Expand Down

0 comments on commit 4dc3370

Please sign in to comment.