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

[ads] Unify iOS kBraveNTPBrandedWallpaper #8702

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading