Created
December 23, 2019 20:41
-
-
Save mugbug/ee172d39b44b21e9eb39f6433be0f249 to your computer and use it in GitHub Desktop.
Remote Config Module
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
import UIKit | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
func application(_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
RemoteConfigModule().setup(withOptions: FirebaseConfigOptions()) | |
return true | |
} | |
} |
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
import Firebase | |
protocol FirebaseAppProtocol { | |
static func configure(options: FirebaseOptionsProtocol) | |
} | |
extension FirebaseApp: FirebaseAppProtocol { | |
static func configure(options: FirebaseOptionsProtocol) { | |
guard let firebaseOptions = options as? FirebaseOptions else { | |
fatalError("Couldn't cast to FirebaseOptions") | |
} | |
configure(options: firebaseOptions) | |
} | |
} |
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
import Firebase | |
protocol FirebaseConfigOptionsProtocol { | |
func fetchOptions(optionsType: FirebaseOptionsProtocol.Type) -> FirebaseOptionsProtocol? | |
} | |
final class FirebaseConfigOptions: FirebaseConfigOptionsProtocol { | |
func fetchOptions(optionsType: FirebaseOptionsProtocol.Type) -> FirebaseOptionsProtocol? { | |
let firebasePlistVariable = "FIREBASE_PLIST" | |
let firebasePlistName = Bundle.main.object(forInfoDictionaryKey: firebasePlistVariable) as? String | |
let firebasePlistPath = Bundle.main.path(forResource: firebasePlistName, ofType: "plist") | |
return optionsType.init(contentsOfFile: firebasePlistPath ?? "") | |
} | |
} |
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
protocol FirebaseOptionsProtocol { | |
init?(contentsOfFile plistPath: String) | |
} | |
extension FirebaseOptions: FirebaseOptionsProtocol { } |
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
protocol RemoteConfigKeyProtocol { | |
var keyValue: String { get } | |
} | |
extension RemoteConfigKeyProtocol where Self: RawRepresentable, Self.RawValue == String { | |
var keyValue: String { | |
return self.rawValue | |
} | |
} | |
struct RemoteConfigKeys { | |
enum SomeFeature: String, RemoteConfigKeyProtocol { | |
case someKey = "SOME_KEY" | |
} | |
enum ForceUpdate: String, RemoteConfigKeyProtocol { | |
case minVersion = "UPDATE_MIN_VERSION" | |
} | |
} |
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
import Firebase | |
class RemoteConfigModule { | |
private let remoteConfig: RemoteStorage | |
private let environmentLinks: EnvironmentLinks | |
init(remoteConfig: RemoteStorage = RemoteConfig.remoteConfig(), | |
environmentLinks: EnvironmentLinks = EnvironmentLinks()) { | |
self.remoteConfig = remoteConfig | |
self.environmentLinks = environmentLinks | |
} | |
func setup(app: FirebaseAppProtocol.Type = FirebaseApp.self, | |
withOptions options: FirebaseConfigOptionsProtocol, | |
optionsType: FirebaseOptionsProtocol.Type = FirebaseOptions.self, | |
cacheExpirationTime: Double = 1800) { | |
setupDefaultValues() | |
if let firebaseOptions = options.fetchOptions(optionsType: optionsType) { | |
app.configure(options: firebaseOptions) | |
remoteConfig.fetch(withExpirationDuration: cacheExpirationTime) { (status, error) in | |
guard status == .success else { return } | |
self.remoteConfig.activateFetched() | |
} | |
} | |
} | |
private func setupDefaultValues() { | |
remoteConfig.setDefaults([ | |
"someKey", "someValue", | |
]) | |
} | |
func get(forKey key: String) -> Bool { | |
return remoteConfig.configValue(forKey: key).boolValue | |
} | |
func get(forKey key: String) -> String? { | |
return remoteConfig.configValue(forKey: key).stringValue | |
} | |
} |
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
protocol RemoteConfigValuesProtocol { | |
func value(for key: RemoteConfigKeyProtocol) -> Bool | |
func value(for key: RemoteConfigKeyProtocol) -> String? | |
} | |
extension RemoteConfigModule: RemoteConfigValuesProtocol { | |
func value(for key: RemoteConfigKeyProtocol) -> Bool { | |
return get(forKey: key.keyValue) | |
} | |
func value(for key: RemoteConfigKeyProtocol) -> String? { | |
return get(forKey: key.keyValue) | |
} | |
} |
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
protocol RemoteStorage { | |
func setDefaults(_ defaults: [String : NSObject]?) | |
func configValue(forKey key: String?) -> RemoteConfigValue | |
func fetch(withExpirationDuration expirationDuration: TimeInterval, completionHandler: RemoteConfigFetchCompletion?) | |
@discardableResult | |
func activateFetched() -> Bool | |
} | |
extension RemoteConfig: RemoteStorage { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment