Created
July 6, 2020 09:59
-
-
Save geor-kasapidi/3e80a03416e43cff9b58011f57eb2065 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| enum ABTests { | |
| static let magicCorrection = ABTest( | |
| settings: UserDefaultsStorage.shared, | |
| keyPath: \.testFlag, | |
| defaultValue: false | |
| ) | |
| } | |
| final class ABTest<T: DefaultsStorageProtocol> { | |
| private let settings: T | |
| private let keyPath: ReferenceWritableKeyPath<T, Bool?> | |
| private let defaultValue: Bool | |
| init(settings: T, keyPath: ReferenceWritableKeyPath<T, Bool?>, defaultValue: Bool) { | |
| self.settings = settings | |
| self.keyPath = keyPath | |
| self.defaultValue = defaultValue | |
| } | |
| private(set) var currentValue: Bool { | |
| get { self.settings[keyPath: self.keyPath] ?? self.defaultValue } | |
| set { self.settings[keyPath: self.keyPath] = newValue } | |
| } | |
| func set(isActive: Bool) { | |
| guard self.settings[keyPath: self.keyPath] == nil else { return } | |
| self.currentValue = isActive | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment