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. * Returns the preference activity that should be used on this device.
* *
@ -436,15 +444,12 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
setParallelDownloadsText(UserPreferences.getParallelDownloads()); setParallelDownloadsText(UserPreferences.getParallelDownloads());
setEpisodeCacheSizeText(UserPreferences.getEpisodeCacheSize()); setEpisodeCacheSizeText(UserPreferences.getEpisodeCacheSize());
setDataFolderText(); setDataFolderText();
GpodnetPreferences.setSyncAttemptListener(() -> ui.getActivity().runOnUiThread( GpodnetPreferences.registerOnSharedPreferenceChangeListener(gpoddernetListener);
() -> updateLastGpodnetSyncReport(
GpodnetPreferences.getLastSyncAttemptResult(),
GpodnetPreferences.getLastSyncAttemptTimestamp())));
updateGpodnetPreferenceScreen(); updateGpodnetPreferenceScreen();
} }
public void onPause() { public void onPause() {
GpodnetPreferences.setSyncAttemptListener(null); GpodnetPreferences.unregisterOnSharedPreferenceChangeListener(gpoddernetListener);
} }
@SuppressLint("NewApi") @SuppressLint("NewApi")

View File

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