2017-01-20 19:09:10 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is part of Tusky.
|
|
|
|
*
|
|
|
|
* Tusky 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.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* 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-08 09:24:02 +11:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
2017-01-19 05:35:07 +11:00
|
|
|
import android.support.design.widget.TabLayout;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
|
|
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;
|
|
|
|
|
|
|
|
import com.android.volley.AuthFailureError;
|
|
|
|
import com.android.volley.Response;
|
|
|
|
import com.android.volley.VolleyError;
|
|
|
|
import com.android.volley.toolbox.JsonArrayRequest;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
public class TimelineFragment extends SFragment implements
|
2017-01-19 05:35:07 +11:00
|
|
|
SwipeRefreshLayout.OnRefreshListener, StatusActionListener, FooterActionListener {
|
2017-02-06 15:13:48 +11:00
|
|
|
private static final String TAG = "Timeline"; // logging tag
|
2017-02-08 08:47:05 +11:00
|
|
|
private static final int EXPECTED_STATUSES_FETCHED = 20;
|
2017-01-08 09:24:02 +11:00
|
|
|
|
|
|
|
public enum Kind {
|
|
|
|
HOME,
|
|
|
|
MENTIONS,
|
|
|
|
PUBLIC,
|
2017-01-27 11:34:32 +11:00
|
|
|
TAG,
|
2017-01-28 14:33:43 +11:00
|
|
|
USER,
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
2017-01-19 05:35:07 +11:00
|
|
|
private RecyclerView recyclerView;
|
2017-01-08 09:24:02 +11:00
|
|
|
private TimelineAdapter adapter;
|
|
|
|
private Kind kind;
|
2017-01-28 14:33:43 +11:00
|
|
|
private String hashtagOrId;
|
2017-01-19 05:35:07 +11:00
|
|
|
private LinearLayoutManager layoutManager;
|
|
|
|
private EndlessOnScrollListener scrollListener;
|
|
|
|
private TabLayout.OnTabSelectedListener onTabSelectedListener;
|
2017-01-08 09:24:02 +11:00
|
|
|
|
|
|
|
public static TimelineFragment newInstance(Kind kind) {
|
|
|
|
TimelineFragment fragment = new TimelineFragment();
|
|
|
|
Bundle arguments = new Bundle();
|
|
|
|
arguments.putString("kind", kind.name());
|
|
|
|
fragment.setArguments(arguments);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
public static TimelineFragment newInstance(Kind kind, String hashtagOrId) {
|
2017-01-27 11:34:32 +11:00
|
|
|
TimelineFragment fragment = new TimelineFragment();
|
|
|
|
Bundle arguments = new Bundle();
|
|
|
|
arguments.putString("kind", kind.name());
|
2017-01-28 14:33:43 +11:00
|
|
|
arguments.putString("hashtag_or_id", hashtagOrId);
|
2017-01-27 11:34:32 +11:00
|
|
|
fragment.setArguments(arguments);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
|
2017-01-27 11:34:32 +11:00
|
|
|
Bundle arguments = getArguments();
|
|
|
|
kind = Kind.valueOf(arguments.getString("kind"));
|
2017-01-28 14:33:43 +11:00
|
|
|
if (kind == Kind.TAG || kind == Kind.USER) {
|
|
|
|
hashtagOrId = arguments.getString("hashtag_or_id");
|
2017-01-27 11:34:32 +11:00
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
|
|
|
|
|
|
|
|
// Setup the SwipeRefreshLayout.
|
2017-01-23 16:19:30 +11:00
|
|
|
Context context = getContext();
|
2017-01-08 09:24:02 +11:00
|
|
|
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
// Setup the RecyclerView.
|
2017-01-19 05:35:07 +11:00
|
|
|
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
|
2017-01-08 09:24:02 +11:00
|
|
|
recyclerView.setHasFixedSize(true);
|
2017-01-19 05:35:07 +11:00
|
|
|
layoutManager = new LinearLayoutManager(context);
|
2017-01-08 09:24:02 +11:00
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
DividerItemDecoration divider = new DividerItemDecoration(
|
|
|
|
context, layoutManager.getOrientation());
|
|
|
|
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.status_divider);
|
|
|
|
divider.setDrawable(drawable);
|
|
|
|
recyclerView.addItemDecoration(divider);
|
2017-01-19 05:35:07 +11:00
|
|
|
scrollListener = new EndlessOnScrollListener(layoutManager) {
|
2017-01-08 09:24:02 +11:00
|
|
|
@Override
|
|
|
|
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
|
|
|
TimelineAdapter adapter = (TimelineAdapter) view.getAdapter();
|
2017-01-19 05:35:07 +11:00
|
|
|
Status status = adapter.getItem(adapter.getItemCount() - 2);
|
|
|
|
if (status != null) {
|
|
|
|
sendFetchTimelineRequest(status.getId());
|
|
|
|
} else {
|
|
|
|
sendFetchTimelineRequest();
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
recyclerView.addOnScrollListener(scrollListener);
|
2017-01-19 05:35:07 +11:00
|
|
|
adapter = new TimelineAdapter(this, this);
|
2017-01-08 09:24:02 +11:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
if (jumpToTopAllowed()) {
|
2017-01-27 11:34:32 +11:00
|
|
|
TabLayout layout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
|
|
|
|
onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
|
|
|
|
@Override
|
2017-01-31 15:51:02 +11:00
|
|
|
public void onTabSelected(TabLayout.Tab tab) {}
|
2017-01-19 05:35:07 +11:00
|
|
|
|
2017-01-27 11:34:32 +11:00
|
|
|
@Override
|
2017-01-31 15:51:02 +11:00
|
|
|
public void onTabUnselected(TabLayout.Tab tab) {}
|
2017-01-19 05:35:07 +11:00
|
|
|
|
2017-01-27 11:34:32 +11:00
|
|
|
@Override
|
|
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
|
|
jumpToTop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
layout.addOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
}
|
2017-01-19 05:35:07 +11:00
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
sendFetchTimelineRequest();
|
|
|
|
|
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:35:07 +11:00
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
2017-01-28 14:33:43 +11:00
|
|
|
if (jumpToTopAllowed()) {
|
2017-01-27 11:34:32 +11:00
|
|
|
TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
|
|
|
|
tabLayout.removeOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
}
|
2017-01-19 05:35:07 +11:00
|
|
|
super.onDestroyView();
|
|
|
|
}
|
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
private boolean jumpToTopAllowed() {
|
|
|
|
return kind != Kind.TAG;
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:35:07 +11:00
|
|
|
private void jumpToTop() {
|
2017-02-13 16:18:17 +11:00
|
|
|
layoutManager.scrollToPosition(0);
|
2017-01-19 05:35:07 +11:00
|
|
|
scrollListener.reset();
|
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
private void sendFetchTimelineRequest(final String fromId) {
|
|
|
|
String endpoint;
|
|
|
|
switch (kind) {
|
|
|
|
default:
|
|
|
|
case HOME: {
|
|
|
|
endpoint = getString(R.string.endpoint_timelines_home);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MENTIONS: {
|
|
|
|
endpoint = getString(R.string.endpoint_timelines_mentions);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PUBLIC: {
|
|
|
|
endpoint = getString(R.string.endpoint_timelines_public);
|
|
|
|
break;
|
|
|
|
}
|
2017-01-27 11:34:32 +11:00
|
|
|
case TAG: {
|
2017-01-28 14:33:43 +11:00
|
|
|
endpoint = String.format(getString(R.string.endpoint_timelines_tag), hashtagOrId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case USER: {
|
|
|
|
endpoint = String.format(getString(R.string.endpoint_statuses), hashtagOrId);
|
2017-01-27 11:34:32 +11:00
|
|
|
break;
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
String url = "https://" + domain + endpoint;
|
|
|
|
if (fromId != null) {
|
|
|
|
url += "?max_id=" + fromId;
|
|
|
|
}
|
|
|
|
JsonArrayRequest request = new JsonArrayRequest(url,
|
|
|
|
new Response.Listener<JSONArray>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONArray response) {
|
|
|
|
List<Status> statuses = null;
|
|
|
|
try {
|
|
|
|
statuses = Status.parse(response);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
onFetchTimelineFailure(e);
|
|
|
|
}
|
|
|
|
if (statuses != null) {
|
|
|
|
onFetchTimelineSuccess(statuses, fromId != null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
|
|
|
onFetchTimelineFailure(error);
|
|
|
|
}
|
|
|
|
}) {
|
|
|
|
@Override
|
|
|
|
public Map<String, String> getHeaders() throws AuthFailureError {
|
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
|
headers.put("Authorization", "Bearer " + accessToken);
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
VolleySingleton.getInstance(getContext()).addToRequestQueue(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendFetchTimelineRequest() {
|
|
|
|
sendFetchTimelineRequest(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onFetchTimelineSuccess(List<Status> statuses, boolean added) {
|
|
|
|
if (added) {
|
|
|
|
adapter.addItems(statuses);
|
|
|
|
} else {
|
|
|
|
adapter.update(statuses);
|
|
|
|
}
|
2017-02-08 08:47:05 +11:00
|
|
|
if (statuses.size() >= EXPECTED_STATUSES_FETCHED) {
|
|
|
|
setFetchTimelineState(FooterViewHolder.State.LOADING);
|
|
|
|
} else {
|
|
|
|
setFetchTimelineState(FooterViewHolder.State.END_OF_TIMELINE);
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onFetchTimelineFailure(Exception exception) {
|
2017-02-08 08:47:05 +11:00
|
|
|
setFetchTimelineState(FooterViewHolder.State.RETRY);
|
2017-01-08 09:24:02 +11:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-02-13 16:18:17 +11:00
|
|
|
Log.e(TAG, "Fetch Failure: " + exception.getMessage());
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
|
2017-02-08 08:47:05 +11:00
|
|
|
private void setFetchTimelineState(FooterViewHolder.State state) {
|
2017-01-19 05:35:07 +11:00
|
|
|
RecyclerView.ViewHolder viewHolder =
|
|
|
|
recyclerView.findViewHolderForAdapterPosition(adapter.getItemCount() - 1);
|
|
|
|
if (viewHolder != null) {
|
2017-01-23 16:19:30 +11:00
|
|
|
FooterViewHolder holder = (FooterViewHolder) viewHolder;
|
2017-02-08 08:47:05 +11:00
|
|
|
holder.setState(state);
|
2017-01-19 05:35:07 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
public void onRefresh() {
|
|
|
|
sendFetchTimelineRequest();
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
public void onLoadMore() {
|
|
|
|
Status status = adapter.getItem(adapter.getItemCount() - 2);
|
|
|
|
if (status != null) {
|
|
|
|
sendFetchTimelineRequest(status.getId());
|
|
|
|
} else {
|
|
|
|
sendFetchTimelineRequest();
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 15:59:21 +11:00
|
|
|
public void onReply(int position) {
|
2017-01-23 16:19:30 +11:00
|
|
|
super.reply(adapter.getItem(position));
|
2017-01-20 15:59:21 +11:00
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
public void onReblog(final boolean reblog, final int position) {
|
2017-01-23 16:19:30 +11:00
|
|
|
super.reblog(adapter.getItem(position), reblog, adapter, position);
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onFavourite(final boolean favourite, final int position) {
|
2017-01-23 16:19:30 +11:00
|
|
|
super.favourite(adapter.getItem(position), favourite, adapter, position);
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onMore(View view, final int position) {
|
2017-01-23 16:19:30 +11:00
|
|
|
super.more(adapter.getItem(position), view, adapter, position);
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
2017-01-10 17:14:27 +11:00
|
|
|
|
|
|
|
public void onViewMedia(String url, Status.MediaAttachment.Type type) {
|
2017-01-23 16:19:30 +11:00
|
|
|
super.viewMedia(url, type);
|
2017-01-10 17:14:27 +11:00
|
|
|
}
|
2017-01-19 05:35:07 +11:00
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
public void onViewThread(int position) {
|
|
|
|
super.viewThread(adapter.getItem(position));
|
2017-01-19 05:35:07 +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
|
|
|
|
|
|
|
public void onViewAccount(String id, String username) {
|
|
|
|
super.viewAccount(id, username);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onViewAccount(int position) {
|
|
|
|
Status status = adapter.getItem(position);
|
|
|
|
String id = status.getAccountId();
|
|
|
|
String username = status.getUsername();
|
|
|
|
super.viewAccount(id, username);
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|