Caching toots (#809)
* Initial timeline cache implementation * Fix build/DI errors for caching * Rename timeline entities tables. Add migration. Add DB scheme file. * Fix uniqueness problem, change offline strategy, improve mapping * Try to merge in new statuses, fix bottom loading, fix saving spans. * Fix reblogs IDs, fix inserting elements from top * Send one more request to get latest timeline statuses * Give Timeline placeholders string id. Rewrite Either in Kotlin * Initial placeholder implementation for caching * Fix crash on removing overlap statuses * Migrate counters to long * Remove unused counters. Add minimal TimelineDAOTest * Fix bug with placeholder ID * Update cache in response to events. Refactor TimelineCases * Fix crash, reduce number of placeholders * Fix crash, fix filtering, improve placeholder handling * Fix migration, add 8-9 migration test * Fix initial timeline update, remove more placeholders * Add cleanup for old statuses * Fix cleanup * Delete ExampleInstrumentedTest * Improve timeline UX regarding caching * Fix typos * Fix initial timeline update * Cleanup/fix initial timeline update * Workaround for weird behavior of first post on initial tl update. * Change counter types back to int * Clear timeline cache on logout * Fix loading when timeline is completely empty * Fix androidx migration issues * Fix tests * Apply caching feedback * Save account emojis to cache * Fix warnings and bugs
This commit is contained in:
parent
75158a3aa0
commit
3ab78a19bc
29 changed files with 1950 additions and 497 deletions
|
|
@ -15,28 +15,11 @@
|
|||
|
||||
package com.keylesspalace.tusky.fragment;
|
||||
|
||||
import androidx.arch.core.util.Function;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.google.android.material.tabs.TabLayout;
|
||||
import androidx.core.util.Pair;
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||
import androidx.appcompat.content.res.AppCompatResources;
|
||||
import androidx.recyclerview.widget.AsyncDifferConfig;
|
||||
import androidx.recyclerview.widget.AsyncListDiffer;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.ListUpdateCallback;
|
||||
import androidx.recyclerview.widget.DividerItemDecoration;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.recyclerview.widget.SimpleItemAnimator;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
|
|
@ -44,6 +27,8 @@ import android.view.ViewGroup;
|
|||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.google.android.material.tabs.TabLayout;
|
||||
import com.keylesspalace.tusky.R;
|
||||
import com.keylesspalace.tusky.adapter.TimelineAdapter;
|
||||
import com.keylesspalace.tusky.appstore.BlockEvent;
|
||||
|
|
@ -62,9 +47,11 @@ import com.keylesspalace.tusky.interfaces.ActionButtonActivity;
|
|||
import com.keylesspalace.tusky.interfaces.StatusActionListener;
|
||||
import com.keylesspalace.tusky.network.MastodonApi;
|
||||
import com.keylesspalace.tusky.network.TimelineCases;
|
||||
import com.keylesspalace.tusky.repository.Placeholder;
|
||||
import com.keylesspalace.tusky.repository.TimelineRepository;
|
||||
import com.keylesspalace.tusky.repository.TimelineRequestMode;
|
||||
import com.keylesspalace.tusky.util.CollectionUtil;
|
||||
import com.keylesspalace.tusky.util.Either;
|
||||
import com.keylesspalace.tusky.util.HttpHeaderLink;
|
||||
import com.keylesspalace.tusky.util.ListUtils;
|
||||
import com.keylesspalace.tusky.util.PairedList;
|
||||
import com.keylesspalace.tusky.util.ThemeUtils;
|
||||
|
|
@ -72,16 +59,34 @@ import com.keylesspalace.tusky.util.ViewDataUtils;
|
|||
import com.keylesspalace.tusky.view.EndlessOnScrollListener;
|
||||
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.content.res.AppCompatResources;
|
||||
import androidx.arch.core.util.Function;
|
||||
import androidx.core.util.Pair;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.recyclerview.widget.AsyncDifferConfig;
|
||||
import androidx.recyclerview.widget.AsyncListDiffer;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.DividerItemDecoration;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.ListUpdateCallback;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.recyclerview.widget.SimpleItemAnimator;
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||
import at.connyduck.sparkbutton.helpers.Utils;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
|
@ -120,6 +125,9 @@ public class TimelineFragment extends SFragment implements
|
|||
public TimelineCases timelineCases;
|
||||
@Inject
|
||||
public EventHub eventHub;
|
||||
@Inject
|
||||
public TimelineRepository timelineRepo;
|
||||
|
||||
@Inject
|
||||
public AccountManager accountManager;
|
||||
|
||||
|
|
@ -143,14 +151,9 @@ public class TimelineFragment extends SFragment implements
|
|||
private boolean hideFab;
|
||||
private boolean bottomLoading;
|
||||
|
||||
@Nullable
|
||||
private String bottomId = null;
|
||||
@Nullable
|
||||
private String topId = null;
|
||||
private long maxPlaceholderId = -1;
|
||||
private boolean didLoadEverythingBottom;
|
||||
|
||||
private boolean alwaysShowSensitiveMedia;
|
||||
private boolean initialUpdateFailed = false;
|
||||
|
||||
@Override
|
||||
protected TimelineCases timelineCases() {
|
||||
|
|
@ -161,15 +164,15 @@ public class TimelineFragment extends SFragment implements
|
|||
new PairedList<>(new Function<Either<Placeholder, Status>, StatusViewData>() {
|
||||
@Override
|
||||
public StatusViewData apply(Either<Placeholder, Status> input) {
|
||||
Status status = input.getAsRightOrNull();
|
||||
Status status = input.asRightOrNull();
|
||||
if (status != null) {
|
||||
return ViewDataUtils.statusToViewData(
|
||||
status,
|
||||
alwaysShowSensitiveMedia
|
||||
);
|
||||
} else {
|
||||
Placeholder placeholder = input.getAsLeft();
|
||||
return new StatusViewData.Placeholder(placeholder.id, false);
|
||||
Placeholder placeholder = input.asLeft();
|
||||
return new StatusViewData.Placeholder(placeholder.getId(), false);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -191,18 +194,6 @@ public class TimelineFragment extends SFragment implements
|
|||
return fragment;
|
||||
}
|
||||
|
||||
private static final class Placeholder {
|
||||
final long id;
|
||||
|
||||
public static Placeholder getInstance(long id) {
|
||||
return new Placeholder(id);
|
||||
}
|
||||
|
||||
private Placeholder(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
|
@ -238,7 +229,7 @@ public class TimelineFragment extends SFragment implements
|
|||
if (statuses.isEmpty()) {
|
||||
progressBar.setVisibility(View.VISIBLE);
|
||||
bottomLoading = true;
|
||||
sendFetchTimelineRequest(null, null, FetchEnd.BOTTOM, -1);
|
||||
this.sendInitialRequest();
|
||||
} else {
|
||||
progressBar.setVisibility(View.GONE);
|
||||
}
|
||||
|
|
@ -246,6 +237,80 @@ public class TimelineFragment extends SFragment implements
|
|||
return rootView;
|
||||
}
|
||||
|
||||
private void sendInitialRequest() {
|
||||
if (this.kind == Kind.HOME) {
|
||||
this.tryCache();
|
||||
} else {
|
||||
sendFetchTimelineRequest(null, null, FetchEnd.BOTTOM, -1);
|
||||
}
|
||||
}
|
||||
|
||||
private void tryCache() {
|
||||
// Request timeline from disk to make it quick, then replace it with timeline from
|
||||
// the server to update it
|
||||
this.timelineRepo.getStatuses(null, null, LOAD_AT_ONCE,
|
||||
TimelineRequestMode.DISK)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
|
||||
.subscribe(statuses -> {
|
||||
filterStatuses(statuses);
|
||||
|
||||
if (statuses.size() > 1) {
|
||||
this.clearPlaceholdersForResponse(statuses);
|
||||
this.statuses.clear();
|
||||
this.statuses.addAll(statuses);
|
||||
this.updateAdapter();
|
||||
this.progressBar.setVisibility(View.GONE);
|
||||
// Request statuses including current top to refresh all of them
|
||||
}
|
||||
|
||||
this.updateCurrent();
|
||||
});
|
||||
}
|
||||
|
||||
private void updateCurrent() {
|
||||
String topId;
|
||||
if (this.statuses.isEmpty()) {
|
||||
topId = null;
|
||||
} else {
|
||||
topId = CollectionsKt.first(statuses, Either::isRight).asRight().getId();
|
||||
}
|
||||
this.timelineRepo.getStatuses(topId, null, LOAD_AT_ONCE,
|
||||
TimelineRequestMode.NETWORK)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
|
||||
.subscribe(
|
||||
(statuses) -> {
|
||||
this.initialUpdateFailed = false;
|
||||
// When cached timeline is too old, we would replace it with nothing
|
||||
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<Placeholder, Status> firstOld = this.statuses.get(0);
|
||||
this.statuses.clear();
|
||||
this.statuses.add(firstOld);
|
||||
} else {
|
||||
this.statuses.clear();
|
||||
}
|
||||
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;
|
||||
// Indicate that we are not loading anymore
|
||||
this.progressBar.setVisibility(View.GONE);
|
||||
this.swipeRefreshLayout.setRefreshing(false);
|
||||
});
|
||||
}
|
||||
|
||||
private void setupTimelinePreferences() {
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||
alwaysShowSensitiveMedia = accountManager.getActiveAccount().getAlwaysShowSensitiveMedia();
|
||||
|
|
@ -302,7 +367,7 @@ public class TimelineFragment extends SFragment implements
|
|||
for (int i = 0; i < statuses.size(); i++) {
|
||||
Either<Placeholder, Status> either = statuses.get(i);
|
||||
if (either.isRight()
|
||||
&& id.equals(either.getAsRight().getId())) {
|
||||
&& id.equals(either.asRight().getId())) {
|
||||
statuses.remove(either);
|
||||
updateAdapter();
|
||||
break;
|
||||
|
|
@ -443,31 +508,38 @@ public class TimelineFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
sendFetchTimelineRequest(null, topId, FetchEnd.TOP, -1);
|
||||
if (this.initialUpdateFailed) {
|
||||
updateCurrent();
|
||||
} else {
|
||||
this.loadAbove();
|
||||
}
|
||||
}
|
||||
|
||||
private void loadAbove() {
|
||||
Either<Placeholder, Status> firstOrNull =
|
||||
CollectionsKt.firstOrNull(this.statuses, Either::isRight);
|
||||
if (firstOrNull != null) {
|
||||
this.sendFetchTimelineRequest(null, firstOrNull.asRight().getId(), FetchEnd.TOP, -1);
|
||||
} else {
|
||||
this.sendFetchTimelineRequest(null, null, FetchEnd.BOTTOM, -1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReply(int position) {
|
||||
super.reply(statuses.get(position).getAsRight());
|
||||
super.reply(statuses.get(position).asRight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReblog(final boolean reblog, final int position) {
|
||||
final Status status = statuses.get(position).getAsRight();
|
||||
timelineCases.reblogWithCallback(status, reblog, new Callback<Status>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
|
||||
|
||||
if (response.isSuccessful()) {
|
||||
setRebloggedForStatus(position, status, reblog);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
|
||||
Log.d(TAG, "Failed to reblog status " + status.getId(), t);
|
||||
}
|
||||
});
|
||||
final Status status = statuses.get(position).asRight();
|
||||
timelineCases.reblog(status, reblog)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
|
||||
.subscribe(
|
||||
(newStatus) -> setRebloggedForStatus(position, status, reblog),
|
||||
(err) -> Log.d(TAG, "Failed to reblog status " + status.getId(), err)
|
||||
);
|
||||
}
|
||||
|
||||
private void setRebloggedForStatus(int position, Status status, boolean reblog) {
|
||||
|
|
@ -491,22 +563,15 @@ public class TimelineFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onFavourite(final boolean favourite, final int position) {
|
||||
final Status status = statuses.get(position).getAsRight();
|
||||
final Status status = statuses.get(position).asRight();
|
||||
|
||||
timelineCases.favouriteWithCallback(status, favourite, new Callback<Status>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
|
||||
|
||||
if (response.isSuccessful()) {
|
||||
setFavouriteForStatus(position, status, favourite);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
|
||||
Log.d(TAG, "Failed to favourite status " + status.getId(), t);
|
||||
}
|
||||
});
|
||||
timelineCases.favourite(status, favourite)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
|
||||
.subscribe(
|
||||
(newStatus) -> setFavouriteForStatus(position, newStatus, favourite),
|
||||
(err) -> Log.d(TAG, "Failed to favourite status " + status.getId(), err)
|
||||
);
|
||||
}
|
||||
|
||||
private void setFavouriteForStatus(int position, Status status, boolean favourite) {
|
||||
|
|
@ -530,12 +595,12 @@ public class TimelineFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onMore(View view, final int position) {
|
||||
super.more(statuses.get(position).getAsRight(), view, position);
|
||||
super.more(statuses.get(position).asRight(), view, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpenReblog(int position) {
|
||||
super.openReblog(statuses.get(position).getAsRight());
|
||||
super.openReblog(statuses.get(position).asRight());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -560,16 +625,16 @@ public class TimelineFragment extends SFragment implements
|
|||
public void onLoadMore(int position) {
|
||||
//check bounds before accessing list,
|
||||
if (statuses.size() >= position && position > 0) {
|
||||
Status fromStatus = statuses.get(position - 1).getAsRightOrNull();
|
||||
Status toStatus = statuses.get(position + 1).getAsRightOrNull();
|
||||
Status fromStatus = statuses.get(position - 1).asRightOrNull();
|
||||
Status toStatus = statuses.get(position + 1).asRightOrNull();
|
||||
if (fromStatus == null || toStatus == null) {
|
||||
Log.e(TAG, "Failed to load more at " + position + ", wrong placeholder position");
|
||||
return;
|
||||
}
|
||||
sendFetchTimelineRequest(fromStatus.getId(), toStatus.getId(), FetchEnd.MIDDLE, position);
|
||||
|
||||
Placeholder placeholder = statuses.get(position).getAsLeft();
|
||||
StatusViewData newViewData = new StatusViewData.Placeholder(placeholder.id, true);
|
||||
Placeholder placeholder = statuses.get(position).asLeft();
|
||||
StatusViewData newViewData = new StatusViewData.Placeholder(placeholder.getId(), true);
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
updateAdapter();
|
||||
} else {
|
||||
|
|
@ -606,14 +671,14 @@ public class TimelineFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onViewMedia(int position, int attachmentIndex, View view) {
|
||||
Status status = statuses.get(position).getAsRightOrNull();
|
||||
Status status = statuses.get(position).asRightOrNull();
|
||||
if (status == null) return;
|
||||
super.viewMedia(attachmentIndex, status, view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewThread(int position) {
|
||||
super.viewThread(statuses.get(position).getAsRight());
|
||||
super.viewThread(statuses.get(position).asRight());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -703,7 +768,7 @@ public class TimelineFragment extends SFragment implements
|
|||
// using iterator to safely remove items while iterating
|
||||
Iterator<Either<Placeholder, Status>> iterator = statuses.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Status status = iterator.next().getAsRightOrNull();
|
||||
Status status = iterator.next().asRightOrNull();
|
||||
if (status != null && status.getAccount().getId().equals(accountId)) {
|
||||
iterator.remove();
|
||||
}
|
||||
|
|
@ -720,16 +785,29 @@ public class TimelineFragment extends SFragment implements
|
|||
Either<Placeholder, Status> last = statuses.get(statuses.size() - 1);
|
||||
Placeholder placeholder;
|
||||
if (last.isRight()) {
|
||||
placeholder = newPlaceholder();
|
||||
statuses.add(Either.left(placeholder));
|
||||
final String placeholderId = new BigInteger(last.asRight().getId())
|
||||
.subtract(BigInteger.ONE)
|
||||
.toString();
|
||||
placeholder = new Placeholder(placeholderId);
|
||||
statuses.add(new Either.Left<>(placeholder));
|
||||
} else {
|
||||
placeholder = last.getAsLeft();
|
||||
placeholder = last.asLeft();
|
||||
}
|
||||
statuses.setPairedItem(statuses.size() - 1,
|
||||
new StatusViewData.Placeholder(placeholder.id, true));
|
||||
new StatusViewData.Placeholder(placeholder.getId(), true));
|
||||
|
||||
updateAdapter();
|
||||
|
||||
String bottomId = null;
|
||||
final ListIterator<Either<Placeholder, Status>> iterator =
|
||||
this.statuses.listIterator(this.statuses.size());
|
||||
while (iterator.hasPrevious()) {
|
||||
Either<Placeholder, Status> previous = iterator.previous();
|
||||
if (previous.isRight()) {
|
||||
bottomId = previous.asRight().getId();
|
||||
break;
|
||||
}
|
||||
}
|
||||
sendFetchTimelineRequest(bottomId, null, FetchEnd.BOTTOM, -1);
|
||||
}
|
||||
|
||||
|
|
@ -782,44 +860,54 @@ public class TimelineFragment extends SFragment implements
|
|||
private void sendFetchTimelineRequest(@Nullable String fromId, @Nullable String uptoId,
|
||||
final FetchEnd fetchEnd, final int pos) {
|
||||
|
||||
Callback<List<Status>> callback = new Callback<List<Status>>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<List<Status>> call, @NonNull Response<List<Status>> response) {
|
||||
if (response.isSuccessful()) {
|
||||
String linkHeader = response.headers().get("Link");
|
||||
onFetchTimelineSuccess(response.body(), linkHeader, fetchEnd, pos);
|
||||
} else {
|
||||
onFetchTimelineFailure(new Exception(response.message()), fetchEnd, pos);
|
||||
if (kind == Kind.HOME) {
|
||||
TimelineRequestMode mode;
|
||||
// allow getting old statuses/fallbacks for network only for for bottom loading
|
||||
if (fetchEnd == FetchEnd.BOTTOM) {
|
||||
mode = TimelineRequestMode.ANY;
|
||||
} else {
|
||||
mode = TimelineRequestMode.NETWORK;
|
||||
}
|
||||
timelineRepo.getStatuses(fromId, uptoId, LOAD_AT_ONCE, mode)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
|
||||
.subscribe(
|
||||
(result) -> onFetchTimelineSuccess(result, fetchEnd, pos),
|
||||
(err) -> onFetchTimelineFailure(new Exception(err), fetchEnd, pos)
|
||||
);
|
||||
} else {
|
||||
Callback<List<Status>> callback = new Callback<List<Status>>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<List<Status>> call, @NonNull Response<List<Status>> response) {
|
||||
if (response.isSuccessful()) {
|
||||
onFetchTimelineSuccess(liftStatusList(response.body()), fetchEnd, pos);
|
||||
} else {
|
||||
onFetchTimelineFailure(new Exception(response.message()), fetchEnd, pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<List<Status>> call, @NonNull Throwable t) {
|
||||
onFetchTimelineFailure((Exception) t, fetchEnd, pos);
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<List<Status>> call, @NonNull Throwable t) {
|
||||
onFetchTimelineFailure((Exception) t, fetchEnd, pos);
|
||||
}
|
||||
};
|
||||
|
||||
Call<List<Status>> listCall = getFetchCallByTimelineType(kind, hashtagOrId, fromId, uptoId);
|
||||
callList.add(listCall);
|
||||
listCall.enqueue(callback);
|
||||
Call<List<Status>> listCall = getFetchCallByTimelineType(kind, hashtagOrId, fromId, uptoId);
|
||||
callList.add(listCall);
|
||||
listCall.enqueue(callback);
|
||||
}
|
||||
}
|
||||
|
||||
private void onFetchTimelineSuccess(List<Status> statuses, String linkHeader,
|
||||
private void onFetchTimelineSuccess(List<Either<Placeholder, Status>> statuses,
|
||||
FetchEnd fetchEnd, int pos) {
|
||||
|
||||
// We filled the hole (or reached the end) if the server returned less statuses than we
|
||||
// we asked for.
|
||||
boolean fullFetch = statuses.size() >= LOAD_AT_ONCE;
|
||||
filterStatuses(statuses);
|
||||
List<HttpHeaderLink> links = HttpHeaderLink.parse(linkHeader);
|
||||
switch (fetchEnd) {
|
||||
case TOP: {
|
||||
HttpHeaderLink previous = HttpHeaderLink.findByRelationType(links, "prev");
|
||||
String uptoId = null;
|
||||
if (previous != null) {
|
||||
uptoId = previous.uri.getQueryParameter("since_id");
|
||||
}
|
||||
updateStatuses(statuses, null, uptoId, fullFetch);
|
||||
updateStatuses(statuses, fullFetch);
|
||||
break;
|
||||
}
|
||||
case MIDDLE: {
|
||||
|
|
@ -827,29 +915,21 @@ public class TimelineFragment extends SFragment implements
|
|||
break;
|
||||
}
|
||||
case BOTTOM: {
|
||||
HttpHeaderLink next = HttpHeaderLink.findByRelationType(links, "next");
|
||||
String fromId = null;
|
||||
if (next != null) {
|
||||
fromId = next.uri.getQueryParameter("max_id");
|
||||
}
|
||||
if (!this.statuses.isEmpty()
|
||||
&& !this.statuses.get(this.statuses.size() - 1).isRight()) {
|
||||
this.statuses.remove(this.statuses.size() - 1);
|
||||
updateAdapter();
|
||||
}
|
||||
|
||||
if (!statuses.isEmpty() && !statuses.get(statuses.size() - 1).isRight()) {
|
||||
// Removing placeholder if it's the last one from the cache
|
||||
statuses.remove(statuses.size() - 1);
|
||||
}
|
||||
int oldSize = this.statuses.size();
|
||||
if (this.statuses.size() > 1) {
|
||||
addItems(statuses, fromId);
|
||||
addItems(statuses);
|
||||
} else {
|
||||
/* If this is the first fetch, also save the id from the "previous" link and
|
||||
* treat this operation as a refresh so the scroll position doesn't get pushed
|
||||
* down to the end. */
|
||||
HttpHeaderLink previous = HttpHeaderLink.findByRelationType(links, "prev");
|
||||
String uptoId = null;
|
||||
if (previous != null) {
|
||||
uptoId = previous.uri.getQueryParameter("since_id");
|
||||
}
|
||||
updateStatuses(statuses, fromId, uptoId, fullFetch);
|
||||
updateStatuses(statuses, fullFetch);
|
||||
}
|
||||
if (this.statuses.size() == oldSize) {
|
||||
// This may be a brittle check but seems like it works
|
||||
|
|
@ -859,7 +939,7 @@ public class TimelineFragment extends SFragment implements
|
|||
break;
|
||||
}
|
||||
}
|
||||
fulfillAnyQueuedFetches(fetchEnd);
|
||||
updateBottomLoadingState(fetchEnd);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
swipeRefreshLayout.setRefreshing(false);
|
||||
if (this.statuses.size() == 0) {
|
||||
|
|
@ -874,23 +954,25 @@ public class TimelineFragment extends SFragment implements
|
|||
swipeRefreshLayout.setRefreshing(false);
|
||||
|
||||
if (fetchEnd == FetchEnd.MIDDLE && !statuses.get(position).isRight()) {
|
||||
Placeholder placeholder = statuses.get(position).getAsLeftOrNull();
|
||||
Placeholder placeholder = statuses.get(position).asLeftOrNull();
|
||||
StatusViewData newViewData;
|
||||
if (placeholder == null) {
|
||||
placeholder = newPlaceholder();
|
||||
Status above = statuses.get(position - 1).asRight();
|
||||
String newId = this.idPlus(above.getId(), -1);
|
||||
placeholder = new Placeholder(newId);
|
||||
}
|
||||
newViewData = new StatusViewData.Placeholder(placeholder.id, false);
|
||||
newViewData = new StatusViewData.Placeholder(placeholder.getId(), false);
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
updateAdapter();
|
||||
}
|
||||
|
||||
Log.e(TAG, "Fetch Failure: " + exception.getMessage());
|
||||
fulfillAnyQueuedFetches(fetchEnd);
|
||||
updateBottomLoadingState(fetchEnd);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
private void fulfillAnyQueuedFetches(FetchEnd fetchEnd) {
|
||||
private void updateBottomLoadingState(FetchEnd fetchEnd) {
|
||||
switch (fetchEnd) {
|
||||
case BOTTOM: {
|
||||
bottomLoading = false;
|
||||
|
|
@ -899,80 +981,90 @@ public class TimelineFragment extends SFragment implements
|
|||
}
|
||||
}
|
||||
|
||||
private void filterStatuses(List<Status> statuses) {
|
||||
Iterator<Status> it = statuses.iterator();
|
||||
private void filterStatuses(List<Either<Placeholder, Status>> statuses) {
|
||||
Iterator<Either<Placeholder, Status>> it = statuses.iterator();
|
||||
while (it.hasNext()) {
|
||||
Status status = it.next();
|
||||
if ((status.getInReplyToId() != null && filterRemoveReplies)
|
||||
Status status = it.next().asRightOrNull();
|
||||
if (status != null
|
||||
&& ((status.getInReplyToId() != null && filterRemoveReplies)
|
||||
|| (status.getReblog() != null && filterRemoveReblogs)
|
||||
|| (filterRemoveRegex && (filterRemoveRegexMatcher.reset(status.getContent()).find()
|
||||
|| (!status.getSpoilerText().isEmpty() && filterRemoveRegexMatcher.reset(status.getSpoilerText()).find())))) {
|
||||
|| (!status.getSpoilerText().isEmpty() && filterRemoveRegexMatcher.reset(status.getSpoilerText()).find()))))) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateStatuses(List<Status> newStatuses, @Nullable String fromId,
|
||||
@Nullable String toId, boolean fullFetch) {
|
||||
private void updateStatuses(List<Either<Placeholder, Status>> newStatuses, boolean fullFetch) {
|
||||
if (ListUtils.isEmpty(newStatuses)) {
|
||||
return;
|
||||
}
|
||||
if (fromId != null) {
|
||||
bottomId = fromId;
|
||||
}
|
||||
if (toId != null) {
|
||||
topId = toId;
|
||||
}
|
||||
|
||||
List<Either<Placeholder, Status>> liftedNew = liftStatusList(newStatuses);
|
||||
|
||||
if (statuses.isEmpty()) {
|
||||
statuses.addAll(liftedNew);
|
||||
statuses.addAll(newStatuses);
|
||||
} else {
|
||||
Either<Placeholder, Status> lastOfNew = liftedNew.get(newStatuses.size() - 1);
|
||||
Either<Placeholder, Status> lastOfNew = newStatuses.get(newStatuses.size() - 1);
|
||||
int index = statuses.indexOf(lastOfNew);
|
||||
|
||||
for (int i = 0; i < index; i++) {
|
||||
statuses.remove(0);
|
||||
}
|
||||
int newIndex = liftedNew.indexOf(statuses.get(0));
|
||||
int newIndex = newStatuses.indexOf(statuses.get(0));
|
||||
if (newIndex == -1) {
|
||||
if (index == -1 && fullFetch) {
|
||||
liftedNew.add(Either.left(newPlaceholder()));
|
||||
String placeholderId = idPlus(CollectionsKt.last(newStatuses, Either::isRight)
|
||||
.asRight().getId(), 1);
|
||||
newStatuses.add(new Either.Left<>(new Placeholder(placeholderId)));
|
||||
}
|
||||
statuses.addAll(0, liftedNew);
|
||||
statuses.addAll(0, newStatuses);
|
||||
} else {
|
||||
statuses.addAll(0, liftedNew.subList(0, newIndex));
|
||||
statuses.addAll(0, newStatuses.subList(0, newIndex));
|
||||
}
|
||||
}
|
||||
// Remove all consecutive placeholders
|
||||
removeConsecutivePlaceholders();
|
||||
updateAdapter();
|
||||
}
|
||||
|
||||
private void addItems(List<Status> newStatuses, @Nullable String fromId) {
|
||||
private void removeConsecutivePlaceholders() {
|
||||
for (int i = 0; i < statuses.size() - 1; i++) {
|
||||
if (!statuses.get(i).isRight() && !statuses.get(i + 1).isRight()) {
|
||||
statuses.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addItems(List<Either<Placeholder, Status>> newStatuses) {
|
||||
if (ListUtils.isEmpty(newStatuses)) {
|
||||
return;
|
||||
}
|
||||
Status last = null;
|
||||
Either<Placeholder, Status> last = null;
|
||||
for (int i = statuses.size() - 1; i >= 0; i--) {
|
||||
if (statuses.get(i).isRight()) {
|
||||
last = statuses.get(i).getAsRight();
|
||||
last = statuses.get(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// I was about to replace findStatus with indexOf but it is incorrect to compare value
|
||||
// types by ID anyway and we should change equals() for Status, I think, so this makes sense
|
||||
if (last != null && !findStatus(newStatuses, last.getId())) {
|
||||
statuses.addAll(liftStatusList(newStatuses));
|
||||
if (fromId != null) {
|
||||
bottomId = fromId;
|
||||
}
|
||||
if (last != null && !newStatuses.contains(last)) {
|
||||
statuses.addAll(newStatuses);
|
||||
removeConsecutivePlaceholders();
|
||||
updateAdapter();
|
||||
}
|
||||
}
|
||||
|
||||
private void replacePlaceholderWithStatuses(List<Status> newStatuses, boolean fullFetch, int pos) {
|
||||
Status status = statuses.get(pos).getAsRightOrNull();
|
||||
if (status == null) {
|
||||
/**
|
||||
* For certain requests we don't want to see placeholders, they will be removed some other way
|
||||
*/
|
||||
private void clearPlaceholdersForResponse(List<Either<Placeholder, Status>> statuses) {
|
||||
CollectionsKt.removeAll(statuses, s -> !s.isRight());
|
||||
}
|
||||
|
||||
private void replacePlaceholderWithStatuses(List<Either<Placeholder, Status>> newStatuses,
|
||||
boolean fullFetch, int pos) {
|
||||
Either<Placeholder, Status> placeholder = statuses.get(pos);
|
||||
if (!placeholder.isRight()) {
|
||||
statuses.remove(pos);
|
||||
}
|
||||
|
||||
|
|
@ -981,29 +1073,20 @@ public class TimelineFragment extends SFragment implements
|
|||
return;
|
||||
}
|
||||
|
||||
List<Either<Placeholder, Status>> liftedNew = liftStatusList(newStatuses);
|
||||
|
||||
if (fullFetch) {
|
||||
liftedNew.add(Either.left(newPlaceholder()));
|
||||
newStatuses.add(placeholder);
|
||||
}
|
||||
|
||||
statuses.addAll(pos, liftedNew);
|
||||
statuses.addAll(pos, newStatuses);
|
||||
removeConsecutivePlaceholders();
|
||||
|
||||
updateAdapter();
|
||||
|
||||
}
|
||||
|
||||
private static boolean findStatus(List<Status> statuses, String id) {
|
||||
for (Status status : statuses) {
|
||||
if (status.getId().equals(id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private int findStatusOrReblogPositionById(@NonNull String statusId) {
|
||||
for (int i = 0; i < statuses.size(); i++) {
|
||||
Status status = statuses.get(i).getAsRightOrNull();
|
||||
Status status = statuses.get(i).asRightOrNull();
|
||||
if (status != null
|
||||
&& (statusId.equals(status.getId())
|
||||
|| (status.getReblog() != null
|
||||
|
|
@ -1015,7 +1098,7 @@ public class TimelineFragment extends SFragment implements
|
|||
}
|
||||
|
||||
private final Function<Status, Either<Placeholder, Status>> statusLifter =
|
||||
Either::right;
|
||||
Either.Right::new;
|
||||
|
||||
private @Nullable
|
||||
Pair<StatusViewData.Concrete, Integer>
|
||||
|
|
@ -1028,7 +1111,7 @@ public class TimelineFragment extends SFragment implements
|
|||
if ((someOldViewData instanceof StatusViewData.Placeholder) ||
|
||||
!((StatusViewData.Concrete) someOldViewData).getId().equals(status.getId())) {
|
||||
// try to find the status we need to update
|
||||
int foundPos = statuses.indexOf(Either.<Placeholder, Status>right(status));
|
||||
int foundPos = statuses.indexOf(new Either.Right<>(status));
|
||||
if (foundPos < 0) return null; // okay, it's hopeless, give up
|
||||
statusToUpdate = ((StatusViewData.Concrete)
|
||||
statuses.getPairedItem(foundPos));
|
||||
|
|
@ -1043,14 +1126,14 @@ public class TimelineFragment extends SFragment implements
|
|||
private void handleReblogEvent(@NonNull ReblogEvent reblogEvent) {
|
||||
int pos = findStatusOrReblogPositionById(reblogEvent.getStatusId());
|
||||
if (pos < 0) return;
|
||||
Status status = statuses.get(pos).getAsRight();
|
||||
Status status = statuses.get(pos).asRight();
|
||||
setRebloggedForStatus(pos, status, reblogEvent.getReblog());
|
||||
}
|
||||
|
||||
private void handleFavEvent(@NonNull FavoriteEvent favEvent) {
|
||||
int pos = findStatusOrReblogPositionById(favEvent.getStatusId());
|
||||
if (pos < 0) return;
|
||||
Status status = statuses.get(pos).getAsRight();
|
||||
Status status = statuses.get(pos).asRight();
|
||||
setFavouriteForStatus(pos, status, favEvent.getFavourite());
|
||||
}
|
||||
|
||||
|
|
@ -1079,12 +1162,6 @@ public class TimelineFragment extends SFragment implements
|
|||
return CollectionUtil.map(list, statusLifter);
|
||||
}
|
||||
|
||||
private Placeholder newPlaceholder() {
|
||||
Placeholder placeholder = Placeholder.getInstance(maxPlaceholderId);
|
||||
maxPlaceholderId--;
|
||||
return placeholder;
|
||||
}
|
||||
|
||||
private void updateAdapter() {
|
||||
differ.submitList(statuses.getPairedCopy());
|
||||
}
|
||||
|
|
@ -1144,8 +1221,12 @@ public class TimelineFragment extends SFragment implements
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(StatusViewData oldItem, StatusViewData newItem) {
|
||||
public boolean areContentsTheSame(StatusViewData oldItem, @NonNull StatusViewData newItem) {
|
||||
return oldItem.deepEquals(newItem);
|
||||
}
|
||||
};
|
||||
|
||||
private String idPlus(String id, int delta) {
|
||||
return new BigInteger(id).add(BigInteger.valueOf(delta)).toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue