<commit_before>package nl.haroid;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;

/**
 * @author <NAME>
 * @author Xilv3r
 */
public final class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    public static final String USERNAME_PREF_KEY = "pref_username";
    public static final String TEGOED_PREF_KEY = "pref_max_tegoed";
    public static final String STARTDAG_PREF_KEY = "pref_start_tegoed";

    private EditTextPreference usernameText;
    private EditTextPreference tegoedText;
    private ListPreference startdagList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        //Get instance of specific preferance
        usernameText = (EditTextPreference)getPreferenceScreen().findPreference(USERNAME_PREF_KEY);
        tegoedText = (EditTextPreference)getPreferenceScreen().findPreference(TEGOED_PREF_KEY);
        startdagList = (ListPreference)getPreferenceScreen().findPreference(STARTDAG_PREF_KEY);

    }

    @Override
    protected void onResume() {
        super.onResume();
        //Set initial values
        usernameText.setSummary(getPreferenceScreen().getSharedPreferences().getString(USERNAME_PREF_KEY, getString(R.string.change_emailadres_sum)));
        tegoedText.setSummary(getString(R.string.tegoed_setting_pretext) + " " + getPreferenceScreen().getSharedPreferences().getString(TEGOED_PREF_KEY, "0"));
        startdagList.setSummary(getString(R.string.startdag_setting_pretext) + " " + getPreferenceScreen().getSharedPreferences().getString(STARTDAG_PREF_KEY, "0"));
        //Reregister Listener due to application being resumed
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //Unregister Listener due to application being paused
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        //todo (Dave) get rid of if else flow
        if (s.equals(USERNAME_PREF_KEY)){
            usernameText.setSummary(getString(R.string.emailadres_setting_pretext) + " " + sharedPreferences.getString(s, ""));
        } else if (s.equals(TEGOED_PREF_KEY)){
            tegoedText.setSummary(getString(R.string.tegoed_setting_pretext) + " " + sharedPreferences.getString(s, ""));
        } else if (s.endsWith(STARTDAG_PREF_KEY)){
            startdagList.setSummary(getString(R.string.startdag_setting_pretext) + " " + sharedPreferences.getString(s, ""));
        }
    }
}<commit_msg>Haroid 1.5-SNAPSHOT: Optimized preference pre summary setter code<commit_after>package nl.haroid;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * @author <NAME>
 * @author Xilv3r
 */
public final class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    public static final String USERNAME_PREF_KEY = "pref_username";
    public static final String TEGOED_PREF_KEY = "pref_max_tegoed";
    public static final String STARTDAG_PREF_KEY = "pref_start_tegoed";

    private Map<String, String> pretextMap = new HashMap<String, String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //Load map
        pretextMap.put(USERNAME_PREF_KEY, getString(R.string.emailadres_setting_pretext));
        pretextMap.put(TEGOED_PREF_KEY, getString(R.string.tegoed_setting_pretext));
        pretextMap.put(STARTDAG_PREF_KEY, getString(R.string.startdag_setting_pretext));

        //Iterate over all preferance objects and set initial values
        for (Map.Entry<String, String> entry : pretextMap.entrySet()){
            changePreferanceSummary(entry.getKey(), getPreferenceScreen().getSharedPreferences());
        }

        //Reregister Listener due to application being resumed
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //Unregister Listener due to application being paused
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        changePreferanceSummary(s, sharedPreferences);
    }

    private void changePreferanceSummary(String s, SharedPreferences sharedPreferences) {
        Preference preference = getPreferenceScreen().findPreference(s);
        preference.setSummary(pretextMap.get(s) + " " + sharedPreferences.getString(s, ""));
    }

}
{'fraction_non_alphanumeric': 0.09130913091309131, 'fraction_numerical': 0.0011001100110011, 'mean_word_length': 4.444111776447106, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6535780', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1497, 'n_words': 274}