2017-01-20 09:09:10 +01:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* This file is a part of Tusky.
|
2017-01-20 09:09:10 +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-20 09:09:10 +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-20 09:09:10 +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-20 09:09:10 +01:00
|
|
|
|
2017-05-05 00:55:34 +02:00
|
|
|
package com.keylesspalace.tusky.fragment;
|
2017-01-07 23:24:02 +01:00
|
|
|
|
|
|
|
import android.content.Context;
|
2017-04-07 09:40:59 +02:00
|
|
|
import android.content.SharedPreferences;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
2017-04-07 09:40:59 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2017-03-10 21:12:40 +01:00
|
|
|
import android.support.annotation.Nullable;
|
2017-04-07 09:40:59 +02:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2017-01-18 19:35:07 +01:00
|
|
|
import android.support.design.widget.TabLayout;
|
2017-05-11 11:11:15 +02:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
|
|
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-07 23:24:02 +01:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.MainActivity;
|
|
|
|
import com.keylesspalace.tusky.R;
|
2017-07-01 00:30:25 +02:00
|
|
|
import com.keylesspalace.tusky.adapter.FooterViewHolder;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.adapter.TimelineAdapter;
|
2017-03-09 00:27:37 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.interfaces.StatusActionListener;
|
2017-06-30 08:31:58 +02:00
|
|
|
import com.keylesspalace.tusky.network.MastodonApi;
|
2017-05-15 12:05:10 +02:00
|
|
|
import com.keylesspalace.tusky.receiver.TimelineReceiver;
|
2017-06-30 08:31:58 +02:00
|
|
|
import com.keylesspalace.tusky.util.HttpHeaderLink;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
2017-05-29 12:14:09 +02:00
|
|
|
import com.keylesspalace.tusky.view.EndlessOnScrollListener;
|
2017-01-07 23:24:02 +01:00
|
|
|
|
2017-04-15 20:23:07 +02:00
|
|
|
import java.util.Iterator;
|
2017-01-07 23:24:02 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-03-09 00:27:37 +01:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2017-05-10 04:36:05 +02:00
|
|
|
import retrofit2.Response;
|
2017-03-09 00:27:37 +01:00
|
|
|
|
2017-01-23 06:19:30 +01:00
|
|
|
public class TimelineFragment extends SFragment implements
|
2017-04-25 13:30:57 +02:00
|
|
|
SwipeRefreshLayout.OnRefreshListener,
|
|
|
|
StatusActionListener,
|
|
|
|
SharedPreferences.OnSharedPreferenceChangeListener {
|
2017-03-10 00:20:08 +01:00
|
|
|
private static final String TAG = "Timeline"; // logging tag
|
2017-01-07 23:24:02 +01:00
|
|
|
|
2017-05-05 00:55:34 +02:00
|
|
|
public enum Kind {
|
2017-01-07 23:24:02 +01:00
|
|
|
HOME,
|
2017-03-31 04:31:17 +02:00
|
|
|
PUBLIC_LOCAL,
|
|
|
|
PUBLIC_FEDERATED,
|
2017-01-27 01:34:32 +01:00
|
|
|
TAG,
|
2017-01-28 04:33:43 +01:00
|
|
|
USER,
|
2017-02-21 03:32:10 +01:00
|
|
|
FAVOURITES
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
|
|
|
|
2017-06-30 08:31:58 +02:00
|
|
|
private enum FetchEnd {
|
|
|
|
TOP,
|
|
|
|
BOTTOM,
|
|
|
|
}
|
|
|
|
|
2017-01-07 23:24:02 +01:00
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
|
|
|
private TimelineAdapter adapter;
|
|
|
|
private Kind kind;
|
2017-01-28 04:33:43 +01:00
|
|
|
private String hashtagOrId;
|
2017-04-12 10:32:05 +02:00
|
|
|
private RecyclerView recyclerView;
|
2017-01-18 19:35:07 +01:00
|
|
|
private LinearLayoutManager layoutManager;
|
|
|
|
private EndlessOnScrollListener scrollListener;
|
|
|
|
private TabLayout.OnTabSelectedListener onTabSelectedListener;
|
2017-04-15 20:23:07 +02:00
|
|
|
private boolean filterRemoveReplies;
|
|
|
|
private boolean filterRemoveReblogs;
|
2017-04-22 10:41:49 +02:00
|
|
|
private boolean hideFab;
|
2017-05-11 11:11:15 +02:00
|
|
|
private TimelineReceiver timelineReceiver;
|
2017-06-30 08:31:58 +02:00
|
|
|
private boolean topLoading;
|
|
|
|
private int topFetches;
|
|
|
|
private boolean bottomLoading;
|
|
|
|
private int bottomFetches;
|
2017-01-07 23:24:02 +01: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 04:33:43 +01:00
|
|
|
public static TimelineFragment newInstance(Kind kind, String hashtagOrId) {
|
2017-01-27 01:34:32 +01:00
|
|
|
TimelineFragment fragment = new TimelineFragment();
|
|
|
|
Bundle arguments = new Bundle();
|
|
|
|
arguments.putString("kind", kind.name());
|
2017-01-28 04:33:43 +01:00
|
|
|
arguments.putString("hashtag_or_id", hashtagOrId);
|
2017-01-27 01:34:32 +01:00
|
|
|
fragment.setArguments(arguments);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2017-01-07 23:24:02 +01:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
2017-01-27 01:34:32 +01:00
|
|
|
Bundle arguments = getArguments();
|
|
|
|
kind = Kind.valueOf(arguments.getString("kind"));
|
2017-01-28 04:33:43 +01:00
|
|
|
if (kind == Kind.TAG || kind == Kind.USER) {
|
|
|
|
hashtagOrId = arguments.getString("hashtag_or_id");
|
2017-01-27 01:34:32 +01:00
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
|
2017-04-07 09:40:59 +02:00
|
|
|
final View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
|
2017-01-07 23:24:02 +01:00
|
|
|
|
|
|
|
// Setup the SwipeRefreshLayout.
|
2017-01-23 06:19:30 +01:00
|
|
|
Context context = getContext();
|
2017-01-07 23:24:02 +01:00
|
|
|
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
// Setup the RecyclerView.
|
2017-04-12 10:32:05 +02:00
|
|
|
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
|
2017-01-07 23:24:02 +01:00
|
|
|
recyclerView.setHasFixedSize(true);
|
2017-01-18 19:35:07 +01:00
|
|
|
layoutManager = new LinearLayoutManager(context);
|
2017-01-07 23:24:02 +01:00
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
DividerItemDecoration divider = new DividerItemDecoration(
|
|
|
|
context, layoutManager.getOrientation());
|
2017-02-17 03:11:05 +01:00
|
|
|
Drawable drawable = ThemeUtils.getDrawable(context, R.attr.status_divider_drawable,
|
|
|
|
R.drawable.status_divider_dark);
|
2017-01-07 23:24:02 +01:00
|
|
|
divider.setDrawable(drawable);
|
|
|
|
recyclerView.addItemDecoration(divider);
|
2017-03-10 21:12:40 +01:00
|
|
|
adapter = new TimelineAdapter(this);
|
2017-06-26 11:15:47 +02:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
|
|
|
|
getActivity());
|
|
|
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
boolean mediaPreviewEnabled = preferences.getBoolean("mediaPreviewEnabled", true);
|
|
|
|
adapter.setMediaPreviewEnabled(mediaPreviewEnabled);
|
2017-01-07 23:24:02 +01:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-06-07 00:20:55 +02:00
|
|
|
timelineReceiver = new TimelineReceiver(adapter, this);
|
2017-06-06 23:15:29 +02:00
|
|
|
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
|
|
|
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(kind));
|
2017-05-10 04:36:05 +02:00
|
|
|
|
2017-06-26 11:15:47 +02:00
|
|
|
return rootView;
|
2017-05-10 04:36:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
2017-01-28 04:33:43 +01:00
|
|
|
if (jumpToTopAllowed()) {
|
2017-01-27 01:34:32 +01:00
|
|
|
TabLayout layout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
|
|
|
|
onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
|
|
|
|
@Override
|
2017-01-31 05:51:02 +01:00
|
|
|
public void onTabSelected(TabLayout.Tab tab) {}
|
2017-01-18 19:35:07 +01:00
|
|
|
|
2017-01-27 01:34:32 +01:00
|
|
|
@Override
|
2017-01-31 05:51:02 +01:00
|
|
|
public void onTabUnselected(TabLayout.Tab tab) {}
|
2017-01-18 19:35:07 +01:00
|
|
|
|
2017-01-27 01:34:32 +01:00
|
|
|
@Override
|
|
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
|
|
jumpToTop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
layout.addOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
}
|
2017-01-18 19:35:07 +01:00
|
|
|
|
2017-04-12 10:32:05 +02:00
|
|
|
/* This is delayed until onActivityCreated solely because MainActivity.composeButton isn't
|
|
|
|
* guaranteed to be set until then. */
|
2017-04-22 05:16:05 +02:00
|
|
|
if (composeButtonPresent()) {
|
2017-04-12 10:32:05 +02:00
|
|
|
/* Use a modified scroll listener that both loads more statuses as it goes, and hides
|
|
|
|
* the follow button on down-scroll. */
|
|
|
|
MainActivity activity = (MainActivity) getActivity();
|
|
|
|
final FloatingActionButton composeButton = activity.composeButton;
|
|
|
|
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
|
|
|
|
activity);
|
2017-04-22 10:41:49 +02:00
|
|
|
hideFab = preferences.getBoolean("fabHide", false);
|
2017-04-12 10:32:05 +02:00
|
|
|
scrollListener = new EndlessOnScrollListener(layoutManager) {
|
|
|
|
@Override
|
|
|
|
public void onScrolled(RecyclerView view, int dx, int dy) {
|
|
|
|
super.onScrolled(view, dx, dy);
|
|
|
|
|
2017-04-22 10:41:49 +02:00
|
|
|
if (hideFab) {
|
2017-04-12 10:32:05 +02:00
|
|
|
if (dy > 0 && composeButton.isShown()) {
|
|
|
|
composeButton.hide(); // hides the button if we're scrolling down
|
|
|
|
} else if (dy < 0 && !composeButton.isShown()) {
|
|
|
|
composeButton.show(); // shows it if we are scrolling up
|
|
|
|
}
|
2017-04-22 10:41:49 +02:00
|
|
|
} else if (!composeButton.isShown()) {
|
|
|
|
composeButton.show();
|
2017-04-12 10:32:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
|
|
|
TimelineFragment.this.onLoadMore(view);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// Just use the basic scroll listener to load more statuses.
|
|
|
|
scrollListener = new EndlessOnScrollListener(layoutManager) {
|
|
|
|
@Override
|
|
|
|
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
|
|
|
TimelineFragment.this.onLoadMore(view);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
recyclerView.addOnScrollListener(scrollListener);
|
|
|
|
}
|
|
|
|
|
2017-01-18 19:35:07 +01:00
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
2017-01-28 04:33:43 +01:00
|
|
|
if (jumpToTopAllowed()) {
|
2017-01-27 01:34:32 +01:00
|
|
|
TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
|
|
|
|
tabLayout.removeOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
}
|
2017-05-11 11:11:15 +02:00
|
|
|
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(timelineReceiver);
|
2017-01-18 19:35:07 +01:00
|
|
|
super.onDestroyView();
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:15:47 +02:00
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
2017-06-30 08:31:58 +02:00
|
|
|
sendFetchTimelineRequest(null, adapter.getTopId(), FetchEnd.TOP);
|
2017-06-26 11:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReply(int position) {
|
|
|
|
super.reply(adapter.getItem(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReblog(final boolean reblog, final int position) {
|
|
|
|
super.reblog(adapter.getItem(position), reblog, adapter, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFavourite(final boolean favourite, final int position) {
|
|
|
|
super.favourite(adapter.getItem(position), favourite, adapter, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMore(View view, final int position) {
|
|
|
|
super.more(adapter.getItem(position), view, adapter, position);
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:10:56 +02:00
|
|
|
@Override
|
|
|
|
public void onOpenReblog(int position) {
|
|
|
|
super.openReblog(adapter.getItem(position));
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:15:47 +02:00
|
|
|
@Override
|
|
|
|
public void onViewMedia(String[] urls, int urlIndex, Status.MediaAttachment.Type type) {
|
|
|
|
super.viewMedia(urls, urlIndex, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewThread(int position) {
|
|
|
|
super.viewThread(adapter.getItem(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewTag(String tag) {
|
|
|
|
if (kind == Kind.TAG && hashtagOrId.equals(tag)) {
|
|
|
|
// If already viewing a tag page, then ignore any request to view that tag again.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.viewTag(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewAccount(String id) {
|
|
|
|
if (kind == Kind.USER && hashtagOrId.equals(id)) {
|
|
|
|
/* If already viewing an account page, then any requests to view that account page
|
|
|
|
* should be ignored. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.viewAccount(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
|
|
|
switch (key) {
|
|
|
|
case "fabHide": {
|
|
|
|
hideFab = sharedPreferences.getBoolean("fabHide", false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "mediaPreviewEnabled": {
|
|
|
|
boolean enabled = sharedPreferences.getBoolean("mediaPreviewEnabled", true);
|
|
|
|
adapter.setMediaPreviewEnabled(enabled);
|
|
|
|
fullyRefresh();
|
|
|
|
break;
|
|
|
|
}
|
2017-06-30 08:31:58 +02:00
|
|
|
case "tabFilterHomeReplies": {
|
|
|
|
boolean filter = sharedPreferences.getBoolean("tabFilterHomeReplies", true);
|
|
|
|
boolean oldRemoveReplies = filterRemoveReplies;
|
|
|
|
filterRemoveReplies = kind == Kind.HOME && !filter;
|
|
|
|
if (adapter.getItemCount() > 1 && oldRemoveReplies != filterRemoveReplies) {
|
|
|
|
fullyRefresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "tabFilterHomeBoosts": {
|
|
|
|
boolean filter = sharedPreferences.getBoolean("tabFilterHomeBoosts", true);
|
|
|
|
boolean oldRemoveReblogs = filterRemoveReblogs;
|
|
|
|
filterRemoveReblogs = kind == Kind.HOME && !filter;
|
|
|
|
if (adapter.getItemCount() > 1 && oldRemoveReblogs != filterRemoveReblogs) {
|
|
|
|
fullyRefresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-06-26 11:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onLoadMore(RecyclerView view) {
|
|
|
|
TimelineAdapter adapter = (TimelineAdapter) view.getAdapter();
|
2017-06-30 08:31:58 +02:00
|
|
|
sendFetchTimelineRequest(adapter.getBottomId(), null, FetchEnd.BOTTOM);
|
2017-06-26 11:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void fullyRefresh() {
|
|
|
|
adapter.clear();
|
2017-06-30 08:31:58 +02:00
|
|
|
sendFetchTimelineRequest(null, null, FetchEnd.TOP);
|
2017-06-26 11:15:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-28 04:33:43 +01:00
|
|
|
private boolean jumpToTopAllowed() {
|
2017-02-21 03:32:10 +01:00
|
|
|
return kind != Kind.TAG && kind != Kind.FAVOURITES;
|
2017-01-28 04:33:43 +01:00
|
|
|
}
|
|
|
|
|
2017-04-22 05:16:05 +02:00
|
|
|
private boolean composeButtonPresent() {
|
2017-04-13 03:54:37 +02:00
|
|
|
return kind != Kind.TAG && kind != Kind.FAVOURITES && kind != Kind.USER;
|
2017-04-12 10:32:05 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 19:35:07 +01:00
|
|
|
private void jumpToTop() {
|
2017-02-13 06:18:17 +01:00
|
|
|
layoutManager.scrollToPosition(0);
|
2017-01-18 19:35:07 +01:00
|
|
|
scrollListener.reset();
|
|
|
|
}
|
|
|
|
|
2017-06-30 08:31:58 +02:00
|
|
|
private Call<List<Status>> getFetchCallByTimelineType(Kind kind, String tagOrId, String fromId,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendFetchTimelineRequest(@Nullable String fromId, @Nullable String uptoId,
|
|
|
|
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) {
|
|
|
|
topFetches++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fetchEnd == FetchEnd.BOTTOM && bottomLoading) {
|
|
|
|
bottomFetches++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-15 19:25:39 +02:00
|
|
|
if (fromId != null || adapter.getItemCount() <= 1) {
|
2017-07-02 05:23:42 +02:00
|
|
|
/* When this is called by the EndlessScrollListener it cannot refresh the footer state
|
|
|
|
* using adapter.notifyItemChanged. So its necessary to postpone doing so until a
|
|
|
|
* convenient time for the UI thread using a Runnable. */
|
|
|
|
recyclerView.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
adapter.setFooterState(FooterViewHolder.State.LOADING);
|
|
|
|
}
|
|
|
|
});
|
2017-04-15 19:25:39 +02:00
|
|
|
}
|
|
|
|
|
2017-05-10 04:36:05 +02:00
|
|
|
Callback<List<Status>> callback = new Callback<List<Status>>() {
|
2017-03-09 00:27:37 +01:00
|
|
|
@Override
|
2017-05-10 04:36:05 +02:00
|
|
|
public void onResponse(Call<List<Status>> call, Response<List<Status>> response) {
|
2017-03-14 01:49:12 +01:00
|
|
|
if (response.isSuccessful()) {
|
2017-06-30 08:31:58 +02:00
|
|
|
String linkHeader = response.headers().get("Link");
|
|
|
|
onFetchTimelineSuccess(response.body(), linkHeader, fetchEnd);
|
2017-03-14 01:49:12 +01:00
|
|
|
} else {
|
2017-06-30 08:31:58 +02:00
|
|
|
onFetchTimelineFailure(new Exception(response.message()), fetchEnd);
|
2017-03-14 01:49:12 +01:00
|
|
|
}
|
2017-03-09 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<List<Status>> call, Throwable t) {
|
2017-06-30 08:31:58 +02:00
|
|
|
onFetchTimelineFailure((Exception) t, fetchEnd);
|
2017-03-09 00:27:37 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-30 08:31:58 +02:00
|
|
|
Call<List<Status>> listCall = getFetchCallByTimelineType(kind, hashtagOrId, fromId, uptoId);
|
|
|
|
callList.add(listCall);
|
|
|
|
listCall.enqueue(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onFetchTimelineSuccess(List<Status> statuses, String linkHeader,
|
|
|
|
FetchEnd fetchEnd) {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
adapter.update(statuses, null, uptoId);
|
2017-01-27 01:34:32 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-06-30 08:31:58 +02:00
|
|
|
case BOTTOM: {
|
|
|
|
HttpHeaderLink next = HttpHeaderLink.findByRelationType(links, "next");
|
|
|
|
String fromId = null;
|
|
|
|
if (next != null) {
|
|
|
|
fromId = next.uri.getQueryParameter("max_id");
|
|
|
|
}
|
|
|
|
if (adapter.getItemCount() > 1) {
|
|
|
|
adapter.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
|
|
|
|
* down to the end. */
|
|
|
|
HttpHeaderLink previous = HttpHeaderLink.findByRelationType(links, "prev");
|
|
|
|
String uptoId = null;
|
|
|
|
if (previous != null) {
|
|
|
|
uptoId = previous.uri.getQueryParameter("since_id");
|
|
|
|
}
|
|
|
|
adapter.update(statuses, fromId, uptoId);
|
|
|
|
}
|
2017-02-21 03:32:10 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
2017-06-30 08:31:58 +02:00
|
|
|
fulfillAnyQueuedFetches(fetchEnd);
|
|
|
|
if (statuses.size() == 0 && adapter.getItemCount() == 1) {
|
2017-07-02 05:23:42 +02:00
|
|
|
adapter.setFooterState(FooterViewHolder.State.EMPTY);
|
2017-06-30 08:31:58 +02:00
|
|
|
} else {
|
2017-07-02 05:23:42 +02:00
|
|
|
adapter.setFooterState(FooterViewHolder.State.END);
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onFetchTimelineFailure(Exception exception, FetchEnd fetchEnd) {
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
Log.e(TAG, "Fetch Failure: " + exception.getMessage());
|
|
|
|
fulfillAnyQueuedFetches(fetchEnd);
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
|
|
|
|
2017-06-30 08:31:58 +02:00
|
|
|
private void fulfillAnyQueuedFetches(FetchEnd fetchEnd) {
|
|
|
|
switch (fetchEnd) {
|
|
|
|
case BOTTOM: {
|
|
|
|
bottomLoading = false;
|
|
|
|
if (bottomFetches > 0) {
|
|
|
|
bottomFetches--;
|
|
|
|
onLoadMore(recyclerView);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TOP: {
|
|
|
|
topLoading = false;
|
|
|
|
if (topFetches > 0) {
|
|
|
|
topFetches--;
|
|
|
|
onRefresh();
|
|
|
|
}
|
|
|
|
break;
|
2017-02-21 23:55:37 +01:00
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
2017-02-21 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-15 20:23:07 +02:00
|
|
|
protected void filterStatuses(List<Status> statuses) {
|
|
|
|
Iterator<Status> it = statuses.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Status status = it.next();
|
2017-06-30 08:31:58 +02:00
|
|
|
if ((status.inReplyToId != null && filterRemoveReplies)
|
|
|
|
|| (status.reblog != null && filterRemoveReblogs)) {
|
2017-04-15 20:23:07 +02:00
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|