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

Commit

Permalink
Fix #7969: [Follow up to #7860] Data on NTP with SI is not aligned pr…
Browse files Browse the repository at this point in the history
…operly (#7975)
  • Loading branch information
soner-yuksel authored Aug 28, 2023
1 parent 96a6ef8 commit 1f3e1a7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultBrowserCalloutView>

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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1f3e1a7

Please sign in to comment.