Skip to content

Instantly share code, notes, and snippets.

@mugbug
Created December 23, 2019 20:41
Show Gist options
  • Save mugbug/ee172d39b44b21e9eb39f6433be0f249 to your computer and use it in GitHub Desktop.
Save mugbug/ee172d39b44b21e9eb39f6433be0f249 to your computer and use it in GitHub Desktop.
Remote Config Module
import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
RemoteConfigModule().setup(withOptions: FirebaseConfigOptions())
return true
}
}
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)
}
}
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 ?? "")
}
}
protocol FirebaseOptionsProtocol {
init?(contentsOfFile plistPath: String)
}
extension FirebaseOptions: FirebaseOptionsProtocol { }
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"
}
}
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
}
}
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)
}
}
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