Skip to content

Commit

Permalink
Hide Next Cards options when all cards are closed
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx committed Nov 19, 2024
1 parent 1689c1a commit 7c6e8b8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 36 deletions.
1 change: 1 addition & 0 deletions DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public struct UserDefaultsWrapper<T> {
case homePageIsContinueSetupVisible = "home.page.is.continue.setup.visible"
case continueSetUpCardsLastDemonstrated = "home.page.contiune.setup.last.demonstrated"
case continueSetUpCardsNumberOfDaysDemonstrated = "home.page.contiune.setup.demo.days"
case continueSetUpCardsClosed = "home.page.contiune.setup.cards.closed"
case homePageIsRecentActivityVisible = "home.page.is.recent.activity.visible"
case homePageIsSearchBarVisible = "home.page.is.search.bar.visible"
case homePageIsFirstSession = "home.page.is.first.session"
Expand Down
75 changes: 43 additions & 32 deletions DuckDuckGo/HomePage/Model/HomePageContinueSetUpModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,36 @@ extension HomePage.Models {
}
}

@UserDefaultsWrapper(key: .homePageShowMakeDefault, defaultValue: true)
private var shouldShowMakeDefaultSetting: Bool
struct Settings {
@UserDefaultsWrapper(key: .homePageShowMakeDefault, defaultValue: true)
var shouldShowMakeDefaultSetting: Bool

@UserDefaultsWrapper(key: .homePageShowAddToDock, defaultValue: true)
private var shouldShowAddToDockSetting: Bool
@UserDefaultsWrapper(key: .homePageShowAddToDock, defaultValue: true)
var shouldShowAddToDockSetting: Bool

@UserDefaultsWrapper(key: .homePageShowImport, defaultValue: true)
private var shouldShowImportSetting: Bool
@UserDefaultsWrapper(key: .homePageShowImport, defaultValue: true)
var shouldShowImportSetting: Bool

@UserDefaultsWrapper(key: .homePageShowDuckPlayer, defaultValue: true)
private var shouldShowDuckPlayerSetting: Bool
@UserDefaultsWrapper(key: .homePageShowDuckPlayer, defaultValue: true)
var shouldShowDuckPlayerSetting: Bool

@UserDefaultsWrapper(key: .homePageShowEmailProtection, defaultValue: true)
private var shouldShowEmailProtectionSetting: Bool
@UserDefaultsWrapper(key: .homePageShowEmailProtection, defaultValue: true)
var shouldShowEmailProtectionSetting: Bool

@UserDefaultsWrapper(key: .homePageIsFirstSession, defaultValue: true)
private var isFirstSession: Bool
@UserDefaultsWrapper(key: .homePageIsFirstSession, defaultValue: true)
var isFirstSession: Bool

func clear() {
_shouldShowMakeDefaultSetting.clear()
_shouldShowAddToDockSetting.clear()
_shouldShowImportSetting.clear()
_shouldShowDuckPlayerSetting.clear()
_shouldShowEmailProtectionSetting.clear()
_isFirstSession.clear()
}
}

private let settings: Settings

