Skip to content

Commit

Permalink
Fix brave/brave-ios#8098: Allow enabling Rewards if Ads is previously…
Browse files Browse the repository at this point in the history
… started by News (brave/brave-ios#8099)
  • Loading branch information
kylehickinson authored Sep 20, 2023
1 parent 261c511 commit d21f87c
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions Sources/Brave/Frontend/Rewards/BraveRewards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@ public class BraveRewards: NSObject {
}
}

private(set) var isAdsInitialized: Bool = false
private func fetchWalletAndInitializeAds(toggleAds: Bool? = nil) {
if isAdsInitialized {
return
}
isAdsInitialized = true
guard let rewardsAPI = rewardsAPI else { return }
rewardsAPI.currentWalletInfo { wallet in
var walletInfo: BraveAds.WalletInfo?
Expand All @@ -82,13 +77,19 @@ public class BraveRewards: NSObject {
recoverySeed: Data(seed).base64EncodedString()
)
}
// If ads is already initialized just toggle rewards ads and update the wallet info
if self.ads.isAdsServiceRunning() {
if let walletInfo {
self.ads.updateWalletInfo(walletInfo.paymentId, base64Seed: walletInfo.recoverySeed)
}
if let toggleAds {
self.ads.isEnabled = toggleAds
}
return
}
self.ads.initialize(walletInfo: walletInfo) { success in
if !success {
self.isAdsInitialized = false
} else {
if let toggleAds {
self.ads.isEnabled = toggleAds
}
if success, let toggleAds {
self.ads.isEnabled = toggleAds
}
}
}
Expand All @@ -97,7 +98,7 @@ public class BraveRewards: NSObject {
private var braveNewsObservation: AnyCancellable?

private var shouldShutdownAds: Bool {
ads.isAdsServiceRunning() && isAdsInitialized && !ads.isEnabled && !Preferences.BraveNews.isEnabled.value
ads.isAdsServiceRunning() && !ads.isEnabled && !Preferences.BraveNews.isEnabled.value
}

/// Propose that the ads service should be shutdown based on whether or not that all features
Expand All @@ -106,7 +107,6 @@ public class BraveRewards: NSObject {
if !shouldShutdownAds { return }
ads.shutdown {
self.ads = BraveAds(stateStoragePath: self.configuration.storageURL.appendingPathComponent("ads").path)
self.isAdsInitialized = false
}
}

Expand Down Expand Up @@ -160,7 +160,7 @@ public class BraveRewards: NSObject {
try? FileManager.default.removeItem(
at: configuration.storageURL.appendingPathComponent("ledger")
)
if ads.isAdsServiceRunning(), isAdsInitialized, !Preferences.BraveNews.isEnabled.value {
if ads.isAdsServiceRunning(), !Preferences.BraveNews.isEnabled.value {
ads.shutdown { [self] in
try? FileManager.default.removeItem(
at: configuration.storageURL.appendingPathComponent("ads")
Expand All @@ -186,7 +186,7 @@ public class BraveRewards: NSObject {
rewardsAPI?.selectedTabId = UInt32(tabId)
tabRetrieved(tabId, url: url, html: nil)
}
if isAdsInitialized && !isPrivate {
if ads.isAdsServiceRunning() && !isPrivate {
ads.reportTabUpdated(tabId, url: url, redirectedFrom: tab.redirectURLs, isSelected: isSelected)
}
}
Expand All @@ -203,7 +203,7 @@ public class BraveRewards: NSObject {
adsInnerText: String?
) {
tabRetrieved(tabId, url: url, html: html)
if let innerText = adsInnerText, isAdsInitialized {
if let innerText = adsInnerText, ads.isAdsServiceRunning() {
ads.reportLoadedPage(
with: url,
redirectedFrom: redirectionURLs ?? [],
Expand All @@ -227,13 +227,13 @@ public class BraveRewards: NSObject {

/// Report that media has started on a tab with a given id
func reportMediaStarted(tabId: Int) {
if !isAdsInitialized { return }
if !ads.isAdsServiceRunning() { return }
ads.reportMediaStarted(tabId: tabId)
}

/// Report that media has stopped on a tab with a given id
func reportMediaStopped(tabId: Int) {
if !isAdsInitialized { return }
if !ads.isAdsServiceRunning() { return }
ads.reportMediaStopped(tabId: tabId)
}

Expand All @@ -244,7 +244,7 @@ public class BraveRewards: NSObject {

/// Report that a tab with a given id was closed by the user
func reportTabClosed(tabId: Int) {
if isAdsInitialized {
if ads.isAdsServiceRunning() {
ads.reportTabClosed(tabId: tabId)
}
rewardsAPI?.reportTabNavigationOrClosed(tabId: UInt32(tabId))
Expand Down

0 comments on commit d21f87c

Please sign in to comment.