Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- //MAR: Library
- protocol FeatureFlagKeyType {
- var key: String { get }
- }
- private extension Dictionary where Key == String {
- func value<V>(for key: FeatureFlagKeyType, default defaultExpression: @autoclosure () -> V) -> V {
- return (self[key.key] as? V) ?? defaultExpression()
- }
- }
- struct FeatureFlagValue {
- let dict: [String: Any]
- init(dict: [String: Any]) {
- self.dict = dict
- }
- func getBoolValue(_ boolKey: FeatureFlagBoolKey) -> Bool {
- return dict.value(for: BoolFeatureFlagKey(featureFlagBoolKey: boolKey), default: false)
- }
- func getStringValue(_ stringKey: FeatureFlagStringKey) -> String {
- return dict.value(for: StringFeatureFlagKey(featureFlagStringKey: stringKey), default: "empty")
- }
- }
- struct BoolFeatureFlagKey: FeatureFlagKeyType {
- let featureFlagBoolKey: FeatureFlagBoolKey
- var key: String {
- featureFlagBoolKey.key
- }
- }
- struct StringFeatureFlagKey: FeatureFlagKeyType {
- let featureFlagStringKey: FeatureFlagStringKey
- var key: String {
- featureFlagStringKey.key
- }
- }
- enum FeatureFlagBoolKey: String, Codable {
- case searchEnabled
- case widgetEnable
- var key: String {
- self.rawValue
- }
- }
- enum FeatureFlagStringKey: String, Codable {
- case searchTitle
- var key: String {
- self.rawValue
- }
- }
- final class FeatureFlag {
- static let shared = FeatureFlag()
- private var dict: [String: Any]!
- func initDict(_ dict: [String: Any]) {
- self.dict = dict
- }
- func getBool(_ boolKey: FeatureFlagBoolKey) -> Bool {
- let featureFlagValue = FeatureFlagValue(dict: dict)
- return featureFlagValue.getBoolValue(boolKey)
- }
- func getString(_ stringKey: FeatureFlagStringKey) -> String {
- let featureFlagValue = FeatureFlagValue(dict: dict)
- return featureFlagValue.getStringValue(stringKey)
- }
- }
- //--------------------------
- let remoteConfig: [String: Any] = ["asdbas": false,
- "searchEnabled": false,
- "widgetEnable": true,
- "searchTitle": "This is a title"]
- FeatureFlag.shared.initDict(remoteConfig)
- let bool = FeatureFlag.shared.getBool(.searchEnabled)
- let string = FeatureFlag.shared.getString(.searchTitle)
- print(bool)
- print(string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement