Skip to content

Commit

Permalink
Dismiss Next Steps cards after 1 week
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx committed Oct 29, 2024
1 parent 39ddd30 commit 8794e7c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
26 changes: 18 additions & 8 deletions DuckDuckGo/Preferences/Model/AppearancePreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ final class AppearancePreferences: ObservableObject {

struct Constants {
static let bookmarksBarAlignmentChangedIsCenterAlignedParameter = "isCenterAligned"
static let dismissNextStepsCardsAfterDays = 7
}

static let shared = AppearancePreferences()
Expand Down Expand Up @@ -211,13 +212,22 @@ final class AppearancePreferences: ObservableObject {
}
}

@Published var isContinueSetUpVisible: Bool {
didSet {
persistor.isContinueSetUpVisible = isContinueSetUpVisible
var isContinueSetUpVisible: Bool {
get {
guard persistor.isContinueSetUpVisible else { return false }
if AppDelegate.firstLaunchDate < Calendar.current.date(byAdding: .day, value: -Constants.dismissNextStepsCardsAfterDays, to: dateTimeProvider())! {
self.isContinueSetUpVisible = false
return false
}
return true
}
set {
persistor.isContinueSetUpVisible = newValue
// Temporary Pixel
if !isContinueSetUpVisible {
PixelKit.fire(GeneralPixel.continueSetUpSectionHidden)
}
self.objectWillChange.send()
}
}

Expand Down Expand Up @@ -291,16 +301,17 @@ final class AppearancePreferences: ObservableObject {

init(
persistor: AppearancePreferencesPersistor = AppearancePreferencesUserDefaultsPersistor(),
homePageNavigator: HomePageNavigator = DefaultHomePageNavigator()
homePageNavigator: HomePageNavigator = DefaultHomePageNavigator(),
dateTimeProvider: @escaping () -> Date = Date.init
) {
self.persistor = persistor
self.homePageNavigator = homePageNavigator
self.dateTimeProvider = dateTimeProvider
currentThemeName = .init(rawValue: persistor.currentThemeName) ?? .systemDefault
showFullURL = persistor.showFullURL
favoritesDisplayMode = persistor.favoritesDisplayMode.flatMap(FavoritesDisplayMode.init) ?? .default
isFavoriteVisible = persistor.isFavoriteVisible
isRecentActivityVisible = persistor.isRecentActivityVisible
isContinueSetUpVisible = persistor.isContinueSetUpVisible
isSearchBarVisible = persistor.isSearchBarVisible
showBookmarksBar = persistor.showBookmarksBar
bookmarksBarAppearance = persistor.bookmarksBarAppearance
Expand All @@ -311,12 +322,11 @@ final class AppearancePreferences: ObservableObject {

private var persistor: AppearancePreferencesPersistor
private var homePageNavigator: HomePageNavigator
private let dateTimeProvider: () -> Date

private func requestSync() {
Task { @MainActor in
guard let syncService = (NSApp.delegate as? AppDelegate)?.syncService else {
return
}
guard let syncService = NSApp.delegateTyped.syncService else { return }
Logger.sync.debug("Requesting sync if enabled")
syncService.scheduler.notifyDataChanged()
}
Expand Down
47 changes: 47 additions & 0 deletions UnitTests/Preferences/AppearancePreferencesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,51 @@ final class AppearancePreferencesTests: XCTestCase {
XCTAssertTrue(persister2.isContinueSetUpVisible)
XCTAssertTrue(persister2.isSearchBarVisible)
}

func testContinueSetUpIsDismissedAfterWeek() {
// 1. app installed
var now = Date()
AppDelegate.firstLaunchDate = now

// listen to AppearancePreferences.objectWillChange
let model = AppearancePreferences(persistor: AppearancePreferencesPersistorMock(), dateTimeProvider: { now })
var eObjectWillChange: XCTestExpectation!
let c = model.objectWillChange.sink {
eObjectWillChange.fulfill()
}
func incrementDate() {
now = Calendar.current.date(byAdding: .day, value: 1, to: now)!
}

// check during 7 days
// eObjectWillChange shouldn‘t be called until 1 week
for i in 0..<7 {
XCTAssertTrue(model.isContinueSetUpVisible, "\(i)")
incrementDate()
}
// 1 week passed
// eObjectWillChange should be called once
eObjectWillChange = expectation(description: "AppearancePreferences.objectWillChange called")
incrementDate()
XCTAssertFalse(model.isContinueSetUpVisible, "7")
waitForExpectations(timeout: 0)

// shouldn‘t change after being set once
for i in 8..<20 {
XCTAssertFalse(model.isContinueSetUpVisible, "\(i)")
incrementDate()
}

withExtendedLifetime(c) {}
}

func testWhenLauchedAfterWeek_continueSetUpIsDismissed() {
let now = Date().addingTimeInterval(1)
let weekAgo = Date.weekAgo
AppDelegate.firstLaunchDate = weekAgo

let model = AppearancePreferences(persistor: AppearancePreferencesPersistorMock(), dateTimeProvider: { now })
XCTAssertFalse(model.isContinueSetUpVisible)
}

}

0 comments on commit 8794e7c

Please sign in to comment.