Giter VIP home page Giter VIP logo

xconfigs's Introduction

🛠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

xconfigs's People

Contributors

michaelhenry avatar pre-commit-ci[bot] avatar renovate[bot] avatar svmichael avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xconfigs's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

github-actions
.github/workflows/documentation.yml
  • actions/checkout v4
  • actions/configure-pages v5
  • actions/upload-pages-artifact v3
  • actions/deploy-pages v4
.github/workflows/release-drafter.yml
  • release-drafter/release-drafter v6
.github/workflows/test.yml
  • actions/checkout v4
  • actions/cache v4
  • michaelhenry/swifty-code-coverage v1.0.1
  • actions/upload-artifact v4.3.3
  • macos 14
swift
Package.swift
  • CombineCommunity/CombineExt from: "1.8.1"
  • michaelhenry/CombineCocoa from: "1.0.1"
  • michaelhenry/Prettier.swift from: "1.1.1"
  • raspu/Highlightr from: "2.1.2"
  • apple/swift-docc-plugin from: "1.3.0"

  • Check this box to trigger a request for Renovate to run again on this repository

[feat] swiftUI support

  • Create a View modifier to show the config screen either via the shake gesture or manual call.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.