Skip to content

🛠Configs, a quick, simple and stylish solution for your remote and dev configurations.

License

Notifications You must be signed in to change notification settings

michaelhenry/XConfigs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🛠Configs

A quick, simple and stylish solution for your remote and dev configurations.

codecov test release Swift5.7 Platform Support license



Introduction

As part of software development process, we always need to see how our app will react depending on the different scenarios or configurations especially during testing. At the same time, it would be better if we can control some of app configurations on the fly, especially if there are unexpected things happened in our production environment, we can immediately enable or disable certain app functionality.

Getting Started

Install using SPM

.package(url: "https://github.com/michaelhenry/XConfigs", .upToNextMinor(from: "1.0.0")),

How to use

let kvProvider = SampleKeyValueProvider()

// Register the AppConfigs and set which keyValueProvider and option to use. Note that `.allowInAppModification(InAppModificationOption)` option accepts a `KeyValueStore`.
XConfigs.configure(with: AppConfigs.self, keyValueProvider: kvProvider, option: .allowInAppModification(.init(store: UserDefaults.standard)))

Please note that on production build, it is recommend that the in-app modification is disabled (option is set to readonly), so XConfigs will just use either the value from the keyValueProvider or the default value assigned inside the property wrapper as fallback.

Eg.

#if DEBUG
    XConfigs.configure(with: MockConfigs.self, keyValueProvider: kvProvider, option: .allowInAppModification(.init(store: UserDefaults.standard)))
#else
    XConfigs.configure(with: MockConfigs.self, keyValueProvider: kvProvider, option: .readonly)
#endif

📄 Documentation

Please refer to XConfigs's docs.

Example

Similar with logger tool such as swift-log, You can simply create a single global variable or just a singleton, as long as the it conforms to XConfigSpecification and then use the @XConfig property wrapper inside it.

If you have some custom datatype, you can simply conform them to RawStringValueRepresentable. So the key thing is as long as a value can be represented as a string, it should be fine.

struct AppConfigs: XConfigSpec {

    @XConfig(key: "isOnboardingEnabled", defaultValue: false)
    var isOnboardingEnabled: Bool

    @XConfig(key: "apiURL", defaultValue: URL(string: "https://google.com")!)
    var apiURL: URL

    @XConfig(key: "region", defaultValue: .north)
    var region: Region

    @XConfig(key: "maxRetry", defaultValue: 10)
    var maxRetry: Int

    @XConfig(key: "threshold", defaultValue: 1)
    var threshold: Int

    @XConfig(key: "rate", defaultValue: 2.5)
    var rate: Double
}

enum Region: String, CaseIterable, RawStringValueRepresentable {
    case north
    case south
    case east
    case west
}

For the complete example, please refer to the Demo project which auto-generated the screen(below) using the AppConfigs.swift config specification.

xconfigs-demo.mp4
xconfigs-demo2.mp4

Other Related

You can backed XConfigs by FirebaseRemoteConfig by simply implementing the KeyValueProvider protocol.

Example:

import FirebaseRemoteConfig

struct FirebaseKeyValueProvider: KeyValueProvider {

    private let remoteConfig: RemoteConfig = {
        let rconfig = RemoteConfig.remoteConfig()
        let settings = RemoteConfigSettings()
        settings.minimumFetchInterval = 0
        rconfig.configSettings = settings
        return rconfig
    }()
    
    // Please refer to https://firebase.google.com/docs/remote-config/get-started?platform=ios
    func fetch() {
        remoteConfig.fetch { (status, error) -> Void in
            if status == .success {
                print("Config fetched!")
                self.remoteConfig.activate { changed, error in
                    // ...
                }
            } else {
                print("Config not fetched")
                print("Error: \(error?.localizedDescription ?? "No error available.")")
            }
        }
    }

    // XConfigs KeyValueProvider protocol
    func get<Value>(for key: String) -> Value? where Value : RawStringValueRepresentable {
        guard let rawValue = remoteConfig.configValue(forKey: key).stringValue, let value = Value(rawString: rawValue) else { return nil }
        return value
    }
}

LICENSE

MIT