Last active
January 26, 2016 15:57
-
-
Save justjanne/69f22fba18279a32369c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PreferenceWrapper(BuildConfig.APPLICATION_ID) | |
public abstract class Settings { | |
@Preference(key="pref_theme_id") String themeId; | |
@Preference boolean fullHostmask; | |
int textSize; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class WrappedSettings { | |
public final BooleanPreference fullHostmask; | |
public final IntPreference textSize; | |
public final StringPreference themeId; | |
public WrappedSettings(SharedPreferences pref) { | |
this.fullHostmask = new BooleanPreference(pref, "pref_full_hostmask", null); | |
this.textSize = new IntPreference(pref, "pref_text_size", null); | |
this.themeId = new StringPreference(pref, "pref_theme_id", null); | |
pref.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() { | |
@Override | |
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { | |
if (key == "pref_full_hostmask") fullHostmask.change(); | |
if (key == "pref_text_size") textSize.change(); | |
if (key == "pref_theme_id") themeId.change(); | |
} | |
}); | |
} | |
public WrappedSettings(Context ctx) { | |
this(ctx.getSharedPreferences("de.kuschku.testapp", 0)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringPreference extends AbstractPreferenceElement<String> { | |
public StringPreference(SharedPreferences pref, String key, String init) { | |
super(pref, key, init); | |
} | |
@Override | |
protected void put(String value) { | |
edit.putString(key, value); | |
} | |
@Override | |
public String get() { | |
return pref.getString(key, defValue); | |
} | |
} | |
public abstract class AbstractPreferenceElement<T> implements PreferenceElement<T>, OnChangeListener<T> { | |
private final Set<OnChangeListener<T>> listeners = new HashSet<>(); | |
protected SharedPreferences pref; | |
protected SharedPreferences.Editor edit; | |
protected String key; | |
protected T defValue; | |
public AbstractPreferenceElement(SharedPreferences pref, String key, T defValue) { | |
this.pref = pref; | |
this.key = key; | |
this.defValue = defValue; | |
} | |
public void change() { | |
change(get()); | |
} | |
public void change(T value) { | |
for (OnChangeListener<T> listener : listeners) | |
listener.change(value); | |
} | |
public void addChangeListener(OnChangeListener<T> listener) { | |
listeners.add(listener); | |
} | |
public void removeChangeListener(OnChangeListener<T> listener) { | |
listeners.remove(listener); | |
} | |
protected abstract void put(T value); | |
public void batch(SharedPreferences.Editor edit) { | |
if (this.edit != null) this.edit.commit(); | |
this.edit = edit; | |
} | |
public void set(T value) { | |
edit = pref.edit(); | |
put(value); | |
edit.commit(); | |
} | |
} | |
public interface PreferenceElement<T> { | |
void addChangeListener(OnChangeListener<T> listener); | |
void removeChangeListener(OnChangeListener<T> listener); | |
T get(); | |
void set(T value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment