Created
September 14, 2020 20:53
-
-
Save LeoDroidCoder/5540c10d7c119df64ae4c81f6fcf6fb8 to your computer and use it in GitHub Desktop.
A gist for the article on FirebaseRemote config https://medium.com/p/89687a78e367
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.leodroidcoder.remoteconfigsample | |
import com.google.gson.Gson | |
fun <T> Gson.jsonToObjectOrNull(json: String?, clazz: Class<T>): T? = | |
try { | |
fromJson(json, clazz) | |
} catch (ignored: Exception) { | |
null | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.leodroidcoder.remoteconfigsample | |
interface IRemoteConfigAPI { | |
fun getSomeCoolParameter(): String | |
fun getBannerData(): AdBannerData | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.leodroidcoder.remoteconfigsample | |
import com.google.firebase.remoteconfig.FirebaseRemoteConfig | |
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings | |
import com.google.gson.Gson | |
import javax.inject.Inject | |
import javax.inject.Singleton | |
@Singleton | |
class RemoteConfigManager @Inject constructor( | |
private val gson: Gson | |
) : IRemoteConfigAPI { | |
private val config: FirebaseRemoteConfig = FirebaseRemoteConfig.getInstance().apply { | |
val configSettings = FirebaseRemoteConfigSettings.Builder() | |
.setMinimumFetchIntervalInSeconds(CONFIG_CACHE_EXPIRATION_SECONDS) | |
.build() | |
setConfigSettingsAsync(configSettings) | |
fetch(CONFIG_CACHE_EXPIRATION_SECONDS) | |
.addOnCompleteListener { | |
if (it.isSuccessful) { | |
activateFetched() | |
} | |
} | |
} | |
override fun getSomeCoolParameter(): String = | |
read<String>(ConfigParam.SOME_COOL_PARAMETER) ?: SOME_DEFAULT_VALUE | |
override fun getBannerData(): AdBannerData = | |
read<AdBannerData>(ConfigParam.BANNER_DATA) ?: AdBannerData("manage_it_yourself", false) | |
private inline fun <reified T> read(param: ConfigParam): T? = read(param, T::class.java) | |
private fun <T> read(param: ConfigParam, returnType: Class<T>): T? { | |
val value: Any? = when (returnType) { | |
String::class.java -> config.getString(param.key) | |
Boolean::class.java -> config.getBoolean(param.key) | |
Long::class.java -> config.getLong(param.key) | |
Int::class.java -> config.getLong(param.key).toInt() | |
Double::class.java -> config.getDouble(param.key) | |
Float::class.java -> config.getDouble(param.key).toFloat() | |
else -> { | |
val json = config.getString(param.key) | |
json.takeIf { it.isNotBlank() }?.let { gson.jsonToObjectOrNull(json, returnType) } | |
} | |
} | |
@Suppress("UNCHECKED_CAST") | |
return (value as? T) | |
} | |
private enum class ConfigParam(val key: String) { | |
SOME_COOL_PARAMETER("some_cool_parameter"), | |
BANNER_DATA("banner_data"), | |
} | |
private companion object { | |
/** | |
* Config expiration interval 30 minutes. | |
*/ | |
private const val CONFIG_CACHE_EXPIRATION_SECONDS = 900L | |
private const val SOME_DEFAULT_VALUE = "Any default value" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment