Last active
June 25, 2019 19:45
-
-
Save taimila/733917f0f3e50609c39688fa02c9fb72 to your computer and use it in GitHub Desktop.
Feature Toggle implementation in 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
import Foundation | |
enum Feature: String, CaseIterable { | |
case newThing | |
case otherThing | |
var isEnabled: Bool { | |
return UserDefaults.standard.bool(forKey: "feature-\(self.rawValue)") | |
} | |
func enable() { | |
UserDefaults.standard.set(true, forKey: "feature-\(self.rawValue)") | |
} | |
func disable() { | |
UserDefaults.standard.set(false, forKey: "feature-\(self.rawValue)") | |
} | |
func toggle() { | |
if self.isEnabled { | |
disable() | |
} else { | |
enable() | |
} | |
} | |
static func enableAll() { | |
for feature in self.allCases { | |
feature.enable() | |
} | |
} | |
static func disableAll() { | |
for feature in self.allCases { | |
feature.disable() | |
} | |
} | |
} | |
/// Examples how to use Feature toggle implementation above. | |
func exampleUsage() { | |
if Feature.newThing.isEnabled { | |
print("New Thing is enabled") | |
} | |
Feature.otherThing.enable() | |
Feature.enableAll() | |
Feature.disableAll() | |
Feature.newThing.toggle() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment