Last active
January 6, 2021 13:28
-
-
Save fernandospr/1398b92ef64fbcfd9310e5137387915c to your computer and use it in GitHub Desktop.
CountryCode to Resource Mapper
This file contains hidden or 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
interface CountryProvider { | |
fun getCountryCode(): String | |
} |
This file contains hidden or 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
abstract class ResourceProviderContainer<T>(private val countryProvider: CountryProvider) { | |
private val map: Map<String, T> by lazy { getCountryToResourceProviderMap() } | |
fun get() = map[countryProvider.getCountryCode()] ?: getDefault() | |
protected abstract fun getCountryToResourceProviderMap(): Map<String, T> | |
protected abstract fun getDefault(): T | |
} |
This file contains hidden or 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
class SingleResourceProviderContainer(countryProvider: CountryProvider) : | |
ResourceProviderContainer<Int>(countryProvider) { | |
override fun getCountryToResourceProviderMap() = mapOf( | |
"MX" to R.string.title_mx, | |
"CO" to R.string.title_co | |
) | |
override fun getDefault() = R.string.title_default | |
} |
This file contains hidden or 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
interface MultipleResourceProvider { | |
fun getSubtitle(): Int | |
fun getDrawable(): Int | |
} | |
class MultipleResourceProviderContainer(countryProvider: CountryProvider) : | |
ResourceProviderContainer<MultipleResourceProvider>(countryProvider) { | |
override fun getCountryToResourceProviderMap() = mapOf( | |
"MX" to object : MultipleResourceProvider { | |
override fun getSubtitle() = R.string.subtitle_mx | |
override fun getDrawable() = R.drawable.ic_drawable_mx | |
}, | |
"CO" to object : MultipleResourceProvider { | |
override fun getSubtitle() = R.string.subtitle_co | |
override fun getDrawable() = R.drawable.ic_drawable_co | |
} | |
) | |
override fun getDefault() = object : MultipleResourceProvider { | |
override fun getSubtitle() = R.string.subtitle_default | |
override fun getDrawable() = R.drawable.ic_drawable_default | |
} | |
} |
This file contains hidden or 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
val countryProvider: CountryProvider // Should be injected | |
val singleResourceProvider: ResourceProviderContainer<Int> // Should be injected as SingleResourceProviderContainer | |
val multipleResourceProvider: ResourceProviderContainer<MultipleResourceProvider> // Should be injected as MultipleResourceProviderContainer | |
... | |
val title = singleResourceProvider.get() | |
val subtitle = multipleResourceProvider.get().getSubtitle() | |
val drawable = multipleResourceProvider.get().getDrawable() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment