Skip to content

Latest commit

 

History

History
 
 

livedata

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

LiveData Preferences

Maven Central

Download

implementation 'com.frybits.rx.preferences:livedata:1.1.0'

Usage

Create a LiveDataSharedPreferences instance which wraps a SharedPreferences:

val preferences = PreferenceManager.getDefaultSharedPreferences(context)
val liveDataSharedPreferences = LiveDataSharedPreferences.create(preferences)

Hint: Keep a strong reference on your LiveDataSharedPreferences instance for as long as you want to observe them to prevent listeners from being GCed.

Create individual LiveDataPreference objects:

val username = liveDataSharedPreferences.getString("username")
val showWhatsNew = liveDataSharedPreferences.getBoolean("show-whats-new", true)

Observe changes to individual preferences:

username.asLiveData().observeForever { username ->
  Log.d(TAG, "Username: $username")
}

Subscribe preferences to streams to store values:

someBooleanLiveData.observeForever(showWhatsNew.asObserver())