Issue #3: Added Settings class to store selected BlocklistOption. Update system DoH settings, when user selects another blocklist.

This commit is contained in:
Benjamin Erhart 2026-04-15 13:40:29 +02:00
parent 807176f0b5
commit 6cbad66a87
2 changed files with 39 additions and 10 deletions

24
dns/Settings.swift Normal file
View file

@ -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)
}
}
}

View file

@ -12,21 +12,19 @@ import OSLog
class ViewModel: NSObject, ObservableObject { class ViewModel: NSObject, ObservableObject {
// TODO: Store this in UserDefaults
@Published @Published
var blocklist: BlocklistOption = .secure var blocklist: BlocklistOption = .secure {
didSet {
Settings.blocklist = blocklist
toggleDns()
}
}
// TODO: Store this in UserDefaults
@Published @Published
var isDnsEnabled = false { var isDnsEnabled = false {
didSet { didSet {
if !isProgrammaticChange { toggleDns()
toggleDns()
}
else {
// Reset, so next one is recognized as coming from the user again.
isProgrammaticChange = false
}
} }
} }
@ -39,6 +37,13 @@ class ViewModel: NSObject, ObservableObject {
func toggleDns() { func toggleDns() {
guard !isProgrammaticChange else {
// Reset, so next one is recognized as coming from the user again.
isProgrammaticChange = false
return
}
Task { Task {
if isDnsEnabled { if isDnsEnabled {
manager.dnsSettings = blocklist.settings manager.dnsSettings = blocklist.settings