Skip to content

Latest commit

 

History

History
 
 

coroutines

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Coroutines/Flow Preferences

Maven Central

Download

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

Usage

Create a CoroutineSharedPreferences instance which wraps a SharedPreferences:

val preferences = PreferenceManager.getDefaultSharedPreferences(context)
val coroutinePreferences = CoroutineSharedPreferences.create(preferences)

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

Create individual CoroutinePreference objects:

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

Observe changes to individual preferences:

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

Subscribe preferences to streams to store values:

showWhatsNewView.checkedChanges().skipInitialValue()
    .collect(showWhatsNew.asCollector())

(Note: checkedChanges() is from FlowBinding)