Skip to content

Instantly share code, notes, and snippets.

@reactivedroid
Last active October 9, 2022 11:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save reactivedroid/efd28badd08dac80dd46dc31adee80a9 to your computer and use it in GitHub Desktop.
Implementation of the BuildConfigProvider.kt
class BuildConfigProviderImpl
@Inject
constructor(@ApplicationContext private val context: Context) : BuildConfigProvider {
private fun getFlavorMap(): MutableMap<String, String> {
return if (BuildConfig.BUILD_TYPE == "release") {
BuildConfig.RELEASE_MAP
} else {
buildConfigurationFor(getFlavor())
}
}
@Suppress("UNCHECKED_CAST")
private fun getFieldFromBuildConfig(
clazz: Class<*>,
fieldName: String
): MutableMap<String, String> {
return clazz.getField(fieldName).get(null) as MutableMap<String, String>
}
override fun setFlavor(text: String) {
// Sets the selected flavor as chosen by the user
}
override fun getFlavor(): String {
val flavor = // // Gets the selected flavor as chosen by the user
return flavor ?: BuildConfig.FLAVOR
}
override fun getValue(key: String): String {
val flavorMap: MutableMap<String, String> = getFlavorMap()
return (flavorMap[key].toString())
}
private fun buildConfigurationFor(flavorName: String): MutableMap<String, String> {
val flavorPropertiesMap: MutableMap<String, String> by lazy {
getFieldFromBuildConfig(
BuildConfig::class.java,
"${flavorName.uppercase(Locale.ENGLISH)}_MAP"
)
}
return flavorPropertiesMap
}
override fun allFlavors(): List<String> {
return BuildConfig.SET_OF_FLAVORS
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment