Created
November 19, 2015 08:52
-
-
Save dpolishuk/6f228d67bdaec1b84c9e to your computer and use it in GitHub Desktop.
MyPreference.kt
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
fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Array<Pair<String, Any>>) : SharedPreferences.Editor { | |
val editor = edit() | |
val pairs = editor.func() | |
for ((key, value) in pairs) { | |
when (value) { | |
is String -> editor.putString(key, value) | |
is Set<*> -> { | |
if (!value.all { it is String }) { | |
throw IllegalArgumentException("Only Set<String> is supported") | |
} | |
editor.putStringSet(key, value as Set<String>) | |
} | |
is Int -> editor.putInt(key, value) | |
is Long -> editor.putLong(key, value) | |
is Float -> editor.putFloat(key, value) | |
is Boolean -> editor.putBoolean(key, value) | |
else -> throw IllegalArgumentException("Unsupported value type: ${value.javaClass}") | |
} | |
} | |
if (pairs.size > 0) { | |
editor.apply() | |
} | |
return editor | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment