move processing of unit dependent preferences to extra function

This commit is contained in:
Milos Kozak 2019-12-01 22:03:17 +01:00
parent c0ad125934
commit 36b009d56c

View file

@ -87,6 +87,23 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
updatePrefSummary(myPreferenceFragment.findPreference(key)); updatePrefSummary(myPreferenceFragment.findPreference(key));
} }
private static void adjustUnitDependentPrefs(Preference pref) {
// convert preferences values to current units
String[] unitDependent = new String[]{
MainApp.gs(R.string.key_hypo_target),
MainApp.gs(R.string.key_activity_target),
MainApp.gs(R.string.key_eatingsoon_target),
MainApp.gs(R.string.key_high_mark),
MainApp.gs(R.string.key_low_mark)
};
if (Arrays.asList(unitDependent).contains(pref.getKey())) {
EditTextPreference editTextPref = (EditTextPreference) pref;
String converted = Profile.toCurrentUnitsString(SafeParse.stringToDouble(editTextPref.getText()));
editTextPref.setSummary(converted);
editTextPref.setText(converted);
}
}
private static void updatePrefSummary(Preference pref) { private static void updatePrefSummary(Preference pref) {
if (pref instanceof ListPreference) { if (pref instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref; ListPreference listPref = (ListPreference) pref;
@ -104,21 +121,8 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
plugin.updatePreferenceSummary(pref); plugin.updatePreferenceSummary(pref);
} }
} }
// convert preferences values to current units
String[] unitDependent = new String[]{
MainApp.gs(R.string.key_hypo_target),
MainApp.gs(R.string.key_activity_target),
MainApp.gs(R.string.key_eatingsoon_target),
MainApp.gs(R.string.key_high_mark),
MainApp.gs(R.string.key_low_mark)
};
if (Arrays.asList(unitDependent).contains(pref.getKey())) {
editTextPref = (EditTextPreference) pref;
String converted = Profile.toCurrentUnitsString(SafeParse.stringToDouble(editTextPref.getText()));
editTextPref.setSummary(converted);
editTextPref.setText(converted);
}
} }
adjustUnitDependentPrefs(pref);
} }
public static void initSummary(Preference p) { public static void initSummary(Preference p) {