2017-01-23 16:19:30 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* This file is a part of Tusky.
|
2017-01-23 16:19:30 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10: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 16:19:30 +11:00
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-04-10 10:12:31 +10:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
2017-01-23 16:19:30 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10: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 16:19:30 +11:00
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.Nullable;
|
2017-02-27 11:14:50 +11:00
|
|
|
import android.support.design.widget.Snackbar;
|
2017-03-13 04:46:45 +11:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2017-04-15 20:28:22 +10:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2017-01-23 16:19:30 +11:00
|
|
|
import android.support.v7.widget.DividerItemDecoration;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2017-03-09 10:27:37 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
|
|
|
import com.keylesspalace.tusky.entity.StatusContext;
|
2017-01-23 16:19:30 +11:00
|
|
|
|
2017-03-09 10:27:37 +11:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2017-05-04 06:28:46 +10:00
|
|
|
import retrofit2.Response;
|
2017-03-09 10:27:37 +11:00
|
|
|
|
2017-04-15 20:28:22 +10:00
|
|
|
public class ViewThreadFragment extends SFragment implements
|
2017-04-25 21:30:57 +10:00
|
|
|
SwipeRefreshLayout.OnRefreshListener, StatusActionListener, StatusRemoveListener {
|
2017-03-16 07:38:19 +11:00
|
|
|
private static final String TAG = "ViewThreadFragment";
|
|
|
|
|
2017-04-15 20:28:22 +10:00
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
2017-01-23 16:19:30 +11:00
|
|
|
private RecyclerView recyclerView;
|
|
|
|
private ThreadAdapter adapter;
|
2017-05-04 06:28:46 +10:00
|
|
|
private MastodonAPI mastodonApi;
|
2017-03-03 11:25:35 +11:00
|
|
|
private String thisThreadsStatusId;
|
2017-01-23 16:19:30 +11: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,
|
|
|
|
@Nullable Bundle savedInstanceState) {
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_view_thread, container, false);
|
|
|
|
|
|
|
|
Context context = getContext();
|
2017-04-15 20:28:22 +10:00
|
|
|
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
|
|
|
|
recyclerView.setHasFixedSize(true);
|
|
|
|
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
|
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
DividerItemDecoration divider = new DividerItemDecoration(
|
|
|
|
context, layoutManager.getOrientation());
|
2017-03-03 11:25:35 +11:00
|
|
|
Drawable drawable = ThemeUtils.getDrawable(context, R.attr.status_divider_drawable,
|
|
|
|
R.drawable.status_divider_dark);
|
2017-01-23 16:19:30 +11:00
|
|
|
divider.setDrawable(drawable);
|
|
|
|
recyclerView.addItemDecoration(divider);
|
2017-05-04 06:28:46 +10:00
|
|
|
recyclerView.addItemDecoration(new ConversationLineItemDecoration(context,
|
|
|
|
ContextCompat.getDrawable(context, R.drawable.conversation_divider_dark)));
|
2017-01-23 16:19:30 +11:00
|
|
|
adapter = new ThreadAdapter(this);
|
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-05-04 06:28:46 +10:00
|
|
|
mastodonApi = null;
|
|
|
|
thisThreadsStatusId = null;
|
2017-01-23 16:19:30 +11:00
|
|
|
|
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
2017-05-04 06:28:46 +10:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
2017-03-09 10:27:37 +11:00
|
|
|
|
2017-05-04 06:28:46 +10:00
|
|
|
/* BaseActivity's MastodonAPI object isn't guaranteed to be valid until after its onCreate
|
|
|
|
* is run, so all calls that need it can't be done until here. */
|
|
|
|
mastodonApi = ((BaseActivity) getActivity()).mastodonAPI;
|
|
|
|
|
|
|
|
thisThreadsStatusId = getArguments().getString("id");
|
|
|
|
onRefresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendStatusRequest(final String id) {
|
|
|
|
Call<Status> call = mastodonApi.status(id);
|
2017-03-15 11:34:27 +11:00
|
|
|
call.enqueue(new Callback<Status>() {
|
2017-03-09 10:27:37 +11:00
|
|
|
@Override
|
2017-05-04 06:28:46 +10:00
|
|
|
public void onResponse(Call<Status> call, Response<Status> response) {
|
2017-03-14 11:49:12 +11:00
|
|
|
if (response.isSuccessful()) {
|
2017-04-15 20:28:22 +10:00
|
|
|
int position = adapter.setStatus(response.body());
|
2017-03-14 11:49:12 +11:00
|
|
|
recyclerView.scrollToPosition(position);
|
|
|
|
} else {
|
|
|
|
onThreadRequestFailure(id);
|
|
|
|
}
|
2017-03-09 10:27:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<Status> call, Throwable t) {
|
|
|
|
onThreadRequestFailure(id);
|
|
|
|
}
|
|
|
|
});
|
2017-03-15 11:34:27 +11:00
|
|
|
callList.add(call);
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
|
2017-02-27 11:14:50 +11:00
|
|
|
private void sendThreadRequest(final String id) {
|
2017-05-04 06:28:46 +10:00
|
|
|
Call<StatusContext> call = mastodonApi.statusContext(id);
|
2017-03-15 11:34:27 +11:00
|
|
|
call.enqueue(new Callback<StatusContext>() {
|
2017-03-09 10:27:37 +11:00
|
|
|
@Override
|
2017-05-04 06:28:46 +10:00
|
|
|
public void onResponse(Call<StatusContext> call, Response<StatusContext> response) {
|
2017-03-14 11:49:12 +11:00
|
|
|
if (response.isSuccessful()) {
|
2017-04-15 20:28:22 +10:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-03-14 11:49:12 +11:00
|
|
|
StatusContext context = response.body();
|
|
|
|
|
2017-04-15 20:28:22 +10:00
|
|
|
adapter.setContext(context.ancestors, context.descendants);
|
2017-03-14 11:49:12 +11:00
|
|
|
} else {
|
|
|
|
onThreadRequestFailure(id);
|
|
|
|
}
|
2017-03-09 10:27:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<StatusContext> call, Throwable t) {
|
|
|
|
onThreadRequestFailure(id);
|
|
|
|
}
|
|
|
|
});
|
2017-03-15 11:34:27 +11:00
|
|
|
callList.add(call);
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
|
2017-02-27 11:14:50 +11:00
|
|
|
private void onThreadRequestFailure(final String id) {
|
2017-03-16 06:27:49 +11:00
|
|
|
View view = getView();
|
2017-04-15 20:28:22 +10:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-03-16 06:27:49 +11:00
|
|
|
if (view != null) {
|
2017-04-06 17:09:49 +10:00
|
|
|
Snackbar.make(view, R.string.error_generic, Snackbar.LENGTH_LONG)
|
2017-03-16 06:27:49 +11:00
|
|
|
.setAction(R.string.action_retry, new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
sendThreadRequest(id);
|
|
|
|
sendStatusRequest(id);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
2017-03-16 07:38:19 +11:00
|
|
|
} else {
|
|
|
|
Log.e(TAG, "Couldn't display thread fetch error message");
|
2017-03-16 06:27:49 +11:00
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
|
2017-04-25 21:30:57 +10:00
|
|
|
@Override
|
|
|
|
public void removePostsByUser(String accountId) {
|
|
|
|
adapter.removeAllByAccountId(accountId);
|
|
|
|
}
|
|
|
|
|
2017-04-15 20:28:22 +10:00
|
|
|
public void onRefresh() {
|
|
|
|
sendStatusRequest(thisThreadsStatusId);
|
|
|
|
sendThreadRequest(thisThreadsStatusId);
|
|
|
|
}
|
|
|
|
|
2017-04-16 03:44:29 +10:00
|
|
|
@Override
|
|
|
|
public void onSuccessfulStatus() {
|
|
|
|
onRefresh();
|
|
|
|
super.onSuccessfulStatus();
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
public void onReply(int position) {
|
|
|
|
super.reply(adapter.getItem(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onReblog(boolean reblog, int position) {
|
|
|
|
super.reblog(adapter.getItem(position), reblog, adapter, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onFavourite(boolean favourite, int position) {
|
|
|
|
super.favourite(adapter.getItem(position), favourite, adapter, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onMore(View view, int position) {
|
|
|
|
super.more(adapter.getItem(position), view, adapter, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onViewMedia(String url, Status.MediaAttachment.Type type) {
|
|
|
|
super.viewMedia(url, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onViewThread(int position) {
|
2017-03-03 11:25:35 +11:00
|
|
|
Status status = adapter.getItem(position);
|
2017-03-09 10:27:37 +11:00
|
|
|
if (thisThreadsStatusId.equals(status.id)) {
|
2017-03-03 11:25:35 +11:00
|
|
|
// If already viewing this thread, don't reopen it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.viewThread(status);
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
2017-01-27 11:34:32 +11:00
|
|
|
|
|
|
|
public void onViewTag(String tag) {
|
|
|
|
super.viewTag(tag);
|
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-03-03 11:25:35 +11:00
|
|
|
public void onViewAccount(String id) {
|
|
|
|
super.viewAccount(id);
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|