Skip to content

Commit

Permalink
preference value change was not correctly displayed (listener was ove…
Browse files Browse the repository at this point in the history
…rwritten)
  • Loading branch information
mathisdt committed Jan 6, 2019
1 parent 8ee4a67 commit 4d259ef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
20 changes: 10 additions & 10 deletions app/src/main/java/org/zephyrsoft/sdbviewer/SettingsActivity.java
Expand Up @@ -36,8 +36,9 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
* This has to be the ONLY CHANGELISTENER - there can be only one (no add method, but just a set method).
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> {
private static Preference.OnPreferenceChangeListener bindPreferenceSummaryToValueListener = (preference, value) -> {
String stringValue = value.toString();

if (preference instanceof ListPreference) {
Expand All @@ -57,6 +58,11 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
// simple string representation.
preference.setSummary(stringValue);
}

if (preference.getKey().equals(preference.getContext().getString(R.string.pref_songs_url))) {
Registry.get(SDBFetcher.class).invalidateSavedSongs(preference.getContext());
}

return true;
};

Expand All @@ -76,15 +82,15 @@ private static boolean isXLargeTablet(Context context) {
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
* @see #bindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
preference.setOnPreferenceChangeListener(bindPreferenceSummaryToValueListener);

// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
bindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
Expand Down Expand Up @@ -170,12 +176,6 @@ public void onCreate(Bundle savedInstanceState) {
// guidelines.
bindPreferenceSummaryToValue(findPreference(getActivity().getString(R.string.pref_songs_url)));
bindPreferenceSummaryToValue(findPreference(getActivity().getString(R.string.pref_songs_reload_interval)));

findPreference(getActivity().getString(R.string.pref_songs_url))
.setOnPreferenceChangeListener((preference, newValue) -> {
Registry.get(SDBFetcher.class).invalidateSavedSongs(getActivity());
return true;
});
}

@Override
Expand Down
Expand Up @@ -99,6 +99,7 @@ public void invalidateSavedSongs(Context context) {
if (fileExists(context, Constants.FILE_LAST_UPDATED)) {
context.deleteFile(Constants.FILE_LAST_UPDATED);
}
Log.i(Constants.LOG_TAG, "invalidated the locally saved file");
} catch(Exception e) {
Log.e(Constants.LOG_TAG, "error while deleting file " + Constants.FILE_LAST_UPDATED, e);
throw new IllegalStateException("error while deleting file " + Constants.FILE_LAST_UPDATED);
Expand Down

0 comments on commit 4d259ef

Please sign in to comment.