Skip to content

Instantly share code, notes, and snippets.

@idish
Created January 30, 2019 23:18
Show Gist options
  • Save idish/f46a8327da7f293f943a5bda31078c95 to your computer and use it in GitHub Desktop.
Save idish/f46a8327da7f293f943a5bda31078c95 to your computer and use it in GitHub Desktop.
abstract class SharedPreferenceLiveData<T> extends LiveData<T> {
SharedPreferences sharedPrefs;
String key;
T defValue;
public SharedPreferenceLiveData(SharedPreferences prefs, String key, T defValue) {
this.sharedPrefs = prefs;
this.key = key;
this.defValue = defValue;
}
private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (SharedPreferenceLiveData.this.key.equals(key)) {
setValue(getValueFromPreferences(key, defValue));
}
}
};
abstract T getValueFromPreferences(String key, T defValue);
@Override
protected void onActive() {
super.onActive();
setValue(getValueFromPreferences(key, defValue));
sharedPrefs.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
}
@Override
protected void onInactive() {
sharedPrefs.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
super.onInactive();
}
}
public class SharedPreferenceBooleanLiveData extends SharedPreferenceLiveData<Boolean>{
public SharedPreferenceBooleanLiveData(SharedPreferences prefs, String key, Boolean defValue) {
super(prefs, key, defValue);
}
@Override
Boolean getValueFromPreferences(String key, Boolean defValue) {
return sharedPrefs.getBoolean(key, defValue);
}
}
@fabads
Copy link

fabads commented Apr 5, 2019

Hello, I would like to implement your example above. How do you observe for the updates. Do you have an example of code I could use to put in my activity / fragment ?
I do not see any function returning a livedata I could observe. (Ok I must admit I'm not an expert :-)
Thx for your help.

@fabads
Copy link

fabads commented Apr 5, 2019

I answer to myself with what I did:

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
 SharedPreferenceStringLiveData sharedPreferenceStringLiveData = new SharedPreferenceStringLiveData(preferences, "cid", "");
 sharedPreferenceStringLiveData.getStringLiveData("cid", "").observe(this, cid -> {
            Toast.makeText(this, "Change in CID "+cid, Toast.LENGTH_SHORT).show();
 });

In SharedPreferenceLiveData, I included a new function :

public SharedPreferenceLiveData<String> getStringLiveData(String key, String defaultValue) {
    return new SharedPreferenceStringLiveData(sharedPreferences, key, defaultValue);
 }

Not sure this is the right way to do. In addition, I do not see why we need to pass key and defaultvalue in the constructor.

@maxpinto
Copy link

SharedPreferenceLiveData should extends MutableLiveData?, or am i wrong?

@cuiyiming007
Copy link

SharedPreferenceLiveData should extends MutableLiveData?, or am i wrong?
You don not need extend MutableLiveData. setValue and postValue are protected, so you can use them as long as you extend LiveData.

@baggednismo
Copy link

How do you use this with Transformation switchmap? I would like to use it with my ViewModel and the ViewModel needs to be aware of changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment