Skip to content

Commit

Permalink
automati currenciesc
Browse files Browse the repository at this point in the history
  • Loading branch information
midorikocak committed Oct 24, 2017
1 parent 05f4466 commit bb4598e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
mTextView = (TextView) view.findViewById(R.id.selectedCurrencyPreference);
preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
preferences.registerOnSharedPreferenceChangeListener(this);
mCurrencyPicker.setCurrenciesList(preferences.getStringSet("selectedCurrencies", new HashSet<String>()));


String selectedCurrency = preferences.getString("selectedCurrency", getString(R.string.default_currency));
mTextView.setText(selectedCurrency);

Expand All @@ -71,7 +72,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
// and decide, in which order they will be displayed
//Collections.reverse(nc);
mCurrencyPicker.setCurrenciesList(nc);

mCurrencyPicker.setCurrenciesList(preferences.getStringSet("selectedCurrencies", new HashSet<String>()));

mCurrencyPicker.setListener(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

public class CurrencySettingsActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new CurrencyPreferenceFragment()).commit();
}
}

public static class CurrencyPreferenceFragment extends PreferenceFragment implements CurrencyPickerListener {
public static class CurrencyPreferenceFragment extends PreferenceFragment implements CurrencyPickerListener, SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences preferences;
private CurrencyPicker mCurrencyPicker;
private CurrencyPreference currencyPreference;
Expand All @@ -41,7 +40,7 @@ public void onCreate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.preferences);
preferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
currencyPreference = (CurrencyPreference) findPreference("selectedCurrency");
currencyPreference.setCurrenciesList(preferences.getStringSet("selectedCurrencies",new HashSet<String>()));
currencyPreference.setCurrenciesList(preferences.getStringSet("selectedCurrencies", new HashSet<String>()));
mCurrencyPicker = CurrencyPicker.newInstance("Select Currency");
multiSelectListPreference = (MultiCurrencyPreference) findPreference("selectedCurrencies");

Expand All @@ -68,6 +67,25 @@ public void onCreate(Bundle savedInstanceState) {
mCurrencyPicker.setListener(this);
}

@Override
public void onResume() {
preferences.registerOnSharedPreferenceChangeListener(this);
super.onResume();
}

@Override
public void onPause() {
preferences.registerOnSharedPreferenceChangeListener(this);
super.onPause();
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("selectedCurrencies")) {
currencyPreference.setCurrenciesList(preferences.getStringSet("selectedCurrencies", new HashSet<String>()));
}
}

@Override
public void onSelectCurrency(String name, String code, String symbol, int flagDrawableResID) {
currencyPreference.setValue(code);
Expand Down

0 comments on commit bb4598e

Please sign in to comment.