var isMoreOrLessButtonNeeded: Bool {
return featuresMatrix.count > itemsRowCountWhenCollapsed
Expand All @@ -87,7 +100,7 @@ extension HomePage.Models {
return !featuresMatrix.isEmpty
}

lazy var listOfFeatures = isFirstSession ? firstRunFeatures : randomisedFeatures
lazy var listOfFeatures = settings.isFirstSession ? firstRunFeatures : randomisedFeatures

private var featuresMatrix: [[FeatureType]] = [[]] {
didSet {
Expand All @@ -113,6 +126,7 @@ extension HomePage.Models {
self.duckPlayerPreferences = duckPlayerPreferences
self.privacyConfigurationManager = privacyConfigurationManager
self.subscriptionManager = subscriptionManager
self.settings = .init()

refreshFeaturesMatrix()

Expand Down Expand Up @@ -171,22 +185,25 @@ extension HomePage.Models {
func removeItem(for featureType: FeatureType) {
switch featureType {
case .defaultBrowser:
shouldShowMakeDefaultSetting = false
settings.shouldShowMakeDefaultSetting = false
case .dock:
shouldShowAddToDockSetting = false
settings.shouldShowAddToDockSetting = false
case .importBookmarksAndPasswords:
shouldShowImportSetting = false
settings.shouldShowImportSetting = false
case .duckplayer:
shouldShowDuckPlayerSetting = false
settings.shouldShowDuckPlayerSetting = false
case .emailProtection:
shouldShowEmailProtectionSetting = false
settings.shouldShowEmailProtectionSetting = false
}
refreshFeaturesMatrix()
}

func refreshFeaturesMatrix() {
var features: [FeatureType] = []
appendFeatureCards(&features)
if features.isEmpty {
AppearancePreferences.shared.continueSetUpCardsClosed = true
}
featuresMatrix = features.chunked(into: itemsPerRow)
}

Expand Down Expand Up @@ -214,14 +231,14 @@ extension HomePage.Models {
// Helper Functions
@MainActor
@objc private func newTabOpenNotification(_ notification: Notification) {
if !isFirstSession {
if !settings.isFirstSession {
listOfFeatures = randomisedFeatures
}
#if DEBUG
isFirstSession = false
settings.isFirstSession = false
#endif
if OnboardingViewModel.isOnboardingFinished {
isFirstSession = false
settings.isFirstSession = false
}
}

Expand Down Expand Up @@ -252,33 +269,27 @@ extension HomePage.Models {
}

private var shouldMakeDefaultCardBeVisible: Bool {
shouldShowMakeDefaultSetting &&
!defaultBrowserProvider.isDefault
settings.shouldShowMakeDefaultSetting && !defaultBrowserProvider.isDefault
}

private var shouldDockCardBeVisible: Bool {
#if !APPSTORE
shouldShowAddToDockSetting &&
!dockCustomizer.isAddedToDock
settings.shouldShowAddToDockSetting && !dockCustomizer.isAddedToDock
#else
return false
#endif
}

private var shouldImportCardBeVisible: Bool {
shouldShowImportSetting &&
!dataImportProvider.didImport
settings.shouldShowImportSetting && !dataImportProvider.didImport
}

private var shouldDuckPlayerCardBeVisible: Bool {
shouldShowDuckPlayerSetting &&
duckPlayerPreferences.duckPlayerModeBool == nil &&
!duckPlayerPreferences.youtubeOverlayAnyButtonPressed
settings.shouldShowDuckPlayerSetting && duckPlayerPreferences.duckPlayerModeBool == nil && !duckPlayerPreferences.youtubeOverlayAnyButtonPressed
}

private var shouldEmailProtectionCardBeVisible: Bool {
shouldShowEmailProtectionSetting &&
!emailManager.isSignedIn
settings.shouldShowEmailProtectionSetting && !emailManager.isSignedIn
}

}
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/HomePage/View/HomePageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ extension HomePage.Views {
.contextMenu(menuItems: sectionsVisibilityContextMenuItems)

if settingsVisibilityModel.isSettingsVisible {
SettingsView(includingContinueSetUpCards: model.isContinueSetUpAvailable && !model.isContinueSetUpCardsViewOutdated,
SettingsView(includingContinueSetUpCards: model.isContinueSetUpAvailable && !model.isContinueSetUpCardsViewOutdated && !model.continueSetUpCardsClosed,
isSettingsVisible: $settingsVisibilityModel.isSettingsVisible)
.frame(width: Self.settingsPanelWidth)
.transition(.move(edge: .trailing))
Expand Down Expand Up @@ -240,7 +240,7 @@ extension HomePage.Views {
Toggle(UserText.newTabMenuItemShowSearchBar, isOn: $model.isSearchBarVisible)
.toggleStyle(.checkbox)
}
if model.isContinueSetUpAvailable && !model.isContinueSetUpCardsViewOutdated {
if model.isContinueSetUpAvailable && !model.isContinueSetUpCardsViewOutdated && !model.continueSetUpCardsClosed {
Toggle(UserText.newTabMenuItemShowContinuteSetUp, isOn: $model.isContinueSetUpVisible)
.toggleStyle(.checkbox)
.visibility(continueSetUpModel.hasContent ? .visible : .gone)
Expand Down
3 changes: 3 additions & 0 deletions DuckDuckGo/Menus/MainMenuActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,10 @@ extension MainViewController {
AppearancePreferencesUserDefaultsPersistor().continueSetUpCardsLastDemonstrated = nil
AppearancePreferencesUserDefaultsPersistor().continueSetUpCardsNumberOfDaysDemonstrated = 0
AppearancePreferences.shared.isContinueSetUpCardsViewOutdated = false
AppearancePreferences.shared.continueSetUpCardsClosed = false
AppearancePreferences.shared.isContinueSetUpVisible = true
HomePage.Models.ContinueSetUpModel.Settings().clear()
NotificationCenter.default.post(name: NSApplication.didBecomeActiveNotification, object: NSApp)
}

@objc func debugShiftNewTabOpeningDate(_ sender: Any?) {
Expand Down
13 changes: 12 additions & 1 deletion DuckDuckGo/Preferences/Model/AppearancePreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protocol AppearancePreferencesPersistor {
var isContinueSetUpVisible: Bool { get set }
var continueSetUpCardsLastDemonstrated: Date? { get set }
var continueSetUpCardsNumberOfDaysDemonstrated: Int { get set }
var continueSetUpCardsClosed: Bool { get set }
var isRecentActivityVisible: Bool { get set }
var isSearchBarVisible: Bool { get set }
var showBookmarksBar: Bool { get set }
Expand Down Expand Up @@ -62,6 +63,9 @@ struct AppearancePreferencesUserDefaultsPersistor: AppearancePreferencesPersisto
@UserDefaultsWrapper(key: .continueSetUpCardsNumberOfDaysDemonstrated, defaultValue: 0)
var continueSetUpCardsNumberOfDaysDemonstrated: Int

@UserDefaultsWrapper(key: .continueSetUpCardsClosed, defaultValue: false)
var continueSetUpCardsClosed: Bool

@UserDefaultsWrapper(key: .homePageIsRecentActivityVisible, defaultValue: true)
var isRecentActivityVisible: Bool

Expand Down Expand Up @@ -222,9 +226,15 @@ final class AppearancePreferences: ObservableObject {

@Published var isContinueSetUpCardsViewOutdated: Bool

@Published var continueSetUpCardsClosed: Bool {
didSet {
persistor.continueSetUpCardsClosed = continueSetUpCardsClosed
}
}

var isContinueSetUpVisible: Bool {
get {
return persistor.isContinueSetUpVisible && !isContinueSetUpCardsViewOutdated
return persistor.isContinueSetUpVisible && !persistor.continueSetUpCardsClosed && !isContinueSetUpCardsViewOutdated
}
set {
persistor.isContinueSetUpVisible = newValue
Expand Down Expand Up @@ -333,6 +343,7 @@ final class AppearancePreferences: ObservableObject {
self.homePageNavigator = homePageNavigator
self.dateTimeProvider = dateTimeProvider
self.isContinueSetUpCardsViewOutdated = persistor.continueSetUpCardsNumberOfDaysDemonstrated >= Constants.dismissNextStepsCardsAfterDays
self.continueSetUpCardsClosed = persistor.continueSetUpCardsClosed
currentThemeName = .init(rawValue: persistor.currentThemeName) ?? .systemDefault
showFullURL = persistor.showFullURL
favoritesDisplayMode = persistor.favoritesDisplayMode.flatMap(FavoritesDisplayMode.init) ?? .default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ extension Preferences {
if addressBarModel.shouldShowAddressBar {
ToggleMenuItem(UserText.newTabSearchBarSectionTitle, isOn: $model.isSearchBarVisible)
}
if model.isContinueSetUpAvailable && !model.isContinueSetUpCardsViewOutdated {
if model.isContinueSetUpAvailable && !model.isContinueSetUpCardsViewOutdated && !model.continueSetUpCardsClosed {
ToggleMenuItem(UserText.newTabSetUpSectionTitle, isOn: $model.isContinueSetUpVisible)
}
ToggleMenuItem(UserText.newTabFavoriteSectionTitle, isOn: $model.isFavoriteVisible).accessibilityIdentifier("Preferences.AppearanceView.showFavoritesToggle")
Expand Down

0 comments on commit 7c6e8b8

Please sign in to comment.