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

Commit

Permalink
Fix #8254, Fix #8251: Ensure Brave News uses the correct Ads instance (
Browse files Browse the repository at this point in the history
…#8256)

When Rewards or Brave News is turned off the `BraveAds` instance is replaced, however `FeedDataSource` could still hold onto a reference to the old instance which could cause a crash if opting into News via Brave News settings and if opted in via the NTP it could cause ads to not appear since the old ads instance is not initialized.
  • Loading branch information
kylehickinson committed Oct 16, 2023
1 parent 3c3c502 commit 8575a82
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ public class BrowserViewController: UIViewController {
}
}

self.feedDataSource.ads = rewards.ads
self.feedDataSource.getAdsAPI = {
// The ads object gets re-recreated when shutdown, so we need to make sure News fetches it out of
// the BraveRewards container
return rewards.ads
}

// Observer watching tab information is sent by another device
openTabsModelStateListener = braveCore.sendTabAPI.add(
Expand Down
4 changes: 2 additions & 2 deletions Sources/BraveNews/Composer/FeedDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public class FeedDataSource: ObservableObject {
}

/// An ads object to handle inserting Inline Content Ads within the Brave News sequence
public var ads: BraveAds?
public var getAdsAPI: (() -> BraveAds)?
public var historyAPI: BraveHistoryAPI?

private let todayQueue = DispatchQueue(label: "com.brave.today")
Expand Down Expand Up @@ -939,7 +939,7 @@ public class FeedDataSource: ObservableObject {
followedSources: followedSources,
hiddenSources: hiddenSources,
followedChannels: followedChannels.mapValues(Set.init),
ads: ads
ads: getAdsAPI?()
)
// Move to OSSignposter when we're 15+
let log = OSLog(subsystem: Bundle.main.bundleIdentifier ?? "com.brave.ios", category: "Brave News")
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraveNews/Customize/NewsSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public struct NewsSettingsView: View {
OptInView { @MainActor in
Preferences.BraveNews.isShowingOptIn.value = false
// Initialize ads if it hasn't already been done
await dataSource.ads?.initialize()
await dataSource.getAdsAPI?().initialize()
if dataSource.isSourcesExpired {
await withCheckedContinuation { c in
dataSource.load {
Expand Down

0 comments on commit 8575a82

Please sign in to comment.