Skip to content

Instantly share code, notes, and snippets.

@chibatching
Last active January 21, 2020 03:31
Show Gist options
  • Save chibatching/9186eebec7600ab50834c7f96143da63 to your computer and use it in GitHub Desktop.
Save chibatching/9186eebec7600ab50834c7f96143da63 to your computer and use it in GitHub Desktop.
Save all values include default values declared in KotprefModel
SomePref::class.declaredMemberProperties
.forEach { property ->
property.isAccessible = true
(property.getDelegate(SomePref) as? AbstractPref<Any>)?.let {
it.setToPreference(property, property.get(SomePref)!!, SomePref.preferences)
println("save preference ${property.name} -> ${property.get(SomePref)}")
}
(property.getDelegate(SomePref) as? AbstractStringSetPref)?.let {
getSharedPreferences(SomePref.kotprefName, Context.MODE_PRIVATE)
.edit()
.putStringSet(it.preferenceKey, property.get(SomePref) as MutableSet<String>)
.commit()
println("save preference ${property.name} -> ${property.get(SomePref)}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment