Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.32 KB | None | 0 0
  1. import UIKit
  2.  
  3. //MAR: Library
  4.  
  5. protocol FeatureFlagKeyType {
  6.     var key: String { get }
  7. }
  8.  
  9. private extension Dictionary where Key == String {
  10.     func value<V>(for key: FeatureFlagKeyType, default defaultExpression: @autoclosure () -> V) -> V {
  11.         return (self[key.key] as? V) ?? defaultExpression()
  12.     }
  13. }
  14.  
  15. struct FeatureFlagValue {
  16.     let dict: [String: Any]
  17.     init(dict: [String: Any]) {
  18.         self.dict = dict
  19.     }
  20.    
  21.     func getBoolValue(_ boolKey: FeatureFlagBoolKey) -> Bool {
  22.         return dict.value(for: BoolFeatureFlagKey(featureFlagBoolKey: boolKey), default: false)
  23.     }
  24.    
  25.     func getStringValue(_ stringKey: FeatureFlagStringKey) -> String {
  26.         return dict.value(for: StringFeatureFlagKey(featureFlagStringKey: stringKey), default: "empty")
  27.     }
  28. }
  29.  
  30. struct BoolFeatureFlagKey: FeatureFlagKeyType {
  31.     let featureFlagBoolKey: FeatureFlagBoolKey
  32.     var key: String {
  33.         featureFlagBoolKey.key
  34.     }
  35. }
  36.  
  37. struct StringFeatureFlagKey: FeatureFlagKeyType {
  38.     let featureFlagStringKey: FeatureFlagStringKey
  39.     var key: String {
  40.         featureFlagStringKey.key
  41.     }
  42. }
  43.  
  44. enum FeatureFlagBoolKey: String, Codable {
  45.     case searchEnabled
  46.     case widgetEnable
  47.     var key: String {
  48.         self.rawValue
  49.     }
  50. }
  51. enum FeatureFlagStringKey: String, Codable {
  52.     case searchTitle
  53.     var key: String {
  54.         self.rawValue
  55.     }
  56. }
  57.  
  58. final class FeatureFlag {
  59.     static let shared = FeatureFlag()
  60.    
  61.     private var dict: [String: Any]!
  62.    
  63.     func initDict(_ dict: [String: Any]) {
  64.         self.dict = dict
  65.     }
  66.    
  67.     func getBool(_ boolKey: FeatureFlagBoolKey) -> Bool {
  68.         let featureFlagValue = FeatureFlagValue(dict: dict)
  69.         return featureFlagValue.getBoolValue(boolKey)
  70.     }
  71.     func getString(_ stringKey: FeatureFlagStringKey) -> String {
  72.         let featureFlagValue = FeatureFlagValue(dict: dict)
  73.         return featureFlagValue.getStringValue(stringKey)
  74.     }
  75. }
  76. //--------------------------
  77.  
  78.  
  79. let remoteConfig: [String: Any] = ["asdbas": false,
  80.     "searchEnabled": false,
  81.     "widgetEnable": true,
  82.     "searchTitle": "This is a title"]
  83.  
  84. FeatureFlag.shared.initDict(remoteConfig)
  85.  
  86. let bool = FeatureFlag.shared.getBool(.searchEnabled)
  87. let string = FeatureFlag.shared.getString(.searchTitle)
  88. print(bool)
  89. print(string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement