Files
Qiu Yuzhou 32709333f5 refactor(rules): require explicit preset selection
Remove lossy default-action writes that can replace china-proxy
with gfwlist. Preserve legacy preference reads and derive the
default action from the selected preset.

Update tests to use explicit presets and verify identity,
legacy compatibility, and no-op reselection.
2026-10-25 14:32:12 +08:00

47 lines
1.6 KiB
Swift

import Foundation
/// 当前用户的代理控制偏好;采用 UserDefaults 的即时内存更新与异步写盘语义。
struct ProxyControlPreferences {
let defaults: UserDefaults
init(defaults: UserDefaults = .standard) { self.defaults = defaults }
var preferredMode: ProxyModeKind {
get {
defaults.string(forKey: "proxyControl.preferredMode").flatMap(ProxyModeKind.init(rawValue:))
?? .rule
}
nonmutating set { defaults.set(newValue.rawValue, forKey: "proxyControl.preferredMode") }
}
var rulePreset: RulePreset {
get {
if let saved = defaults.string(forKey: "proxyControl.rulePreset").flatMap(
RulePreset.init(rawValue:))
{
return saved
}
let legacy =
defaults.string(forKey: "proxyControl.ruleDefaultAction")
.flatMap(RuleDefaultAction.init(rawValue:)) ?? .directWhenUnmatched
return RulePreset(legacy: legacy)
}
nonmutating set { defaults.set(newValue.rawValue, forKey: "proxyControl.rulePreset") }
}
var agentEnabled: Bool {
get { defaults.object(forKey: "proxyControl.agentEnabled") as? Bool ?? false }
nonmutating set { defaults.set(newValue, forKey: "proxyControl.agentEnabled") }
}
var systemProxyEnabled: Bool {
get { defaults.object(forKey: "proxyControl.systemProxyEnabled") as? Bool ?? false }
nonmutating set { defaults.set(newValue, forKey: "proxyControl.systemProxyEnabled") }
}
var proxyExceptions: String {
get { defaults.object(forKey: "proxyControl.proxyExceptions") as? String ?? "" }
nonmutating set { defaults.set(newValue, forKey: "proxyControl.proxyExceptions") }
}
}