convert gpodder last sync attempt listener to OnSharedPreferenceChangeListener

This commit is contained in:
Domingos Lopes 2016-07-03 18:03:19 -04:00
parent 21a7ef168f
commit fc364c7dfe
2 changed files with 20 additions and 14 deletions

View File

@ -123,6 +123,14 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
}
}
private final SharedPreferences.OnSharedPreferenceChangeListener gpoddernetListener =
(sharedPreferences, key) -> {
if (GpodnetPreferences.PREF_LAST_SYNC_ATTEMPT_TIMESTAMP.equals(key)) {
updateLastGpodnetSyncReport(GpodnetPreferences.getLastSyncAttemptResult(),
GpodnetPreferences.getLastSyncAttemptTimestamp());
}
};
/**
* Returns the preference activity that should be used on this device.
*
@ -436,15 +444,12 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
setParallelDownloadsText(UserPreferences.getParallelDownloads());
setEpisodeCacheSizeText(UserPreferences.getEpisodeCacheSize());
setDataFolderText();
GpodnetPreferences.setSyncAttemptListener(() -> ui.getActivity().runOnUiThread(
() -> updateLastGpodnetSyncReport(
GpodnetPreferences.getLastSyncAttemptResult(),
GpodnetPreferences.getLastSyncAttemptTimestamp())));
GpodnetPreferences.registerOnSharedPreferenceChangeListener(gpoddernetListener);
updateGpodnetPreferenceScreen();
}
public void onPause() {
GpodnetPreferences.setSyncAttemptListener(null);
GpodnetPreferences.unregisterOnSharedPreferenceChangeListener(gpoddernetListener);
}
@SuppressLint("NewApi")

View File

@ -62,14 +62,22 @@ public class GpodnetPreferences {
private static boolean lastSyncAttemptResult;
private static Runnable syncAttemptListener;
private static boolean preferencesLoaded = false;
private static SharedPreferences getPreferences() {
return ClientConfig.applicationCallbacks.getApplicationInstance().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public static void registerOnSharedPreferenceChangeListener(
SharedPreferences.OnSharedPreferenceChangeListener listener) {
getPreferences().registerOnSharedPreferenceChangeListener(listener);
}
public static void unregisterOnSharedPreferenceChangeListener(
SharedPreferences.OnSharedPreferenceChangeListener listener) {
getPreferences().unregisterOnSharedPreferenceChangeListener(listener);
}
private static synchronized void ensurePreferencesLoaded() {
if (!preferencesLoaded) {
SharedPreferences prefs = getPreferences();
@ -178,9 +186,6 @@ public class GpodnetPreferences {
GpodnetPreferences.lastSyncAttemptTimestamp = timestamp;
writePreference(PREF_LAST_SYNC_ATTEMPT_RESULT, result);
writePreference(PREF_LAST_SYNC_ATTEMPT_TIMESTAMP, timestamp);
if (timestamp != 0 && syncAttemptListener != null) {
syncAttemptListener.run();
}
}
public static String getHostname() {
@ -309,10 +314,6 @@ public class GpodnetPreferences {
UserPreferences.setGpodnetNotificationsEnabled();
}
public static void setSyncAttemptListener(Runnable listener) {
syncAttemptListener = listener;
}
private static Set<String> readListFromString(String s) {
Set<String> result = new HashSet<>();
for (String item : s.split(" ")) {