Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LiveData with shared preferences

I have a settings screen where I am setting some values. When I set those values it gets saved in shared preferences and these values are needed in my request to the network api call as parameters.

Now I can use a listener for shared preferences in my activity then make an api call and get fresh data, but I want to use this with LiveData.

How can I listen for changes in the shared preferences using LiveData and then make the network call using the new parameters.

like image 737
nick.tdr Avatar asked Jun 01 '18 18:06

nick.tdr


People also ask

How can I listen for changes in shared preferences using livedata?

How can I listen for changes in the shared preferences using LiveData and then make the network call using the new parameters. The following awesome piece of code is LiveData Implementation of SharedPreference. It works perfectly. For someone who is wondering why sharedPrefs.getString (key, defValue) could be null, this post explains it well.

How to create a shared preferences file for an app?

The first thing we need to do is to create one shared preferences file per app. So name it with the package name of your app- unique and easy to associate with the app. When you want to get the values, call the getSharedPreferences () method. Shared Preferences provide modes of storing the data (private mode and public mode).

Can activity/fragment observe livedata in ViewModel?

And sure the preferenceLiveData can be inside ViewModel and let Activity/Fragment observe it. Check out the example here: Activity , ViewModel i see your challenge is calling Shared Preferences Value when API Calling in ViewModel or LiveData.

Where is the data stored using shared preferences?

The data stored using shared preferences are kept private within the scope of the application. However, shared preferences are different from that activity’s instance state. How is Shared Preferences different from Saved Instance State?


1 Answers

The following awesome piece of code is LiveData Implementation of SharedPreference. It works perfectly.

package com.chargingwatts.chargingalarm.util.preference;  import android.arch.lifecycle.LiveData import android.content.SharedPreferences  abstract class SharedPreferenceLiveData<T>(val sharedPrefs: SharedPreferences,                                            val key: String,                                            val defValue: T) : LiveData<T>() {      private val preferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->         if (key == this.key) {             value = getValueFromPreferences(key, defValue)         }     }      abstract fun getValueFromPreferences(key: String, defValue: T): T      override fun onActive() {         super.onActive()         value = getValueFromPreferences(key, defValue)         sharedPrefs.registerOnSharedPreferenceChangeListener(preferenceChangeListener)     }      override fun onInactive() {         sharedPrefs.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener)         super.onInactive()     } }  class SharedPreferenceIntLiveData(sharedPrefs: SharedPreferences, key: String, defValue: Int) :         SharedPreferenceLiveData<Int>(sharedPrefs, key, defValue) {     override fun getValueFromPreferences(key: String, defValue: Int): Int = sharedPrefs.getInt(key, defValue) }  class SharedPreferenceStringLiveData(sharedPrefs: SharedPreferences, key: String, defValue: String) :         SharedPreferenceLiveData<String>(sharedPrefs, key, defValue) {     override fun getValueFromPreferences(key: String, defValue: String): String = sharedPrefs.getString(key, defValue) }  class SharedPreferenceBooleanLiveData(sharedPrefs: SharedPreferences, key: String, defValue: Boolean) :         SharedPreferenceLiveData<Boolean>(sharedPrefs, key, defValue) {     override fun getValueFromPreferences(key: String, defValue: Boolean): Boolean = sharedPrefs.getBoolean(key, defValue) }  class SharedPreferenceFloatLiveData(sharedPrefs: SharedPreferences, key: String, defValue: Float) :         SharedPreferenceLiveData<Float>(sharedPrefs, key, defValue) {     override fun getValueFromPreferences(key: String, defValue: Float): Float = sharedPrefs.getFloat(key, defValue) }  class SharedPreferenceLongLiveData(sharedPrefs: SharedPreferences, key: String, defValue: Long) :         SharedPreferenceLiveData<Long>(sharedPrefs, key, defValue) {     override fun getValueFromPreferences(key: String, defValue: Long): Long = sharedPrefs.getLong(key, defValue) }  class SharedPreferenceStringSetLiveData(sharedPrefs: SharedPreferences, key: String, defValue: Set<String>) :         SharedPreferenceLiveData<Set<String>>(sharedPrefs, key, defValue) {     override fun getValueFromPreferences(key: String, defValue: Set<String>): Set<String> = sharedPrefs.getStringSet(key, defValue) }  fun SharedPreferences.intLiveData(key: String, defValue: Int): SharedPreferenceLiveData<Int> {     return SharedPreferenceIntLiveData(this, key, defValue) }  fun SharedPreferences.stringLiveData(key: String, defValue: String): SharedPreferenceLiveData<String> {     return SharedPreferenceStringLiveData(this, key, defValue) }  fun SharedPreferences.booleanLiveData(key: String, defValue: Boolean): SharedPreferenceLiveData<Boolean> {     return SharedPreferenceBooleanLiveData(this, key, defValue) }  fun SharedPreferences.floatLiveData(key: String, defValue: Float): SharedPreferenceLiveData<Float> {     return SharedPreferenceFloatLiveData(this, key, defValue) }  fun SharedPreferences.longLiveData(key: String, defValue: Long): SharedPreferenceLiveData<Long> {     return SharedPreferenceLongLiveData(this, key, defValue) }  fun SharedPreferences.stringSetLiveData(key: String, defValue: Set<String>): SharedPreferenceLiveData<Set<String>> {     return SharedPreferenceStringSetLiveData(this, key, defValue) } 
like image 71
Abhishek Luthra Avatar answered Sep 21 '22 11:09

Abhishek Luthra