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-11-14 05:05:23 +11:00
|
|
|
import android.app.Activity;
|
2017-07-13 05:54:52 +10:00
|
|
|
import android.arch.core.util.Function;
|
2018-05-27 18:22:12 +10:00
|
|
|
import android.arch.lifecycle.Lifecycle;
|
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-10-28 08:39:36 +11:00
|
|
|
import android.support.annotation.NonNull;
|
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;
|
2018-05-27 18:22:12 +10:00
|
|
|
import android.support.v4.util.Pair;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2018-05-27 18:22:12 +10:00
|
|
|
import android.support.v7.content.res.AppCompatResources;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.support.v7.widget.DividerItemDecoration;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2018-05-27 18:22:12 +10:00
|
|
|
import android.support.v7.widget.SimpleItemAnimator;
|
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;
|
2018-05-27 18:22:12 +10:00
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.TextView;
|
2017-01-08 09:24:02 +11:00
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.MainActivity;
|
2018-03-10 06:39:08 +11:00
|
|
|
import com.keylesspalace.tusky.R;
|
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;
|
2018-05-27 18:22:12 +10:00
|
|
|
import com.keylesspalace.tusky.appstore.EventHub;
|
|
|
|
import com.keylesspalace.tusky.appstore.BlockEvent;
|
|
|
|
import com.keylesspalace.tusky.appstore.FavoriteEvent;
|
|
|
|
import com.keylesspalace.tusky.appstore.ReblogEvent;
|
2018-02-04 08:45:14 +11:00
|
|
|
import com.keylesspalace.tusky.db.AccountEntity;
|
|
|
|
import com.keylesspalace.tusky.db.AccountManager;
|
2018-03-28 04:47:00 +11:00
|
|
|
import com.keylesspalace.tusky.di.Injectable;
|
2017-03-09 10:27:37 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Notification;
|
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
2017-08-05 19:34:50 +10:00
|
|
|
import com.keylesspalace.tusky.interfaces.ActionButtonActivity;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.interfaces.StatusActionListener;
|
2018-03-28 04:47:00 +11:00
|
|
|
import com.keylesspalace.tusky.network.TimelineCases;
|
2017-11-06 03:11:00 +11:00
|
|
|
import com.keylesspalace.tusky.util.CollectionUtil;
|
|
|
|
import com.keylesspalace.tusky.util.Either;
|
2017-06-30 16:31:58 +10:00
|
|
|
import com.keylesspalace.tusky.util.HttpHeaderLink;
|
2017-07-15 13:23:14 +10:00
|
|
|
import com.keylesspalace.tusky.util.ListUtils;
|
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
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
import java.math.BigInteger;
|
2017-07-13 05:54:52 +10:00
|
|
|
import java.util.Iterator;
|
2017-01-08 09:24:02 +11:00
|
|
|
import java.util.List;
|
2018-05-27 18:22:12 +10:00
|
|
|
import java.util.Objects;
|
2017-01-08 09:24:02 +11:00
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
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
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
import static com.uber.autodispose.AutoDispose.autoDisposable;
|
|
|
|
import static com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider.from;
|
|
|
|
|
2017-01-23 10:42:05 +11:00
|
|
|
public class NotificationsFragment extends SFragment implements
|
2018-03-28 04:47:00 +11:00
|
|
|
SwipeRefreshLayout.OnRefreshListener,
|
|
|
|
StatusActionListener,
|
2017-06-07 07:15:29 +10:00
|
|
|
NotificationsAdapter.NotificationActionListener,
|
2018-03-28 04:47:00 +11:00
|
|
|
SharedPreferences.OnSharedPreferenceChangeListener,
|
|
|
|
Injectable {
|
2017-11-04 08:17:31 +11:00
|
|
|
private static final String TAG = "NotificationF"; // logging tag
|
|
|
|
|
|
|
|
private static final int LOAD_AT_ONCE = 30;
|
2017-02-07 18:05:50 +11:00
|
|
|
|
2017-06-30 16:31:58 +10:00
|
|
|
private enum FetchEnd {
|
|
|
|
TOP,
|
2017-11-04 08:17:31 +11:00
|
|
|
BOTTOM,
|
|
|
|
MIDDLE
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
|
2017-11-06 03:11:00 +11:00
|
|
|
/**
|
2018-02-04 08:45:14 +11:00
|
|
|
* Placeholder for the notificationsEnabled. Consider moving to the separate class to hide constructor
|
2017-11-06 03:11:00 +11:00
|
|
|
* 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() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
@Inject
|
|
|
|
public TimelineCases timelineCases;
|
|
|
|
@Inject
|
2018-04-23 01:20:01 +10:00
|
|
|
AccountManager accountManager;
|
2018-05-27 18:22:12 +10:00
|
|
|
@Inject
|
|
|
|
EventHub eventHub;
|
2018-03-28 04:47:00 +11:00
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
2017-04-22 18:41:49 +10:00
|
|
|
private RecyclerView recyclerView;
|
2018-05-27 18:22:12 +10:00
|
|
|
private ProgressBar progressBar;
|
|
|
|
private TextView nothingMessageView;
|
|
|
|
|
|
|
|
private LinearLayoutManager layoutManager;
|
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-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;
|
2017-12-01 06:12:09 +11:00
|
|
|
private boolean alwaysShowSensitiveMedia;
|
2017-07-13 05:54:52 +10:00
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
@Override
|
|
|
|
protected TimelineCases timelineCases() {
|
|
|
|
return timelineCases;
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:11:00 +11: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-13 05:54:52 +10:00
|
|
|
@Override
|
2017-11-06 03:11:00 +11:00
|
|
|
public NotificationViewData apply(Either<Placeholder, Notification> input) {
|
|
|
|
if (input.isRight()) {
|
|
|
|
Notification notification = input.getAsRight();
|
2017-12-01 06:12:09 +11:00
|
|
|
return ViewDataUtils.notificationToViewData(notification, alwaysShowSensitiveMedia);
|
2017-11-06 03:11:00 +11:00
|
|
|
} else {
|
|
|
|
return new NotificationViewData.Placeholder(false);
|
|
|
|
}
|
2017-07-13 05:54:52 +10:00
|
|
|
}
|
|
|
|
});
|
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
|
2017-11-14 05:05:23 +11:00
|
|
|
public View onCreateView(@NonNull 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);
|
|
|
|
|
2017-11-14 05:05:23 +11:00
|
|
|
@NonNull Context context = inflater.getContext(); // from inflater to silence warning
|
2017-01-08 09:24:02 +11:00
|
|
|
// Setup the SwipeRefreshLayout.
|
2017-10-18 09:20:26 +11:00
|
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swipe_refresh_layout);
|
2018-05-27 18:22:12 +10:00
|
|
|
recyclerView = rootView.findViewById(R.id.recycler_view);
|
|
|
|
progressBar = rootView.findViewById(R.id.progress_bar);
|
|
|
|
nothingMessageView = rootView.findViewById(R.id.nothing_message);
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
swipeRefreshLayout.setOnRefreshListener(this);
|
2018-03-10 06:39:08 +11:00
|
|
|
swipeRefreshLayout.setColorSchemeResources(R.color.primary);
|
|
|
|
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ThemeUtils.getColor(context, android.R.attr.colorBackground));
|
2017-01-08 09:24:02 +11:00
|
|
|
// Setup the RecyclerView.
|
|
|
|
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());
|
2017-12-01 06:12:09 +11:00
|
|
|
alwaysShowSensitiveMedia = preferences.getBoolean("alwaysShowSensitiveMedia", false);
|
2017-06-26 19:15:47 +10:00
|
|
|
boolean mediaPreviewEnabled = preferences.getBoolean("mediaPreviewEnabled", true);
|
|
|
|
adapter.setMediaPreviewEnabled(mediaPreviewEnabled);
|
2017-01-08 09:24:02 +11:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-07-21 11:17:36 +10:00
|
|
|
notifications.clear();
|
2017-07-15 07:09:44 +10:00
|
|
|
topLoading = false;
|
|
|
|
topFetches = 0;
|
|
|
|
bottomLoading = false;
|
|
|
|
bottomFetches = 0;
|
|
|
|
bottomId = null;
|
|
|
|
topId = null;
|
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
|
|
|
|
setupNothingView();
|
|
|
|
|
2017-06-26 19:15:47 +10:00
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
@Override
|
|
|
|
public void onPostCreate() {
|
|
|
|
super.onPostCreate();
|
|
|
|
eventHub.getEvents()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
|
|
|
|
.subscribe(event -> {
|
|
|
|
if (event instanceof FavoriteEvent) {
|
|
|
|
handleFavEvent((FavoriteEvent) event);
|
|
|
|
} else if (event instanceof ReblogEvent) {
|
|
|
|
handleReblogEvent((ReblogEvent) event);
|
|
|
|
} else if (event instanceof BlockEvent) {
|
|
|
|
removeAllByAccountId(((BlockEvent) event).getAccountId());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setupNothingView() {
|
|
|
|
Drawable top = AppCompatResources.getDrawable(Objects.requireNonNull(getContext()),
|
|
|
|
R.drawable.elephant_friend);
|
|
|
|
if (top != null) {
|
|
|
|
top.setBounds(0, 0, top.getIntrinsicWidth() / 2, top.getIntrinsicHeight() / 2);
|
|
|
|
}
|
|
|
|
nothingMessageView.setCompoundDrawables(null, top, null, null);
|
|
|
|
nothingMessageView.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleFavEvent(FavoriteEvent event) {
|
|
|
|
Pair<Integer, Notification> posAndNotification =
|
|
|
|
findReplyPosition(event.getStatusId());
|
|
|
|
if (posAndNotification == null) return;
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
setFavovouriteForStatus(posAndNotification.first,
|
|
|
|
posAndNotification.second.getStatus(),
|
|
|
|
event.getFavourite());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleReblogEvent(ReblogEvent event) {
|
|
|
|
Pair<Integer, Notification> posAndNotification = findReplyPosition(event.getStatusId());
|
|
|
|
if (posAndNotification == null) return;
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
setReblogForStatus(posAndNotification.first,
|
|
|
|
posAndNotification.second.getStatus(),
|
|
|
|
event.getReblog());
|
|
|
|
}
|
|
|
|
|
2017-06-26 19:15:47 +10:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
|
|
|
MainActivity activity = (MainActivity) getActivity();
|
2017-11-14 05:05:23 +11:00
|
|
|
if (activity == null) throw new AssertionError("Activity is null");
|
2017-06-26 19:15:47 +10:00
|
|
|
|
|
|
|
// MainActivity's layout is guaranteed to be inflated until onCreate returns.
|
2017-10-18 09:20:26 +11:00
|
|
|
TabLayout layout = activity.findViewById(R.id.tab_layout);
|
2017-02-13 16:18:17 +11:00
|
|
|
onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
|
|
|
|
@Override
|
2018-03-10 08:02:32 +11:00
|
|
|
public void onTabSelected(TabLayout.Tab tab) {
|
|
|
|
}
|
2017-02-13 16:18:17 +11:00
|
|
|
|
|
|
|
@Override
|
2018-03-10 08:02:32 +11: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.
|
2018-02-04 08:45:14 +11:00
|
|
|
* Use a modified scroll listener that both loads more notificationsEnabled as it goes, and hides
|
2017-04-22 18:41:49 +10:00
|
|
|
* the compose button on down-scroll. */
|
2017-08-05 19:34:50 +10:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
|
2017-04-22 18:41:49 +10: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 19:34:50 +10:00
|
|
|
ActionButtonActivity activity = (ActionButtonActivity) getActivity();
|
|
|
|
FloatingActionButton composeButton = activity.getActionButton();
|
|
|
|
|
2017-11-06 03:11:00 +11:00
|
|
|
if (composeButton != null) {
|
2017-08-04 19:44:10 +10: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 18:41:49 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-05-27 18:22:12 +10:00
|
|
|
public void onLoadMore(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() {
|
2017-11-14 05:05:23 +11: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);
|
|
|
|
}
|
2017-06-07 07:15:29 +10:00
|
|
|
|
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-11-04 08:17:31 +11:00
|
|
|
sendFetchNotificationsRequest(null, topId, FetchEnd.TOP, -1);
|
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) {
|
2018-03-03 23:24:03 +11:00
|
|
|
super.reply(notifications.get(position).getAsRight().getStatus());
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
2017-10-28 08:39:36 +11:00
|
|
|
public void onReblog(final boolean reblog, final int position) {
|
2017-11-06 03:11:00 +11:00
|
|
|
final Notification notification = notifications.get(position).getAsRight();
|
2018-03-03 23:24:03 +11:00
|
|
|
final Status status = notification.getStatus();
|
2018-03-28 04:47:00 +11:00
|
|
|
timelineCases.reblogWithCallback(status, reblog, new Callback<Status>() {
|
2017-10-28 08:39:36 +11:00
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<Status> call, @NonNull retrofit2.Response<Status> response) {
|
|
|
|
if (response.isSuccessful()) {
|
2018-05-27 18:22:12 +10:00
|
|
|
setReblogForStatus(position, status, reblog);
|
2017-10-28 08:39:36 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
|
2018-03-03 23:24:03 +11:00
|
|
|
Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.getId(), t);
|
2017-10-28 08:39:36 +11:00
|
|
|
}
|
|
|
|
});
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
private void setReblogForStatus(int position, Status status, boolean reblog) {
|
|
|
|
status.setReblogged(reblog);
|
|
|
|
|
|
|
|
if (status.getReblog() != null) {
|
|
|
|
status.getReblog().setReblogged(reblog);
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationViewData.Concrete viewdata = (NotificationViewData.Concrete) notifications.getPairedItem(position);
|
|
|
|
|
|
|
|
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-28 08:39:36 +11:00
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
2017-10-28 08:39:36 +11:00
|
|
|
public void onFavourite(final boolean favourite, final int position) {
|
2017-11-06 03:11:00 +11:00
|
|
|
final Notification notification = notifications.get(position).getAsRight();
|
2018-03-03 23:24:03 +11:00
|
|
|
final Status status = notification.getStatus();
|
2018-03-28 04:47:00 +11:00
|
|
|
timelineCases.favouriteWithCallback(status, favourite, new Callback<Status>() {
|
2017-10-28 08:39:36 +11:00
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<Status> call, @NonNull retrofit2.Response<Status> response) {
|
|
|
|
if (response.isSuccessful()) {
|
2018-05-27 18:22:12 +10:00
|
|
|
setFavovouriteForStatus(position, status, favourite);
|
2017-10-28 08:39:36 +11:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
|
2018-03-03 23:24:03 +11:00
|
|
|
Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.getId(), t);
|
2017-10-28 08:39:36 +11:00
|
|
|
}
|
|
|
|
});
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
private void setFavovouriteForStatus(int position, Status status, boolean favourite) {
|
|
|
|
status.setFavourited(favourite);
|
|
|
|
|
|
|
|
if (status.getReblog() != null) {
|
|
|
|
status.getReblog().setFavourited(favourite);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
adapter.updateItemWithNotify(position, newViewData, true);
|
|
|
|
}
|
|
|
|
|
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-11-06 03:11:00 +11:00
|
|
|
Notification notification = notifications.get(position).getAsRight();
|
2018-03-03 23:24:03 +11:00
|
|
|
super.more(notification.getStatus(), view, position);
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
|
|
|
|
2017-06-25 15:07:41 +10:00
|
|
|
@Override
|
2018-05-11 04:13:25 +10:00
|
|
|
public void onViewMedia(int position, int attachmentIndex, View view) {
|
|
|
|
Notification notification = notifications.get(position).getAsRightOrNull();
|
|
|
|
if (notification == null || notification.getStatus() == null) return;
|
|
|
|
super.viewMedia(attachmentIndex, notification.getStatus(), view);
|
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-11-06 03:11:00 +11:00
|
|
|
Notification notification = notifications.get(position).getAsRight();
|
2018-03-03 23:24:03 +11:00
|
|
|
super.viewThread(notification.getStatus());
|
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-11-06 03:11:00 +11:00
|
|
|
Notification notification = notifications.get(position).getAsRight();
|
2018-03-03 23:24:03 +11:00
|
|
|
onViewAccount(notification.getAccount().getId());
|
2017-06-28 18:10:56 +10:00
|
|
|
}
|
|
|
|
|
2017-07-13 05:54:52 +10:00
|
|
|
@Override
|
|
|
|
public void onExpandedChange(boolean expanded, int position) {
|
2017-11-06 03:11:00 +11:00
|
|
|
NotificationViewData.Concrete old =
|
|
|
|
(NotificationViewData.Concrete) notifications.getPairedItem(position);
|
2017-11-07 02:19:15 +11:00
|
|
|
StatusViewData.Concrete statusViewData =
|
2017-07-13 05:54:52 +10:00
|
|
|
new StatusViewData.Builder(old.getStatusViewData())
|
|
|
|
.setIsExpanded(expanded)
|
|
|
|
.createStatusViewData();
|
2017-11-06 03:11:00 +11:00
|
|
|
NotificationViewData notificationViewData = new NotificationViewData.Concrete(old.getType(),
|
2017-12-01 06:12:09 +11:00
|
|
|
old.getId(), old.getAccount(), statusViewData, expanded);
|
2017-07-13 05:54:52 +10:00
|
|
|
notifications.setPairedItem(position, notificationViewData);
|
|
|
|
adapter.updateItemWithNotify(position, notificationViewData, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onContentHiddenChange(boolean isShowing, int position) {
|
2017-11-06 03:11:00 +11:00
|
|
|
NotificationViewData.Concrete old =
|
|
|
|
(NotificationViewData.Concrete) notifications.getPairedItem(position);
|
2017-11-07 02:19:15 +11:00
|
|
|
StatusViewData.Concrete statusViewData =
|
2017-07-13 05:54:52 +10:00
|
|
|
new StatusViewData.Builder(old.getStatusViewData())
|
|
|
|
.setIsShowingSensitiveContent(isShowing)
|
|
|
|
.createStatusViewData();
|
2017-11-06 03:11:00 +11:00
|
|
|
NotificationViewData notificationViewData = new NotificationViewData.Concrete(old.getType(),
|
2017-12-01 06:12:09 +11:00
|
|
|
old.getId(), old.getAccount(), statusViewData, old.isExpanded());
|
2017-07-13 05:54:52 +10:00
|
|
|
notifications.setPairedItem(position, notificationViewData);
|
|
|
|
adapter.updateItemWithNotify(position, notificationViewData, false);
|
|
|
|
}
|
|
|
|
|
2017-11-04 08:17:31 +11:00
|
|
|
@Override
|
|
|
|
public void onLoadMore(int position) {
|
|
|
|
//check bounds before accessing list,
|
|
|
|
if (notifications.size() >= position && position > 0) {
|
2017-11-07 02:19:15 +11: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;
|
|
|
|
}
|
2018-03-03 23:24:03 +11:00
|
|
|
sendFetchNotificationsRequest(previous.getId(), next.getId(), FetchEnd.MIDDLE, position);
|
2017-11-06 03:11:00 +11:00
|
|
|
NotificationViewData notificationViewData =
|
|
|
|
new NotificationViewData.Placeholder(true);
|
2017-11-04 08:17:31 +11:00
|
|
|
notifications.setPairedItem(position, notificationViewData);
|
|
|
|
adapter.updateItemWithNotify(position, notificationViewData, false);
|
|
|
|
} else {
|
|
|
|
Log.d(TAG, "error loading more");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-11-08 06:36:19 +11:00
|
|
|
@Override
|
|
|
|
public void onViewStatusForNotificationId(String notificationId) {
|
|
|
|
for (Either<Placeholder, Notification> either : notifications) {
|
|
|
|
Notification notification = either.getAsRightOrNull();
|
2018-03-03 23:24:03 +11:00
|
|
|
if (notification != null && notification.getId().equals(notificationId)) {
|
|
|
|
super.viewThread(notification.getStatus());
|
2017-11-08 06:36:19 +11:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.w(TAG, "Didn't find a notification for ID: " + notificationId);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
private void removeAllByAccountId(String accountId) {
|
2017-07-13 05:54:52 +10:00
|
|
|
// using iterator to safely remove items while iterating
|
2017-11-06 03:11:00 +11:00
|
|
|
Iterator<Either<Placeholder, Notification>> iterator = notifications.iterator();
|
2017-07-13 05:54:52 +10:00
|
|
|
while (iterator.hasNext()) {
|
2017-11-06 03:11:00 +11:00
|
|
|
Either<Placeholder, Notification> notification = iterator.next();
|
|
|
|
Notification maybeNotification = notification.getAsRightOrNull();
|
2018-03-03 23:24:03 +11:00
|
|
|
if (maybeNotification != null && maybeNotification.getAccount().getId().equals(accountId)) {
|
2017-07-13 05:54:52 +10:00
|
|
|
iterator.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
adapter.update(notifications.getPairedCopy());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onLoadMore() {
|
2017-11-04 08:17:31 +11:00
|
|
|
sendFetchNotificationsRequest(bottomId, null, FetchEnd.BOTTOM, -1);
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
private void jumpToTop() {
|
|
|
|
layoutManager.scrollToPosition(0);
|
|
|
|
scrollListener.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendFetchNotificationsRequest(String fromId, String uptoId,
|
2017-11-04 08:17:31 +11:00
|
|
|
final FetchEnd fetchEnd, final int pos) {
|
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. */
|
2017-12-02 08:31:34 +11:00
|
|
|
recyclerView.post(() -> adapter.setFooterState(FooterViewHolder.State.LOADING));
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
|
2017-11-04 08:17:31 +11:00
|
|
|
Call<List<Notification>> call = mastodonApi.notifications(fromId, uptoId, LOAD_AT_ONCE);
|
2017-06-30 16:31:58 +10:00
|
|
|
|
|
|
|
call.enqueue(new Callback<List<Notification>>() {
|
|
|
|
@Override
|
2017-10-28 08:39:36 +11:00
|
|
|
public void onResponse(@NonNull Call<List<Notification>> call,
|
|
|
|
@NonNull Response<List<Notification>> response) {
|
2017-06-30 16:31:58 +10:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
String linkHeader = response.headers().get("Link");
|
2017-11-04 08:17:31 +11:00
|
|
|
onFetchNotificationsSuccess(response.body(), linkHeader, fetchEnd, pos);
|
2017-06-30 16:31:58 +10:00
|
|
|
} else {
|
2017-11-04 08:17:31 +11:00
|
|
|
onFetchNotificationsFailure(new Exception(response.message()), fetchEnd, pos);
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-10-28 08:39:36 +11:00
|
|
|
public void onFailure(@NonNull Call<List<Notification>> call, @NonNull Throwable t) {
|
2017-11-04 08:17:31 +11:00
|
|
|
onFetchNotificationsFailure((Exception) t, fetchEnd, pos);
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
});
|
|
|
|
callList.add(call);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onFetchNotificationsSuccess(List<Notification> notifications, String linkHeader,
|
2017-11-04 08:17:31 +11:00
|
|
|
FetchEnd fetchEnd, int pos) {
|
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;
|
|
|
|
}
|
2017-11-04 08:17:31 +11:00
|
|
|
case MIDDLE: {
|
2017-11-06 03:11:00 +11:00
|
|
|
replacePlaceholderWithNotifications(notifications, pos);
|
2017-11-04 08:17:31 +11:00
|
|
|
break;
|
|
|
|
}
|
2017-06-30 16:31:58 +10: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-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
|
|
|
}
|
2018-02-04 08:45:14 +11:00
|
|
|
|
2017-06-30 16:31:58 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-04 08:45:14 +11:00
|
|
|
|
|
|
|
saveNewestNotificationId(notifications);
|
|
|
|
|
2017-06-30 16:31:58 +10:00
|
|
|
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);
|
2018-05-27 18:22:12 +10:00
|
|
|
progressBar.setVisibility(View.GONE);
|
2017-06-30 16:31:58 +10:00
|
|
|
}
|
|
|
|
|
2017-11-07 22:59:46 +11: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);
|
2018-05-27 18:22:12 +10:00
|
|
|
progressBar.setVisibility(View.GONE);
|
2017-11-07 22:59:46 +11:00
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
private void saveNewestNotificationId(List<Notification> notifications) {
|
2018-04-23 01:20:01 +10:00
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
AccountEntity account = accountManager.getActiveAccount();
|
|
|
|
BigInteger lastNoti = new BigInteger(account.getLastNotificationId());
|
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
for (Notification noti : notifications) {
|
2018-03-03 23:24:03 +11:00
|
|
|
BigInteger a = new BigInteger(noti.getId());
|
2018-03-10 08:02:32 +11:00
|
|
|
if (isBiggerThan(a, lastNoti)) {
|
2018-02-04 08:45:14 +11:00
|
|
|
lastNoti = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.d(TAG, "saving newest noti id: " + lastNoti);
|
|
|
|
|
|
|
|
account.setLastNotificationId(lastNoti.toString());
|
|
|
|
accountManager.saveAccount(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isBiggerThan(BigInteger newId, BigInteger lastShownNotificationId) {
|
2018-05-09 03:15:10 +10:00
|
|
|
return lastShownNotificationId.compareTo(newId) < 0;
|
2018-02-04 08:45:14 +11:00
|
|
|
}
|
|
|
|
|
2017-11-04 08:17:31 +11:00
|
|
|
private void update(@Nullable List<Notification> newNotifications, @Nullable String fromId,
|
|
|
|
@Nullable String uptoId) {
|
2017-07-15 13:23:14 +10:00
|
|
|
if (ListUtils.isEmpty(newNotifications)) {
|
2017-07-13 05:54:52 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fromId != null) {
|
|
|
|
bottomId = fromId;
|
|
|
|
}
|
|
|
|
if (uptoId != null) {
|
|
|
|
topId = uptoId;
|
|
|
|
}
|
2017-11-06 03:11:00 +11:00
|
|
|
List<Either<Placeholder, Notification>> liftedNew =
|
|
|
|
liftNotificationList(newNotifications);
|
2017-07-13 05:54:52 +10:00
|
|
|
if (notifications.isEmpty()) {
|
2017-11-06 03:11:00 +11:00
|
|
|
notifications.addAll(liftedNew);
|
2017-07-13 05:54:52 +10:00
|
|
|
} else {
|
2017-11-06 03:11:00 +11:00
|
|
|
int index = notifications.indexOf(liftedNew.get(newNotifications.size() - 1));
|
2017-07-13 05:54:52 +10:00
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
notifications.remove(0);
|
|
|
|
}
|
2017-11-06 03:11:00 +11:00
|
|
|
|
|
|
|
|
|
|
|
int newIndex = liftedNew.indexOf(notifications.get(0));
|
2017-07-13 05:54:52 +10:00
|
|
|
if (newIndex == -1) {
|
2017-11-06 03:11:00 +11:00
|
|
|
if (index == -1 && liftedNew.size() >= LOAD_AT_ONCE) {
|
2017-12-02 08:31:34 +11:00
|
|
|
liftedNew.add(Either.left(Placeholder.getInstance()));
|
2017-11-04 08:17:31 +11:00
|
|
|
}
|
2017-11-06 03:11:00 +11:00
|
|
|
notifications.addAll(0, liftedNew);
|
2017-07-13 05:54:52 +10:00
|
|
|
} else {
|
2017-11-06 03:11:00 +11:00
|
|
|
notifications.addAll(0, liftedNew.subList(0, newIndex));
|
2017-07-13 05:54:52 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
adapter.update(notifications.getPairedCopy());
|
|
|
|
}
|
|
|
|
|
2017-11-04 08:17:31 +11:00
|
|
|
private void addItems(List<Notification> newNotifications, @Nullable String fromId) {
|
2017-07-15 13:23:14 +10:00
|
|
|
if (ListUtils.isEmpty(newNotifications)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-13 05:54:52 +10:00
|
|
|
if (fromId != null) {
|
|
|
|
bottomId = fromId;
|
|
|
|
}
|
|
|
|
int end = notifications.size();
|
2017-11-06 03:11:00 +11: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-13 05:54:52 +10:00
|
|
|
List<NotificationViewData> newViewDatas = notifications.getPairedCopy()
|
|
|
|
.subList(notifications.size() - newNotifications.size(),
|
2017-11-04 08:17:31 +11:00
|
|
|
notifications.size());
|
2017-07-14 11:31:31 +10:00
|
|
|
adapter.addItems(newViewDatas);
|
2017-07-13 05:54:52 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 16:31:58 +10:00
|
|
|
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-11-06 03:11:00 +11:00
|
|
|
private void replacePlaceholderWithNotifications(List<Notification> newNotifications, int pos) {
|
|
|
|
// Remove placeholder
|
2017-11-04 08:17:31 +11:00
|
|
|
notifications.remove(pos);
|
|
|
|
|
|
|
|
if (ListUtils.isEmpty(newNotifications)) {
|
|
|
|
adapter.update(notifications.getPairedCopy());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:11:00 +11: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) {
|
2017-12-02 08:31:34 +11:00
|
|
|
liftedNew.add(Either.left(Placeholder.getInstance()));
|
2017-11-04 08:17:31 +11:00
|
|
|
}
|
|
|
|
|
2017-11-06 03:11:00 +11:00
|
|
|
notifications.addAll(pos, liftedNew);
|
2017-11-04 08:17:31 +11:00
|
|
|
adapter.update(notifications.getPairedCopy());
|
2017-11-06 03:11:00 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
private final Function<Notification, Either<Placeholder, Notification>> notificationLifter =
|
2017-12-02 08:31:34 +11:00
|
|
|
Either::right;
|
2017-11-04 08:17:31 +11:00
|
|
|
|
2017-11-06 03:11:00 +11:00
|
|
|
private List<Either<Placeholder, Notification>> liftNotificationList(List<Notification> list) {
|
|
|
|
return CollectionUtil.map(list, notificationLifter);
|
2017-11-04 08:17:31 +11:00
|
|
|
}
|
|
|
|
|
2017-06-26 19:15:47 +10:00
|
|
|
private void fullyRefresh() {
|
|
|
|
adapter.clear();
|
2017-07-13 05:54:52 +10:00
|
|
|
notifications.clear();
|
2017-11-04 08:17:31 +11:00
|
|
|
sendFetchNotificationsRequest(null, null, FetchEnd.TOP, -1);
|
2017-06-26 19:15:47 +10:00
|
|
|
}
|
2018-05-27 18:22:12 +10:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private Pair<Integer, Notification> findReplyPosition(@NonNull String statusId) {
|
|
|
|
for (int i = 0; i < notifications.size(); i++) {
|
|
|
|
Notification notification = notifications.get(i).getAsRightOrNull();
|
|
|
|
if (notification != null
|
|
|
|
&& notification.getStatus() != null
|
|
|
|
&& notification.getType() == Notification.Type.MENTION
|
|
|
|
&& (statusId.equals(notification.getStatus().getId())
|
|
|
|
|| (notification.getStatus().getReblog() != null
|
|
|
|
&& statusId.equals(notification.getStatus().getReblog().getId())))) {
|
|
|
|
return new Pair<>(i, notification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|