Fixes bug where the order of notifications could be jumbled when removing duplicates using a HashSet.

main
Vavassor 7 years ago
parent 8bc1955122
commit 340a6fad29
  1. 5
      app/src/main/java/com/keylesspalace/tusky/adapter/AccountAdapter.java
  2. 7
      app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java
  3. 7
      app/src/main/java/com/keylesspalace/tusky/adapter/TimelineAdapter.java
  4. 9
      app/src/main/java/com/keylesspalace/tusky/util/ListUtils.java

@ -20,9 +20,9 @@ import android.support.v7.widget.RecyclerView;
import com.keylesspalace.tusky.entity.Account; import com.keylesspalace.tusky.entity.Account;
import com.keylesspalace.tusky.interfaces.AccountActionListener; import com.keylesspalace.tusky.interfaces.AccountActionListener;
import com.keylesspalace.tusky.util.ListUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
public abstract class AccountAdapter extends RecyclerView.Adapter { public abstract class AccountAdapter extends RecyclerView.Adapter {
@ -57,8 +57,7 @@ public abstract class AccountAdapter extends RecyclerView.Adapter {
topId = uptoId; topId = uptoId;
} }
if (accountList.isEmpty()) { if (accountList.isEmpty()) {
// This construction removes duplicates. accountList = ListUtils.removeDuplicates(newAccounts);
accountList = new ArrayList<>(new HashSet<>(newAccounts));
} else { } else {
int index = accountList.indexOf(newAccounts.get(newAccounts.size() - 1)); int index = accountList.indexOf(newAccounts.get(newAccounts.size() - 1));
for (int i = 0; i < index; i++) { for (int i = 0; i < index; i++) {

@ -34,10 +34,10 @@ import com.keylesspalace.tusky.entity.Notification;
import com.keylesspalace.tusky.entity.Status; import com.keylesspalace.tusky.entity.Status;
import com.keylesspalace.tusky.interfaces.AdapterItemRemover; import com.keylesspalace.tusky.interfaces.AdapterItemRemover;
import com.keylesspalace.tusky.interfaces.StatusActionListener; import com.keylesspalace.tusky.interfaces.StatusActionListener;
import com.keylesspalace.tusky.util.ListUtils;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
public class NotificationsAdapter extends RecyclerView.Adapter implements AdapterItemRemover { public class NotificationsAdapter extends RecyclerView.Adapter implements AdapterItemRemover {
@ -181,7 +181,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter implements Adapte
public void update(@Nullable List<Notification> newNotifications, @Nullable String fromId, public void update(@Nullable List<Notification> newNotifications, @Nullable String fromId,
@Nullable String uptoId) { @Nullable String uptoId) {
if (newNotifications == null || newNotifications.isEmpty()) { if (ListUtils.isEmpty(newNotifications)) {
return; return;
} }
if (fromId != null) { if (fromId != null) {
@ -191,8 +191,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter implements Adapte
topId = uptoId; topId = uptoId;
} }
if (notifications.isEmpty()) { if (notifications.isEmpty()) {
// This construction removes duplicates. notifications = ListUtils.removeDuplicates(newNotifications);
notifications = new ArrayList<>(new HashSet<>(newNotifications));
} else { } else {
int index = notifications.indexOf(newNotifications.get(newNotifications.size() - 1)); int index = notifications.indexOf(newNotifications.get(newNotifications.size() - 1));
for (int i = 0; i < index; i++) { for (int i = 0; i < index; i++) {

@ -25,9 +25,9 @@ import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.interfaces.AdapterItemRemover; import com.keylesspalace.tusky.interfaces.AdapterItemRemover;
import com.keylesspalace.tusky.interfaces.StatusActionListener; import com.keylesspalace.tusky.interfaces.StatusActionListener;
import com.keylesspalace.tusky.entity.Status; import com.keylesspalace.tusky.entity.Status;
import com.keylesspalace.tusky.util.ListUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
public class TimelineAdapter extends RecyclerView.Adapter implements AdapterItemRemover { public class TimelineAdapter extends RecyclerView.Adapter implements AdapterItemRemover {
@ -113,7 +113,7 @@ public class TimelineAdapter extends RecyclerView.Adapter implements AdapterItem
public void update(@Nullable List<Status> newStatuses, @Nullable String fromId, public void update(@Nullable List<Status> newStatuses, @Nullable String fromId,
@Nullable String uptoId) { @Nullable String uptoId) {
if (newStatuses == null || newStatuses.isEmpty()) { if (ListUtils.isEmpty(newStatuses)) {
return; return;
} }
if (fromId != null) { if (fromId != null) {
@ -123,8 +123,7 @@ public class TimelineAdapter extends RecyclerView.Adapter implements AdapterItem
topId = uptoId; topId = uptoId;
} }
if (statuses.isEmpty()) { if (statuses.isEmpty()) {
// This construction removes duplicates. statuses = ListUtils.removeDuplicates(newStatuses);
statuses = new ArrayList<>(new HashSet<>(newStatuses));
} else { } else {
int index = statuses.indexOf(newStatuses.get(newStatuses.size() - 1)); int index = statuses.indexOf(newStatuses.get(newStatuses.size() - 1));
for (int i = 0; i < index; i++) { for (int i = 0; i < index; i++) {

@ -17,6 +17,8 @@ package com.keylesspalace.tusky.util;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
public class ListUtils { public class ListUtils {
@ -33,4 +35,11 @@ public class ListUtils {
return list.size(); return list.size();
} }
} }
/** @return a new ArrayList containing the elements without duplicates in the same order */
public static <T> ArrayList<T> removeDuplicates(List<T> list) {
LinkedHashSet<T> set = new LinkedHashSet<>();
set.addAll(list);
return new ArrayList<>(set);
}
} }

Loading…
Cancel
Save