From 70cf6f33405eb45be765e3da45ff22034c029dc0 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Fri, 25 Aug 2023 17:14:23 +0200 Subject: [PATCH] Fix bug with unsubscribing from communities --- .../asynctasks/InsertSubscribedThings.java | 31 ++++++++----------- .../SubscribedSubredditDao.java | 2 +- .../SubscribedSubredditData.java | 4 +-- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/InsertSubscribedThings.java b/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/InsertSubscribedThings.java index a61596e5..fd00435b 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/InsertSubscribedThings.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/InsertSubscribedThings.java @@ -118,25 +118,20 @@ public class InsertSubscribedThings { (List newSubscribedSubreddits, List oldSubscribedSubreddits, List unsubscribedSubredditNames) { - int newIndex = 0; - for (int oldIndex = 0; oldIndex < oldSubscribedSubreddits.size(); oldIndex++) { - if (newIndex >= newSubscribedSubreddits.size()) { - for (; oldIndex < oldSubscribedSubreddits.size(); oldIndex++) { - unsubscribedSubredditNames.add(oldSubscribedSubreddits.get(oldIndex).getQualified_name()); - } - return; - } + List oldSubredditNames = new ArrayList<>(); + for (SubscribedSubredditData subredditData : oldSubscribedSubreddits) { + oldSubredditNames.add(subredditData.getQualified_name()); + } - SubscribedSubredditData old = oldSubscribedSubreddits.get(oldIndex); - for (; newIndex < newSubscribedSubreddits.size(); newIndex++) { - if (newSubscribedSubreddits.get(newIndex).getQualified_name().compareToIgnoreCase(old.getQualified_name()) == 0) { - newIndex++; - break; - } - if (newSubscribedSubreddits.get(newIndex).getQualified_name().compareToIgnoreCase(old.getQualified_name()) > 0) { - unsubscribedSubredditNames.add(old.getQualified_name()); - break; - } + List newSubredditNames = new ArrayList<>(); + for (SubscribedSubredditData subredditData : newSubscribedSubreddits) { + newSubredditNames.add(subredditData.getQualified_name()); + } + + + for (String subredditName : oldSubredditNames) { + if (!newSubredditNames.contains(subredditName)) { + unsubscribedSubredditNames.add(subredditName); } } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditDao.java b/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditDao.java index e040fc0d..29a51ec0 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditDao.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditDao.java @@ -31,7 +31,7 @@ public interface SubscribedSubredditDao { @Query("SELECT * from subscribed_subreddits WHERE qualified_name = :qualified_name COLLATE NOCASE AND username = :accountName COLLATE NOCASE LIMIT 1") SubscribedSubredditData getSubscribedSubredditByQualifiedName(String qualified_name, String accountName); - @Query("DELETE FROM subscribed_subreddits WHERE name = :subredditName COLLATE NOCASE AND username = :accountName COLLATE NOCASE") + @Query("DELETE FROM subscribed_subreddits WHERE qualified_name = :subredditName COLLATE NOCASE AND username = :accountName COLLATE NOCASE") void deleteSubscribedSubreddit(String subredditName, String accountName); @Query("SELECT * from subscribed_subreddits WHERE username = :qualified_name AND name LIKE '%' || :searchQuery || '%' COLLATE NOCASE AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC") diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditData.java b/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditData.java index 8bea43d0..90365f13 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditData.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditData.java @@ -129,13 +129,13 @@ public class SubscribedSubredditData implements Parcelable { @Override public int hashCode() { - return id; + return id + username.hashCode(); } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof SubscribedSubredditData) { - return id == ((SubscribedSubredditData) obj).getId(); + return id == ((SubscribedSubredditData) obj).getId() && username.equalsIgnoreCase(((SubscribedSubredditData) obj).getUsername()); } return false; }