daily pastebin goal
77%
SHARE
TWEET

Untitled

a guest Sep 25th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.content.SharedPreferences
  2. import kotlin.properties.ReadWriteProperty
  3. import kotlin.reflect.KFunction3
  4. import kotlin.reflect.KProperty
  5.  
  6. @Suppress("UNCHECKED_CAST")
  7. fun <T> sharedPreferences(sharedPreferences: SharedPreferences, key: String, defaultValue: T): SharedPreferencesProperty<T> =
  8.         when (defaultValue) {
  9.             is Boolean -> SharedPreferencesProperty(sharedPreferences, key, defaultValue,
  10.                     SharedPreferences::getBoolean, SharedPreferences.Editor::putBoolean)
  11.                     as SharedPreferencesProperty<T>
  12.  
  13.             is Float -> SharedPreferencesProperty(sharedPreferences, key, defaultValue,
  14.                     SharedPreferences::getFloat, SharedPreferences.Editor::putFloat)
  15.                     as SharedPreferencesProperty<T>
  16.  
  17.             is Int -> SharedPreferencesProperty(sharedPreferences, key, defaultValue,
  18.                     SharedPreferences::getInt, SharedPreferences.Editor::putInt)
  19.                     as SharedPreferencesProperty<T>
  20.  
  21.             is Long -> SharedPreferencesProperty(sharedPreferences, key, defaultValue,
  22.                     SharedPreferences::getLong, SharedPreferences.Editor::putLong)
  23.                     as SharedPreferencesProperty<T>
  24.  
  25.             is String -> SharedPreferencesProperty(sharedPreferences, key, defaultValue,
  26.                     SharedPreferences::getString, SharedPreferences.Editor::putString)
  27.                     as SharedPreferencesProperty<T>
  28.  
  29.             is Set<*> -> SharedPreferencesProperty(sharedPreferences, key, defaultValue as Set<String>,
  30.                     SharedPreferences::getStringSet, SharedPreferences.Editor::putStringSet)
  31.                     as SharedPreferencesProperty<T>
  32.  
  33.             else -> throw IllegalStateException("Illegal property type: ${(defaultValue as Any).javaClass}")
  34.         }
  35.  
  36. open class SharedPreferencesProperty<T>(
  37.         private val sharedPreferences: SharedPreferences,
  38.         val key: String,
  39.         val defaultValue: T,
  40.         val getValue: KFunction3<SharedPreferences, String, T, T>,
  41.         val setValue: KFunction3<SharedPreferences.Editor, String, T, SharedPreferences.Editor>)
  42.     : ReadWriteProperty<Any, T> {
  43.  
  44.     override fun getValue(thisRef: Any, property: KProperty<*>): T =
  45.             getValue(sharedPreferences, key, defaultValue)
  46.  
  47.     override fun setValue(thisRef: Any, property: KProperty<*>, value: T) =
  48.             setValue(sharedPreferences.edit(), key, value).apply()
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top