From cc1dcabe7df5614036e23efcd2645a2ddc70ced7 Mon Sep 17 00:00:00 2001 From: Soner Yuksel Date: Fri, 22 Dec 2023 14:55:04 -0500 Subject: [PATCH] Adding linage and showing proper feature handle screen --- .../BrowserViewController/BVC+Menu.swift | 2 +- .../BrowserViewController.swift | 8 +++++ Sources/BraveStrings/BraveStrings.swift | 1 - Sources/Growth/URP/AttributionManager.swift | 34 +++++++++++++++---- .../Welcome/WelcomeViewController.swift | 4 +-- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Menu.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Menu.swift index c5691c39e3b..0c889177456 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Menu.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Menu.swift @@ -208,7 +208,7 @@ extension BrowserViewController { } } - private func presentPlaylistController() { + public func presentPlaylistController() { if PlaylistCarplayManager.shared.isPlaylistControllerPresented { let alert = UIAlertController(title: Strings.PlayList.playlistAlreadyShowingTitle, message: Strings.PlayList.playlistAlreadyShowingBody, diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift index d4f9bfefac2..3e1f4417834 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift @@ -959,6 +959,14 @@ public class BrowserViewController: UIViewController { .removeDuplicates() .sink(receiveValue: { [weak self] featureLinkageType in guard let self = self else { return } + switch featureLinkageType { + case .playlist: + self.presentPlaylistController() + case .vpn: + self.navigationHelper.openVPNBuyScreen(iapObserver: self.iapObserver) + default: + return + } print("Feature is linked \(featureLinkageType)") }) diff --git a/Sources/BraveStrings/BraveStrings.swift b/Sources/BraveStrings/BraveStrings.swift index a09c76e62f2..88cd7f3b13c 100644 --- a/Sources/BraveStrings/BraveStrings.swift +++ b/Sources/BraveStrings/BraveStrings.swift @@ -1237,7 +1237,6 @@ extension Strings { public static let requestCaptureDevicePermissionAllowButtonTitle = NSLocalizedString("requestCaptureDevicePermissionAllowButtonTitle", tableName: "BraveShared", bundle: .module, value: "Allow", comment: "A button shown in a permission dialog that will grant the website the ability to use the device's camera or microphone") public static let downloadsMenuItem = NSLocalizedString("DownloadsMenuItem", tableName: "BraveShared", bundle: .module, value: "Downloads", comment: "Title for downloads menu item") public static let downloadsPanelEmptyStateTitle = NSLocalizedString("DownloadsPanelEmptyStateTitle", tableName: "BraveShared", bundle: .module, value: "Downloaded files will show up here.", comment: "Title for when a user has nothing downloaded onto their device, and the list is empty.") - public static let playlistMenuItem = NSLocalizedString("PlaylistMenuItem", tableName: "BraveShared", bundle: .module, value: "Playlist", comment: "Playlist menu item") // MARK: - Themes diff --git a/Sources/Growth/URP/AttributionManager.swift b/Sources/Growth/URP/AttributionManager.swift index 3a50e0f1985..a20556757d1 100644 --- a/Sources/Growth/URP/AttributionManager.swift +++ b/Sources/Growth/URP/AttributionManager.swift @@ -8,8 +8,19 @@ import Preferences import Combine import Shared -public enum FeatureLinkageType { - case undefined, vpn, playlist +public enum FeatureLinkageType: CaseIterable { + case notdefined, vpn, playlist, leoAI + + var adKeywords: [String] { + switch self { + case .vpn: + return ["vpn, 1.1.1.1"] + case .playlist: + return ["youtube", "video player", "playlist"] + default: + return [] // Return nil for any other case + } + } } public enum FeatureLinkageError: Error { @@ -24,7 +35,7 @@ public class AttributionManager { /// The default Install Referral Code private let organicInstallReferralCode = "BRV001" - @Published public var adFeatureLinkage: FeatureLinkageType = .undefined + @Published public var adFeatureLinkage: FeatureLinkageType = .notdefined public init(dau: DAU, urp: UserReferralProgram) { self.dau = dau @@ -51,7 +62,7 @@ public class AttributionManager { } } - @MainActor public func handleAdsReportingFeatureLinkage() async throws -> (keyword: String, attributionData: AdAttributionData) { + @MainActor public func handleAdsReportingFeatureLinkage() async throws -> (featureType: FeatureLinkageType, attributionData: AdAttributionData) { // This function should run multiple tasks first adCampaignLookup // and adReportsKeywordLookup depending on adCampaignLookup result. // There is maximum threshold of 5 sec for all the tasks to be completed @@ -73,11 +84,13 @@ public class AttributionManager { let task2Timeout = DispatchTime.now() + .seconds(Int(remainingTime)) - let keywordResult = try await withCheckedThrowingContinuation { continuation in + let featureTypeResult = try await withCheckedThrowingContinuation { continuation in Task.detached { do { + self.generateReferralCodeAndPingServer(with: attributionData) let keyword = try await self.urp.adReportsKeywordLookup(attributionData: attributionData) - continuation.resume(returning: keyword) + let featureLinkageType = self.fetchFeatureTypes(for: keyword) + continuation.resume(returning: featureLinkageType) } catch { continuation.resume(throwing: SearchAdError.successfulCampaignFailedKeywordLookup(attributionData)) } @@ -87,12 +100,19 @@ public class AttributionManager { continuation.resume(throwing: FeatureLinkageError.executionTimeout(attributionData)) } } - return (keywordResult, attributionData) + return (featureTypeResult, attributionData) } catch { throw error } } + private func fetchFeatureTypes(for keyword: String) -> FeatureLinkageType { + for linkageType in FeatureLinkageType.allCases where linkageType.adKeywords.contains(keyword) { + return linkageType + } + return .notdefined + } + public func setupReferralCodeAndPingServer(refCode: String? = nil) { let refCode = refCode ?? organicInstallReferralCode diff --git a/Sources/Onboarding/Welcome/WelcomeViewController.swift b/Sources/Onboarding/Welcome/WelcomeViewController.swift index 4267014794b..a87f4f00ca9 100644 --- a/Sources/Onboarding/Welcome/WelcomeViewController.swift +++ b/Sources/Onboarding/Welcome/WelcomeViewController.swift @@ -389,8 +389,8 @@ public class WelcomeViewController: UIViewController { Task { @MainActor in do { // Handle API calls and send linkage type - let featureType = try await controller.attributionManager.handleAdsReportingFeatureLinkage() -// controller.attributionManager.adFeatureLinkage = featureType! + let reportLink = try await controller.attributionManager.handleAdsReportingFeatureLinkage() + controller.attributionManager.adFeatureLinkage = reportLink.featureType controller.calloutView.isLoading = false self?.close()