BradPatras / brc-android

Basic remote configs for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maven Central

brc-android

Bare-bones remote configs for Android. Check out the basic-remote-configs project repo for more context.

🚧   Under active development   🚧

Gradle:

In your root build.gradle file, add the mavenCentral() repo:

allProjects {
    repositories {
        mavenCentral()
    }
}

In your app or module build.gradle file, add the brc-android dependency:

dependencies {
    implementation 'io.github.bradpatras:brc-android:0.3.0'
}

Usage

The usage is pretty straightforward:

  1. Create an instance of BasicRemoteConfigs
  2. Call .fetchConfigs(). (This is a suspend function so you'll need to call it in a coroutine scope)
  3. Access your configs.
val configUrl = URL("https://github.com/BradPatras/basic-remote-configs/raw/main/examples/simple.json")

// #1
val brc = BasicRemoteConfigs(configUrl)

// ...

lifecycleScope.launch { 
    brc.fetchConfigs() // #2
    val someFlag = brc.getBoolean("someFlag") // #3
}

Caching

Configs are stored locally in the app's private storage once they've been fetched from the network. Calling .fetchConfigs() will fetch the locally cached version of configs if either of the following are true:

  1. The cache exists and is not expired (cache expires after one day)
  2. The call to fetch configs from the network failed for any reason.

If you'd like to bypass the cached version and fetch the latest configs from the network, there's an optional param available .fetchConfigs(ignoreCache: Boolean)

Error handling

The call to .fetchConfigs() may make a network request and do some deserialization, so it's bound to fail at some point. BasicRemoteConfigs will log errors under the key "BasicRemoteConfigs" using Log.e with a hint as to where the error happened in regards to fetching configs. It won't do any handling or masking of the exceptions so you need to wrap it in a try/catch or use a CoroutineExceptionHandler yourself.

try {
    brc.fetchConfigs()
} catch (exception: Exception) {
    // What happens here is up to you
}

Important:

  • BasicRemoteConfigs is not a global/static resource out of the box. It makes no assumptions about scope or lifecycle.
  • BasicRemoteConfigs utilizes the App Startup Jetpack library in order to access Context (so it can grab the app's file directory path for caching). Some people may have an issue with this approach, but it seems pretty harmless to me and it looks to be working for the Realm Kotlin project. 🤷🏻‍♂️

About

Basic remote configs for Android

License:MIT License


Languages

Language:Kotlin 100.0%