Swiftpack.co - michaelhenry/XConfigs as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by michaelhenry.
michaelhenry/XConfigs 1.0.0
🛠Configs, A quick, simple and stylish solution for your remote and dev configurations.
⭐️ 3
🕓 4 weeks ago
iOS
.package(url: "https://github.com/michaelhenry/XConfigs.git", from: "1.0.0")

🛠Configs

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

codecov test release Swift5 Platform 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 keyValueStore to use.
XConfigs.configure(with: AppConfigs.self, keyValueProvider: kvProvider, keyValueStore: UserDefaults.standard)

Please note that on production build, it is recommended to DON'T put any keyValueStore, so it will keep using either the value from the keyValueProvider or the default value assigned inside the property wrapper as fallback.

Eg.

#if DEBUG
    XConfigs.configure(with: AppConfigs.self, keyValueProvider: kvProvider, keyValueStore: UserDefaults.standard)
#else
    XConfigs.configure(with: AppConfigs.self, keyValueProvider: kvProvider)
#endif

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.

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.

https://user-images.githubusercontent.com/717992/199521206-3682e8de-ec72-4ed8-af7e-4ad467c9ec31.mp4

Other Related

Firebase Remote Config

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

TODO:

  • ☐ Sample implementation

LICENSE

MIT

GitHub

link
Stars: 3
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Initial release
4 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics