Skip to content

Commit

Permalink
Fixes App Data Clearing State Status In Settings (#3041)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/1204099484721401/1207731330276486/f

Description:
Fixes an issue that caused the "Automatically Clear Data" state in settings to be out of sync.

Fixed by observing Data Clearing updates. – (Eventually, this and all other parts of AppSettings should adopt Combine publishers instead of Notifications, but this does the trick today)
  • Loading branch information
afterxleep authored Jul 4, 2024
1 parent 2a5a11f commit dc1d84e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DuckDuckGo/AppUserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class AppUserDefaults: AppSettings {
public static let showsFullURLAddressSettingChanged = Notification.Name("com.duckduckgo.app.ShowsFullURLAddressSettingChanged")
public static let autofillDebugScriptToggled = Notification.Name("com.duckduckgo.app.DidToggleAutofillDebugScript")
public static let duckPlayerSettingsUpdated = Notification.Name("com.duckduckgo.app.DuckPlayerSettingsUpdated")
public static let appDataClearingUpdated = Notification.Name("com.duckduckgo.app.dataClearingUpdates")
}

private let groupName: String
Expand Down Expand Up @@ -154,6 +155,7 @@ public class AppUserDefaults: AppSettings {

set {
userDefaults?.setValue(newValue.rawValue, forKey: Keys.autoClearActionKey)
NotificationCenter.default.post(name: Notifications.appDataClearingUpdated, object: nil)
}

}
Expand Down
13 changes: 13 additions & 0 deletions DuckDuckGo/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ final class SettingsViewModel: ObservableObject {
private lazy var isPad = UIDevice.current.userInterfaceIdiom == .pad
private var cancellables = Set<AnyCancellable>()

// App Data State Notification Observer
private var appDataClearingObserver: Any?

// Closures to interact with legacy view controllers through the container
var onRequestPushLegacyView: ((UIViewController) -> Void)?
var onRequestPresentLegacyView: ((UIViewController, _ modal: Bool) -> Void)?
Expand Down Expand Up @@ -345,6 +348,7 @@ final class SettingsViewModel: ObservableObject {

deinit {
subscriptionSignOutObserver = nil
appDataClearingObserver = nil
}
}
// swiftlint:enable type_body_length
Expand Down Expand Up @@ -732,6 +736,15 @@ extension SettingsViewModel {
}
}
}

// Observe App Data clearing state
appDataClearingObserver = NotificationCenter.default.addObserver(forName: AppUserDefaults.Notifications.appDataClearingUpdated,
object: nil,
queue: .main) { [weak self] _ in
guard let settings = self?.appSettings else { return }
self?.state.autoclearDataEnabled = (AutoClearSettingsModel(settings: settings) != nil)
}

}

@available(iOS 15.0, *)
Expand Down

0 comments on commit dc1d84e

Please sign in to comment.