Implemented Preference for Canteen Price

This commit is contained in:
Caesar2011
2018-08-02 23:42:02 +02:00
parent 5d2282ed30
commit 44d348c18c
9 changed files with 110 additions and 6 deletions

View File

@@ -1,11 +1,17 @@
package de.sebse.fuplanner.fragments; package de.sebse.fuplanner.fragments;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat; import android.support.v7.preference.PreferenceFragmentCompat;
import de.sebse.fuplanner.R; import de.sebse.fuplanner.R;
import de.sebse.fuplanner.tools.logging.Logger;
public class PrefsFragment extends PreferenceFragmentCompat { public class PrefsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
private Logger log = new Logger(this);
public static PrefsFragment newInstance() { public static PrefsFragment newInstance() {
PrefsFragment fragment = new PrefsFragment(); PrefsFragment fragment = new PrefsFragment();
@@ -18,5 +24,27 @@ public class PrefsFragment extends PreferenceFragmentCompat {
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// Load the preferences from an XML resource // Load the preferences from an XML resource
setPreferencesFromResource(R.xml.preferences, rootKey); setPreferencesFromResource(R.xml.preferences, rootKey);
for (String s : getPreferenceScreen().getSharedPreferences().getAll().keySet()) {
onSharedPreferenceChanged(getPreferenceScreen().getSharedPreferences(), s);
}
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Preference preference = getPreferenceScreen().findPreference(s);
if (preference instanceof ListPreference)
preference.setSummary(((ListPreference) preference).getEntry());
} }
} }

View File

@@ -9,6 +9,7 @@ import android.widget.BaseExpandableListAdapter;
import de.sebse.fuplanner.R; import de.sebse.fuplanner.R;
import de.sebse.fuplanner.services.Canteen.types.Day; import de.sebse.fuplanner.services.Canteen.types.Day;
import de.sebse.fuplanner.services.Canteen.types.Meal; import de.sebse.fuplanner.services.Canteen.types.Meal;
import de.sebse.fuplanner.tools.Preferences;
import de.sebse.fuplanner.tools.ui.ItemViewHolder; import de.sebse.fuplanner.tools.ui.ItemViewHolder;
import de.sebse.fuplanner.tools.ui.StringViewHolder; import de.sebse.fuplanner.tools.ui.StringViewHolder;
@@ -93,7 +94,21 @@ class MealAdapter extends BaseExpandableListAdapter {
ItemViewHolder itemHolder = new ItemViewHolder(convertView); ItemViewHolder itemHolder = new ItemViewHolder(convertView);
itemHolder.mTitle.setText(meal.getName()); itemHolder.mTitle.setText(meal.getName());
itemHolder.mSubLeft.setText(meal.getCategory()); itemHolder.mSubLeft.setText(meal.getCategory());
itemHolder.mSubRight.setText(mContext.getResources().getString(R.string.prices, meal.getPriceStdnt(), meal.getPriceEmply(), meal.getPriceOther())); String value;
switch (Preferences.getString(mContext, R.array.pref_price_group)) {
case "student":
value = mContext.getString(R.string.price, meal.getPriceStdnt());
break;
case "employee":
value = mContext.getString(R.string.price, meal.getPriceEmply());
break;
case "other":
value = mContext.getString(R.string.price, meal.getPriceOther());
break;
default:
value = mContext.getString(R.string.prices, meal.getPriceStdnt(), meal.getPriceEmply(), meal.getPriceOther());
}
itemHolder.mSubRight.setText(value);
return convertView; return convertView;
} }

View File

@@ -0,0 +1,12 @@
package de.sebse.fuplanner.tools;
import android.content.Context;
import android.support.annotation.ArrayRes;
import android.support.v7.preference.PreferenceManager;
public class Preferences {
public static String getString(Context context, @ArrayRes int key) {
String[] strings = context.getResources().getStringArray(key);
return PreferenceManager.getDefaultSharedPreferences(context).getString(strings[0], strings[1]);
}
}

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="price_group_entries">
<item>Alle</item>
<item>Student</item>
<item>Angestellter</item>
<item>Andere</item>
</string-array>
</resources>

View File

@@ -41,4 +41,6 @@
<string name="location_name">Ort: %1$s</string> <string name="location_name">Ort: %1$s</string>
<string name="module_name">Modul: %1$s</string> <string name="module_name">Modul: %1$s</string>
<string name="close">Schließen</string> <string name="close">Schließen</string>
<string name="open_data_policy">Datenschutzrichtlinie</string>
<string name="open_data_policy_summary">Öffne die Datenschutzrichtlinie nach DSGVO</string>
</resources> </resources>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pref_price_group_entries">
<item>All</item>
<item>Student</item>
<item>Employee</item>
<item>Other</item>
</string-array>
<string-array name="pref_price_group_values">
<item>all</item>
<item>student</item>
<item>employee</item>
<item>other</item>
</string-array>
</resources>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pref_price_group">
<item>@string/pref_price_group</item>
<item>@string/pref_price_group_default</item>
</string-array>
<string name="pref_price_group">pref_price_group</string>
<string name="pref_price_group_default">all</string>
</resources>

View File

@@ -48,4 +48,9 @@
<string name="module_name">Module: %1$s</string> <string name="module_name">Module: %1$s</string>
<string name="close">Close</string> <string name="close">Close</string>
<string name="root_preferences" translatable="false">root_preferences</string> <string name="root_preferences" translatable="false">root_preferences</string>
<string name="open_data_policy">Data Policy (German)</string>
<string name="open_data_policy_summary">Open Data Policy according to GDPR</string>
<string name="pref_price_group_title">Canteen Price Group</string>
<string name="pref_price_group_summary">Only show specific price category</string>
<string name="pref_price_group_dialog">Price Group Selection</string>
</resources> </resources>

View File

@@ -1,12 +1,18 @@
<?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"
android:title="@string/root_preferences"> android:title="@string/root_preferences">
<ListPreference
android:key="@string/pref_price_group"
android:defaultValue="@string/pref_price_group_default"
android:title="@string/pref_price_group_title"
android:summary="@string/pref_price_group_summary"
android:entries="@array/pref_price_group_entries"
android:entryValues="@array/pref_price_group_values"
android:dialogTitle="@string/pref_price_group_dialog" />
<PreferenceScreen <PreferenceScreen
android:title="@string/hello_blank_fragment" android:title="@string/open_data_policy"
android:summary="@string/lorem_ipsum_100"> android:summary="@string/open_data_policy_summary">
<intent android:action="android.intent.action.VIEW" <intent android:action="android.intent.action.VIEW"
android:data="http://www.android.com" /> android:data="http://www.android.com" />
</PreferenceScreen> </PreferenceScreen>
</PreferenceScreen> </PreferenceScreen>