diff --git a/dns/Settings.swift b/dns/Settings.swift new file mode 100644 index 0000000..1d4a914 --- /dev/null +++ b/dns/Settings.swift @@ -0,0 +1,24 @@ +// +// Settings.swift +// dns +// +// Created by Benjamin Erhart on 15.04.26. +// + +import Foundation + +class Settings { + + private static let blocklistKey = "blocklist" + + private static let defaults = UserDefaults.standard + + class var blocklist: BlocklistOption { + get { + BlocklistOption(rawValue: defaults.string(forKey: blocklistKey) ?? BlocklistOption.secure.rawValue) ?? .secure + } + set { + defaults.set(newValue.rawValue, forKey: blocklistKey) + } + } +} diff --git a/dns/ViewModel.swift b/dns/ViewModel.swift index c39d5b3..72497bd 100644 --- a/dns/ViewModel.swift +++ b/dns/ViewModel.swift @@ -12,21 +12,19 @@ import OSLog class ViewModel: NSObject, ObservableObject { - // TODO: Store this in UserDefaults @Published - var blocklist: BlocklistOption = .secure + var blocklist: BlocklistOption = .secure { + didSet { + Settings.blocklist = blocklist + + toggleDns() + } + } - // TODO: Store this in UserDefaults @Published var isDnsEnabled = false { didSet { - if !isProgrammaticChange { - toggleDns() - } - else { - // Reset, so next one is recognized as coming from the user again. - isProgrammaticChange = false - } + toggleDns() } } @@ -39,6 +37,13 @@ class ViewModel: NSObject, ObservableObject { func toggleDns() { + guard !isProgrammaticChange else { + // Reset, so next one is recognized as coming from the user again. + isProgrammaticChange = false + + return + } + Task { if isDnsEnabled { manager.dnsSettings = blocklist.settings