From 038847177e33d82669ff61dd730589fb58adbed4 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 2 Apr 2023 10:37:41 +0200 Subject: When both adding and removing a feed before the next sync, remove the other action (#6404) --- .../sync/queue/SynchronizationQueueStorage.java | 38 ++++++++++++++++------ 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'core/src/main/java/de/danoeh/antennapod') diff --git a/core/src/main/java/de/danoeh/antennapod/core/sync/queue/SynchronizationQueueStorage.java b/core/src/main/java/de/danoeh/antennapod/core/sync/queue/SynchronizationQueueStorage.java index 5c6d58fe3..e1e373953 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/sync/queue/SynchronizationQueueStorage.java +++ b/core/src/main/java/de/danoeh/antennapod/core/sync/queue/SynchronizationQueueStorage.java @@ -94,13 +94,15 @@ public class SynchronizationQueueStorage { protected void enqueueFeedAdded(String downloadUrl) { SharedPreferences sharedPreferences = getSharedPreferences(); - String json = sharedPreferences - .getString(QUEUED_FEEDS_ADDED, "[]"); try { - JSONArray queue = new JSONArray(json); - queue.put(downloadUrl); - sharedPreferences - .edit().putString(QUEUED_FEEDS_ADDED, queue.toString()).apply(); + JSONArray addedQueue = new JSONArray(sharedPreferences.getString(QUEUED_FEEDS_ADDED, "[]")); + addedQueue.put(downloadUrl); + JSONArray removedQueue = new JSONArray(sharedPreferences.getString(QUEUED_FEEDS_REMOVED, "[]")); + removedQueue.remove(indexOf(downloadUrl, removedQueue)); + sharedPreferences.edit() + .putString(QUEUED_FEEDS_ADDED, addedQueue.toString()) + .putString(QUEUED_FEEDS_REMOVED, removedQueue.toString()) + .apply(); } catch (JSONException jsonException) { jsonException.printStackTrace(); @@ -109,17 +111,33 @@ public class SynchronizationQueueStorage { protected void enqueueFeedRemoved(String downloadUrl) { SharedPreferences sharedPreferences = getSharedPreferences(); - String json = sharedPreferences.getString(QUEUED_FEEDS_REMOVED, "[]"); try { - JSONArray queue = new JSONArray(json); - queue.put(downloadUrl); - sharedPreferences.edit().putString(QUEUED_FEEDS_REMOVED, queue.toString()) + JSONArray removedQueue = new JSONArray(sharedPreferences.getString(QUEUED_FEEDS_REMOVED, "[]")); + removedQueue.put(downloadUrl); + JSONArray addedQueue = new JSONArray(sharedPreferences.getString(QUEUED_FEEDS_ADDED, "[]")); + addedQueue.remove(indexOf(downloadUrl, addedQueue)); + sharedPreferences.edit() + .putString(QUEUED_FEEDS_ADDED, addedQueue.toString()) + .putString(QUEUED_FEEDS_REMOVED, removedQueue.toString()) .apply(); } catch (JSONException jsonException) { jsonException.printStackTrace(); } } + private int indexOf(String string, JSONArray array) { + try { + for (int i = 0; i < array.length(); i++) { + if (array.getString(i).equals(string)) { + return i; + } + } + } catch (JSONException jsonException) { + jsonException.printStackTrace(); + } + return -1; + } + protected void enqueueEpisodeAction(EpisodeAction action) { SharedPreferences sharedPreferences = getSharedPreferences(); String json = sharedPreferences.getString(QUEUED_EPISODE_ACTIONS, "[]"); -- cgit v1.2.3