Fix widget toggle not disabling On Demand before stopping VPN

Store wasOnDemandEnabled flag in providerConfiguration so the widget
can restore On Demand state on next start. Clear the flag when On
Demand is explicitly disabled in settings or when starting from the
main app.
This commit is contained in:
世界 committed 2026-03-09 13:20:53 +08:00
1 parent c19945f65b
commit efa6088884
3 files changed
+39 -2

No files matched your search

+19
View File
@@ -114,6 +114,14 @@ public class ExtensionProfile: ObservableObject {
public func updateOnDemand(enabled: Bool, useDefaultRules: Bool) async throws {
guard let manager else { return }
manager.isOnDemandEnabled = enabled
if !enabled {
if let proto = manager.protocolConfiguration as? NETunnelProviderProtocol {
var config = proto.providerConfiguration ?? [:]
if config.removeValue(forKey: "wasOnDemandEnabled") != nil {
proto.providerConfiguration = config
}
}
}
await setOnDemandRules(useDefaultRules: useDefaultRules)
try await manager.saveToPreferences()
}
@@ -141,6 +149,12 @@ public class ExtensionProfile: ObservableObject {
manager.isOnDemandEnabled = true
await setOnDemandRules(useDefaultRules: alwaysOn)
}
if let proto = manager.protocolConfiguration as? NETunnelProviderProtocol {
var config = proto.providerConfiguration ?? [:]
if config.removeValue(forKey: "wasOnDemandEnabled") != nil {
proto.providerConfiguration = config
}
}
#if !os(tvOS)
if let protocolConfiguration = manager.protocolConfiguration {
let includeAllNetworks = await SharedPreferences.includeAllNetworks.get()
@@ -239,6 +253,11 @@ public class ExtensionProfile: ObservableObject {
}
guard let manager else { return }
if manager.isOnDemandEnabled {
if let proto = manager.protocolConfiguration as? NETunnelProviderProtocol {
var config = proto.providerConfiguration ?? [:]
config["wasOnDemandEnabled"] = true
proto.providerConfiguration = config
}
manager.isOnDemandEnabled = false
try await manager.saveToPreferences()
}