-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalise WireGuardObfuscationSettings-based SwiftUI view model
- Loading branch information
Showing
4 changed files
with
61 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
... controllers/Settings/Obfuscation/TunnelObfuscationSettingsWatchingObservableObject.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// TunnelObfuscationSettingsWatchingObservableObject.swift | ||
// MullvadVPN | ||
// | ||
// Created by Andrew Bulhak on 2024-11-07. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MullvadSettings | ||
|
||
/// a generic ObservableObject that binds to obfuscation settings in TunnelManager. | ||
/// Used as the basis for ViewModels for SwiftUI interfaces for these settings. | ||
|
||
class TunnelObfuscationSettingsWatchingObservableObject<T: Equatable>: ObservableObject { | ||
let tunnelManager: TunnelManager | ||
let keyPath: WritableKeyPath<WireGuardObfuscationSettings, T> | ||
private var tunnelObserver: TunnelObserver? | ||
|
||
// this is essentially @Published from scratch | ||
var value: T { | ||
willSet(newValue) { | ||
guard newValue != self.value else { return } | ||
objectWillChange.send() | ||
var obfuscationSettings = tunnelManager.settings.wireGuardObfuscation | ||
obfuscationSettings[keyPath: keyPath] = newValue | ||
} | ||
} | ||
|
||
init(tunnelManager: TunnelManager, keyPath: WritableKeyPath<WireGuardObfuscationSettings, T>, _ initialValue: T) { | ||
self.tunnelManager = tunnelManager | ||
self.keyPath = keyPath | ||
self.value = initialValue | ||
tunnelObserver = | ||
TunnelBlockObserver(didUpdateTunnelSettings: { [weak self] _, newSettings in | ||
guard let self else { return } | ||
updateValueFromSettings(newSettings.wireGuardObfuscation) | ||
}) | ||
} | ||
|
||
private func updateValueFromSettings(_ settings: WireGuardObfuscationSettings) { | ||
let newValue = settings | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters