Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AutoClearSettingsViewController to use DI for app settings #3448

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion DuckDuckGo/AutoClearSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ class AutoClearSettingsViewController: UITableViewController {
@IBOutlet weak var clearDataToggle: UISwitch!
@IBOutlet var labels: [UILabel]!

private lazy var appSettings = AppDependencyProvider.shared.appSettings
private var appSettings: AppSettings
private var clearDataSettings: AutoClearSettingsModel?

init?(appSettings: AppSettings, coder: NSCoder) {
self.appSettings = appSettings
super.init(coder: coder)
}

@available(*, unavailable, renamed: "init(appSettings:coder:)")
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
Expand Down
9 changes: 7 additions & 2 deletions DuckDuckGo/SettingsLegacyViewProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@ class SettingsLegacyViewProvider: ObservableObject {
var autoConsent: UIViewController { instantiate("AutoconsentSettingsViewController", fromStoryboard: "Settings") }
var unprotectedSites: UIViewController { instantiate("UnprotectedSites", fromStoryboard: "Settings") }
var fireproofSites: UIViewController { instantiate("FireProofSites", fromStoryboard: "Settings") }
var autoclearData: UIViewController { instantiate("AutoClearSettingsViewController", fromStoryboard: "Settings") }
var keyboard: UIViewController { instantiate("Keyboard", fromStoryboard: "Settings") }
var feedback: UIViewController { instantiate("Feedback", fromStoryboard: "Feedback") }

var autoclearData: UIViewController {
let storyboard = UIStoryboard(name: "Settings", bundle: nil)
return storyboard.instantiateViewController(identifier: "AutoClearSettingsViewController", creator: { coder in
return AutoClearSettingsViewController(appSettings: self.appSettings, coder: coder)
})
}

@MainActor
func syncSettings(source: String? = nil) -> SyncSettingsViewController {
return SyncSettingsViewController(syncService: self.syncService,
Expand Down
39 changes: 13 additions & 26 deletions DuckDuckGoTests/AutoClearSettingsScreenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,20 @@ import XCTest

class AutoClearSettingsScreenTests: XCTestCase {

var mockDependencyProvider: MockDependencyProvider!

override func setUp() {
super.setUp()

mockDependencyProvider = MockDependencyProvider()
AppDependencyProvider.shared = mockDependencyProvider
}

override func tearDown() {
super.tearDown()

AppDependencyProvider.shared = AppDependencyProvider.makeTestingInstance()
}

func testWhenOpeningSettingsThenClearDataToggleIsSetBasedOnAppSettings() {
let appSettigns = AppUserDefaults()
appSettigns.autoClearAction = []
if let settingsController = AutoClearSettingsViewController.loadFromStoryboard() {
let appSettings = AppUserDefaults()
appSettings.autoClearAction = []

if let settingsController = AutoClearSettingsViewController.loadFromStoryboard(appSettings: appSettings) {
settingsController.loadViewIfNeeded()
XCTAssertFalse(settingsController.clearDataToggle.isOn)
} else {
assertionFailure("Could not load View Controller")
}

appSettigns.autoClearAction = .clearData
if let settingsController = AutoClearSettingsViewController.loadFromStoryboard() {
appSettings.autoClearAction = .clearData

if let settingsController = AutoClearSettingsViewController.loadFromStoryboard(appSettings: appSettings) {
settingsController.loadViewIfNeeded()
XCTAssert(settingsController.clearDataToggle.isOn)
} else {
Expand All @@ -63,7 +48,7 @@ class AutoClearSettingsScreenTests: XCTestCase {
let appSettings = AppUserDefaults()
appSettings.autoClearAction = []

guard let settingsController = AutoClearSettingsViewController.loadFromStoryboard() else {
guard let settingsController = AutoClearSettingsViewController.loadFromStoryboard(appSettings: appSettings) else {
assertionFailure("Could not load View Controller")
return
}
Expand All @@ -85,9 +70,11 @@ class AutoClearSettingsScreenTests: XCTestCase {

private extension AutoClearSettingsViewController {

static func loadFromStoryboard() -> AutoClearSettingsViewController? {
let controller = UIStoryboard(name: "Settings", bundle: nil).instantiateViewController(withIdentifier: "AutoClearSettingsViewController")
return controller as? AutoClearSettingsViewController
static func loadFromStoryboard(appSettings: AppSettings) -> AutoClearSettingsViewController? {
let storyboard = UIStoryboard(name: "Settings", bundle: nil)
return storyboard.instantiateViewController(identifier: "AutoClearSettingsViewController", creator: { coder in
return AutoClearSettingsViewController(appSettings: appSettings, coder: coder)
})
}

}
Loading