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.1.0
🛠Configs, a quick, simple and stylish solution for your remote and dev configurations.
⭐️ 4
🕓 38 weeks ago
iOS
.package(url: "https://github.com/michaelhenry/XConfigs.git", from: "1.1.0")

🛠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.

https://user-images.githubusercontent.com/717992/213901399-d4429d63-83fb-4770-ac9c-a016e2128084.mp4

https://github.com/michaelhenry/XConfigs/assets/717992/2b1ef692-647e-4fb0-aea4-aa4be25e9b31

Other Related

Firebase Remote Config

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

GitHub

link
Stars: 4
Last commit: 2 days ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

1.1.0
38 weeks ago

What’s Changed

  • [Chore] Update minimum supported iOS version to 14 (#41) @michaelhenry

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