Guest User

Untitled

a guest
Feb 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. class KPreference<T>(val context:Context, val name:String, private val default:T, val listener:OnPreferenceValueChanged<Any>? = null) : ReadWriteProperty<Any?, T> {
  2. private val TAG = "KPreference"
  3. val prefs by lazy {
  4. context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).apply {
  5. registerOnSharedPreferenceChangeListener({ sharedPreferences, key ->
  6. val listeners = Maps[name]
  7. if (listeners!= null && listeners.isNotEmpty()) {
  8. listeners.forEach { it.onChanged(name, getValueFromPreference(sharedPreferences, name, default) as Any) }
  9. }
  10. })
  11. }
  12. }
  13.  
  14. init {
  15. if (listener != null) {
  16. if (Maps.containsKey(name)) {
  17. val listeners = Maps[name]
  18. if (listeners != null && listeners.isNotEmpty()) {
  19. listeners.add(listener)
  20. } else {
  21. val newListeners = ArrayList<OnPreferenceValueChanged<Any>>()
  22. newListeners.add(listener)
  23. Maps.put(name, newListeners)
  24. }
  25. } else {
  26. val newListeners = ArrayList<OnPreferenceValueChanged<Any>>()
  27. newListeners.add(listener)
  28. Maps.put(name, newListeners)
  29. }
  30. }
  31. }
  32.  
  33. companion object {
  34. val PREF_NAME: String = "default"
  35. val Maps = HashMap<String, MutableList<OnPreferenceValueChanged<Any>>>()
  36. }
  37.  
  38. override fun getValue(thisRef: Any?, property: KProperty<*>): T {
  39. return getPreferenceValue(name, default)
  40. }
  41.  
  42. override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
  43. putPreferenceValue(name, value)
  44. }
  45.  
  46. @Suppress("UNCHECKED_CAST")
  47. private fun <T> getPreferenceValue(name: String, default: T): T = with(prefs) {
  48. val res: Any = when (default) {
  49. is Long -> getLong(name, default)
  50. is String -> getString(name, default)
  51. is Int -> getInt(name, default)
  52. is Boolean -> getBoolean(name, default)
  53. is Float -> getFloat(name, default)
  54. else -> throw IllegalArgumentException("This type can be saved into Preferences")
  55. }
  56. return res as T
  57. }
  58.  
  59. @Suppress("UNCHECKED_CAST")
  60. @SuppressLint("CommitPrefEdits")
  61. private fun <U> putPreferenceValue(name: String, value: U) = with(prefs.edit()) {
  62. when (value) {
  63. is Long -> putLong(name, value)
  64. is String -> putString(name, value)
  65. is Int -> putInt(name, value)
  66. is Boolean -> putBoolean(name, value)
  67. is Float -> putFloat(name, value)
  68. else -> throw IllegalArgumentException("This type can be saved into KPreferences")
  69. }.apply()
  70. }
  71.  
  72. @Suppress("UNCHECKED_CAST")
  73. private fun <T> getValueFromPreference(preferences: SharedPreferences, name: String, default: T): T = with(preferences) {
  74. val res: Any = when (default) {
  75. is Long -> getLong(name, default as Long)
  76. is String -> getString(name, default as String)
  77. is Int -> getInt(name, default as Int)
  78. is Boolean -> getBoolean(name, default as Boolean)
  79. is Float -> getFloat(name, default as Float)
  80. else -> throw IllegalArgumentException("This type can be saved into Preferences")
  81. }
  82. return res as T
  83. }
  84.  
  85. }
Add Comment
Please, Sign In to add comment