Reputation: 1252
I have 2 types of users standard and premium. I want to show different data based on the type of user such as different support numbers, colors of card view. Can I have this as part of some config file or a Locale or any other solution for this? I don't want to add if conditions all over the app. This needs to be changed at run time so flavors is ruled out.
Upvotes: 0
Views: 48
Reputation: 1243
I believe this can be done by create a structure to switch between instances at the runtime.
Create a conf class that holds data like the support number and card view color.
data class Configuration(
val supportNumber: String,
val cardViewColor: Int
)
Create a ConfigManager
which holds the current configuration. It can also change the current configuration based on the type of user as you stated.
object ConfigManager {
var currentConfig: Configuration = Configuration(
supportNumber = "default",
cardViewColor = R.color.defaultColor
)
fun setConfig(userType: UserType) {
currentConfig =
if (userType == UserType.STANDARD) {
Configuration(
supportNumber = "standard_support_number",
cardViewColor = R.color.standardColor
)
}
else {
Configuration(
supportNumber = "premium_support_number",
cardViewColor = R.color.premiumColor
)
}
}
}
enum class UserType {
STANDARD,
PREMIUM
}
Set the configuration based on the user type when the app starts.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val userType = UserType.PREMIUM
ConfigManager.setConfig(userType)
}
}
When you need to use the support number or card view color in your app, use the values from the current configuration.
val supportNumber = ConfigManager.currentConfig.supportNumber
val cardViewColor = ConfigManager.currentConfig.cardViewColor
This allows you to change the user experience based on the type of user without having to scatter if conditions throughout your app. Instead, you just set the configuration at the start and then everywhere else in your app you refer to the current configuration.
Upvotes: 0