mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-22 00:06:48 +02:00
Simplify graph preferences
This commit is contained in:
@@ -15,132 +15,35 @@
|
|||||||
*/
|
*/
|
||||||
package com.health.openscale.gui.preferences;
|
package com.health.openscale.gui.preferences;
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.CheckBoxPreference;
|
|
||||||
import android.preference.EditTextPreference;
|
import android.preference.EditTextPreference;
|
||||||
import android.preference.ListPreference;
|
|
||||||
import android.preference.MultiSelectListPreference;
|
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
import android.preference.PreferenceFragment;
|
import android.preference.PreferenceFragment;
|
||||||
import android.preference.PreferenceGroup;
|
|
||||||
import android.text.method.DigitsKeyListener;
|
import android.text.method.DigitsKeyListener;
|
||||||
|
|
||||||
import com.health.openscale.R;
|
import com.health.openscale.R;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
public class GraphPreferences extends PreferenceFragment {
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class GraphPreferences extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
private static final String PREFERENCE_KEY_REGRESSION_LINE_ORDER = "regressionLineOrder";
|
||||||
|
|
||||||
public static final String PREFERENCE_KEY_REGRESSION_LINE = "regressionLine";
|
|
||||||
public static final String PREFERENCE_KEY_REGRESSION_LINE_ORDER = "regressionLineOrder";
|
|
||||||
|
|
||||||
private CheckBoxPreference regressionLine;
|
|
||||||
private EditTextPreference regressionLineOrder;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState)
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
{
|
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
addPreferencesFromResource(R.xml.graph_preferences);
|
addPreferencesFromResource(R.xml.graph_preferences);
|
||||||
|
|
||||||
regressionLine = (CheckBoxPreference) findPreference(PREFERENCE_KEY_REGRESSION_LINE);
|
EditTextPreference regressionLineOrder =
|
||||||
regressionLineOrder = (EditTextPreference) findPreference(PREFERENCE_KEY_REGRESSION_LINE_ORDER);
|
(EditTextPreference) findPreference(PREFERENCE_KEY_REGRESSION_LINE_ORDER);
|
||||||
|
|
||||||
regressionLineOrder.getEditText().setKeyListener(new DigitsKeyListener());
|
regressionLineOrder.getEditText().setKeyListener(new DigitsKeyListener());
|
||||||
|
regressionLineOrder.getEditText().setSelectAllOnFocus(true);
|
||||||
updateGraphPreferences();
|
regressionLineOrder.setSummary(regressionLineOrder.getText());
|
||||||
initSummary(getPreferenceScreen());
|
regressionLineOrder.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume()
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||||
{
|
preference.setSummary((String) newValue);
|
||||||
super.onResume();
|
return true;
|
||||||
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPause()
|
|
||||||
{
|
|
||||||
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
|
|
||||||
super.onPause();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
|
|
||||||
{
|
|
||||||
updatePrefSummary(findPreference(key));
|
|
||||||
updateGraphPreferences();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initSummary(Preference p)
|
|
||||||
{
|
|
||||||
if (p instanceof PreferenceGroup)
|
|
||||||
{
|
|
||||||
PreferenceGroup pGrp = (PreferenceGroup) p;
|
|
||||||
for (int i = 0; i < pGrp.getPreferenceCount(); i++)
|
|
||||||
{
|
|
||||||
initSummary(pGrp.getPreference(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
updatePrefSummary(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateGraphPreferences()
|
|
||||||
{
|
|
||||||
if (regressionLine.isChecked())
|
|
||||||
{
|
|
||||||
regressionLineOrder.setEnabled(true);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
regressionLineOrder.setEnabled(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updatePrefSummary(Preference p)
|
|
||||||
{
|
|
||||||
if (p instanceof ListPreference)
|
|
||||||
{
|
|
||||||
ListPreference listPref = (ListPreference) p;
|
|
||||||
p.setSummary(listPref.getEntry());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (p instanceof EditTextPreference)
|
|
||||||
{
|
|
||||||
EditTextPreference editTextPref = (EditTextPreference) p;
|
|
||||||
if (p.getTitle().toString().contains("assword"))
|
|
||||||
{
|
|
||||||
p.setSummary("******");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
p.setSummary(editTextPref.getText());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (p instanceof MultiSelectListPreference)
|
|
||||||
{
|
|
||||||
MultiSelectListPreference editMultiListPref = (MultiSelectListPreference) p;
|
|
||||||
|
|
||||||
CharSequence[] entries = editMultiListPref.getEntries();
|
|
||||||
CharSequence[] entryValues = editMultiListPref.getEntryValues();
|
|
||||||
List<String> currentEntries = new ArrayList<>();
|
|
||||||
Set<String> currentEntryValues = editMultiListPref.getValues();
|
|
||||||
|
|
||||||
for (int i = 0; i < entries.length; i++)
|
|
||||||
{
|
|
||||||
if (currentEntryValues.contains(entryValues[i].toString())) currentEntries.add(entries[i].toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
p.setSummary(currentEntries.toString());
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,32 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<CheckBoxPreference android:title="@string/label_enable_labels" android:summaryOn="@string/info_is_visible" android:summaryOff="@string/info_is_not_visible" android:key="labelsEnable" android:defaultValue="true"/>
|
<CheckBoxPreference
|
||||||
<CheckBoxPreference android:title="@string/label_enable_points" android:summaryOn="@string/info_is_visible" android:summaryOff="@string/info_is_not_visible" android:key="pointsEnable" android:defaultValue="true"/>
|
android:defaultValue="true"
|
||||||
<CheckBoxPreference android:title="@string/label_goal_line" android:summaryOn="@string/info_is_visible" android:summaryOff="@string/info_is_not_visible" android:key="goalLine" android:defaultValue="true" />
|
android:key="labelsEnable"
|
||||||
<CheckBoxPreference android:title="@string/label_regression_line" android:summaryOn="@string/info_is_visible" android:summaryOff="@string/info_is_not_visible" android:key="regressionLine" android:defaultValue="false" />
|
android:summaryOff="@string/info_is_not_visible"
|
||||||
<EditTextPreference android:title="@string/label_regression_line_degree" android:key="regressionLineOrder" android:defaultValue="1" />
|
android:summaryOn="@string/info_is_visible"
|
||||||
|
android:title="@string/label_enable_labels" />
|
||||||
|
<CheckBoxPreference
|
||||||
|
android:defaultValue="true"
|
||||||
|
android:key="pointsEnable"
|
||||||
|
android:summaryOff="@string/info_is_not_visible"
|
||||||
|
android:summaryOn="@string/info_is_visible"
|
||||||
|
android:title="@string/label_enable_points" />
|
||||||
|
<CheckBoxPreference
|
||||||
|
android:defaultValue="true"
|
||||||
|
android:key="goalLine"
|
||||||
|
android:summaryOff="@string/info_is_not_visible"
|
||||||
|
android:summaryOn="@string/info_is_visible"
|
||||||
|
android:title="@string/label_goal_line" />
|
||||||
|
<CheckBoxPreference
|
||||||
|
android:defaultValue="false"
|
||||||
|
android:key="regressionLine"
|
||||||
|
android:summaryOff="@string/info_is_not_visible"
|
||||||
|
android:summaryOn="@string/info_is_visible"
|
||||||
|
android:title="@string/label_regression_line" />
|
||||||
|
<EditTextPreference
|
||||||
|
android:defaultValue="1"
|
||||||
|
android:dependency="regressionLine"
|
||||||
|
android:key="regressionLineOrder"
|
||||||
|
android:title="@string/label_regression_line_degree" />
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
Reference in New Issue
Block a user