2017-01-23 06:19:30 +01:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* This file is a part of Tusky.
|
2017-01-23 06:19:30 +01:00
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-01-23 06:19:30 +01:00
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-04-10 02:12:31 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
2017-01-23 06:19:30 +01:00
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2017-05-05 00:55:34 +02:00
|
|
|
package com.keylesspalace.tusky.fragment;
|
2017-01-23 06:19:30 +01:00
|
|
|
|
|
|
|
import android.content.Context;
|
2017-06-26 11:15:47 +02:00
|
|
|
import android.content.SharedPreferences;
|
2017-01-23 06:19:30 +01:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
2017-06-26 11:15:47 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2017-10-24 23:33:05 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2017-01-23 06:19:30 +01:00
|
|
|
import android.support.annotation.Nullable;
|
2017-02-27 01:14:50 +01:00
|
|
|
import android.support.design.widget.Snackbar;
|
2017-03-12 18:46:45 +01:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2017-06-06 23:15:29 +02:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2017-04-15 12:28:22 +02:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2017-01-23 06:19:30 +01:00
|
|
|
import android.support.v7.widget.DividerItemDecoration;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2017-05-23 21:34:31 +02:00
|
|
|
import android.util.Log;
|
2017-01-23 06:19:30 +01:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2017-07-12 21:54:52 +02:00
|
|
|
import com.keylesspalace.tusky.BuildConfig;
|
|
|
|
import com.keylesspalace.tusky.R;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.adapter.ThreadAdapter;
|
2017-10-27 13:20:17 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Card;
|
2017-03-09 00:27:37 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
|
|
|
import com.keylesspalace.tusky.entity.StatusContext;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.interfaces.StatusActionListener;
|
2017-06-06 23:15:29 +02:00
|
|
|
import com.keylesspalace.tusky.receiver.TimelineReceiver;
|
2017-07-12 21:54:52 +02:00
|
|
|
import com.keylesspalace.tusky.util.PairedList;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
2017-07-12 21:54:52 +02:00
|
|
|
import com.keylesspalace.tusky.util.ViewDataUtils;
|
2017-05-29 12:14:09 +02:00
|
|
|
import com.keylesspalace.tusky.view.ConversationLineItemDecoration;
|
2017-07-12 21:54:52 +02:00
|
|
|
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2017-03-09 00:27:37 +01:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2017-05-03 22:28:46 +02:00
|
|
|
import retrofit2.Response;
|
2017-03-09 00:27:37 +01:00
|
|
|
|
2017-04-15 12:28:22 +02:00
|
|
|
public class ViewThreadFragment extends SFragment implements
|
2017-06-06 23:15:29 +02:00
|
|
|
SwipeRefreshLayout.OnRefreshListener, StatusActionListener {
|
2017-03-15 21:38:19 +01:00
|
|
|
private static final String TAG = "ViewThreadFragment";
|
|
|
|
|
2017-04-15 12:28:22 +02:00
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
2017-01-23 06:19:30 +01:00
|
|
|
private RecyclerView recyclerView;
|
|
|
|
private ThreadAdapter adapter;
|
2017-03-03 01:25:35 +01:00
|
|
|
private String thisThreadsStatusId;
|
2017-06-06 23:15:29 +02:00
|
|
|
private TimelineReceiver timelineReceiver;
|
2017-10-27 13:20:17 +02:00
|
|
|
private Card card;
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2017-10-18 00:20:26 +02:00
|
|
|
private int statusIndex = 0;
|
2017-07-12 21:54:52 +02:00
|
|
|
|
2017-11-06 16:19:15 +01:00
|
|
|
private final PairedList<Status, StatusViewData.Concrete> statuses =
|
2017-07-12 21:54:52 +02:00
|
|
|
new PairedList<>(ViewDataUtils.statusMapper());
|
|
|
|
|
2017-01-23 06:19:30 +01:00
|
|
|
public static ViewThreadFragment newInstance(String id) {
|
|
|
|
Bundle arguments = new Bundle();
|
|
|
|
ViewThreadFragment fragment = new ViewThreadFragment();
|
|
|
|
arguments.putString("id", id);
|
|
|
|
fragment.setArguments(arguments);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
2017-11-06 16:19:15 +01:00
|
|
|
@Nullable Bundle savedInstanceState) {
|
2017-01-23 06:19:30 +01:00
|
|
|
View rootView = inflater.inflate(R.layout.fragment_view_thread, container, false);
|
|
|
|
|
|
|
|
Context context = getContext();
|
2017-10-18 00:20:26 +02:00
|
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swipe_refresh_layout);
|
2017-04-15 12:28:22 +02:00
|
|
|
swipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
|
2017-10-18 00:20:26 +02:00
|
|
|
recyclerView = rootView.findViewById(R.id.recycler_view);
|
2017-01-23 06:19:30 +01:00
|
|
|
recyclerView.setHasFixedSize(true);
|
|
|
|
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
|
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
DividerItemDecoration divider = new DividerItemDecoration(
|
|
|
|
context, layoutManager.getOrientation());
|
2017-03-03 01:25:35 +01:00
|
|
|
Drawable drawable = ThemeUtils.getDrawable(context, R.attr.status_divider_drawable,
|
|
|
|
R.drawable.status_divider_dark);
|
2017-01-23 06:19:30 +01:00
|
|
|
divider.setDrawable(drawable);
|
|
|
|
recyclerView.addItemDecoration(divider);
|
2017-05-03 22:28:46 +02:00
|
|
|
recyclerView.addItemDecoration(new ConversationLineItemDecoration(context,
|
|
|
|
ContextCompat.getDrawable(context, R.drawable.conversation_divider_dark)));
|
2017-01-23 06:19:30 +01:00
|
|
|
adapter = new ThreadAdapter(this);
|
2017-06-26 11:15:47 +02:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
|
|
|
|
getActivity());
|
|
|
|
boolean mediaPreviewEnabled = preferences.getBoolean("mediaPreviewEnabled", true);
|
|
|
|
adapter.setMediaPreviewEnabled(mediaPreviewEnabled);
|
2017-01-23 06:19:30 +01:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-07-21 03:17:36 +02:00
|
|
|
statuses.clear();
|
2017-05-03 22:28:46 +02:00
|
|
|
thisThreadsStatusId = null;
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2017-07-12 21:54:52 +02:00
|
|
|
timelineReceiver = new TimelineReceiver(this, this);
|
2017-06-06 23:15:29 +02:00
|
|
|
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
|
|
|
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(null));
|
|
|
|
|
2017-01-23 06:19:30 +01:00
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
2017-06-06 23:15:29 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
LocalBroadcastManager.getInstance(getContext())
|
|
|
|
.unregisterReceiver(timelineReceiver);
|
|
|
|
super.onDestroyView();
|
|
|
|
}
|
|
|
|
|
2017-05-03 22:28:46 +02:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
thisThreadsStatusId = getArguments().getString("id");
|
|
|
|
onRefresh();
|
|
|
|
}
|
|
|
|
|
2017-06-30 08:31:58 +02:00
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
|
|
|
sendStatusRequest(thisThreadsStatusId);
|
|
|
|
sendThreadRequest(thisThreadsStatusId);
|
2017-10-27 13:20:17 +02:00
|
|
|
sendCardRequest(thisThreadsStatusId);
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReply(int position) {
|
2017-07-12 21:54:52 +02:00
|
|
|
super.reply(statuses.get(position));
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-12 21:54:52 +02:00
|
|
|
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
|
2017-10-24 23:33:05 +02:00
|
|
|
public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
|
2017-07-12 21:54:52 +02:00
|
|
|
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
|
2017-10-24 23:33:05 +02:00
|
|
|
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
|
2017-07-12 21:54:52 +02:00
|
|
|
Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id);
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-12 21:54:52 +02:00
|
|
|
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
|
2017-10-24 23:33:05 +02:00
|
|
|
public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
|
2017-07-12 21:54:52 +02:00
|
|
|
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
|
2017-10-24 23:33:05 +02:00
|
|
|
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
|
2017-07-12 21:54:52 +02:00
|
|
|
Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id);
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMore(View view, int position) {
|
2017-07-14 03:31:31 +02:00
|
|
|
super.more(statuses.get(position), view, position);
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-14 07:26:58 +02:00
|
|
|
public void onViewMedia(String[] urls, int urlIndex, Status.MediaAttachment.Type type,
|
|
|
|
View view) {
|
|
|
|
super.viewMedia(urls, urlIndex, type, view);
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewThread(int position) {
|
2017-07-12 21:54:52 +02:00
|
|
|
Status status = statuses.get(position);
|
2017-06-30 08:31:58 +02:00
|
|
|
if (thisThreadsStatusId.equals(status.id)) {
|
|
|
|
// If already viewing this thread, don't reopen it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.viewThread(status);
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:10:56 +02:00
|
|
|
@Override
|
|
|
|
public void onOpenReblog(int position) {
|
|
|
|
// there should be no reblogs in the thread but let's implement it to be sure
|
2017-07-12 21:54:52 +02:00
|
|
|
super.openReblog(statuses.get(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onExpandedChange(boolean expanded, int position) {
|
2017-11-06 16:19:15 +01:00
|
|
|
StatusViewData.Concrete newViewData =
|
|
|
|
new StatusViewData.Builder(statuses.getPairedItem(position))
|
|
|
|
.setIsExpanded(expanded)
|
|
|
|
.createStatusViewData();
|
2017-07-12 21:54:52 +02:00
|
|
|
statuses.setPairedItem(position, newViewData);
|
|
|
|
adapter.setItem(position, newViewData, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onContentHiddenChange(boolean isShowing, int position) {
|
2017-11-06 16:19:15 +01:00
|
|
|
StatusViewData.Concrete newViewData =
|
|
|
|
new StatusViewData.Builder(statuses.getPairedItem(position))
|
|
|
|
.setIsShowingSensitiveContent(isShowing)
|
|
|
|
.createStatusViewData();
|
2017-07-12 21:54:52 +02:00
|
|
|
statuses.setPairedItem(position, newViewData);
|
|
|
|
adapter.setItem(position, newViewData, false);
|
2017-06-28 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:17:31 +01:00
|
|
|
@Override
|
|
|
|
public void onLoadMore(int pos) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-30 08:31:58 +02:00
|
|
|
@Override
|
|
|
|
public void onViewTag(String tag) {
|
|
|
|
super.viewTag(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewAccount(String id) {
|
|
|
|
super.viewAccount(id);
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:54:52 +02:00
|
|
|
@Override
|
|
|
|
public void removeItem(int position) {
|
2017-11-06 16:19:15 +01:00
|
|
|
if (position == statusIndex) {
|
2017-10-24 23:33:05 +02:00
|
|
|
//the status got removed, close the activity
|
|
|
|
getActivity().finish();
|
|
|
|
}
|
2017-07-12 21:54:52 +02:00
|
|
|
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);
|
2017-11-06 16:19:15 +01:00
|
|
|
if (statusIndex == -1) {
|
2017-10-24 23:33:05 +02:00
|
|
|
//the status got removed, close the activity
|
|
|
|
getActivity().finish();
|
|
|
|
return;
|
|
|
|
}
|
2017-08-03 06:29:31 +02:00
|
|
|
adapter.setDetailedStatusPosition(statusIndex);
|
2017-07-12 21:54:52 +02:00
|
|
|
adapter.setStatuses(statuses.getPairedCopy());
|
|
|
|
}
|
|
|
|
|
2017-05-03 22:28:46 +02:00
|
|
|
private void sendStatusRequest(final String id) {
|
|
|
|
Call<Status> call = mastodonApi.status(id);
|
2017-03-15 01:34:27 +01:00
|
|
|
call.enqueue(new Callback<Status>() {
|
2017-03-09 00:27:37 +01:00
|
|
|
@Override
|
2017-10-24 23:33:05 +02:00
|
|
|
public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
|
2017-03-14 01:49:12 +01:00
|
|
|
if (response.isSuccessful()) {
|
2017-07-12 21:54:52 +02:00
|
|
|
int position = setStatus(response.body());
|
2017-03-14 01:49:12 +01:00
|
|
|
recyclerView.scrollToPosition(position);
|
|
|
|
} else {
|
|
|
|
onThreadRequestFailure(id);
|
|
|
|
}
|
2017-03-09 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-10-24 23:33:05 +02:00
|
|
|
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
|
2017-03-09 00:27:37 +01:00
|
|
|
onThreadRequestFailure(id);
|
|
|
|
}
|
|
|
|
});
|
2017-03-15 01:34:27 +01:00
|
|
|
callList.add(call);
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 01:14:50 +01:00
|
|
|
private void sendThreadRequest(final String id) {
|
2017-05-03 22:28:46 +02:00
|
|
|
Call<StatusContext> call = mastodonApi.statusContext(id);
|
2017-03-15 01:34:27 +01:00
|
|
|
call.enqueue(new Callback<StatusContext>() {
|
2017-03-09 00:27:37 +01:00
|
|
|
@Override
|
2017-10-24 23:33:05 +02:00
|
|
|
public void onResponse(@NonNull Call<StatusContext> call, @NonNull Response<StatusContext> response) {
|
2017-03-14 01:49:12 +01:00
|
|
|
if (response.isSuccessful()) {
|
2017-04-15 12:28:22 +02:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-03-14 01:49:12 +01:00
|
|
|
StatusContext context = response.body();
|
2017-07-12 21:54:52 +02:00
|
|
|
setContext(context.ancestors, context.descendants);
|
2017-03-14 01:49:12 +01:00
|
|
|
} else {
|
|
|
|
onThreadRequestFailure(id);
|
|
|
|
}
|
2017-03-09 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-10-24 23:33:05 +02:00
|
|
|
public void onFailure(@NonNull Call<StatusContext> call, @NonNull Throwable t) {
|
2017-03-09 00:27:37 +01:00
|
|
|
onThreadRequestFailure(id);
|
|
|
|
}
|
|
|
|
});
|
2017-03-15 01:34:27 +01:00
|
|
|
callList.add(call);
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-27 13:20:17 +02:00
|
|
|
private void sendCardRequest(final String id) {
|
|
|
|
Call<Card> call = mastodonApi.statusCard(id);
|
|
|
|
call.enqueue(new Callback<Card>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<Card> call, @NonNull Response<Card> response) {
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
showCard(response.body());
|
|
|
|
} else {
|
|
|
|
onThreadRequestFailure(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Card> call, @NonNull Throwable t) {
|
|
|
|
onThreadRequestFailure(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
callList.add(call);
|
|
|
|
}
|
|
|
|
|
2017-02-27 01:14:50 +01:00
|
|
|
private void onThreadRequestFailure(final String id) {
|
2017-03-15 20:27:49 +01:00
|
|
|
View view = getView();
|
2017-04-15 12:28:22 +02:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-03-15 20:27:49 +01:00
|
|
|
if (view != null) {
|
2017-04-06 09:09:49 +02:00
|
|
|
Snackbar.make(view, R.string.error_generic, Snackbar.LENGTH_LONG)
|
2017-03-15 20:27:49 +01:00
|
|
|
.setAction(R.string.action_retry, new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
sendThreadRequest(id);
|
|
|
|
sendStatusRequest(id);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
2017-03-15 21:38:19 +01:00
|
|
|
} else {
|
|
|
|
Log.e(TAG, "Couldn't display thread fetch error message");
|
2017-03-15 20:27:49 +01:00
|
|
|
}
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|
2017-07-12 21:54:52 +02:00
|
|
|
|
2017-10-18 00:20:26 +02:00
|
|
|
private int setStatus(Status status) {
|
2017-07-12 21:54:52 +02:00
|
|
|
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);
|
2017-08-03 06:29:31 +02:00
|
|
|
adapter.setDetailedStatusPosition(i);
|
2017-11-06 16:19:15 +01:00
|
|
|
StatusViewData.Concrete viewData = statuses.getPairedItem(i);
|
|
|
|
if (viewData.getCard() == null && card != null) {
|
2017-10-27 13:20:17 +02:00
|
|
|
viewData = new StatusViewData.Builder(viewData)
|
|
|
|
.setCard(card)
|
|
|
|
.createStatusViewData();
|
|
|
|
}
|
|
|
|
adapter.addItem(i, viewData);
|
2017-07-12 21:54:52 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:20:26 +02:00
|
|
|
private void setContext(List<Status> ancestors, List<Status> descendants) {
|
2017-07-12 21:54:52 +02:00
|
|
|
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();
|
2017-08-03 06:29:31 +02:00
|
|
|
adapter.setDetailedStatusPosition(statusIndex);
|
2017-07-12 21:54:52 +02:00
|
|
|
statuses.addAll(0, ancestors);
|
2017-11-06 16:19:15 +01:00
|
|
|
List<StatusViewData.Concrete> ancestorsViewDatas = statuses.getPairedCopy().subList(0, statusIndex);
|
2017-07-12 21:54:52 +02:00
|
|
|
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);
|
2017-11-06 16:19:15 +01:00
|
|
|
StatusViewData.Concrete viewData = statuses.getPairedItem(statusIndex);
|
|
|
|
if (viewData.getCard() == null && card != null) {
|
2017-10-27 13:20:17 +02:00
|
|
|
viewData = new StatusViewData.Builder(viewData)
|
|
|
|
.setCard(card)
|
|
|
|
.createStatusViewData();
|
|
|
|
}
|
|
|
|
adapter.addItem(statusIndex, viewData);
|
2017-07-12 21:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert newly fetched descendants
|
|
|
|
statuses.addAll(descendants);
|
2017-11-06 16:19:15 +01:00
|
|
|
List<StatusViewData.Concrete> descendantsViewData;
|
|
|
|
descendantsViewData = statuses.getPairedCopy()
|
|
|
|
.subList(statuses.size() - descendants.size(), statuses.size());
|
2017-07-12 21:54:52 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-27 13:20:17 +02:00
|
|
|
private void showCard(Card card) {
|
|
|
|
this.card = card;
|
2017-11-06 16:19:15 +01:00
|
|
|
if (statuses.size() != 0) {
|
|
|
|
StatusViewData.Concrete newViewData =
|
|
|
|
new StatusViewData.Builder(statuses.getPairedItem(statusIndex))
|
|
|
|
.setCard(card)
|
|
|
|
.createStatusViewData();
|
|
|
|
|
|
|
|
statuses.setPairedItem(statusIndex, newViewData);
|
|
|
|
adapter.setItem(statusIndex, newViewData, true);
|
2017-10-27 13:20:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:54:52 +02:00
|
|
|
public void clear() {
|
|
|
|
statuses.clear();
|
|
|
|
adapter.clear();
|
|
|
|
}
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|