Skip to content

Commit

Permalink
Refactor FXIOS-10718 #23420 [sponsored tiles] Integrate unified ads A…
Browse files Browse the repository at this point in the history
…PI (#23460)

* Integrate Unified Tiles into Sponsored tiles in Top sites

* Swiftlint

* Fix stuff

* Apply suggestions from code review

Co-authored-by: Issam Mani <[email protected]>

---------

Co-authored-by: Issam Mani <[email protected]>
  • Loading branch information
lmarceau and issammani authored Nov 29, 2024
1 parent 3f1f216 commit f3e5d55
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
4 changes: 4 additions & 0 deletions firefox-ios/Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@
8A4AC0EC28C929D700439F83 /* URLSessionProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A4AC0EA28C929D700439F83 /* URLSessionProtocol.swift */; };
8A4B14852CF8D67800FCE2D0 /* UnifiedTile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A4B14842CF8D67300FCE2D0 /* UnifiedTile.swift */; };
8A4B14872CF8D81800FCE2D0 /* UnifiedAdsProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A4B14862CF8D80F00FCE2D0 /* UnifiedAdsProviderTests.swift */; };
8A4B148B2CF919C800FCE2D0 /* UnifiedAdsConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A4B148A2CF919C300FCE2D0 /* UnifiedAdsConverter.swift */; };
8A4EA0D12C010BE700E4E4F1 /* MicrosurveySurfaceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A4EA0D02C010BE700E4E4F1 /* MicrosurveySurfaceManager.swift */; };
8A4EA0D42C01100200E4E4F1 /* MicrosurveySurfaceManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A4EA0D22C010BF800E4E4F1 /* MicrosurveySurfaceManagerTests.swift */; };
8A4EA0D92C01127C00E4E4F1 /* MicrosurveyMockModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A4EA0D72C01125100E4E4F1 /* MicrosurveyMockModel.swift */; };
Expand Down Expand Up @@ -7412,6 +7413,7 @@
8A4AC0EA28C929D700439F83 /* URLSessionProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLSessionProtocol.swift; sourceTree = "<group>"; };
8A4B14842CF8D67300FCE2D0 /* UnifiedTile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnifiedTile.swift; sourceTree = "<group>"; };
8A4B14862CF8D80F00FCE2D0 /* UnifiedAdsProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnifiedAdsProviderTests.swift; sourceTree = "<group>"; };
8A4B148A2CF919C300FCE2D0 /* UnifiedAdsConverter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnifiedAdsConverter.swift; sourceTree = "<group>"; };
8A4EA0D02C010BE700E4E4F1 /* MicrosurveySurfaceManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MicrosurveySurfaceManager.swift; sourceTree = "<group>"; };
8A4EA0D22C010BF800E4E4F1 /* MicrosurveySurfaceManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MicrosurveySurfaceManagerTests.swift; sourceTree = "<group>"; };
8A4EA0D72C01125100E4E4F1 /* MicrosurveyMockModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MicrosurveyMockModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -12056,6 +12058,7 @@
isa = PBXGroup;
children = (
8A7892042CF91FE100490CA4 /* UnifiedAdsCallbackTelemetry.swift */,
8A4B148A2CF919C300FCE2D0 /* UnifiedAdsConverter.swift */,
8A4B14842CF8D67300FCE2D0 /* UnifiedTile.swift */,
8AE9FD252CF662FF001053EE /* UnifiedAdsProvider.swift */,
);
Expand Down Expand Up @@ -16404,6 +16407,7 @@
0B3A153A2CE1FE710049746C /* PullRefreshView.swift in Sources */,
8CE1E4392B8C76C80026530B /* LoginCellView.swift in Sources */,
D3B6923D1B9F9444004B87A4 /* FindInPageBar.swift in Sources */,
8A4B148B2CF919C800FCE2D0 /* UnifiedAdsConverter.swift in Sources */,
1D5CBF492B17E3CB0001D033 /* NotificationPayloads.swift in Sources */,
2F44FC721A9E840300FD20CC /* SettingsNavigationController.swift in Sources */,
AB9CBC052C53B64C00102610 /* TrackingProtectionState.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TopSitesDataAdaptorImplementation: TopSitesDataAdaptor, FeatureFlaggable {
private let topSiteHistoryManager: TopSiteHistoryManager
private let googleTopSiteManager: GoogleTopSiteManager
private let contileProvider: ContileProviderInterface
private let unifiedAdsProvider: UnifiedAdsProviderInterface
private let dispatchGroup: DispatchGroupInterface

// Pre-loading the data with a default number of tiles so we always show section when needed
Expand All @@ -49,13 +50,15 @@ class TopSitesDataAdaptorImplementation: TopSitesDataAdaptor, FeatureFlaggable {
topSiteHistoryManager: TopSiteHistoryManager,
googleTopSiteManager: GoogleTopSiteManager,
contileProvider: ContileProviderInterface = ContileProvider(),
unifiedAdsProvider: UnifiedAdsProviderInterface = UnifiedAdsProvider(),
notificationCenter: NotificationProtocol = NotificationCenter.default,
dispatchGroup: DispatchGroupInterface = DispatchGroup()
) {
self.profile = profile
self.topSiteHistoryManager = topSiteHistoryManager
self.googleTopSiteManager = googleTopSiteManager
self.contileProvider = contileProvider
self.unifiedAdsProvider = unifiedAdsProvider
self.notificationCenter = notificationCenter
self.dispatchGroup = dispatchGroup
topSiteHistoryManager.delegate = self
Expand Down Expand Up @@ -124,13 +127,25 @@ class TopSitesDataAdaptorImplementation: TopSitesDataAdaptor, FeatureFlaggable {
guard shouldLoadSponsoredTiles else { return }

dispatchGroup.enter()
contileProvider.fetchContiles { [weak self] result in
if case .success(let contiles) = result {
self?.contiles = contiles
} else {
self?.contiles = []

if featureFlags.isFeatureEnabled(.unifiedAds, checking: .buildOnly) {
unifiedAdsProvider.fetchTiles { [weak self] result in
if case .success(let unifiedTiles) = result {
self?.contiles = UnifiedAdsConverter.convert(unifiedTiles: unifiedTiles)
} else {
self?.contiles = []
}
self?.dispatchGroup.leave()
}
} else {
contileProvider.fetchContiles { [weak self] result in
if case .success(let contiles) = result {
self?.contiles = contiles
} else {
self?.contiles = []
}
self?.dispatchGroup.leave()
}
self?.dispatchGroup.leave()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/

/// To keep the integration of unified ads simple with minimal changes, we are converting Unified tiles
/// into the old API Contile object. This will avoid logic changes inside the TopSitesDataAdaptor, which will be deprecated
/// soon anyway due to the refactor of the homepage.
struct UnifiedAdsConverter {
static func convert(unifiedTiles: [UnifiedTile]) -> [Contile] {
return unifiedTiles.enumerated().map { (index, tile) in
Contile(
id: 0, // Was relevant for old telemetry, but not with unified ads
name: tile.name,
url: tile.url,
clickUrl: tile.callbacks.click,
imageUrl: tile.imageUrl,
imageSize: 0, // Zero since not used anyway
impressionUrl: tile.callbacks.impression,
position: index + 1 // Keeping the same tile position as provided in the array
)
}
}
}

0 comments on commit f3e5d55

Please sign in to comment.