2017-01-20 19:09:10 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* This file is a part of Tusky.
|
2017-01-20 19:09:10 +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-20 19:09:10 +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-20 19:09:10 +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-20 19:09:10 +11:00
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
package com.keylesspalace.tusky.fragment;
|
2017-01-08 09:24:02 +11:00
|
|
|
|
2017-07-13 05:54:52 +10:00
|
|
|
import android.arch.core.util.Function;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.content.Context;
|
2017-03-12 05:20:10 +11:00
|
|
|
import android.content.SharedPreferences;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
2017-04-22 18:41:49 +10:00
|
|
|
import android.preference.PreferenceManager;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.support.annotation.Nullable;
|
2017-04-22 18:41:49 +10:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2017-02-13 16:18:17 +11:00
|
|
|
import android.support.design.widget.TabLayout;
|
2017-06-07 07:15:29 +10:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2017-01-08 09:24:02 +11: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-24 05:34:31 +10:00
|
|
|
import android.util.Log;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.MainActivity;
|
2017-07-01 08:30:25 +10:00
|
|
|
import com.keylesspalace.tusky.adapter.FooterViewHolder;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.adapter.NotificationsAdapter;
|
|
|
|
import com.keylesspalace.tusky.R;
|
2017-03-09 10:27:37 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Notification;
|
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.interfaces.StatusActionListener;
|
2017-06-07 07:15:29 +10:00
|
|
|
import com.keylesspalace.tusky.receiver.TimelineReceiver;
|
2017-06-30 16:31:58 +10:00
|
|
|
import com.keylesspalace.tusky.util.HttpHeaderLink;
|
2017-07-13 05:54:52 +10:00
|
|
|
import com.keylesspalace.tusky.util.PairedList;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
2017-07-13 05:54:52 +10:00
|
|
|
import com.keylesspalace.tusky.util.ViewDataUtils;
|
2017-05-29 20:14:09 +10:00
|
|
|
import com.keylesspalace.tusky.view.EndlessOnScrollListener;
|
2017-07-13 05:54:52 +10:00
|
|
|
import com.keylesspalace.tusky.viewdata.NotificationViewData;
|
|
|
|
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
2017-01-08 09:24:02 +11:00
|
|
|
|
2017-07-13 05:54:52 +10:00
|
|
|
import java.util.Iterator;
|
2017-07-14 11:39:17 +10:00
|
|
|
import java.util.LinkedHashSet;
|
2017-01-08 09:24:02 +11:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-03-09 10:27:37 +11:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2017-03-20 18:03:03 +11:00
|
|
|
import retrofit2.Response;
|
2017-03-09 10:27:37 +11:00
|
|
|
|
2017-01-23 10:42:05 +11:00
|
|
|
public class NotificationsFragment extends SFragment implements
|
2017-06-07 07:15:29 +10:00
|
|
|
SwipeRefreshLayout.OnRefreshListener, StatusActionListener,
|
|
|
|
NotificationsAdapter.NotificationActionListener,
|
|
|
|
SharedPreferences.OnSharedPreferenceChangeListener {
|
2017-03-10 10:20:08 +11:00
|
|
|
private static final String TAG = "Notifications"; // logging tag
|
2017-02-07 18:05:50 +11:00
|
|
|
|
2017-06-30 16:31:58 +10:00
|
|
|
private enum FetchEnd {
|
|
|
|
TOP,
|
2017-07-13 05:54:52 +10:00
|
|
|
BOTTOM
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
2017-07-13 05:54:52 +10:00
|
|
|
|
2017-02-13 16:18:17 +11:00
|
|
|
private LinearLayoutManager layoutManager;
|
2017-04-22 18:41:49 +10:00
|
|
|
private RecyclerView recyclerView;
|
2017-02-13 16:18:17 +11:00
|
|
|
private EndlessOnScrollListener scrollListener;
|
2017-01-08 09:24:02 +11:00
|
|
|
private NotificationsAdapter adapter;
|
2017-02-13 16:18:17 +11:00
|
|
|
private TabLayout.OnTabSelectedListener onTabSelectedListener;
|
2017-04-22 18:41:49 +10:00
|
|
|
private boolean hideFab;
|
2017-06-07 07:15:29 +10:00
|
|
|
private TimelineReceiver timelineReceiver;
|
2017-06-30 16:31:58 +10:00
|
|
|
private boolean topLoading;
|
|
|
|
private int topFetches;
|
|
|
|
private boolean bottomLoading;
|
|
|
|
private int bottomFetches;
|
2017-07-13 05:54:52 +10:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2017-01-08 09:24:02 +11:00
|
|
|
|
|
|
|
public static NotificationsFragment newInstance() {
|
|
|
|
NotificationsFragment fragment = new NotificationsFragment();
|
|
|
|
Bundle arguments = new Bundle();
|
|
|
|
fragment.setArguments(arguments);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
2017-04-22 18:41:49 +10:00
|
|
|
@Nullable Bundle savedInstanceState) {
|
2017-01-08 09:24:02 +11:00
|
|
|
View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
|
|
|
|
|
|
|
|
// Setup the SwipeRefreshLayout.
|
2017-01-23 10:42:05 +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-04-22 18:41:49 +10:00
|
|
|
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
|
2017-01-08 09:24:02 +11:00
|
|
|
recyclerView.setHasFixedSize(true);
|
2017-02-13 16:18:17 +11:00
|
|
|
layoutManager = new LinearLayoutManager(context);
|
2017-01-08 09:24:02 +11:00
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
DividerItemDecoration divider = new DividerItemDecoration(
|
|
|
|
context, layoutManager.getOrientation());
|
2017-02-22 09:55:37 +11:00
|
|
|
Drawable drawable = ThemeUtils.getDrawable(context, R.attr.status_divider_drawable,
|
|
|
|
R.drawable.status_divider_dark);
|
2017-01-08 09:24:02 +11:00
|
|
|
divider.setDrawable(drawable);
|
|
|
|
recyclerView.addItemDecoration(divider);
|
2017-04-22 18:41:49 +10:00
|
|
|
|
2017-03-11 07:12:40 +11:00
|
|
|
adapter = new NotificationsAdapter(this, this);
|
2017-06-26 19:15:47 +10:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
|
|
|
|
getActivity());
|
|
|
|
boolean mediaPreviewEnabled = preferences.getBoolean("mediaPreviewEnabled", true);
|
|
|
|
adapter.setMediaPreviewEnabled(mediaPreviewEnabled);
|
2017-01-08 09:24:02 +11:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-07-14 11:31:31 +10:00
|
|
|
timelineReceiver = new TimelineReceiver(this);
|
2017-06-26 19:15:47 +10:00
|
|
|
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
|
|
|
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(null));
|
|
|
|
|
2017-07-15 07:09:44 +10:00
|
|
|
topLoading = false;
|
|
|
|
topFetches = 0;
|
|
|
|
bottomLoading = false;
|
|
|
|
bottomFetches = 0;
|
|
|
|
bottomId = null;
|
|
|
|
topId = null;
|
|
|
|
|
2017-06-26 19:15:47 +10:00
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
|
|
|
MainActivity activity = (MainActivity) getActivity();
|
|
|
|
|
|
|
|
// MainActivity's layout is guaranteed to be inflated until onCreate returns.
|
|
|
|
TabLayout layout = (TabLayout) activity.findViewById(R.id.tab_layout);
|
2017-02-13 16:18:17 +11:00
|
|
|
onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
|
|
|
|
@Override
|
2017-07-13 05:54:52 +10:00
|
|
|
public void onTabSelected(TabLayout.Tab tab) {
|
|
|
|
}
|
2017-02-13 16:18:17 +11:00
|
|
|
|
|
|
|
@Override
|
2017-07-13 05:54:52 +10:00
|
|
|
public void onTabUnselected(TabLayout.Tab tab) {
|
|
|
|
}
|
2017-02-13 16:18:17 +11:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
|
|
jumpToTop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
layout.addOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
|
2017-04-22 18:41:49 +10:00
|
|
|
/* This is delayed until onActivityCreated solely because MainActivity.composeButton isn't
|
|
|
|
* guaranteed to be set until then.
|
|
|
|
* Use a modified scroll listener that both loads more notifications as it goes, and hides
|
|
|
|
* the compose button on down-scroll. */
|
|
|
|
final FloatingActionButton composeButton = activity.composeButton;
|
|
|
|
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
|
|
|
|
activity);
|
|
|
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
hideFab = preferences.getBoolean("fabHide", false);
|
|
|
|
scrollListener = new EndlessOnScrollListener(layoutManager) {
|
|
|
|
@Override
|
|
|
|
public void onScrolled(RecyclerView view, int dx, int dy) {
|
|
|
|
super.onScrolled(view, dx, dy);
|
|
|
|
|
|
|
|
if (hideFab) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
} else if (!composeButton.isShown()) {
|
|
|
|
composeButton.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
2017-07-13 05:54:52 +10:00
|
|
|
NotificationsFragment.this.onLoadMore();
|
2017-04-22 18:41:49 +10:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
recyclerView.addOnScrollListener(scrollListener);
|
|
|
|
}
|
|
|
|
|
2017-02-13 16:18:17 +11:00
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
|
|
|
|
tabLayout.removeOnTabSelectedListener(onTabSelectedListener);
|
2017-06-07 07:15:29 +10:00
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(getContext())
|
|
|
|
.unregisterReceiver(timelineReceiver);
|
|
|
|
|
2017-02-13 16:18:17 +11:00
|
|
|
super.onDestroyView();
|
|
|
|
}
|
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
2017-01-08 09:24:02 +11:00
|
|
|
public void onRefresh() {
|
2017-07-13 05:54:52 +10:00
|
|
|
sendFetchNotificationsRequest(null, topId, FetchEnd.TOP);
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
2017-01-23 10:42:05 +11:00
|
|
|
public void onReply(int position) {
|
2017-07-13 05:54:52 +10:00
|
|
|
Notification notification = notifications.get(position);
|
2017-03-09 10:27:37 +11:00
|
|
|
super.reply(notification.status);
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
2017-01-23 10:42:05 +11:00
|
|
|
public void onReblog(boolean reblog, int position) {
|
2017-07-13 05:54:52 +10:00
|
|
|
Notification notification = notifications.get(position);
|
2017-03-09 10:27:37 +11:00
|
|
|
super.reblog(notification.status, reblog, adapter, position);
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
2017-01-23 10:42:05 +11:00
|
|
|
public void onFavourite(boolean favourite, int position) {
|
2017-07-13 05:54:52 +10:00
|
|
|
Notification notification = notifications.get(position);
|
2017-03-09 10:27:37 +11:00
|
|
|
super.favourite(notification.status, favourite, adapter, position);
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
2017-01-23 10:42:05 +11:00
|
|
|
public void onMore(View view, int position) {
|
2017-07-13 05:54:52 +10:00
|
|
|
Notification notification = notifications.get(position);
|
2017-07-14 11:31:31 +10:00
|
|
|
super.more(notification.status, view, position);
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
|
|
|
public void onViewMedia(String[] urls, int urlIndex, Status.MediaAttachment.Type type) {
|
|
|
|
super.viewMedia(urls, urlIndex, type);
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
2017-01-23 16:19:30 +11:00
|
|
|
public void onViewThread(int position) {
|
2017-07-13 05:54:52 +10:00
|
|
|
Notification notification = notifications.get(position);
|
2017-03-09 10:27:37 +11:00
|
|
|
super.viewThread(notification.status);
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
2017-01-27 11:34:32 +11:00
|
|
|
|
2017-06-28 18:10:56 +10:00
|
|
|
@Override
|
|
|
|
public void onOpenReblog(int position) {
|
2017-07-13 05:54:52 +10:00
|
|
|
Notification notification = notifications.get(position);
|
2017-06-28 18:10:56 +10:00
|
|
|
if (notification != null) onViewAccount(notification.account.id);
|
|
|
|
}
|
|
|
|
|
2017-07-13 05:54:52 +10:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
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-06-25 15:07:41 +10:00
|
|
|
@Override
|
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-04-22 18:41:49 +10:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
2017-06-26 19:15:47 +10:00
|
|
|
switch (key) {
|
|
|
|
case "fabHide": {
|
|
|
|
hideFab = sharedPreferences.getBoolean("fabHide", false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "mediaPreviewEnabled": {
|
|
|
|
boolean enabled = sharedPreferences.getBoolean("mediaPreviewEnabled", true);
|
|
|
|
adapter.setMediaPreviewEnabled(enabled);
|
|
|
|
fullyRefresh();
|
|
|
|
break;
|
|
|
|
}
|
2017-04-22 18:41:49 +10:00
|
|
|
}
|
|
|
|
}
|
2017-06-26 19:15:47 +10:00
|
|
|
|
2017-07-13 05:54:52 +10:00
|
|
|
@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);
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
private void jumpToTop() {
|
|
|
|
layoutManager.scrollToPosition(0);
|
|
|
|
scrollListener.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendFetchNotificationsRequest(String fromId, String uptoId,
|
2017-07-13 05:54:52 +10:00
|
|
|
final FetchEnd fetchEnd) {
|
2017-06-30 16:31:58 +10:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fromId != null || adapter.getItemCount() <= 1) {
|
2017-07-02 13:23:42 +10: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-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
Call<List<Notification>> call = mastodonApi.notifications(fromId, uptoId, null);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<List<Notification>>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call<List<Notification>> call,
|
2017-07-13 05:54:52 +10:00
|
|
|
Response<List<Notification>> response) {
|
2017-06-30 16:31:58 +10:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
String linkHeader = response.headers().get("Link");
|
|
|
|
onFetchNotificationsSuccess(response.body(), linkHeader, fetchEnd);
|
|
|
|
} else {
|
|
|
|
onFetchNotificationsFailure(new Exception(response.message()), fetchEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<List<Notification>> call, Throwable t) {
|
|
|
|
onFetchNotificationsFailure((Exception) t, fetchEnd);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
callList.add(call);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onFetchNotificationsSuccess(List<Notification> notifications, String linkHeader,
|
2017-07-13 05:54:52 +10:00
|
|
|
FetchEnd fetchEnd) {
|
2017-06-30 16:31:58 +10:00
|
|
|
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");
|
|
|
|
}
|
2017-07-13 05:54:52 +10:00
|
|
|
update(notifications, null, uptoId);
|
2017-06-30 16:31:58 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BOTTOM: {
|
|
|
|
HttpHeaderLink next = HttpHeaderLink.findByRelationType(links, "next");
|
|
|
|
String fromId = null;
|
|
|
|
if (next != null) {
|
|
|
|
fromId = next.uri.getQueryParameter("max_id");
|
|
|
|
}
|
|
|
|
if (adapter.getItemCount() > 1) {
|
2017-07-13 05:54:52 +10:00
|
|
|
addItems(notifications, fromId);
|
2017-06-30 16:31:58 +10:00
|
|
|
} 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");
|
|
|
|
}
|
2017-07-13 05:54:52 +10:00
|
|
|
update(notifications, fromId, uptoId);
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
/* Set last update id for pull notifications so that we don't get notified
|
|
|
|
* about things we already loaded here */
|
|
|
|
getPrivatePreferences().edit()
|
|
|
|
.putString("lastUpdateId", fromId)
|
|
|
|
.apply();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fulfillAnyQueuedFetches(fetchEnd);
|
|
|
|
if (notifications.size() == 0 && adapter.getItemCount() == 1) {
|
2017-07-02 13:23:42 +10:00
|
|
|
adapter.setFooterState(FooterViewHolder.State.EMPTY);
|
2017-06-30 16:31:58 +10:00
|
|
|
} else {
|
2017-07-02 13:23:42 +10:00
|
|
|
adapter.setFooterState(FooterViewHolder.State.END);
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
}
|
|
|
|
|
2017-07-13 05:54:52 +10:00
|
|
|
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()) {
|
2017-07-14 11:39:17 +10:00
|
|
|
// This construction removes duplicates while preserving order.
|
|
|
|
notifications.addAll(new LinkedHashSet<>(newNotifications));
|
2017-07-13 05:54:52 +10:00
|
|
|
} 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);
|
2017-07-14 19:11:38 +10:00
|
|
|
if (last != null && !findNotification(newNotifications, last.id) && newNotifications.size() > 0) {
|
2017-07-13 05:54:52 +10:00
|
|
|
notifications.addAll(newNotifications);
|
|
|
|
List<NotificationViewData> newViewDatas = notifications.getPairedCopy()
|
|
|
|
.subList(notifications.size() - newNotifications.size(),
|
|
|
|
notifications.size() - 1);
|
2017-07-14 11:31:31 +10:00
|
|
|
adapter.addItems(newViewDatas);
|
2017-07-13 05:54:52 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean findNotification(List<Notification> notifications, String id) {
|
|
|
|
for (Notification notification : notifications) {
|
|
|
|
if (notification.id.equals(id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-30 16:31:58 +10:00
|
|
|
private void onFetchNotificationsFailure(Exception exception, FetchEnd fetchEnd) {
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
Log.e(TAG, "Fetch failure: " + exception.getMessage());
|
|
|
|
fulfillAnyQueuedFetches(fetchEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fulfillAnyQueuedFetches(FetchEnd fetchEnd) {
|
|
|
|
switch (fetchEnd) {
|
|
|
|
case BOTTOM: {
|
|
|
|
bottomLoading = false;
|
|
|
|
if (bottomFetches > 0) {
|
|
|
|
bottomFetches--;
|
2017-07-13 05:54:52 +10:00
|
|
|
onLoadMore();
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TOP: {
|
|
|
|
topLoading = false;
|
|
|
|
if (topFetches > 0) {
|
|
|
|
topFetches--;
|
|
|
|
onRefresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 19:15:47 +10:00
|
|
|
private void fullyRefresh() {
|
|
|
|
adapter.clear();
|
2017-07-13 05:54:52 +10:00
|
|
|
notifications.clear();
|
2017-06-30 16:31:58 +10:00
|
|
|
sendFetchNotificationsRequest(null, null, FetchEnd.TOP);
|
2017-06-26 19:15:47 +10:00
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|