Skip to content

BradPatras/brc-multiplatform

Repository files navigation

Basic Remote Configs 🛰📝

gradle build workflow spm version maven central version

one brc to rule them all

🚧️   Under Construction   🚧

Bare-bones remote config library for Android and iOS. Motivation for this project is having simple remote configs in my app projects where pulling in Firebase would be overkill.

Previously I had implemented this library separately for both Android and iOS using their respective languages, those repos still exist, here: brc-android, brc-ios but this library effectively replaces those.

Config file format

Configs are in JSON format. Compatible data types include integer, string, boolean, null, and array(integer|string|boolean). Nested objects are not officially supported.

Example of config format

{
  "v": 1,
  "someFlag": false,
  "someMessage": "Welcome to easy config!",
  "nullValue": null,
  "arrayOfWords": ["super", "duper", "simple", "configs"]
}

v is an integer value representing the config file's version. It's not required, but if it's present the client libraries will use this value when deciding if their local cache needs updating.

In practice

This repo is hosting an example config json file. This example config is currently being consumed by the Android and iOS demo apps.

🔐   Custom request headers can be passed into the initializer to facilitate auth tokens or whatever else.

Adding the dependency

iOS

// In Xcode, File > Add Package Dependencies, then search for this repo:
// https://github.com/bradpatras/brc-multiplatform

Android

// Add this to you build.gradle.kts file
implementation("io.github.bradpatras:brc-android:0.4.0")

Initialization

You are responsible for creating and managing your Brc instance in whatever way makes sense for your use case.

// Swift
let brc = BasicRemoteConfigs(
  remoteUrl: "https://github.com/BradPatras/brc-multiplatform/raw/main/simple-config.json",
  customHeaders: .init()
)
// Kotlin
private val brc = BasicRemoteConfigs(
  remoteUrl = "https://github.com/BradPatras/brc-multiplatform/raw/main/simple-config.json"
)

Fetching

// Swift
Task {
  do {
    try await brc.fetchConfigs(ignoreCache: false)
    text = brc.getKeys().joined(separator: "\n")
  } catch {
    text = error.localizedDescription
  }
}
// Kotlin
coroutineScope {
    try {
        brc.fetchConfigs()
        withContext(Dispatchers.Main) {
            updateUI(brc.getKeys().joinToString(separator = ",\n"))
        }
    } catch (error: Throwable) {
        withContext(Dispatchers.Main) {
            updateUI("Encountered an error when fetching configs")
        }
    }
}

Why KMP

I originally wrote this library twice, a version in swift for iOS apps and a version in kotlin for Android apps. After I learned more about Kotlin Multiplatform, I realized this simple library may be a good candidate for a shared codebase.

Publishing

Android and iOS apps use completely different dependency management, Swift Package Manager for iOS and Gradle for Android. Since this library is made to be consumed by both platforms, it needs to be published two different ways.

The Android/Multiplatform artifact is published to Maven Central and since it's a KMP Library, multiple variants get published (brc-android, brc-iosX64, etc) in order to support KMP Apps.

The iOS package is published right here in this repository using the typical SPM library setup: a Package.swift file in the root directory and the actual xcframework artifacts attached to github releases.

Future

I am patiently waiting for the day when direct kotlin to swift export is released because the reliance on Objective-C is one of the biggest downsides of kmp in my opinion (coming from an iOS developer). Hopefully that'll improve the ergonomics on the iOS side a little bit. The roadmap says they're aiming for a 2025 public release! 🤞

About

Bare-bones remote config library for Android and iOS written using Kotlin Multiplatform

Topics

Resources

Stars

Watchers

Forks