Skip to content

Instantly share code, notes, and snippets.

@zmdominguez
Last active July 20, 2019 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmdominguez/6eaa2788c16988d391f45d8445d561f4 to your computer and use it in GitHub Desktop.
Save zmdominguez/6eaa2788c16988d391f45d8445d561f4 to your computer and use it in GitHub Desktop.
fun showThemeSwitcher(activity: Activity, prefs: SharedPreferences) {
val builder = AlertDialog.Builder(activity)
val names = mutableListOf<String>()
var selectedTheme = getSelectedTheme(activity, prefs).ordinal
builder.setTitle("Choose theme to apply")
builder.setSingleChoiceItems(SwitchableThemes.values().mapTo(names) {
it.label
}.toTypedArray(), selectedTheme) { _, which ->
selectedTheme = which
}.setPositiveButton(R.string.ok) { dialog, which ->
prefs.edit {
putInt(activity.getString(R.string.debug_switch_theme), selectedTheme)
}
activity.recreate()
dialog.dismiss()
}.show()
}
fun getSelectedTheme(context: Context, prefs: SharedPreferences): SwitchableThemes {
val themePos = prefs.getInt(context.getString(R.string.debug_switch_theme), 0)
return SwitchableThemes.values()[themePos]
}
enum class SwitchableThemes(val label: String, @StyleRes val id: Int) {
DEFAULT("WOW", R.style.Theme_Wow),
REWARDS("Variant 1", R.style.Theme_Variant_1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment