From 56b53c458a5272adcca5422a82ded822686167be Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Mon, 24 Jun 2019 21:43:14 +0200 Subject: [PATCH] fix vanishing toots bug when refreshing (#1343) --- .../tusky/fragment/TimelineFragment.java | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java index 6764911b9..e48c5fa72 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java @@ -284,6 +284,7 @@ public class TimelineFragment extends SFragment implements } this.updateCurrent(); + this.loadAbove(); }); } @@ -292,7 +293,7 @@ public class TimelineFragment extends SFragment implements if (this.statuses.isEmpty()) { topId = null; } else { - topId = CollectionsKt.first(statuses, Either::isRight).asRight().getId(); + topId = CollectionsKt.first(this.statuses, Either::isRight).asRight().getId(); } this.timelineRepo.getStatuses(topId, null, null, LOAD_AT_ONCE, TimelineRequestMode.NETWORK) @@ -305,22 +306,32 @@ public class TimelineFragment extends SFragment implements if (!statuses.isEmpty()) { filterStatuses(statuses); - // Working around a bug when Mastodon API doesn't return the first - // status because of string "id < maxId". Hacking with ID doesn't - // help. - if (!this.statuses.isEmpty()) { - Either firstOld = this.statuses.get(0); - this.statuses.clear(); - this.statuses.add(firstOld); - } else { - this.statuses.clear(); + + if (!this.statuses.isEmpty() && topId != null) { + // clear old cached statuses + Iterator> iterator = statuses.iterator(); + while (iterator.hasNext()) { + Either item = iterator.next(); + if(item.isRight()) { + Status status = item.asRight(); + if (status.getId().length() < topId.length() || status.getId().compareTo(topId) <= 0) { + iterator.remove(); + } + } else { + Placeholder placeholder = item.asLeft(); + if (placeholder.getId().length() < topId.length() || placeholder.getId().compareTo(topId) <= 0) { + iterator.remove(); + } + } + + } } + this.statuses.addAll(statuses); this.updateAdapter(); } this.bottomLoading = false; - // Get more statuses so that users know that something is there - this.loadAbove(); + }, (e) -> { this.initialUpdateFailed = true; @@ -557,9 +568,10 @@ public class TimelineFragment extends SFragment implements isNeedRefresh = false; if (this.initialUpdateFailed) { updateCurrent(); - } else { - this.loadAbove(); } + + this.loadAbove(); + } private void loadAbove() { @@ -1171,7 +1183,7 @@ public class TimelineFragment extends SFragment implements private void removeConsecutivePlaceholders() { for (int i = 0; i < statuses.size() - 1; i++) { - if (!statuses.get(i).isRight() && !statuses.get(i + 1).isRight()) { + if (statuses.get(i).isLeft() && statuses.get(i + 1).isLeft()) { statuses.remove(i); } } @@ -1201,13 +1213,13 @@ public class TimelineFragment extends SFragment implements * For certain requests we don't want to see placeholders, they will be removed some other way */ private void clearPlaceholdersForResponse(List> statuses) { - CollectionsKt.removeAll(statuses, s -> !s.isRight()); + CollectionsKt.removeAll(statuses, Either::isLeft); } private void replacePlaceholderWithStatuses(List> newStatuses, boolean fullFetch, int pos) { Either placeholder = statuses.get(pos); - if (!placeholder.isRight()) { + if (placeholder.isLeft()) { statuses.remove(pos); }