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
|
|
|
|
2017-11-13 19:05:23 +01:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.NotificationManager;
|
2017-07-12 21:54:52 +02:00
|
|
|
import android.arch.core.util.Function;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.content.Context;
|
2017-03-11 19:20:10 +01:00
|
|
|
import android.content.SharedPreferences;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
2017-04-22 10:41:49 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2017-10-27 23:39:36 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.support.annotation.Nullable;
|
2017-04-22 10:41:49 +02:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2017-02-13 06:18:17 +01:00
|
|
|
import android.support.design.widget.TabLayout;
|
2017-06-06 23:15:29 +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;
|
2017-11-13 19:05:23 +01:00
|
|
|
import com.keylesspalace.tusky.NotificationPullJobCreator;
|
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.NotificationsAdapter;
|
|
|
|
import com.keylesspalace.tusky.R;
|
2017-11-30 20:12:09 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Attachment;
|
2017-03-09 00:27:37 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Notification;
|
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
2017-08-05 11:34:50 +02:00
|
|
|
import com.keylesspalace.tusky.interfaces.ActionButtonActivity;
|
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-11-05 17:11:00 +01:00
|
|
|
import com.keylesspalace.tusky.util.CollectionUtil;
|
|
|
|
import com.keylesspalace.tusky.util.Either;
|
2017-06-30 08:31:58 +02:00
|
|
|
import com.keylesspalace.tusky.util.HttpHeaderLink;
|
2017-07-15 05:23:14 +02:00
|
|
|
import com.keylesspalace.tusky.util.ListUtils;
|
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.EndlessOnScrollListener;
|
2017-07-12 21:54:52 +02:00
|
|
|
import com.keylesspalace.tusky.viewdata.NotificationViewData;
|
|
|
|
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
2017-01-07 23:24:02 +01:00
|
|
|
|
2017-07-12 21:54:52 +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-03-20 08:03:03 +01:00
|
|
|
import retrofit2.Response;
|
2017-03-09 00:27:37 +01:00
|
|
|
|
2017-01-23 00:42:05 +01:00
|
|
|
public class NotificationsFragment extends SFragment implements
|
2017-06-06 23:15:29 +02:00
|
|
|
SwipeRefreshLayout.OnRefreshListener, StatusActionListener,
|
|
|
|
NotificationsAdapter.NotificationActionListener,
|
|
|
|
SharedPreferences.OnSharedPreferenceChangeListener {
|
2017-11-03 22:17:31 +01:00
|
|
|
private static final String TAG = "NotificationF"; // logging tag
|
|
|
|
|
|
|
|
private static final int LOAD_AT_ONCE = 30;
|
2017-02-07 08:05:50 +01:00
|
|
|
|
2017-06-30 08:31:58 +02:00
|
|
|
private enum FetchEnd {
|
|
|
|
TOP,
|
2017-11-03 22:17:31 +01:00
|
|
|
BOTTOM,
|
|
|
|
MIDDLE
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 17:11:00 +01:00
|
|
|
/**
|
|
|
|
* Placeholder for the notifications. Consider moving to the separate class to hide constructor
|
|
|
|
* and reuse in different places as needed.
|
|
|
|
*/
|
|
|
|
private static final class Placeholder {
|
|
|
|
private static final Placeholder INSTANCE = new Placeholder();
|
|
|
|
|
|
|
|
public static Placeholder getInstance() {
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Placeholder() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 23:24:02 +01:00
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
2017-02-13 06:18:17 +01:00
|
|
|
private LinearLayoutManager layoutManager;
|
2017-04-22 10:41:49 +02:00
|
|
|
private RecyclerView recyclerView;
|
2017-02-13 06:18:17 +01:00
|
|
|
private EndlessOnScrollListener scrollListener;
|
2017-01-07 23:24:02 +01:00
|
|
|
private NotificationsAdapter adapter;
|
2017-02-13 06:18:17 +01:00
|
|
|
private TabLayout.OnTabSelectedListener onTabSelectedListener;
|
2017-04-22 10:41:49 +02:00
|
|
|
private boolean hideFab;
|
2017-06-06 23:15:29 +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-07-12 21:54:52 +02:00
|
|
|
private String bottomId;
|
|
|
|
private String topId;
|
2017-11-30 20:12:09 +01:00
|
|
|
private boolean alwaysShowSensitiveMedia;
|
2017-07-12 21:54:52 +02:00
|
|
|
|
2017-11-05 17:11:00 +01:00
|
|
|
// Each element is either a Notification for loading data or a Placeholder
|
|
|
|
private final PairedList<Either<Placeholder, Notification>, NotificationViewData> notifications
|
|
|
|
= new PairedList<>(new Function<Either<Placeholder, Notification>, NotificationViewData>() {
|
2017-07-12 21:54:52 +02:00
|
|
|
@Override
|
2017-11-05 17:11:00 +01:00
|
|
|
public NotificationViewData apply(Either<Placeholder, Notification> input) {
|
|
|
|
if (input.isRight()) {
|
|
|
|
Notification notification = input.getAsRight();
|
2017-11-30 20:12:09 +01:00
|
|
|
return ViewDataUtils.notificationToViewData(notification, alwaysShowSensitiveMedia);
|
2017-11-05 17:11:00 +01:00
|
|
|
} else {
|
|
|
|
return new NotificationViewData.Placeholder(false);
|
|
|
|
}
|
2017-07-12 21:54:52 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-07 23:24:02 +01:00
|
|
|
|
|
|
|
public static NotificationsFragment newInstance() {
|
|
|
|
NotificationsFragment fragment = new NotificationsFragment();
|
|
|
|
Bundle arguments = new Bundle();
|
|
|
|
fragment.setArguments(arguments);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2017-11-13 19:05:23 +01:00
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
|
2017-04-22 10:41:49 +02:00
|
|
|
@Nullable Bundle savedInstanceState) {
|
2017-01-07 23:24:02 +01:00
|
|
|
View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
|
|
|
|
|
2017-11-13 19:05:23 +01:00
|
|
|
@NonNull Context context = inflater.getContext(); // from inflater to silence warning
|
2017-01-07 23:24:02 +01:00
|
|
|
// Setup the SwipeRefreshLayout.
|
2017-10-18 00:20:26 +02:00
|
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swipe_refresh_layout);
|
2017-01-07 23:24:02 +01:00
|
|
|
swipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
// Setup the RecyclerView.
|
2017-10-18 00:20:26 +02:00
|
|
|
recyclerView = rootView.findViewById(R.id.recycler_view);
|
2017-01-07 23:24:02 +01:00
|
|
|
recyclerView.setHasFixedSize(true);
|
2017-02-13 06:18:17 +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-21 23:55:37 +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-04-22 10:41:49 +02:00
|
|
|
|
2017-03-10 21:12:40 +01:00
|
|
|
adapter = new NotificationsAdapter(this, this);
|
2017-06-26 11:15:47 +02:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
|
|
|
|
getActivity());
|
2017-11-30 20:12:09 +01:00
|
|
|
alwaysShowSensitiveMedia = preferences.getBoolean("alwaysShowSensitiveMedia", false);
|
2017-06-26 11:15:47 +02:00
|
|
|
boolean mediaPreviewEnabled = preferences.getBoolean("mediaPreviewEnabled", true);
|
|
|
|
adapter.setMediaPreviewEnabled(mediaPreviewEnabled);
|
2017-01-07 23:24:02 +01:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-07-14 03:31:31 +02:00
|
|
|
timelineReceiver = new TimelineReceiver(this);
|
2017-06-26 11:15:47 +02:00
|
|
|
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
|
|
|
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(null));
|
|
|
|
|
2017-07-21 03:17:36 +02:00
|
|
|
notifications.clear();
|
2017-07-14 23:09:44 +02:00
|
|
|
topLoading = false;
|
|
|
|
topFetches = 0;
|
|
|
|
bottomLoading = false;
|
|
|
|
bottomFetches = 0;
|
|
|
|
bottomId = null;
|
|
|
|
topId = null;
|
|
|
|
|
2017-06-26 11:15:47 +02:00
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
|
|
|
MainActivity activity = (MainActivity) getActivity();
|
2017-11-13 19:05:23 +01:00
|
|
|
if (activity == null) throw new AssertionError("Activity is null");
|
2017-06-26 11:15:47 +02:00
|
|
|
|
|
|
|
// MainActivity's layout is guaranteed to be inflated until onCreate returns.
|
2017-10-18 00:20:26 +02:00
|
|
|
TabLayout layout = activity.findViewById(R.id.tab_layout);
|
2017-02-13 06:18:17 +01:00
|
|
|
onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
|
|
|
|
@Override
|
2017-11-03 22:17:31 +01:00
|
|
|
public void onTabSelected(TabLayout.Tab tab) {}
|
2017-02-13 06:18:17 +01:00
|
|
|
|
|
|
|
@Override
|
2017-11-03 22:17:31 +01:00
|
|
|
public void onTabUnselected(TabLayout.Tab tab) {}
|
2017-02-13 06:18:17 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
|
|
jumpToTop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
layout.addOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
|
2017-04-22 10:41:49 +02: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. */
|
2017-08-05 11:34:50 +02:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
|
2017-04-22 10:41:49 +02:00
|
|
|
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);
|
|
|
|
|
2017-08-05 11:34:50 +02:00
|
|
|
ActionButtonActivity activity = (ActionButtonActivity) getActivity();
|
|
|
|
FloatingActionButton composeButton = activity.getActionButton();
|
|
|
|
|
2017-11-05 17:11:00 +01:00
|
|
|
if (composeButton != null) {
|
2017-08-04 11:44:10 +02:00
|
|
|
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();
|
2017-04-22 10:41:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
2017-07-12 21:54:52 +02:00
|
|
|
NotificationsFragment.this.onLoadMore();
|
2017-04-22 10:41:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
recyclerView.addOnScrollListener(scrollListener);
|
|
|
|
}
|
|
|
|
|
2017-02-13 06:18:17 +01:00
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
2017-11-13 19:05:23 +01:00
|
|
|
Activity activity = getActivity();
|
|
|
|
if (activity == null) {
|
|
|
|
Log.e(TAG, "Activity is null");
|
|
|
|
} else {
|
|
|
|
TabLayout tabLayout = activity.findViewById(R.id.tab_layout);
|
|
|
|
tabLayout.removeOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(activity)
|
|
|
|
.unregisterReceiver(timelineReceiver);
|
|
|
|
}
|
2017-06-06 23:15:29 +02:00
|
|
|
|
2017-02-13 06:18:17 +01:00
|
|
|
super.onDestroyView();
|
|
|
|
}
|
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
@Override
|
2017-01-07 23:24:02 +01:00
|
|
|
public void onRefresh() {
|
2017-11-03 22:17:31 +01:00
|
|
|
sendFetchNotificationsRequest(null, topId, FetchEnd.TOP, -1);
|
2017-01-23 00:42:05 +01:00
|
|
|
}
|
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
@Override
|
2017-01-23 00:42:05 +01:00
|
|
|
public void onReply(int position) {
|
2017-11-05 17:11:00 +01:00
|
|
|
super.reply(notifications.get(position).getAsRight().status);
|
2017-01-23 00:42:05 +01:00
|
|
|
}
|
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
@Override
|
2017-10-27 23:39:36 +02:00
|
|
|
public void onReblog(final boolean reblog, final int position) {
|
2017-11-05 17:11:00 +01:00
|
|
|
final Notification notification = notifications.get(position).getAsRight();
|
2017-10-27 23:39:36 +02:00
|
|
|
final Status status = notification.status;
|
|
|
|
reblogWithCallback(status, reblog, new Callback<Status>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<Status> call, @NonNull retrofit2.Response<Status> response) {
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
status.reblogged = reblog;
|
|
|
|
|
|
|
|
if (status.reblog != null) {
|
|
|
|
status.reblog.reblogged = reblog;
|
|
|
|
}
|
|
|
|
|
2017-11-30 20:12:09 +01:00
|
|
|
NotificationViewData.Concrete viewdata = (NotificationViewData.Concrete)notifications.getPairedItem(position);
|
2017-10-27 23:39:36 +02:00
|
|
|
|
2017-11-30 20:12:09 +01:00
|
|
|
StatusViewData.Builder viewDataBuilder = new StatusViewData.Builder(viewdata.getStatusViewData());
|
|
|
|
viewDataBuilder.setReblogged(reblog);
|
|
|
|
|
|
|
|
NotificationViewData.Concrete newViewData = new NotificationViewData.Concrete(
|
|
|
|
viewdata.getType(), viewdata.getId(), viewdata.getAccount(),
|
|
|
|
viewDataBuilder.createStatusViewData(), viewdata.isExpanded());
|
|
|
|
|
|
|
|
notifications.setPairedItem(position, newViewData);
|
|
|
|
|
|
|
|
adapter.updateItemWithNotify(position, newViewData, true);
|
2017-10-27 23:39:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
|
2017-11-03 22:17:31 +01:00
|
|
|
Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id, t);
|
2017-10-27 23:39:36 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-23 00:42:05 +01:00
|
|
|
}
|
|
|
|
|
2017-10-27 23:39:36 +02:00
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
@Override
|
2017-10-27 23:39:36 +02:00
|
|
|
public void onFavourite(final boolean favourite, final int position) {
|
2017-11-05 17:11:00 +01:00
|
|
|
final Notification notification = notifications.get(position).getAsRight();
|
2017-10-27 23:39:36 +02:00
|
|
|
final Status status = notification.status;
|
|
|
|
favouriteWithCallback(status, favourite, new Callback<Status>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<Status> call, @NonNull retrofit2.Response<Status> response) {
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
status.favourited = favourite;
|
|
|
|
|
|
|
|
if (status.reblog != null) {
|
|
|
|
status.reblog.favourited = favourite;
|
|
|
|
}
|
|
|
|
|
2017-11-30 20:12:09 +01:00
|
|
|
NotificationViewData.Concrete viewdata = (NotificationViewData.Concrete)notifications.getPairedItem(position);
|
|
|
|
|
|
|
|
StatusViewData.Builder viewDataBuilder = new StatusViewData.Builder(viewdata.getStatusViewData());
|
|
|
|
viewDataBuilder.setFavourited(favourite);
|
|
|
|
|
|
|
|
NotificationViewData.Concrete newViewData = new NotificationViewData.Concrete(
|
|
|
|
viewdata.getType(), viewdata.getId(), viewdata.getAccount(),
|
|
|
|
viewDataBuilder.createStatusViewData(), viewdata.isExpanded());
|
|
|
|
|
|
|
|
notifications.setPairedItem(position, newViewData);
|
2017-10-27 23:39:36 +02:00
|
|
|
|
2017-11-30 20:12:09 +01:00
|
|
|
adapter.updateItemWithNotify(position, newViewData, true);
|
2017-10-27 23:39:36 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
|
2017-11-03 22:17:31 +01:00
|
|
|
Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id, t);
|
2017-10-27 23:39:36 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-23 00:42:05 +01:00
|
|
|
}
|
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
@Override
|
2017-01-23 00:42:05 +01:00
|
|
|
public void onMore(View view, int position) {
|
2017-11-05 17:11:00 +01:00
|
|
|
Notification notification = notifications.get(position).getAsRight();
|
2017-07-14 03:31:31 +02:00
|
|
|
super.more(notification.status, view, position);
|
2017-01-23 00:42:05 +01:00
|
|
|
}
|
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
@Override
|
2017-11-30 20:12:09 +01:00
|
|
|
public void onViewMedia(String[] urls, int urlIndex, Attachment.Type type,
|
2017-07-14 07:26:58 +02:00
|
|
|
View view) {
|
|
|
|
super.viewMedia(urls, urlIndex, type, view);
|
2017-01-23 00:42:05 +01:00
|
|
|
}
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
@Override
|
2017-01-23 06:19:30 +01:00
|
|
|
public void onViewThread(int position) {
|
2017-11-05 17:11:00 +01:00
|
|
|
Notification notification = notifications.get(position).getAsRight();
|
2017-03-09 00:27:37 +01:00
|
|
|
super.viewThread(notification.status);
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|
2017-01-27 01:34:32 +01:00
|
|
|
|
2017-06-28 10:10:56 +02:00
|
|
|
@Override
|
|
|
|
public void onOpenReblog(int position) {
|
2017-11-05 17:11:00 +01:00
|
|
|
Notification notification = notifications.get(position).getAsRight();
|
|
|
|
onViewAccount(notification.account.id);
|
2017-06-28 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:54:52 +02:00
|
|
|
@Override
|
|
|
|
public void onExpandedChange(boolean expanded, int position) {
|
2017-11-05 17:11:00 +01:00
|
|
|
NotificationViewData.Concrete old =
|
|
|
|
(NotificationViewData.Concrete) notifications.getPairedItem(position);
|
2017-11-06 16:19:15 +01:00
|
|
|
StatusViewData.Concrete statusViewData =
|
2017-07-12 21:54:52 +02:00
|
|
|
new StatusViewData.Builder(old.getStatusViewData())
|
|
|
|
.setIsExpanded(expanded)
|
|
|
|
.createStatusViewData();
|
2017-11-05 17:11:00 +01:00
|
|
|
NotificationViewData notificationViewData = new NotificationViewData.Concrete(old.getType(),
|
2017-11-30 20:12:09 +01:00
|
|
|
old.getId(), old.getAccount(), statusViewData, expanded);
|
2017-07-12 21:54:52 +02:00
|
|
|
notifications.setPairedItem(position, notificationViewData);
|
|
|
|
adapter.updateItemWithNotify(position, notificationViewData, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onContentHiddenChange(boolean isShowing, int position) {
|
2017-11-05 17:11:00 +01:00
|
|
|
NotificationViewData.Concrete old =
|
|
|
|
(NotificationViewData.Concrete) notifications.getPairedItem(position);
|
2017-11-06 16:19:15 +01:00
|
|
|
StatusViewData.Concrete statusViewData =
|
2017-07-12 21:54:52 +02:00
|
|
|
new StatusViewData.Builder(old.getStatusViewData())
|
|
|
|
.setIsShowingSensitiveContent(isShowing)
|
|
|
|
.createStatusViewData();
|
2017-11-05 17:11:00 +01:00
|
|
|
NotificationViewData notificationViewData = new NotificationViewData.Concrete(old.getType(),
|
2017-11-30 20:12:09 +01:00
|
|
|
old.getId(), old.getAccount(), statusViewData, old.isExpanded());
|
2017-07-12 21:54:52 +02:00
|
|
|
notifications.setPairedItem(position, notificationViewData);
|
|
|
|
adapter.updateItemWithNotify(position, notificationViewData, false);
|
|
|
|
}
|
|
|
|
|
2017-11-03 22:17:31 +01:00
|
|
|
@Override
|
|
|
|
public void onLoadMore(int position) {
|
|
|
|
//check bounds before accessing list,
|
|
|
|
if (notifications.size() >= position && position > 0) {
|
2017-11-06 16:19:15 +01:00
|
|
|
Notification previous = notifications.get(position - 1).getAsRightOrNull();
|
|
|
|
Notification next = notifications.get(position + 1).getAsRightOrNull();
|
|
|
|
if (previous == null || next == null) {
|
|
|
|
Log.e(TAG, "Failed to load more, invalid placeholder position: " + position);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sendFetchNotificationsRequest(previous.id, next.id, FetchEnd.MIDDLE, position);
|
2017-11-05 17:11:00 +01:00
|
|
|
NotificationViewData notificationViewData =
|
|
|
|
new NotificationViewData.Placeholder(true);
|
2017-11-03 22:17:31 +01:00
|
|
|
notifications.setPairedItem(position, notificationViewData);
|
|
|
|
adapter.updateItemWithNotify(position, notificationViewData, false);
|
|
|
|
} else {
|
|
|
|
Log.d(TAG, "error loading more");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
@Override
|
2017-01-27 01:34:32 +01:00
|
|
|
public void onViewTag(String tag) {
|
|
|
|
super.viewTag(tag);
|
|
|
|
}
|
2017-01-28 04:33:43 +01:00
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
@Override
|
2017-03-03 01:25:35 +01:00
|
|
|
public void onViewAccount(String id) {
|
|
|
|
super.viewAccount(id);
|
2017-01-28 04:33:43 +01:00
|
|
|
}
|
2017-04-22 10:41:49 +02:00
|
|
|
|
2017-11-07 20:36:19 +01:00
|
|
|
@Override
|
|
|
|
public void onViewStatusForNotificationId(String notificationId) {
|
|
|
|
for (Either<Placeholder, Notification> either : notifications) {
|
|
|
|
Notification notification = either.getAsRightOrNull();
|
|
|
|
if (notification != null && notification.id.equals(notificationId)) {
|
|
|
|
super.viewThread(notification.status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.w(TAG, "Didn't find a notification for ID: " + notificationId);
|
|
|
|
}
|
|
|
|
|
2017-04-22 10:41:49 +02:00
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
2017-06-26 11:15:47 +02: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 10:41:49 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-26 11:15:47 +02:00
|
|
|
|
2017-07-12 21:54:52 +02: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
|
2017-11-05 17:11:00 +01:00
|
|
|
Iterator<Either<Placeholder, Notification>> iterator = notifications.iterator();
|
2017-07-12 21:54:52 +02:00
|
|
|
while (iterator.hasNext()) {
|
2017-11-05 17:11:00 +01:00
|
|
|
Either<Placeholder, Notification> notification = iterator.next();
|
|
|
|
Notification maybeNotification = notification.getAsRightOrNull();
|
|
|
|
if (maybeNotification != null && maybeNotification.account.id.equals(accountId)) {
|
2017-07-12 21:54:52 +02:00
|
|
|
iterator.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
adapter.update(notifications.getPairedCopy());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onLoadMore() {
|
2017-11-03 22:17:31 +01:00
|
|
|
sendFetchNotificationsRequest(bottomId, null, FetchEnd.BOTTOM, -1);
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void jumpToTop() {
|
|
|
|
layoutManager.scrollToPosition(0);
|
|
|
|
scrollListener.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendFetchNotificationsRequest(String fromId, String uptoId,
|
2017-11-03 22:17:31 +01:00
|
|
|
final FetchEnd fetchEnd, final int pos) {
|
2017-06-30 08:31:58 +02: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 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-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:17:31 +01:00
|
|
|
Call<List<Notification>> call = mastodonApi.notifications(fromId, uptoId, LOAD_AT_ONCE);
|
2017-06-30 08:31:58 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<List<Notification>>() {
|
|
|
|
@Override
|
2017-10-27 23:39:36 +02:00
|
|
|
public void onResponse(@NonNull Call<List<Notification>> call,
|
|
|
|
@NonNull Response<List<Notification>> response) {
|
2017-06-30 08:31:58 +02:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
String linkHeader = response.headers().get("Link");
|
2017-11-03 22:17:31 +01:00
|
|
|
onFetchNotificationsSuccess(response.body(), linkHeader, fetchEnd, pos);
|
2017-06-30 08:31:58 +02:00
|
|
|
} else {
|
2017-11-03 22:17:31 +01:00
|
|
|
onFetchNotificationsFailure(new Exception(response.message()), fetchEnd, pos);
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-10-27 23:39:36 +02:00
|
|
|
public void onFailure(@NonNull Call<List<Notification>> call, @NonNull Throwable t) {
|
2017-11-03 22:17:31 +01:00
|
|
|
onFetchNotificationsFailure((Exception) t, fetchEnd, pos);
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
callList.add(call);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onFetchNotificationsSuccess(List<Notification> notifications, String linkHeader,
|
2017-11-03 22:17:31 +01:00
|
|
|
FetchEnd fetchEnd, int pos) {
|
2017-06-30 08:31:58 +02: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-12 21:54:52 +02:00
|
|
|
update(notifications, null, uptoId);
|
2017-06-30 08:31:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-11-03 22:17:31 +01:00
|
|
|
case MIDDLE: {
|
2017-11-05 17:11:00 +01:00
|
|
|
replacePlaceholderWithNotifications(notifications, pos);
|
2017-11-03 22:17:31 +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) {
|
2017-07-12 21:54:52 +02:00
|
|
|
addItems(notifications, fromId);
|
2017-06-30 08:31:58 +02: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-12 21:54:52 +02:00
|
|
|
update(notifications, fromId, uptoId);
|
2017-06-30 08:31:58 +02: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 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);
|
|
|
|
}
|
|
|
|
|
2017-11-07 12:59:46 +01:00
|
|
|
private void onFetchNotificationsFailure(Exception exception, FetchEnd fetchEnd, int position) {
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
if (fetchEnd == FetchEnd.MIDDLE && !notifications.get(position).isRight()) {
|
|
|
|
NotificationViewData placeholderVD =
|
|
|
|
new NotificationViewData.Placeholder(false);
|
|
|
|
notifications.setPairedItem(position, placeholderVD);
|
|
|
|
adapter.updateItemWithNotify(position, placeholderVD, true);
|
|
|
|
}
|
|
|
|
Log.e(TAG, "Fetch failure: " + exception.getMessage());
|
|
|
|
fulfillAnyQueuedFetches(fetchEnd);
|
|
|
|
}
|
|
|
|
|
2017-11-03 22:17:31 +01:00
|
|
|
private void update(@Nullable List<Notification> newNotifications, @Nullable String fromId,
|
|
|
|
@Nullable String uptoId) {
|
2017-07-15 05:23:14 +02:00
|
|
|
if (ListUtils.isEmpty(newNotifications)) {
|
2017-07-12 21:54:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fromId != null) {
|
|
|
|
bottomId = fromId;
|
|
|
|
}
|
|
|
|
if (uptoId != null) {
|
|
|
|
topId = uptoId;
|
|
|
|
}
|
2017-11-05 17:11:00 +01:00
|
|
|
List<Either<Placeholder, Notification>> liftedNew =
|
|
|
|
liftNotificationList(newNotifications);
|
2017-07-12 21:54:52 +02:00
|
|
|
if (notifications.isEmpty()) {
|
2017-11-05 17:11:00 +01:00
|
|
|
notifications.addAll(liftedNew);
|
2017-07-12 21:54:52 +02:00
|
|
|
} else {
|
2017-11-05 17:11:00 +01:00
|
|
|
int index = notifications.indexOf(liftedNew.get(newNotifications.size() - 1));
|
2017-07-12 21:54:52 +02:00
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
notifications.remove(0);
|
|
|
|
}
|
2017-11-05 17:11:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
int newIndex = liftedNew.indexOf(notifications.get(0));
|
2017-07-12 21:54:52 +02:00
|
|
|
if (newIndex == -1) {
|
2017-11-05 17:11:00 +01:00
|
|
|
if (index == -1 && liftedNew.size() >= LOAD_AT_ONCE) {
|
|
|
|
liftedNew.add(Either.<Placeholder, Notification>left(Placeholder.getInstance()));
|
2017-11-03 22:17:31 +01:00
|
|
|
}
|
2017-11-05 17:11:00 +01:00
|
|
|
notifications.addAll(0, liftedNew);
|
2017-07-12 21:54:52 +02:00
|
|
|
} else {
|
2017-11-05 17:11:00 +01:00
|
|
|
notifications.addAll(0, liftedNew.subList(0, newIndex));
|
2017-07-12 21:54:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
adapter.update(notifications.getPairedCopy());
|
|
|
|
}
|
|
|
|
|
2017-11-03 22:17:31 +01:00
|
|
|
private void addItems(List<Notification> newNotifications, @Nullable String fromId) {
|
2017-07-15 05:23:14 +02:00
|
|
|
if (ListUtils.isEmpty(newNotifications)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-12 21:54:52 +02:00
|
|
|
if (fromId != null) {
|
|
|
|
bottomId = fromId;
|
|
|
|
}
|
|
|
|
int end = notifications.size();
|
2017-11-05 17:11:00 +01:00
|
|
|
List<Either<Placeholder, Notification>> liftedNew = liftNotificationList(newNotifications);
|
|
|
|
Either<Placeholder, Notification> last = notifications.get(end - 1);
|
|
|
|
if (last != null && liftedNew.indexOf(last) == -1) {
|
|
|
|
notifications.addAll(liftedNew);
|
2017-07-12 21:54:52 +02:00
|
|
|
List<NotificationViewData> newViewDatas = notifications.getPairedCopy()
|
|
|
|
.subList(notifications.size() - newNotifications.size(),
|
2017-11-03 22:17:31 +01:00
|
|
|
notifications.size());
|
2017-07-14 03:31:31 +02:00
|
|
|
adapter.addItems(newViewDatas);
|
2017-07-12 21:54:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 08:31:58 +02:00
|
|
|
private void fulfillAnyQueuedFetches(FetchEnd fetchEnd) {
|
|
|
|
switch (fetchEnd) {
|
|
|
|
case BOTTOM: {
|
|
|
|
bottomLoading = false;
|
|
|
|
if (bottomFetches > 0) {
|
|
|
|
bottomFetches--;
|
2017-07-12 21:54:52 +02:00
|
|
|
onLoadMore();
|
2017-06-30 08:31:58 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TOP: {
|
|
|
|
topLoading = false;
|
|
|
|
if (topFetches > 0) {
|
|
|
|
topFetches--;
|
|
|
|
onRefresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 17:11:00 +01:00
|
|
|
private void replacePlaceholderWithNotifications(List<Notification> newNotifications, int pos) {
|
|
|
|
// Remove placeholder
|
2017-11-03 22:17:31 +01:00
|
|
|
notifications.remove(pos);
|
|
|
|
|
|
|
|
if (ListUtils.isEmpty(newNotifications)) {
|
|
|
|
adapter.update(notifications.getPairedCopy());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-05 17:11:00 +01:00
|
|
|
List<Either<Placeholder, Notification>> liftedNew = liftNotificationList(newNotifications);
|
|
|
|
|
|
|
|
// If we fetched less posts than in the limit, it means that the hole is not filled
|
|
|
|
// If we fetched at least as much it means that there are more posts to load and we should
|
|
|
|
// insert new placeholder
|
|
|
|
if (newNotifications.size() >= LOAD_AT_ONCE) {
|
|
|
|
liftedNew.add(Either.<Placeholder, Notification>left(Placeholder.getInstance()));
|
2017-11-03 22:17:31 +01:00
|
|
|
}
|
|
|
|
|
2017-11-05 17:11:00 +01:00
|
|
|
notifications.addAll(pos, liftedNew);
|
2017-11-03 22:17:31 +01:00
|
|
|
adapter.update(notifications.getPairedCopy());
|
2017-11-05 17:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private final Function<Notification, Either<Placeholder, Notification>> notificationLifter =
|
|
|
|
new Function<Notification, Either<Placeholder, Notification>>() {
|
|
|
|
@Override
|
|
|
|
public Either<Placeholder, Notification> apply(Notification input) {
|
|
|
|
return Either.right(input);
|
|
|
|
}
|
|
|
|
};
|
2017-11-03 22:17:31 +01:00
|
|
|
|
2017-11-05 17:11:00 +01:00
|
|
|
private List<Either<Placeholder, Notification>> liftNotificationList(List<Notification> list) {
|
|
|
|
return CollectionUtil.map(list, notificationLifter);
|
2017-11-03 22:17:31 +01:00
|
|
|
}
|
|
|
|
|
2017-06-26 11:15:47 +02:00
|
|
|
private void fullyRefresh() {
|
|
|
|
adapter.clear();
|
2017-07-12 21:54:52 +02:00
|
|
|
notifications.clear();
|
2017-11-03 22:17:31 +01:00
|
|
|
sendFetchNotificationsRequest(null, null, FetchEnd.TOP, -1);
|
2017-06-26 11:15:47 +02:00
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|