diff --git a/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageViewController.swift b/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageViewController.swift index d0507a172c2..33d7379ef8a 100644 --- a/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageViewController.swift +++ b/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageViewController.swift @@ -199,13 +199,15 @@ class NewTabPageViewController: UIViewController { }), ] + var isBackgroundNTPSI = false + if let ntpBackground = background.currentBackground, case .sponsoredImage = ntpBackground { + isBackgroundNTPSI = true + } + let ntpDefaultBrowserCalloutProvider = NTPDefaultBrowserCalloutProvider(isBackgroundNTPSI: isBackgroundNTPSI) + // This is a one-off view, adding it to the NTP only if necessary. - if NTPDefaultBrowserCalloutProvider.shouldShowCallout { - // Never show Default Browser Notification over an NPT SI - if let ntpBackground = background.currentBackground, case .sponsoredImage = ntpBackground { - return - } - sections.insert(NTPDefaultBrowserCalloutProvider(), at: 0) + if ntpDefaultBrowserCalloutProvider.shouldShowCallout() { + sections.insert(ntpDefaultBrowserCalloutProvider, at: 0) } if !privateBrowsingManager.isPrivateBrowsing { diff --git a/Sources/Brave/Frontend/Browser/New Tab Page/Sections/NTPDefaultBrowserCalloutProvider.swift b/Sources/Brave/Frontend/Browser/New Tab Page/Sections/NTPDefaultBrowserCalloutProvider.swift index 271431ff8af..867ad1922d8 100644 --- a/Sources/Brave/Frontend/Browser/New Tab Page/Sections/NTPDefaultBrowserCalloutProvider.swift +++ b/Sources/Brave/Frontend/Browser/New Tab Page/Sections/NTPDefaultBrowserCalloutProvider.swift @@ -12,41 +12,19 @@ import UIKit class NTPDefaultBrowserCalloutProvider: NSObject, NTPObservableSectionProvider { var sectionDidChange: (() -> Void)? private var defaultCalloutView = DefaultBrowserCalloutView() + private let isBackgroundNTPSI: Bool private typealias DefaultBrowserCalloutCell = NewTabCenteredCollectionViewCell - static var shouldShowCallout: Bool { - let defaultBrowserDisplayCriteria = !Preferences.General.defaultBrowserCalloutDismissed.value && - !Preferences.Onboarding.basicOnboardingDefaultBrowserSelected.value && - AppConstants.buildChannel == .release - - guard let appRetentionLaunchDate = Preferences.DAU.appRetentionLaunchDate.value else { - return defaultBrowserDisplayCriteria - } - - // User should not see default browser first 7 days - // also after 14 days - var defaultBrowserTimeConstraintCriteria = false - - let rightNow = Date() - let first7DayPeriod = appRetentionLaunchDate.addingTimeInterval( - AppConstants.buildChannel.isPublic ? 7.days : 7.minutes) - let first14DayPeriod = appRetentionLaunchDate.addingTimeInterval( - AppConstants.buildChannel.isPublic ? 17.days : 14.minutes) - - if rightNow > first7DayPeriod, rightNow < first14DayPeriod { - defaultBrowserTimeConstraintCriteria = true - } - - return defaultBrowserDisplayCriteria && defaultBrowserTimeConstraintCriteria + // MARK: Lifecycle + init(isBackgroundNTPSI: Bool) { + self.isBackgroundNTPSI = isBackgroundNTPSI } - func registerCells(to collectionView: UICollectionView) { - collectionView.register(DefaultBrowserCalloutCell.self) - } + // MARK: UICollectionViewDelegate func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - Self.shouldShowCallout ? 1 : 0 + shouldShowCallout() ? 1 : 0 } func collectionView( @@ -76,13 +54,45 @@ class NTPDefaultBrowserCalloutProvider: NSObject, NTPObservableSectionProvider { } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { - if !Self.shouldShowCallout { + if !shouldShowCallout() { return .zero } return UIEdgeInsets(top: 12, left: 16, bottom: 0, right: 16) } + func registerCells(to collectionView: UICollectionView) { + collectionView.register(DefaultBrowserCalloutCell.self) + } + + func shouldShowCallout() -> Bool { + // Never show Default Browser Notification over an NPT SI + if isBackgroundNTPSI { + return false + } + + let defaultBrowserDisplayCriteria = !Preferences.General.defaultBrowserCalloutDismissed.value && + !Preferences.Onboarding.basicOnboardingDefaultBrowserSelected.value && AppConstants.buildChannel == .release + + guard let appRetentionLaunchDate = Preferences.DAU.appRetentionLaunchDate.value else { + return defaultBrowserDisplayCriteria + } + + // User should not see default browser first 7 days + // also after 14 days + var defaultBrowserTimeConstraintCriteria = false + + let rightNow = Date() + let first7DayPeriod = appRetentionLaunchDate.addingTimeInterval(7.days) + let first14DayPeriod = appRetentionLaunchDate.addingTimeInterval(14.days) + + if rightNow > first7DayPeriod, rightNow < first14DayPeriod { + defaultBrowserTimeConstraintCriteria = true + } + + return defaultBrowserDisplayCriteria && defaultBrowserTimeConstraintCriteria + } + @objc func openSettings() { guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return