Resolves merge conflicts and makes NotificationAdapter no longer implement AdapterItemRemover
This commit is contained in:
commit
fc1a24be11
15 changed files with 1188 additions and 387 deletions
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
package com.keylesspalace.tusky.fragment;
|
||||
|
||||
import android.arch.core.util.Function;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
|
@ -39,12 +40,19 @@ import com.keylesspalace.tusky.adapter.NotificationsAdapter;
|
|||
import com.keylesspalace.tusky.R;
|
||||
import com.keylesspalace.tusky.entity.Notification;
|
||||
import com.keylesspalace.tusky.entity.Status;
|
||||
import com.keylesspalace.tusky.interfaces.AdapterItemRemover;
|
||||
import com.keylesspalace.tusky.interfaces.StatusActionListener;
|
||||
import com.keylesspalace.tusky.receiver.TimelineReceiver;
|
||||
import com.keylesspalace.tusky.util.HttpHeaderLink;
|
||||
import com.keylesspalace.tusky.util.PairedList;
|
||||
import com.keylesspalace.tusky.util.ThemeUtils;
|
||||
import com.keylesspalace.tusky.util.ViewDataUtils;
|
||||
import com.keylesspalace.tusky.view.EndlessOnScrollListener;
|
||||
import com.keylesspalace.tusky.viewdata.NotificationViewData;
|
||||
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import retrofit2.Call;
|
||||
|
|
@ -59,10 +67,11 @@ public class NotificationsFragment extends SFragment implements
|
|||
|
||||
private enum FetchEnd {
|
||||
TOP,
|
||||
BOTTOM,
|
||||
BOTTOM
|
||||
}
|
||||
|
||||
private SwipeRefreshLayout swipeRefreshLayout;
|
||||
|
||||
private LinearLayoutManager layoutManager;
|
||||
private RecyclerView recyclerView;
|
||||
private EndlessOnScrollListener scrollListener;
|
||||
|
|
@ -74,6 +83,16 @@ public class NotificationsFragment extends SFragment implements
|
|||
private int topFetches;
|
||||
private boolean bottomLoading;
|
||||
private int bottomFetches;
|
||||
private String bottomId;
|
||||
private String topId;
|
||||
|
||||
private final PairedList<Notification, NotificationViewData> notifications
|
||||
= new PairedList<>(new Function<Notification, NotificationViewData>() {
|
||||
@Override
|
||||
public NotificationViewData apply(Notification input) {
|
||||
return ViewDataUtils.notificationToViewData(input);
|
||||
}
|
||||
});
|
||||
|
||||
public static NotificationsFragment newInstance() {
|
||||
NotificationsFragment fragment = new NotificationsFragment();
|
||||
|
|
@ -111,7 +130,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
adapter.setMediaPreviewEnabled(mediaPreviewEnabled);
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
timelineReceiver = new TimelineReceiver(adapter);
|
||||
timelineReceiver = new TimelineReceiver(this);
|
||||
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
||||
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(null));
|
||||
|
||||
|
|
@ -128,10 +147,12 @@ public class NotificationsFragment extends SFragment implements
|
|||
TabLayout layout = (TabLayout) activity.findViewById(R.id.tab_layout);
|
||||
onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
|
||||
@Override
|
||||
public void onTabSelected(TabLayout.Tab tab) {}
|
||||
public void onTabSelected(TabLayout.Tab tab) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabUnselected(TabLayout.Tab tab) {}
|
||||
public void onTabUnselected(TabLayout.Tab tab) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabReselected(TabLayout.Tab tab) {
|
||||
|
|
@ -167,7 +188,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
||||
NotificationsFragment.this.onLoadMore(view);
|
||||
NotificationsFragment.this.onLoadMore();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -187,31 +208,31 @@ public class NotificationsFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
sendFetchNotificationsRequest(null, adapter.getTopId(), FetchEnd.TOP);
|
||||
sendFetchNotificationsRequest(null, topId, FetchEnd.TOP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReply(int position) {
|
||||
Notification notification = adapter.getItem(position);
|
||||
Notification notification = notifications.get(position);
|
||||
super.reply(notification.status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReblog(boolean reblog, int position) {
|
||||
Notification notification = adapter.getItem(position);
|
||||
Notification notification = notifications.get(position);
|
||||
super.reblog(notification.status, reblog, adapter, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFavourite(boolean favourite, int position) {
|
||||
Notification notification = adapter.getItem(position);
|
||||
Notification notification = notifications.get(position);
|
||||
super.favourite(notification.status, favourite, adapter, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMore(View view, int position) {
|
||||
Notification notification = adapter.getItem(position);
|
||||
super.more(notification.status, view, adapter, position);
|
||||
Notification notification = notifications.get(position);
|
||||
super.more(notification.status, view, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -221,16 +242,42 @@ public class NotificationsFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onViewThread(int position) {
|
||||
Notification notification = adapter.getItem(position);
|
||||
Notification notification = notifications.get(position);
|
||||
super.viewThread(notification.status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpenReblog(int position) {
|
||||
Notification notification = adapter.getItem(position);
|
||||
Notification notification = notifications.get(position);
|
||||
if (notification != null) onViewAccount(notification.account.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExpandedChange(boolean expanded, int position) {
|
||||
NotificationViewData old = notifications.getPairedItem(position);
|
||||
StatusViewData statusViewData =
|
||||
new StatusViewData.Builder(old.getStatusViewData())
|
||||
.setIsExpanded(expanded)
|
||||
.createStatusViewData();
|
||||
NotificationViewData notificationViewData = new NotificationViewData(old.getType(),
|
||||
old.getId(), old.getAccount(), statusViewData);
|
||||
notifications.setPairedItem(position, notificationViewData);
|
||||
adapter.updateItemWithNotify(position, notificationViewData, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContentHiddenChange(boolean isShowing, int position) {
|
||||
NotificationViewData old = notifications.getPairedItem(position);
|
||||
StatusViewData statusViewData =
|
||||
new StatusViewData.Builder(old.getStatusViewData())
|
||||
.setIsShowingSensitiveContent(isShowing)
|
||||
.createStatusViewData();
|
||||
NotificationViewData notificationViewData = new NotificationViewData(old.getType(),
|
||||
old.getId(), old.getAccount(), statusViewData);
|
||||
notifications.setPairedItem(position, notificationViewData);
|
||||
adapter.updateItemWithNotify(position, notificationViewData, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewTag(String tag) {
|
||||
super.viewTag(tag);
|
||||
|
|
@ -257,9 +304,27 @@ public class NotificationsFragment extends SFragment implements
|
|||
}
|
||||
}
|
||||
|
||||
private void onLoadMore(RecyclerView view) {
|
||||
NotificationsAdapter adapter = (NotificationsAdapter) view.getAdapter();
|
||||
sendFetchNotificationsRequest(adapter.getBottomId(), null, FetchEnd.BOTTOM);
|
||||
@Override
|
||||
public void removeItem(int position) {
|
||||
notifications.remove(position);
|
||||
adapter.update(notifications.getPairedCopy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAllByAccountId(String accountId) {
|
||||
// using iterator to safely remove items while iterating
|
||||
Iterator<Notification> iterator = notifications.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Notification notification = iterator.next();
|
||||
if (notification.account.id.equals(accountId)) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
adapter.update(notifications.getPairedCopy());
|
||||
}
|
||||
|
||||
private void onLoadMore() {
|
||||
sendFetchNotificationsRequest(bottomId, null, FetchEnd.BOTTOM);
|
||||
}
|
||||
|
||||
private void jumpToTop() {
|
||||
|
|
@ -268,7 +333,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
}
|
||||
|
||||
private void sendFetchNotificationsRequest(String fromId, String uptoId,
|
||||
final FetchEnd fetchEnd) {
|
||||
final FetchEnd fetchEnd) {
|
||||
/* If there is a fetch already ongoing, record however many fetches are requested and
|
||||
* fulfill them after it's complete. */
|
||||
if (fetchEnd == FetchEnd.TOP && topLoading) {
|
||||
|
|
@ -297,7 +362,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
call.enqueue(new Callback<List<Notification>>() {
|
||||
@Override
|
||||
public void onResponse(Call<List<Notification>> call,
|
||||
Response<List<Notification>> response) {
|
||||
Response<List<Notification>> response) {
|
||||
if (response.isSuccessful()) {
|
||||
String linkHeader = response.headers().get("Link");
|
||||
onFetchNotificationsSuccess(response.body(), linkHeader, fetchEnd);
|
||||
|
|
@ -315,7 +380,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
}
|
||||
|
||||
private void onFetchNotificationsSuccess(List<Notification> notifications, String linkHeader,
|
||||
FetchEnd fetchEnd) {
|
||||
FetchEnd fetchEnd) {
|
||||
List<HttpHeaderLink> links = HttpHeaderLink.parse(linkHeader);
|
||||
switch (fetchEnd) {
|
||||
case TOP: {
|
||||
|
|
@ -324,7 +389,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
if (previous != null) {
|
||||
uptoId = previous.uri.getQueryParameter("since_id");
|
||||
}
|
||||
adapter.update(notifications, null, uptoId);
|
||||
update(notifications, null, uptoId);
|
||||
break;
|
||||
}
|
||||
case BOTTOM: {
|
||||
|
|
@ -334,7 +399,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
fromId = next.uri.getQueryParameter("max_id");
|
||||
}
|
||||
if (adapter.getItemCount() > 1) {
|
||||
adapter.addItems(notifications, fromId);
|
||||
addItems(notifications, fromId);
|
||||
} 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
|
||||
|
|
@ -344,7 +409,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
if (previous != null) {
|
||||
uptoId = previous.uri.getQueryParameter("since_id");
|
||||
}
|
||||
adapter.update(notifications, fromId, uptoId);
|
||||
update(notifications, fromId, uptoId);
|
||||
}
|
||||
/* Set last update id for pull notifications so that we don't get notified
|
||||
* about things we already loaded here */
|
||||
|
|
@ -363,6 +428,60 @@ public class NotificationsFragment extends SFragment implements
|
|||
swipeRefreshLayout.setRefreshing(false);
|
||||
}
|
||||
|
||||
public void update(@Nullable List<Notification> newNotifications, @Nullable String fromId,
|
||||
@Nullable String uptoId) {
|
||||
if (newNotifications == null || newNotifications.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (fromId != null) {
|
||||
bottomId = fromId;
|
||||
}
|
||||
if (uptoId != null) {
|
||||
topId = uptoId;
|
||||
}
|
||||
if (notifications.isEmpty()) {
|
||||
// This construction removes duplicates.
|
||||
notifications.addAll(new HashSet<>(newNotifications));
|
||||
} else {
|
||||
int index = notifications.indexOf(newNotifications.get(newNotifications.size() - 1));
|
||||
for (int i = 0; i < index; i++) {
|
||||
notifications.remove(0);
|
||||
}
|
||||
int newIndex = newNotifications.indexOf(notifications.get(0));
|
||||
if (newIndex == -1) {
|
||||
notifications.addAll(0, newNotifications);
|
||||
} else {
|
||||
List<Notification> sublist = newNotifications.subList(0, newIndex);
|
||||
notifications.addAll(0, sublist);
|
||||
}
|
||||
}
|
||||
adapter.update(notifications.getPairedCopy());
|
||||
}
|
||||
|
||||
public void addItems(List<Notification> newNotifications, @Nullable String fromId) {
|
||||
if (fromId != null) {
|
||||
bottomId = fromId;
|
||||
}
|
||||
int end = notifications.size();
|
||||
Notification last = notifications.get(end - 1);
|
||||
if (last != null && !findNotification(newNotifications, last.id)) {
|
||||
notifications.addAll(newNotifications);
|
||||
List<NotificationViewData> newViewDatas = notifications.getPairedCopy()
|
||||
.subList(notifications.size() - newNotifications.size(),
|
||||
notifications.size() - 1);
|
||||
adapter.addItems(newViewDatas);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean findNotification(List<Notification> notifications, String id) {
|
||||
for (Notification notification : notifications) {
|
||||
if (notification.id.equals(id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void onFetchNotificationsFailure(Exception exception, FetchEnd fetchEnd) {
|
||||
swipeRefreshLayout.setRefreshing(false);
|
||||
Log.e(TAG, "Fetch failure: " + exception.getMessage());
|
||||
|
|
@ -375,7 +494,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
bottomLoading = false;
|
||||
if (bottomFetches > 0) {
|
||||
bottomFetches--;
|
||||
onLoadMore(recyclerView);
|
||||
onLoadMore();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -392,6 +511,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
|
||||
private void fullyRefresh() {
|
||||
adapter.clear();
|
||||
notifications.clear();
|
||||
sendFetchNotificationsRequest(null, null, FetchEnd.TOP);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import android.support.v4.content.LocalBroadcastManager;
|
|||
import android.support.v7.widget.PopupMenu;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.text.Spanned;
|
||||
import android.util.Log;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
|
||||
|
|
@ -56,7 +57,7 @@ import retrofit2.Response;
|
|||
* adapters. I feel like the profile pages and thread viewer, which I haven't made yet, will also
|
||||
* overlap functionality. So, I'm momentarily leaving it and hopefully working on those will clear
|
||||
* up what needs to be where. */
|
||||
public abstract class SFragment extends BaseFragment {
|
||||
public abstract class SFragment extends BaseFragment implements AdapterItemRemover {
|
||||
protected static final int COMPOSE_RESULT = 1;
|
||||
|
||||
protected String loggedInAccountId;
|
||||
|
|
@ -107,9 +108,7 @@ public abstract class SFragment extends BaseFragment {
|
|||
|
||||
protected void reblog(final Status status, final boolean reblog,
|
||||
final RecyclerView.Adapter adapter, final int position) {
|
||||
String id = status.getActionableId();
|
||||
|
||||
Callback<Status> cb = new Callback<Status>() {
|
||||
reblogWithCallback(status, reblog, new Callback<Status>() {
|
||||
@Override
|
||||
public void onResponse(Call<Status> call, retrofit2.Response<Status> response) {
|
||||
if (response.isSuccessful()) {
|
||||
|
|
@ -124,8 +123,16 @@ public abstract class SFragment extends BaseFragment {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Status> call, Throwable t) {}
|
||||
};
|
||||
public void onFailure(Call<Status> call, Throwable t) {
|
||||
Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id);
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void reblogWithCallback(final Status status, final boolean reblog,
|
||||
Callback<Status> callback) {
|
||||
String id = status.getActionableId();
|
||||
|
||||
Call<Status> call;
|
||||
if (reblog) {
|
||||
|
|
@ -133,15 +140,12 @@ public abstract class SFragment extends BaseFragment {
|
|||
} else {
|
||||
call = mastodonApi.unreblogStatus(id);
|
||||
}
|
||||
call.enqueue(cb);
|
||||
callList.add(call);
|
||||
call.enqueue(callback);
|
||||
}
|
||||
|
||||
protected void favourite(final Status status, final boolean favourite,
|
||||
final RecyclerView.Adapter adapter, final int position) {
|
||||
String id = status.getActionableId();
|
||||
|
||||
Callback<Status> cb = new Callback<Status>() {
|
||||
favouriteWithCallback(status, favourite, new Callback<Status>() {
|
||||
@Override
|
||||
public void onResponse(Call<Status> call, retrofit2.Response<Status> response) {
|
||||
if (response.isSuccessful()) {
|
||||
|
|
@ -156,8 +160,16 @@ public abstract class SFragment extends BaseFragment {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Status> call, Throwable t) {}
|
||||
};
|
||||
public void onFailure(Call<Status> call, Throwable t) {
|
||||
Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id);
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void favouriteWithCallback(final Status status, final boolean favourite,
|
||||
final Callback<Status> callback) {
|
||||
String id = status.getActionableId();
|
||||
|
||||
Call<Status> call;
|
||||
if (favourite) {
|
||||
|
|
@ -165,7 +177,7 @@ public abstract class SFragment extends BaseFragment {
|
|||
} else {
|
||||
call = mastodonApi.unfavouriteStatus(id);
|
||||
}
|
||||
call.enqueue(cb);
|
||||
call.enqueue(callback);
|
||||
callList.add(call);
|
||||
}
|
||||
|
||||
|
|
@ -218,8 +230,7 @@ public abstract class SFragment extends BaseFragment {
|
|||
callList.add(call);
|
||||
}
|
||||
|
||||
protected void more(final Status status, View view, final AdapterItemRemover adapter,
|
||||
final int position) {
|
||||
protected void more(final Status status, View view, final int position) {
|
||||
final String id = status.getActionableId();
|
||||
final String accountId = status.getActionableStatus().account.id;
|
||||
final String accountUsename = status.getActionableStatus().account.username;
|
||||
|
|
@ -272,7 +283,7 @@ public abstract class SFragment extends BaseFragment {
|
|||
}
|
||||
case R.id.status_delete: {
|
||||
delete(id);
|
||||
adapter.removeItem(position);
|
||||
removeItem(position);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import android.view.LayoutInflater;
|
|||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.keylesspalace.tusky.BuildConfig;
|
||||
import com.keylesspalace.tusky.MainActivity;
|
||||
import com.keylesspalace.tusky.R;
|
||||
import com.keylesspalace.tusky.adapter.FooterViewHolder;
|
||||
|
|
@ -42,11 +43,16 @@ import com.keylesspalace.tusky.interfaces.StatusActionListener;
|
|||
import com.keylesspalace.tusky.network.MastodonApi;
|
||||
import com.keylesspalace.tusky.receiver.TimelineReceiver;
|
||||
import com.keylesspalace.tusky.util.HttpHeaderLink;
|
||||
import com.keylesspalace.tusky.util.PairedList;
|
||||
import com.keylesspalace.tusky.util.ThemeUtils;
|
||||
import com.keylesspalace.tusky.util.ViewDataUtils;
|
||||
import com.keylesspalace.tusky.view.EndlessOnScrollListener;
|
||||
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
|
|
@ -57,6 +63,8 @@ public class TimelineFragment extends SFragment implements
|
|||
StatusActionListener,
|
||||
SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
private static final String TAG = "Timeline"; // logging tag
|
||||
private static final String KIND_ARG = "kind";
|
||||
private static final String HASHTAG_OR_ID_ARG = "hashtag_or_id";
|
||||
|
||||
public enum Kind {
|
||||
HOME,
|
||||
|
|
@ -88,11 +96,17 @@ public class TimelineFragment extends SFragment implements
|
|||
private int topFetches;
|
||||
private boolean bottomLoading;
|
||||
private int bottomFetches;
|
||||
@Nullable
|
||||
private String bottomId;
|
||||
@Nullable
|
||||
private String upToId;
|
||||
private PairedList<Status, StatusViewData> statuses =
|
||||
new PairedList<>(ViewDataUtils.statusMapper());
|
||||
|
||||
public static TimelineFragment newInstance(Kind kind) {
|
||||
TimelineFragment fragment = new TimelineFragment();
|
||||
Bundle arguments = new Bundle();
|
||||
arguments.putString("kind", kind.name());
|
||||
arguments.putString(KIND_ARG, kind.name());
|
||||
fragment.setArguments(arguments);
|
||||
return fragment;
|
||||
}
|
||||
|
|
@ -100,19 +114,19 @@ public class TimelineFragment extends SFragment implements
|
|||
public static TimelineFragment newInstance(Kind kind, String hashtagOrId) {
|
||||
TimelineFragment fragment = new TimelineFragment();
|
||||
Bundle arguments = new Bundle();
|
||||
arguments.putString("kind", kind.name());
|
||||
arguments.putString("hashtag_or_id", hashtagOrId);
|
||||
arguments.putString(KIND_ARG, kind.name());
|
||||
arguments.putString(HASHTAG_OR_ID_ARG, hashtagOrId);
|
||||
fragment.setArguments(arguments);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
Bundle savedInstanceState) {
|
||||
Bundle arguments = getArguments();
|
||||
kind = Kind.valueOf(arguments.getString("kind"));
|
||||
kind = Kind.valueOf(arguments.getString(KIND_ARG));
|
||||
if (kind == Kind.TAG || kind == Kind.USER) {
|
||||
hashtagOrId = arguments.getString("hashtag_or_id");
|
||||
hashtagOrId = arguments.getString(HASHTAG_OR_ID_ARG);
|
||||
}
|
||||
|
||||
final View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
|
||||
|
|
@ -140,7 +154,7 @@ public class TimelineFragment extends SFragment implements
|
|||
adapter.setMediaPreviewEnabled(mediaPreviewEnabled);
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
timelineReceiver = new TimelineReceiver(adapter, this);
|
||||
timelineReceiver = new TimelineReceiver(this, this);
|
||||
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
||||
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(kind));
|
||||
|
||||
|
|
@ -155,10 +169,12 @@ public class TimelineFragment extends SFragment implements
|
|||
TabLayout layout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
|
||||
onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
|
||||
@Override
|
||||
public void onTabSelected(TabLayout.Tab tab) {}
|
||||
public void onTabSelected(TabLayout.Tab tab) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabUnselected(TabLayout.Tab tab) {}
|
||||
public void onTabUnselected(TabLayout.Tab tab) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabReselected(TabLayout.Tab tab) {
|
||||
|
|
@ -196,7 +212,7 @@ public class TimelineFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
||||
TimelineFragment.this.onLoadMore(view);
|
||||
TimelineFragment.this.onLoadMore();
|
||||
}
|
||||
};
|
||||
} else {
|
||||
|
|
@ -204,7 +220,7 @@ public class TimelineFragment extends SFragment implements
|
|||
scrollListener = new EndlessOnScrollListener(layoutManager) {
|
||||
@Override
|
||||
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
||||
TimelineFragment.this.onLoadMore(view);
|
||||
TimelineFragment.this.onLoadMore();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -223,32 +239,98 @@ public class TimelineFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
sendFetchTimelineRequest(null, adapter.getTopId(), FetchEnd.TOP);
|
||||
sendFetchTimelineRequest(null, upToId, FetchEnd.TOP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReply(int position) {
|
||||
super.reply(adapter.getItem(position));
|
||||
super.reply(statuses.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReblog(final boolean reblog, final int position) {
|
||||
super.reblog(adapter.getItem(position), reblog, adapter, position);
|
||||
final Status status = statuses.get(position);
|
||||
super.reblogWithCallback(status, reblog, new Callback<Status>() {
|
||||
@Override
|
||||
public void onResponse(Call<Status> call, retrofit2.Response<Status> response) {
|
||||
if (response.isSuccessful()) {
|
||||
status.reblogged = reblog;
|
||||
|
||||
if (status.reblog != null) {
|
||||
status.reblog.reblogged = reblog;
|
||||
}
|
||||
|
||||
StatusViewData newViewData =
|
||||
new StatusViewData.Builder(statuses.getPairedItem(position))
|
||||
.setReblogged(reblog)
|
||||
.createStatusViewData();
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
adapter.changeItem(position, newViewData, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Status> call, Throwable t) {
|
||||
Log.d(TAG, "Failed to reblog status " + status.id);
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFavourite(final boolean favourite, final int position) {
|
||||
super.favourite(adapter.getItem(position), favourite, adapter, position);
|
||||
final Status status = statuses.get(position);
|
||||
|
||||
super.favouriteWithCallback(status, favourite, new Callback<Status>() {
|
||||
@Override
|
||||
public void onResponse(Call<Status> call, retrofit2.Response<Status> response) {
|
||||
if (response.isSuccessful()) {
|
||||
status.favourited = favourite;
|
||||
|
||||
if (status.reblog != null) {
|
||||
status.reblog.favourited = favourite;
|
||||
}
|
||||
StatusViewData newViewData = new StatusViewData
|
||||
.Builder(statuses.getPairedItem(position))
|
||||
.setFavourited(favourite)
|
||||
.createStatusViewData();
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
adapter.changeItem(position, newViewData, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Status> call, Throwable t) {
|
||||
Log.d(TAG, "Failed to favourite status " + status.id);
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMore(View view, final int position) {
|
||||
super.more(adapter.getItem(position), view, adapter, position);
|
||||
super.more(statuses.get(position), view, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpenReblog(int position) {
|
||||
super.openReblog(adapter.getItem(position));
|
||||
super.openReblog(statuses.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExpandedChange(boolean expanded, int position) {
|
||||
StatusViewData newViewData = new StatusViewData.Builder(statuses.getPairedItem(position))
|
||||
.setIsExpanded(expanded).createStatusViewData();
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
adapter.changeItem(position, newViewData, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContentHiddenChange(boolean isShowing, int position) {
|
||||
StatusViewData newViewData = new StatusViewData.Builder(statuses.getPairedItem(position))
|
||||
.setIsShowingSensitiveContent(isShowing).createStatusViewData();
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
adapter.changeItem(position, newViewData, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -258,7 +340,7 @@ public class TimelineFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onViewThread(int position) {
|
||||
super.viewThread(adapter.getItem(position));
|
||||
super.viewThread(statuses.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -314,9 +396,27 @@ public class TimelineFragment extends SFragment implements
|
|||
}
|
||||
}
|
||||
|
||||
private void onLoadMore(RecyclerView view) {
|
||||
TimelineAdapter adapter = (TimelineAdapter) view.getAdapter();
|
||||
sendFetchTimelineRequest(adapter.getBottomId(), null, FetchEnd.BOTTOM);
|
||||
@Override
|
||||
public void removeItem(int position) {
|
||||
statuses.remove(position);
|
||||
adapter.update(statuses.getPairedCopy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAllByAccountId(String accountId) {
|
||||
// using iterator to safely remove items while iterating
|
||||
Iterator<Status> iterator = statuses.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Status status = iterator.next();
|
||||
if (status.account.id.equals(accountId)) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
adapter.update(statuses.getPairedCopy());
|
||||
}
|
||||
|
||||
private void onLoadMore() {
|
||||
sendFetchTimelineRequest(bottomId, null, FetchEnd.BOTTOM);
|
||||
}
|
||||
|
||||
private void fullyRefresh() {
|
||||
|
|
@ -338,21 +438,27 @@ public class TimelineFragment extends SFragment implements
|
|||
}
|
||||
|
||||
private Call<List<Status>> getFetchCallByTimelineType(Kind kind, String tagOrId, String fromId,
|
||||
String uptoId) {
|
||||
String uptoId) {
|
||||
MastodonApi api = mastodonApi;
|
||||
switch (kind) {
|
||||
default:
|
||||
case HOME: return api.homeTimeline(fromId, uptoId, null);
|
||||
case PUBLIC_FEDERATED: return api.publicTimeline(null, fromId, uptoId, null);
|
||||
case PUBLIC_LOCAL: return api.publicTimeline(true, fromId, uptoId, null);
|
||||
case TAG: return api.hashtagTimeline(tagOrId, null, fromId, uptoId, null);
|
||||
case USER: return api.accountStatuses(tagOrId, fromId, uptoId, null);
|
||||
case FAVOURITES: return api.favourites(fromId, uptoId, null);
|
||||
case HOME:
|
||||
return api.homeTimeline(fromId, uptoId, null);
|
||||
case PUBLIC_FEDERATED:
|
||||
return api.publicTimeline(null, fromId, uptoId, null);
|
||||
case PUBLIC_LOCAL:
|
||||
return api.publicTimeline(true, fromId, uptoId, null);
|
||||
case TAG:
|
||||
return api.hashtagTimeline(tagOrId, null, fromId, uptoId, null);
|
||||
case USER:
|
||||
return api.accountStatuses(tagOrId, fromId, uptoId, null);
|
||||
case FAVOURITES:
|
||||
return api.favourites(fromId, uptoId, null);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendFetchTimelineRequest(@Nullable String fromId, @Nullable String uptoId,
|
||||
final FetchEnd fetchEnd) {
|
||||
final FetchEnd fetchEnd) {
|
||||
/* If there is a fetch already ongoing, record however many fetches are requested and
|
||||
* fulfill them after it's complete. */
|
||||
if (fetchEnd == FetchEnd.TOP && topLoading) {
|
||||
|
|
@ -399,7 +505,7 @@ public class TimelineFragment extends SFragment implements
|
|||
}
|
||||
|
||||
public void onFetchTimelineSuccess(List<Status> statuses, String linkHeader,
|
||||
FetchEnd fetchEnd) {
|
||||
FetchEnd fetchEnd) {
|
||||
filterStatuses(statuses);
|
||||
List<HttpHeaderLink> links = HttpHeaderLink.parse(linkHeader);
|
||||
switch (fetchEnd) {
|
||||
|
|
@ -409,7 +515,7 @@ public class TimelineFragment extends SFragment implements
|
|||
if (previous != null) {
|
||||
uptoId = previous.uri.getQueryParameter("since_id");
|
||||
}
|
||||
adapter.update(statuses, null, uptoId);
|
||||
updateStatuses(statuses, null, uptoId);
|
||||
break;
|
||||
}
|
||||
case BOTTOM: {
|
||||
|
|
@ -419,7 +525,7 @@ public class TimelineFragment extends SFragment implements
|
|||
fromId = next.uri.getQueryParameter("max_id");
|
||||
}
|
||||
if (adapter.getItemCount() > 1) {
|
||||
adapter.addItems(statuses, fromId);
|
||||
addItems(statuses, fromId);
|
||||
} 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
|
||||
|
|
@ -429,7 +535,7 @@ public class TimelineFragment extends SFragment implements
|
|||
if (previous != null) {
|
||||
uptoId = previous.uri.getQueryParameter("since_id");
|
||||
}
|
||||
adapter.update(statuses, fromId, uptoId);
|
||||
updateStatuses(statuses, fromId, uptoId);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -455,7 +561,7 @@ public class TimelineFragment extends SFragment implements
|
|||
bottomLoading = false;
|
||||
if (bottomFetches > 0) {
|
||||
bottomFetches--;
|
||||
onLoadMore(recyclerView);
|
||||
onLoadMore();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -480,4 +586,62 @@ public class TimelineFragment extends SFragment implements
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateStatuses(List<Status> newStatuses, @Nullable String fromId,
|
||||
@Nullable String toId) {
|
||||
if (newStatuses == null || newStatuses.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (fromId != null) {
|
||||
bottomId = fromId;
|
||||
}
|
||||
if (toId != null) {
|
||||
upToId = toId;
|
||||
}
|
||||
if (statuses.isEmpty()) {
|
||||
// This construction removes duplicates.
|
||||
statuses.addAll(new HashSet<>(newStatuses));
|
||||
} else {
|
||||
Status lastOfNew = newStatuses.get(newStatuses.size() - 1);
|
||||
int index = statuses.indexOf(lastOfNew);
|
||||
for (int i = 0; i < index; i++) {
|
||||
statuses.remove(0);
|
||||
}
|
||||
int newIndex = newStatuses.indexOf(statuses.get(0));
|
||||
if (newIndex == -1) {
|
||||
statuses.addAll(0, newStatuses);
|
||||
} else {
|
||||
statuses.addAll(0, newStatuses.subList(0, newIndex));
|
||||
}
|
||||
}
|
||||
adapter.update(statuses.getPairedCopy());
|
||||
}
|
||||
|
||||
private void addItems(List<Status> newStatuses, @Nullable String fromId) {
|
||||
int end = statuses.size();
|
||||
Status last = statuses.get(end - 1);
|
||||
if (last != null && !findStatus(newStatuses, last.id)) {
|
||||
statuses.addAll(newStatuses);
|
||||
List<StatusViewData> newViewDatas = statuses.getPairedCopy()
|
||||
.subList(statuses.size() - newStatuses.size(), statuses.size());
|
||||
if (BuildConfig.DEBUG && newStatuses.size() != newViewDatas.size()) {
|
||||
String error = String.format(Locale.getDefault(),
|
||||
"Incorrectly got statusViewData sublist." +
|
||||
" newStatuses.size == %d newViewDatas.size == %d, statuses.size == %d",
|
||||
newStatuses.size(), newViewDatas.size(), statuses.size());
|
||||
throw new AssertionError(error);
|
||||
}
|
||||
if (fromId != null) bottomId = fromId;
|
||||
adapter.addItems(newViewDatas);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean findStatus(List<Status> statuses, String id) {
|
||||
for (Status status : statuses) {
|
||||
if (status.id.equals(id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,15 +33,22 @@ import android.view.LayoutInflater;
|
|||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
|
||||
import com.keylesspalace.tusky.BuildConfig;
|
||||
import com.keylesspalace.tusky.R;
|
||||
import com.keylesspalace.tusky.adapter.ThreadAdapter;
|
||||
import com.keylesspalace.tusky.entity.Status;
|
||||
import com.keylesspalace.tusky.entity.StatusContext;
|
||||
import com.keylesspalace.tusky.R;
|
||||
import com.keylesspalace.tusky.interfaces.StatusActionListener;
|
||||
import com.keylesspalace.tusky.receiver.TimelineReceiver;
|
||||
import com.keylesspalace.tusky.util.PairedList;
|
||||
import com.keylesspalace.tusky.util.ThemeUtils;
|
||||
import com.keylesspalace.tusky.util.ViewDataUtils;
|
||||
import com.keylesspalace.tusky.view.ConversationLineItemDecoration;
|
||||
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
|
|
@ -57,6 +64,11 @@ public class ViewThreadFragment extends SFragment implements
|
|||
private String thisThreadsStatusId;
|
||||
private TimelineReceiver timelineReceiver;
|
||||
|
||||
int statusIndex = 0;
|
||||
|
||||
private final PairedList<Status, StatusViewData> statuses =
|
||||
new PairedList<>(ViewDataUtils.statusMapper());
|
||||
|
||||
public static ViewThreadFragment newInstance(String id) {
|
||||
Bundle arguments = new Bundle();
|
||||
ViewThreadFragment fragment = new ViewThreadFragment();
|
||||
|
|
@ -96,7 +108,7 @@ public class ViewThreadFragment extends SFragment implements
|
|||
|
||||
thisThreadsStatusId = null;
|
||||
|
||||
timelineReceiver = new TimelineReceiver(adapter, this);
|
||||
timelineReceiver = new TimelineReceiver(this, this);
|
||||
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
||||
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(null));
|
||||
|
||||
|
|
@ -125,22 +137,65 @@ public class ViewThreadFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onReply(int position) {
|
||||
super.reply(adapter.getItem(position));
|
||||
super.reply(statuses.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReblog(boolean reblog, int position) {
|
||||
super.reblog(adapter.getItem(position), reblog, adapter, position);
|
||||
public void onReblog(final boolean reblog, final int position) {
|
||||
final Status status = statuses.get(position);
|
||||
super.reblogWithCallback(statuses.get(position), reblog, new Callback<Status>() {
|
||||
@Override
|
||||
public void onResponse(Call<Status> call, Response<Status> response) {
|
||||
if (response.isSuccessful()) {
|
||||
status.reblogged = reblog;
|
||||
|
||||
if (status.reblog != null) {
|
||||
status.reblog.reblogged = reblog;
|
||||
}
|
||||
// create new viewData as side effect
|
||||
statuses.set(position, status);
|
||||
|
||||
adapter.setItem(position, statuses.getPairedItem(position), true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Status> call, Throwable t) {
|
||||
Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id);
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFavourite(boolean favourite, int position) {
|
||||
super.favourite(adapter.getItem(position), favourite, adapter, position);
|
||||
public void onFavourite(final boolean favourite, final int position) {
|
||||
final Status status = statuses.get(position);
|
||||
super.favouriteWithCallback(statuses.get(position), favourite, new Callback<Status>() {
|
||||
@Override
|
||||
public void onResponse(Call<Status> call, Response<Status> response) {
|
||||
if (response.isSuccessful()) {
|
||||
status.favourited = favourite;
|
||||
|
||||
if (status.reblog != null) {
|
||||
status.reblog.favourited = favourite;
|
||||
}
|
||||
// create new viewData as side effect
|
||||
statuses.set(position, status);
|
||||
adapter.setItem(position, statuses.getPairedItem(position), true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Status> call, Throwable t) {
|
||||
Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id);
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMore(View view, int position) {
|
||||
super.more(adapter.getItem(position), view, adapter, position);
|
||||
super.more(statuses.get(position), view, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -150,7 +205,7 @@ public class ViewThreadFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onViewThread(int position) {
|
||||
Status status = adapter.getItem(position);
|
||||
Status status = statuses.get(position);
|
||||
if (thisThreadsStatusId.equals(status.id)) {
|
||||
// If already viewing this thread, don't reopen it.
|
||||
return;
|
||||
|
|
@ -161,7 +216,25 @@ public class ViewThreadFragment extends SFragment implements
|
|||
@Override
|
||||
public void onOpenReblog(int position) {
|
||||
// there should be no reblogs in the thread but let's implement it to be sure
|
||||
super.openReblog(adapter.getItem(position));
|
||||
super.openReblog(statuses.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExpandedChange(boolean expanded, int position) {
|
||||
StatusViewData newViewData = new StatusViewData.Builder(statuses.getPairedItem(position))
|
||||
.setIsExpanded(expanded)
|
||||
.createStatusViewData();
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
adapter.setItem(position, newViewData, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContentHiddenChange(boolean isShowing, int position) {
|
||||
StatusViewData newViewData = new StatusViewData.Builder(statuses.getPairedItem(position))
|
||||
.setIsShowingSensitiveContent(isShowing)
|
||||
.createStatusViewData();
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
adapter.setItem(position, newViewData, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -174,13 +247,37 @@ public class ViewThreadFragment extends SFragment implements
|
|||
super.viewAccount(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeItem(int position) {
|
||||
statuses.remove(position);
|
||||
adapter.setStatuses(statuses.getPairedCopy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAllByAccountId(String accountId) {
|
||||
Status status = null;
|
||||
if (!statuses.isEmpty()) {
|
||||
status = statuses.get(statusIndex);
|
||||
}
|
||||
// using iterator to safely remove items while iterating
|
||||
Iterator<Status> iterator = statuses.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Status s = iterator.next();
|
||||
if (s.account.id.equals(accountId)) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
statusIndex = statuses.indexOf(status);
|
||||
adapter.setStatuses(statuses.getPairedCopy());
|
||||
}
|
||||
|
||||
private void sendStatusRequest(final String id) {
|
||||
Call<Status> call = mastodonApi.status(id);
|
||||
call.enqueue(new Callback<Status>() {
|
||||
@Override
|
||||
public void onResponse(Call<Status> call, Response<Status> response) {
|
||||
if (response.isSuccessful()) {
|
||||
int position = adapter.setStatus(response.body());
|
||||
int position = setStatus(response.body());
|
||||
recyclerView.scrollToPosition(position);
|
||||
} else {
|
||||
onThreadRequestFailure(id);
|
||||
|
|
@ -203,7 +300,7 @@ public class ViewThreadFragment extends SFragment implements
|
|||
if (response.isSuccessful()) {
|
||||
swipeRefreshLayout.setRefreshing(false);
|
||||
StatusContext context = response.body();
|
||||
adapter.setContext(context.ancestors, context.descendants);
|
||||
setContext(context.ancestors, context.descendants);
|
||||
} else {
|
||||
onThreadRequestFailure(id);
|
||||
}
|
||||
|
|
@ -234,4 +331,72 @@ public class ViewThreadFragment extends SFragment implements
|
|||
Log.e(TAG, "Couldn't display thread fetch error message");
|
||||
}
|
||||
}
|
||||
|
||||
public int setStatus(Status status) {
|
||||
if (statuses.size() > 0
|
||||
&& statusIndex < statuses.size()
|
||||
&& statuses.get(statusIndex).equals(status)) {
|
||||
// Do not add this status on refresh, it's already in there.
|
||||
statuses.set(statusIndex, status);
|
||||
return statusIndex;
|
||||
}
|
||||
int i = statusIndex;
|
||||
statuses.add(i, status);
|
||||
adapter.addItem(i, statuses.getPairedItem(i));
|
||||
return i;
|
||||
}
|
||||
|
||||
public void setContext(List<Status> ancestors, List<Status> descendants) {
|
||||
Status mainStatus = null;
|
||||
|
||||
// In case of refresh, remove old ancestors and descendants first. We'll remove all blindly,
|
||||
// as we have no guarantee on their order to be the same as before
|
||||
int oldSize = statuses.size();
|
||||
if (oldSize > 1) {
|
||||
mainStatus = statuses.get(statusIndex);
|
||||
statuses.clear();
|
||||
adapter.clearItems();
|
||||
}
|
||||
|
||||
// Insert newly fetched ancestors
|
||||
statusIndex = ancestors.size();
|
||||
statuses.addAll(0, ancestors);
|
||||
List<StatusViewData> ancestorsViewDatas = statuses.getPairedCopy().subList(0, statusIndex);
|
||||
if (BuildConfig.DEBUG && ancestors.size() != ancestorsViewDatas.size()) {
|
||||
String error = String.format(Locale.getDefault(),
|
||||
"Incorrectly got statusViewData sublist." +
|
||||
" ancestors.size == %d ancestorsViewDatas.size == %d," +
|
||||
" statuses.size == %d",
|
||||
ancestors.size(), ancestorsViewDatas.size(), statuses.size());
|
||||
throw new AssertionError(error);
|
||||
}
|
||||
adapter.addAll(0, ancestorsViewDatas);
|
||||
|
||||
if (mainStatus != null) {
|
||||
// In case we needed to delete everything (which is way easier than deleting
|
||||
// everything except one), re-insert the remaining status here.
|
||||
statuses.add(statusIndex, mainStatus);
|
||||
adapter.addItem(statusIndex, statuses.getPairedItem(statusIndex));
|
||||
}
|
||||
|
||||
// Insert newly fetched descendants
|
||||
statuses.addAll(descendants);
|
||||
List<StatusViewData> descendantsViewData;
|
||||
descendantsViewData = statuses.getPairedCopy()
|
||||
.subList(statuses.size() - descendants.size(), statuses.size());
|
||||
if (BuildConfig.DEBUG && descendants.size() != descendantsViewData.size()) {
|
||||
String error = String.format(Locale.getDefault(),
|
||||
"Incorrectly got statusViewData sublist." +
|
||||
" descendants.size == %d descendantsViewData.size == %d," +
|
||||
" statuses.size == %d",
|
||||
descendants.size(), descendantsViewData.size(), statuses.size());
|
||||
throw new AssertionError(error);
|
||||
}
|
||||
adapter.addAll(descendantsViewData);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
statuses.clear();
|
||||
adapter.clear();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue