1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 17:59:33 +02:00

PreferenceSet refactoring

This commit is contained in:
Nikolay Pultsin 2014-05-03 21:55:54 +01:00
parent 49651f5a6d
commit 0274d73aa9
2 changed files with 45 additions and 19 deletions

View file

@ -183,7 +183,12 @@ public class PreferenceActivity extends ZLPreferenceActivity {
if (DeviceType.Instance().isEInk()) { if (DeviceType.Instance().isEInk()) {
final EInkOptions einkOptions = new EInkOptions(); final EInkOptions einkOptions = new EInkOptions();
final Screen einkScreen = createPreferenceScreen("eink"); final Screen einkScreen = createPreferenceScreen("eink");
final ZLPreferenceSet einkPreferences = new ZLPreferenceSet(); final PreferenceSet einkPreferences = new PreferenceSet.Enabler() {
@Override
protected Boolean detectState() {
return einkOptions.EnableFastRefresh.getValue();
}
};
einkScreen.addPreference(new ZLBooleanPreference( einkScreen.addPreference(new ZLBooleanPreference(
this, einkOptions.EnableFastRefresh, einkScreen.Resource, "enableFastRefresh" this, einkOptions.EnableFastRefresh, einkScreen.Resource, "enableFastRefresh"
@ -191,7 +196,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
@Override @Override
protected void onClick() { protected void onClick() {
super.onClick(); super.onClick();
einkPreferences.setEnabled(einkOptions.EnableFastRefresh.getValue()); einkPreferences.update();
} }
}); });
@ -201,7 +206,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
einkScreen.addPreference(updateIntervalPreference); einkScreen.addPreference(updateIntervalPreference);
einkPreferences.add(updateIntervalPreference); einkPreferences.add(updateIntervalPreference);
einkPreferences.setEnabled(einkOptions.EnableFastRefresh.getValue()); einkPreferences.update();
} }
final Screen textScreen = createPreferenceScreen("text"); final Screen textScreen = createPreferenceScreen("text");
@ -357,8 +362,18 @@ public class PreferenceActivity extends ZLPreferenceActivity {
} }
} }
final ZLPreferenceSet footerPreferences = new ZLPreferenceSet(); final PreferenceSet footerPreferences = new PreferenceSet.Enabler() {
final ZLPreferenceSet bgPreferences = new ZLPreferenceSet(); @Override
protected Boolean detectState() {
return viewOptions.ScrollbarType.getValue() == FBView.SCROLLBAR_SHOW_AS_FOOTER;
}
};
final PreferenceSet bgPreferences = new PreferenceSet.Enabler() {
@Override
protected Boolean detectState() {
return "".equals(profile.WallpaperOption.getValue());
}
};
final Screen cssScreen = createPreferenceScreen("css"); final Screen cssScreen = createPreferenceScreen("css");
cssScreen.addOption(baseStyle.UseCSSFontFamilyOption, "fontFamily"); cssScreen.addOption(baseStyle.UseCSSFontFamilyOption, "fontFamily");
@ -373,7 +388,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
@Override @Override
protected void onDialogClosed(boolean result) { protected void onDialogClosed(boolean result) {
super.onDialogClosed(result); super.onDialogClosed(result);
bgPreferences.setEnabled("".equals(getValue())); bgPreferences.update();
} }
}; };
colorsScreen.addPreference(wallpaperPreference); colorsScreen.addPreference(wallpaperPreference);
@ -381,7 +396,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
bgPreferences.add( bgPreferences.add(
colorsScreen.addOption(profile.BackgroundOption, "backgroundColor") colorsScreen.addOption(profile.BackgroundOption, "backgroundColor")
); );
bgPreferences.setEnabled("".equals(profile.WallpaperOption.getValue())); bgPreferences.update();
colorsScreen.addOption(profile.HighlightingOption, "highlighting"); colorsScreen.addOption(profile.HighlightingOption, "highlighting");
colorsScreen.addOption(profile.RegularTextOption, "text"); colorsScreen.addOption(profile.RegularTextOption, "text");
colorsScreen.addOption(profile.HyperlinkTextOption, "hyperlink"); colorsScreen.addOption(profile.HyperlinkTextOption, "hyperlink");
@ -422,9 +437,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
@Override @Override
protected void onDialogClosed(boolean result) { protected void onDialogClosed(boolean result) {
super.onDialogClosed(result); super.onDialogClosed(result);
footerPreferences.setEnabled( footerPreferences.update();
findIndexOfValue(getValue()) == FBView.SCROLLBAR_SHOW_AS_FOOTER
);
} }
}); });
@ -442,9 +455,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
this, statusLineScreen.Resource, "font", this, statusLineScreen.Resource, "font",
footerOptions.Font, false footerOptions.Font, false
))); )));
footerPreferences.setEnabled( footerPreferences.update();
viewOptions.ScrollbarType.getValue() == FBView.SCROLLBAR_SHOW_AS_FOOTER
);
/* /*
final Screen colorProfileScreen = createPreferenceScreen("colorProfile"); final Screen colorProfileScreen = createPreferenceScreen("colorProfile");
@ -461,7 +472,12 @@ public class PreferenceActivity extends ZLPreferenceActivity {
scrollingScreen.addOption(pageTurningOptions.FingerScrolling, "fingerScrolling"); scrollingScreen.addOption(pageTurningOptions.FingerScrolling, "fingerScrolling");
scrollingScreen.addOption(miscOptions.EnableDoubleTap, "enableDoubleTapDetection"); scrollingScreen.addOption(miscOptions.EnableDoubleTap, "enableDoubleTapDetection");
final ZLPreferenceSet volumeKeysPreferences = new ZLPreferenceSet(); final PreferenceSet volumeKeysPreferences = new PreferenceSet.Enabler() {
@Override
protected Boolean detectState() {
return keyBindings.hasBinding(KeyEvent.KEYCODE_VOLUME_UP, false);
}
};
scrollingScreen.addPreference(new ZLCheckBoxPreference( scrollingScreen.addPreference(new ZLCheckBoxPreference(
this, scrollingScreen.Resource, "volumeKeys" this, scrollingScreen.Resource, "volumeKeys"
) { ) {
@ -479,7 +495,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
keyBindings.bindKey(KeyEvent.KEYCODE_VOLUME_DOWN, false, FBReaderApp.NoAction); keyBindings.bindKey(KeyEvent.KEYCODE_VOLUME_DOWN, false, FBReaderApp.NoAction);
keyBindings.bindKey(KeyEvent.KEYCODE_VOLUME_UP, false, FBReaderApp.NoAction); keyBindings.bindKey(KeyEvent.KEYCODE_VOLUME_UP, false, FBReaderApp.NoAction);
} }
volumeKeysPreferences.setEnabled(isChecked()); volumeKeysPreferences.update();
} }
}); });
volumeKeysPreferences.add(scrollingScreen.addPreference(new ZLCheckBoxPreference( volumeKeysPreferences.add(scrollingScreen.addPreference(new ZLCheckBoxPreference(
@ -503,7 +519,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
} }
} }
})); }));
volumeKeysPreferences.setEnabled(keyBindings.hasBinding(KeyEvent.KEYCODE_VOLUME_UP, false)); volumeKeysPreferences.update();
scrollingScreen.addOption(pageTurningOptions.Animation, "animation"); scrollingScreen.addOption(pageTurningOptions.Animation, "animation");
scrollingScreen.addPreference(new AnimationSpeedPreference( scrollingScreen.addPreference(new AnimationSpeedPreference(

View file

@ -23,16 +23,26 @@ import java.util.LinkedList;
import android.preference.Preference; import android.preference.Preference;
class ZLPreferenceSet { abstract class PreferenceSet<T> {
private final LinkedList<Preference> myPreferences = new LinkedList<Preference>(); private final LinkedList<Preference> myPreferences = new LinkedList<Preference>();
final void add(Preference preference) { final void add(Preference preference) {
myPreferences.add(preference); myPreferences.add(preference);
} }
final void setEnabled(boolean enable) { final void update() {
final T state = detectState();
for (Preference preference : myPreferences) { for (Preference preference : myPreferences) {
preference.setEnabled(enable); update(preference, state);
}
}
protected abstract T detectState();
protected abstract void update(Preference preference, T state);
static abstract class Enabler extends PreferenceSet<Boolean> {
protected void update(Preference preference, Boolean state) {
preference.setEnabled(state);
} }
} }
} }