Skip to content

Instantly share code, notes, and snippets.

@Sloy
Created September 6, 2017 13:45
Show Gist options
  • Save Sloy/5f950b62f302d17378856f7884d887ee to your computer and use it in GitHub Desktop.
Save Sloy/5f950b62f302d17378856f7884d887ee to your computer and use it in GitHub Desktop.
public class NetworkEndpointPreference extends StringPreference {
@Inject
public NetworkEndpointPreference(SharedPreferences preferences) {
super(preferences, "debug_network_endpoint_url", DebugApiEndpoints.PROD.getUrl());
}
}
public class StringPreference {
private final SharedPreferences preferences;
private final String key;
private final String defaultValue;
public StringPreference(SharedPreferences preferences, String key) {
this(preferences, key, null);
}
public StringPreference(SharedPreferences preferences, String key, String defaultValue) {
this.preferences = preferences;
this.key = key;
this.defaultValue = defaultValue;
}
public String get() {
return preferences.getString(key, defaultValue);
}
public boolean isSet() {
return preferences.contains(key);
}
public void set(String value) {
preferences.edit().putString(key, value).apply();
}
public void delete() {
preferences.edit().remove(key).apply();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment