2022-04-27 15:20:42 +02:00
|
|
|
package app.fedilab.android.ui.fragment.timeline;
|
|
|
|
/* Copyright 2021 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of Fedilab
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
|
|
|
|
import static app.fedilab.android.BaseMainActivity.networkAvailable;
|
2022-10-21 16:33:57 +02:00
|
|
|
import static app.fedilab.android.BaseMainActivity.slugOfFirstFragment;
|
2022-04-27 15:20:42 +02:00
|
|
|
|
2022-04-30 17:04:13 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2022-06-28 19:30:45 +02:00
|
|
|
import android.content.SharedPreferences;
|
2022-04-27 15:20:42 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2022-04-30 17:04:13 +02:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2022-06-28 19:30:45 +02:00
|
|
|
import androidx.preference.PreferenceManager;
|
2022-04-27 15:20:42 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import app.fedilab.android.BaseMainActivity;
|
|
|
|
import app.fedilab.android.R;
|
2022-05-24 10:12:04 +02:00
|
|
|
import app.fedilab.android.client.entities.api.Account;
|
|
|
|
import app.fedilab.android.client.entities.api.Attachment;
|
|
|
|
import app.fedilab.android.client.entities.api.Pagination;
|
|
|
|
import app.fedilab.android.client.entities.api.Status;
|
|
|
|
import app.fedilab.android.client.entities.api.Statuses;
|
2022-06-27 18:16:41 +02:00
|
|
|
import app.fedilab.android.client.entities.app.PinnedTimeline;
|
|
|
|
import app.fedilab.android.client.entities.app.RemoteInstance;
|
2022-09-29 17:59:53 +02:00
|
|
|
import app.fedilab.android.client.entities.app.StatusCache;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.client.entities.app.TagTimeline;
|
2022-05-24 10:12:04 +02:00
|
|
|
import app.fedilab.android.client.entities.app.Timeline;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.databinding.FragmentPaginationBinding;
|
2022-09-29 17:59:53 +02:00
|
|
|
import app.fedilab.android.exception.DBException;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.helper.MastodonHelper;
|
2022-05-05 18:55:28 +02:00
|
|
|
import app.fedilab.android.helper.ThemeHelper;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.ui.drawer.StatusAdapter;
|
|
|
|
import app.fedilab.android.viewmodel.mastodon.AccountsVM;
|
|
|
|
import app.fedilab.android.viewmodel.mastodon.SearchVM;
|
|
|
|
import app.fedilab.android.viewmodel.mastodon.TimelinesVM;
|
2022-07-30 18:47:30 +02:00
|
|
|
import es.dmoral.toasty.Toasty;
|
2022-04-27 15:20:42 +02:00
|
|
|
|
|
|
|
|
2022-06-02 07:40:49 +02:00
|
|
|
public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.FetchMoreCallBack {
|
2022-04-27 15:20:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
private FragmentPaginationBinding binding;
|
|
|
|
private TimelinesVM timelinesVM;
|
|
|
|
private AccountsVM accountsVM;
|
|
|
|
private boolean flagLoading;
|
|
|
|
private String search, searchCache;
|
|
|
|
private Status statusReport;
|
2022-07-08 19:02:18 +02:00
|
|
|
private String max_id, min_id, min_id_fetch_more, max_id_fetch_more;
|
2022-04-27 15:20:42 +02:00
|
|
|
private StatusAdapter statusAdapter;
|
|
|
|
private Timeline.TimeLineEnum timelineType;
|
2022-09-27 11:19:49 +02:00
|
|
|
private List<Status> timelineStatuses;
|
2022-10-01 17:00:03 +02:00
|
|
|
public UpdateCounters update;
|
2022-10-21 16:33:57 +02:00
|
|
|
private boolean isViewInitialized;
|
|
|
|
private Statuses initialStatuses;
|
2022-10-01 17:00:03 +02:00
|
|
|
|
2022-04-30 17:04:13 +02:00
|
|
|
//Handle actions that can be done in other fragments
|
|
|
|
private final BroadcastReceiver receive_action = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
Bundle b = intent.getExtras();
|
|
|
|
if (b != null) {
|
|
|
|
Status receivedStatus = (Status) b.getSerializable(Helper.ARG_STATUS_ACTION);
|
|
|
|
String delete_statuses_for_user = b.getString(Helper.ARG_STATUS_ACCOUNT_ID_DELETED);
|
|
|
|
Status status_to_delete = (Status) b.getSerializable(Helper.ARG_STATUS_DELETED);
|
2022-05-07 18:56:08 +02:00
|
|
|
Status statusPosted = (Status) b.getSerializable(Helper.ARG_STATUS_DELETED);
|
2022-04-30 17:04:13 +02:00
|
|
|
if (receivedStatus != null && statusAdapter != null) {
|
|
|
|
int position = getPosition(receivedStatus);
|
|
|
|
if (position >= 0) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelineStatuses.get(position).reblog = receivedStatus.reblog;
|
|
|
|
timelineStatuses.get(position).reblogged = receivedStatus.reblogged;
|
|
|
|
timelineStatuses.get(position).favourited = receivedStatus.favourited;
|
|
|
|
timelineStatuses.get(position).bookmarked = receivedStatus.bookmarked;
|
|
|
|
timelineStatuses.get(position).reblogs_count = receivedStatus.reblogs_count;
|
|
|
|
timelineStatuses.get(position).favourites_count = receivedStatus.favourites_count;
|
2022-04-30 17:04:13 +02:00
|
|
|
statusAdapter.notifyItemChanged(position);
|
|
|
|
}
|
|
|
|
} else if (delete_statuses_for_user != null && statusAdapter != null) {
|
|
|
|
List<Status> statusesToRemove = new ArrayList<>();
|
2022-09-27 11:19:49 +02:00
|
|
|
for (Status status : timelineStatuses) {
|
2022-06-22 15:07:42 +02:00
|
|
|
if (status != null && status.account != null && status.account.id != null && status.account.id.equals(delete_statuses_for_user)) {
|
2022-04-30 17:04:13 +02:00
|
|
|
statusesToRemove.add(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Status statusToRemove : statusesToRemove) {
|
|
|
|
int position = getPosition(statusToRemove);
|
|
|
|
if (position >= 0) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelineStatuses.remove(position);
|
2022-04-30 17:04:13 +02:00
|
|
|
statusAdapter.notifyItemRemoved(position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (status_to_delete != null && statusAdapter != null) {
|
|
|
|
int position = getPosition(status_to_delete);
|
|
|
|
if (position >= 0) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelineStatuses.remove(position);
|
2022-04-30 17:04:13 +02:00
|
|
|
statusAdapter.notifyItemRemoved(position);
|
|
|
|
}
|
2022-05-07 18:56:08 +02:00
|
|
|
} else if (statusPosted != null && statusAdapter != null && timelineType == Timeline.TimeLineEnum.HOME) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelineStatuses.add(0, statusPosted);
|
2022-07-16 09:21:43 +02:00
|
|
|
statusAdapter.notifyItemInserted(0);
|
2022-04-30 17:04:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-05-24 10:12:04 +02:00
|
|
|
private String list_id;
|
|
|
|
private TagTimeline tagTimeline;
|
|
|
|
private LinearLayoutManager mLayoutManager;
|
|
|
|
private Account accountTimeline;
|
|
|
|
private boolean exclude_replies, exclude_reblogs, show_pinned, media_only, minified;
|
|
|
|
private String viewModelKey, remoteInstance;
|
2022-06-27 18:16:41 +02:00
|
|
|
private PinnedTimeline pinnedTimeline;
|
2022-05-24 10:12:04 +02:00
|
|
|
private String ident;
|
2022-09-26 17:09:28 +02:00
|
|
|
private String slug;
|
2022-09-27 11:19:49 +02:00
|
|
|
private TimelinesVM.TimelineParams timelineParams;
|
2022-06-29 16:33:11 +02:00
|
|
|
private boolean canBeFederated;
|
2022-04-30 17:04:13 +02:00
|
|
|
|
2022-10-21 16:33:57 +02:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2022-10-22 11:53:09 +02:00
|
|
|
if (slug != null && slugOfFirstFragment != null && slug.compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) {
|
2022-10-21 16:33:57 +02:00
|
|
|
isViewInitialized = true;
|
|
|
|
initializeStatusesCommonView(initialStatuses);
|
|
|
|
}
|
|
|
|
if (timelineStatuses != null && timelineStatuses.size() > 0) {
|
|
|
|
route(DIRECTION.FETCH_NEW, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 17:04:13 +02:00
|
|
|
/**
|
|
|
|
* Return the position of the status in the ArrayList
|
|
|
|
*
|
|
|
|
* @param status - Status to fetch
|
|
|
|
* @return position or -1 if not found
|
|
|
|
*/
|
|
|
|
private int getPosition(Status status) {
|
|
|
|
int position = 0;
|
|
|
|
boolean found = false;
|
2022-06-12 11:28:13 +02:00
|
|
|
if (status.id == null) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-09-27 11:19:49 +02:00
|
|
|
for (Status _status : timelineStatuses) {
|
2022-06-12 11:28:13 +02:00
|
|
|
if (_status.id != null && _status.id.compareTo(status.id) == 0) {
|
2022-04-30 17:04:13 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
position++;
|
|
|
|
}
|
|
|
|
return found ? position : -1;
|
|
|
|
}
|
|
|
|
|
2022-05-25 14:41:27 +02:00
|
|
|
/**
|
|
|
|
* Returned list of checked status id for reports
|
|
|
|
*
|
|
|
|
* @return List<String>
|
|
|
|
*/
|
|
|
|
public List<String> getCheckedStatusesId() {
|
|
|
|
List<String> stringList = new ArrayList<>();
|
2022-09-27 11:19:49 +02:00
|
|
|
for (Status status : timelineStatuses) {
|
2022-05-25 14:41:27 +02:00
|
|
|
if (status.isChecked) {
|
|
|
|
stringList.add(status.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stringList;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void scrollToTop() {
|
2022-07-19 15:49:47 +02:00
|
|
|
if (binding != null) {
|
|
|
|
binding.swipeContainer.setRefreshing(true);
|
|
|
|
flagLoading = false;
|
|
|
|
route(DIRECTION.SCROLL_TOP, true);
|
|
|
|
}
|
2022-05-25 14:41:27 +02:00
|
|
|
}
|
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
|
|
|
ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
timelineType = Timeline.TimeLineEnum.HOME;
|
2022-10-21 16:33:57 +02:00
|
|
|
|
2022-06-29 16:33:11 +02:00
|
|
|
canBeFederated = true;
|
2022-04-27 15:20:42 +02:00
|
|
|
if (getArguments() != null) {
|
|
|
|
timelineType = (Timeline.TimeLineEnum) getArguments().get(Helper.ARG_TIMELINE_TYPE);
|
|
|
|
list_id = getArguments().getString(Helper.ARG_LIST_ID, null);
|
|
|
|
search = getArguments().getString(Helper.ARG_SEARCH_KEYWORD, null);
|
|
|
|
searchCache = getArguments().getString(Helper.ARG_SEARCH_KEYWORD_CACHE, null);
|
2022-06-27 18:16:41 +02:00
|
|
|
pinnedTimeline = (PinnedTimeline) getArguments().getSerializable(Helper.ARG_REMOTE_INSTANCE);
|
|
|
|
if (pinnedTimeline != null && pinnedTimeline.remoteInstance != null) {
|
2022-06-28 19:30:45 +02:00
|
|
|
if (pinnedTimeline.remoteInstance.type != RemoteInstance.InstanceType.NITTER) {
|
|
|
|
remoteInstance = pinnedTimeline.remoteInstance.host;
|
|
|
|
} else {
|
|
|
|
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
|
|
|
|
remoteInstance = sharedpreferences.getString(getString(R.string.SET_NITTER_HOST), getString(R.string.DEFAULT_NITTER_HOST)).toLowerCase();
|
2022-06-29 16:33:11 +02:00
|
|
|
canBeFederated = false;
|
2022-06-28 19:30:45 +02:00
|
|
|
}
|
2022-06-27 18:16:41 +02:00
|
|
|
}
|
2022-10-22 11:53:09 +02:00
|
|
|
isViewInitialized = getArguments().getBoolean(Helper.ARG_INITIALIZE_VIEW, true);
|
2022-04-27 15:20:42 +02:00
|
|
|
tagTimeline = (TagTimeline) getArguments().getSerializable(Helper.ARG_TAG_TIMELINE);
|
|
|
|
accountTimeline = (Account) getArguments().getSerializable(Helper.ARG_ACCOUNT);
|
2022-04-29 17:12:03 +02:00
|
|
|
exclude_replies = !getArguments().getBoolean(Helper.ARG_SHOW_REPLIES, true);
|
2022-04-27 15:20:42 +02:00
|
|
|
show_pinned = getArguments().getBoolean(Helper.ARG_SHOW_PINNED, false);
|
2022-04-29 17:12:03 +02:00
|
|
|
exclude_reblogs = !getArguments().getBoolean(Helper.ARG_SHOW_REBLOGS, true);
|
2022-04-27 15:20:42 +02:00
|
|
|
media_only = getArguments().getBoolean(Helper.ARG_SHOW_MEDIA_ONY, false);
|
|
|
|
viewModelKey = getArguments().getString(Helper.ARG_VIEW_MODEL_KEY, "");
|
|
|
|
minified = getArguments().getBoolean(Helper.ARG_MINIFIED, false);
|
|
|
|
statusReport = (Status) getArguments().getSerializable(Helper.ARG_STATUS_REPORT);
|
|
|
|
}
|
2022-05-06 19:18:53 +02:00
|
|
|
if (tagTimeline != null) {
|
2022-05-19 15:48:22 +02:00
|
|
|
ident = "@T@" + tagTimeline.name;
|
2022-05-14 17:41:35 +02:00
|
|
|
if (tagTimeline.isART) {
|
|
|
|
timelineType = Timeline.TimeLineEnum.ART;
|
|
|
|
}
|
2022-05-06 19:18:53 +02:00
|
|
|
} else if (list_id != null) {
|
2022-05-19 15:48:22 +02:00
|
|
|
ident = "@l@" + list_id;
|
2022-05-06 19:18:53 +02:00
|
|
|
} else if (remoteInstance != null) {
|
2022-06-30 14:38:38 +02:00
|
|
|
if (pinnedTimeline.remoteInstance.type == RemoteInstance.InstanceType.NITTER) {
|
|
|
|
ident = "@R@" + pinnedTimeline.remoteInstance.host;
|
|
|
|
} else {
|
|
|
|
ident = "@R@" + remoteInstance;
|
|
|
|
}
|
2022-05-19 15:48:22 +02:00
|
|
|
} else if (search != null) {
|
|
|
|
ident = "@S@" + search;
|
2022-05-06 19:18:53 +02:00
|
|
|
} else {
|
|
|
|
ident = null;
|
|
|
|
}
|
2022-09-30 10:07:23 +02:00
|
|
|
if (timelineType != null) {
|
|
|
|
slug = timelineType.getValue() + (ident != null ? "|" + ident : "");
|
|
|
|
}
|
2022-10-22 11:53:09 +02:00
|
|
|
//Only fragment in main view pager should not have the view initialized
|
|
|
|
//AND Only the first fragment will initialize its view
|
|
|
|
if (!isViewInitialized) {
|
|
|
|
if (slug != null && slugOfFirstFragment != null) {
|
|
|
|
isViewInitialized = slug.compareTo(slugOfFirstFragment) == 0;
|
2022-10-22 16:39:43 +02:00
|
|
|
} else if (timelineType != null) {
|
2022-10-22 11:53:09 +02:00
|
|
|
isViewInitialized = timelineType.compareTo(Timeline.TimeLineEnum.HOME) == 0;
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 17:09:28 +02:00
|
|
|
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
|
|
|
|
|
2022-04-30 17:04:13 +02:00
|
|
|
LocalBroadcastManager.getInstance(requireActivity()).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION));
|
2022-04-27 15:20:42 +02:00
|
|
|
binding = FragmentPaginationBinding.inflate(inflater, container, false);
|
2022-05-05 18:55:28 +02:00
|
|
|
binding.getRoot().setBackgroundColor(ThemeHelper.getBackgroundColor(requireActivity()));
|
2022-04-27 15:20:42 +02:00
|
|
|
|
|
|
|
int c1 = getResources().getColor(R.color.cyanea_accent_reference);
|
|
|
|
binding.swipeContainer.setProgressBackgroundColorSchemeColor(getResources().getColor(R.color.cyanea_primary_reference));
|
|
|
|
binding.swipeContainer.setColorSchemeColors(
|
|
|
|
c1, c1, c1
|
|
|
|
);
|
|
|
|
|
|
|
|
timelinesVM = new ViewModelProvider(FragmentMastodonTimeline.this).get(viewModelKey, TimelinesVM.class);
|
|
|
|
accountsVM = new ViewModelProvider(FragmentMastodonTimeline.this).get(viewModelKey, AccountsVM.class);
|
|
|
|
|
|
|
|
binding.loader.setVisibility(View.VISIBLE);
|
|
|
|
binding.recyclerView.setVisibility(View.GONE);
|
|
|
|
max_id = statusReport != null ? statusReport.id : null;
|
2022-09-26 17:09:28 +02:00
|
|
|
if (max_id == null) {
|
|
|
|
max_id = sharedpreferences.getString(getString(R.string.SET_INNER_MARKER) + BaseMainActivity.currentUserID + BaseMainActivity.currentInstance + slug, null);
|
|
|
|
}
|
2022-04-27 15:20:42 +02:00
|
|
|
flagLoading = false;
|
2022-06-11 18:19:37 +02:00
|
|
|
router(null);
|
2022-05-25 14:41:27 +02:00
|
|
|
|
|
|
|
return binding.getRoot();
|
|
|
|
}
|
|
|
|
|
2022-10-01 17:00:03 +02:00
|
|
|
/**
|
|
|
|
* Update view and pagination when scrolling down
|
|
|
|
*
|
|
|
|
* @param fetched_statuses Statuses
|
|
|
|
*/
|
|
|
|
private synchronized void dealWithPagination(Statuses fetched_statuses, DIRECTION direction, boolean fetchingMissing, Status statusToUpdate) {
|
|
|
|
if (binding == null || !isAdded() || getActivity() == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
binding.swipeContainer.setRefreshing(false);
|
|
|
|
binding.loadingNextElements.setVisibility(View.GONE);
|
|
|
|
flagLoading = false;
|
|
|
|
if (timelineStatuses != null && fetched_statuses != null && fetched_statuses.statuses != null && fetched_statuses.statuses.size() > 0) {
|
|
|
|
try {
|
|
|
|
if (statusToUpdate != null) {
|
|
|
|
new Thread(() -> {
|
|
|
|
StatusCache statusCache = new StatusCache();
|
|
|
|
statusCache.instance = BaseMainActivity.currentInstance;
|
|
|
|
statusCache.user_id = BaseMainActivity.currentUserID;
|
|
|
|
statusToUpdate.isFetchMore = false;
|
|
|
|
statusCache.status = statusToUpdate;
|
|
|
|
statusCache.status_id = statusToUpdate.id;
|
|
|
|
try {
|
|
|
|
new StatusCache(requireActivity()).updateIfExists(statusCache);
|
|
|
|
} catch (DBException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
|
|
|
|
flagLoading = fetched_statuses.pagination.max_id == null;
|
|
|
|
binding.noAction.setVisibility(View.GONE);
|
|
|
|
if (timelineType == Timeline.TimeLineEnum.ART) {
|
|
|
|
//We have to split media in different statuses
|
|
|
|
List<Status> mediaStatuses = new ArrayList<>();
|
|
|
|
for (Status status : fetched_statuses.statuses) {
|
|
|
|
if (status.media_attachments.size() > 1) {
|
|
|
|
for (Attachment attachment : status.media_attachments) {
|
|
|
|
status.media_attachments = new ArrayList<>();
|
|
|
|
status.media_attachments.add(0, attachment);
|
|
|
|
mediaStatuses.add(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fetched_statuses.statuses = mediaStatuses;
|
|
|
|
}
|
|
|
|
//Update the timeline with new statuses
|
|
|
|
int insertedStatus = updateStatusListWith(fetched_statuses.statuses);
|
|
|
|
//For these directions, the app will display counters for new messages
|
2022-10-04 09:37:20 +02:00
|
|
|
if (insertedStatus >= 0 && update != null && direction != DIRECTION.FETCH_NEW && !fetchingMissing) {
|
2022-10-01 17:00:03 +02:00
|
|
|
update.onUpdate(insertedStatus, timelineType, slug);
|
2022-10-08 18:00:22 +02:00
|
|
|
} else if (update != null && insertedStatus == 0 && direction == DIRECTION.REFRESH) {
|
|
|
|
update.onUpdate(0, timelineType, slug);
|
2022-10-01 17:00:03 +02:00
|
|
|
}
|
|
|
|
if (direction == DIRECTION.TOP && fetchingMissing) {
|
|
|
|
binding.recyclerView.scrollToPosition(getPosition(fetched_statuses.statuses.get(fetched_statuses.statuses.size() - 1)) + 1);
|
|
|
|
}
|
|
|
|
if (!fetchingMissing) {
|
|
|
|
if (fetched_statuses.pagination.max_id == null) {
|
|
|
|
flagLoading = true;
|
|
|
|
} else if (max_id == null || fetched_statuses.pagination.max_id.compareTo(max_id) < 0) {
|
|
|
|
max_id = fetched_statuses.pagination.max_id;
|
|
|
|
}
|
|
|
|
if (min_id == null || (fetched_statuses.pagination.min_id != null && fetched_statuses.pagination.min_id.compareTo(min_id) > 0)) {
|
|
|
|
min_id = fetched_statuses.pagination.min_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (direction == DIRECTION.BOTTOM) {
|
|
|
|
flagLoading = true;
|
|
|
|
}
|
|
|
|
if (direction == DIRECTION.SCROLL_TOP) {
|
|
|
|
binding.recyclerView.scrollToPosition(0);
|
|
|
|
}
|
|
|
|
}
|
2022-06-23 16:08:36 +02:00
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
/**
|
|
|
|
* Intialize the common view for statuses on different timelines
|
|
|
|
*
|
|
|
|
* @param statuses {@link Statuses}
|
|
|
|
*/
|
2022-09-26 17:09:28 +02:00
|
|
|
private void initializeStatusesCommonView(final Statuses statuses) {
|
2022-05-30 15:22:11 +02:00
|
|
|
flagLoading = false;
|
2022-10-21 16:33:57 +02:00
|
|
|
if (!isViewInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-25 19:35:12 +02:00
|
|
|
if (binding == null || !isAdded() || getActivity() == null) {
|
2022-10-21 15:29:21 +02:00
|
|
|
if (binding != null) {
|
|
|
|
binding.loader.setVisibility(View.GONE);
|
|
|
|
}
|
2022-04-27 15:20:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
binding.loader.setVisibility(View.GONE);
|
|
|
|
binding.noAction.setVisibility(View.GONE);
|
|
|
|
binding.swipeContainer.setRefreshing(false);
|
2022-07-09 17:34:08 +02:00
|
|
|
if (searchCache == null && timelineType != Timeline.TimeLineEnum.TREND_MESSAGE) {
|
|
|
|
binding.swipeContainer.setOnRefreshListener(() -> {
|
|
|
|
binding.swipeContainer.setRefreshing(true);
|
|
|
|
flagLoading = false;
|
|
|
|
route(DIRECTION.REFRESH, true);
|
|
|
|
});
|
|
|
|
}
|
2022-04-27 15:20:42 +02:00
|
|
|
if (statuses == null || statuses.statuses == null || statuses.statuses.size() == 0) {
|
|
|
|
binding.noAction.setVisibility(View.VISIBLE);
|
|
|
|
return;
|
2022-05-14 17:41:35 +02:00
|
|
|
} else if (timelineType == Timeline.TimeLineEnum.ART) {
|
|
|
|
//We have to split media in different statuses
|
|
|
|
List<Status> mediaStatuses = new ArrayList<>();
|
|
|
|
for (Status status : statuses.statuses) {
|
|
|
|
if (!tagTimeline.isNSFW && status.sensitive) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (Attachment attachment : status.media_attachments) {
|
|
|
|
try {
|
|
|
|
Status statusTmp = (Status) status.clone();
|
|
|
|
statusTmp.art_attachment = attachment;
|
|
|
|
mediaStatuses.add(statusTmp);
|
|
|
|
} catch (CloneNotSupportedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mediaStatuses.size() > 0) {
|
|
|
|
statuses.statuses = mediaStatuses;
|
|
|
|
}
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
2022-05-28 18:02:30 +02:00
|
|
|
flagLoading = statuses.pagination.max_id == null;
|
2022-04-27 15:20:42 +02:00
|
|
|
binding.recyclerView.setVisibility(View.VISIBLE);
|
2022-09-27 11:19:49 +02:00
|
|
|
if (statusAdapter != null && timelineStatuses != null) {
|
|
|
|
int size = timelineStatuses.size();
|
|
|
|
timelineStatuses.clear();
|
|
|
|
timelineStatuses = new ArrayList<>();
|
2022-04-27 15:20:42 +02:00
|
|
|
statusAdapter.notifyItemRangeRemoved(0, size);
|
|
|
|
}
|
2022-09-27 11:19:49 +02:00
|
|
|
if (timelineStatuses == null) {
|
|
|
|
timelineStatuses = new ArrayList<>();
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
if (statusReport != null) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelineStatuses.add(statusReport);
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
2022-09-27 11:19:49 +02:00
|
|
|
timelineStatuses.addAll(statuses.statuses);
|
2022-05-04 16:13:30 +02:00
|
|
|
if (max_id == null || (statuses.pagination.max_id != null && statuses.pagination.max_id.compareTo(max_id) < 0)) {
|
|
|
|
max_id = statuses.pagination.max_id;
|
|
|
|
}
|
2022-06-11 18:19:37 +02:00
|
|
|
if (min_id == null || (statuses.pagination.min_id != null && statuses.pagination.min_id.compareTo(min_id) > 0)) {
|
2022-05-04 16:13:30 +02:00
|
|
|
min_id = statuses.pagination.min_id;
|
|
|
|
}
|
2022-09-27 11:19:49 +02:00
|
|
|
statusAdapter = new StatusAdapter(timelineStatuses, timelineType, minified, canBeFederated);
|
2022-06-11 18:19:37 +02:00
|
|
|
statusAdapter.fetchMoreCallBack = this;
|
2022-04-27 15:20:42 +02:00
|
|
|
if (statusReport != null) {
|
|
|
|
scrollToTop();
|
|
|
|
}
|
|
|
|
mLayoutManager = new LinearLayoutManager(requireActivity());
|
2022-06-29 16:33:11 +02:00
|
|
|
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
|
2022-04-27 15:20:42 +02:00
|
|
|
binding.recyclerView.setLayoutManager(mLayoutManager);
|
|
|
|
binding.recyclerView.setAdapter(statusAdapter);
|
|
|
|
|
2022-07-09 17:34:08 +02:00
|
|
|
if (searchCache == null && timelineType != Timeline.TimeLineEnum.TREND_MESSAGE) {
|
2022-04-27 15:20:42 +02:00
|
|
|
binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
@Override
|
|
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
2022-09-27 11:19:49 +02:00
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
if (requireActivity() instanceof BaseMainActivity) {
|
|
|
|
if (dy < 0 && !((BaseMainActivity) requireActivity()).getFloatingVisibility())
|
|
|
|
((BaseMainActivity) requireActivity()).manageFloatingButton(true);
|
|
|
|
if (dy > 0 && ((BaseMainActivity) requireActivity()).getFloatingVisibility())
|
|
|
|
((BaseMainActivity) requireActivity()).manageFloatingButton(false);
|
|
|
|
}
|
|
|
|
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
|
|
|
|
if (dy > 0) {
|
|
|
|
int visibleItemCount = mLayoutManager.getChildCount();
|
|
|
|
int totalItemCount = mLayoutManager.getItemCount();
|
|
|
|
if (firstVisibleItem + visibleItemCount == totalItemCount) {
|
|
|
|
if (!flagLoading) {
|
|
|
|
flagLoading = true;
|
|
|
|
binding.loadingNextElements.setVisibility(View.VISIBLE);
|
2022-06-11 18:19:37 +02:00
|
|
|
router(DIRECTION.BOTTOM);
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
binding.loadingNextElements.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
} else if (firstVisibleItem == 0) { //Scroll top and item is zero
|
|
|
|
if (!flagLoading) {
|
|
|
|
flagLoading = true;
|
|
|
|
binding.loadingNextElements.setVisibility(View.VISIBLE);
|
2022-06-11 18:19:37 +02:00
|
|
|
router(DIRECTION.TOP);
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-10-20 17:29:04 +02:00
|
|
|
//For home (first tab) we fetch new messages
|
|
|
|
if (timelineType == Timeline.TimeLineEnum.HOME) {
|
|
|
|
route(DIRECTION.FETCH_NEW, true);
|
|
|
|
}
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
2022-10-02 12:18:13 +02:00
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:39:55 +02:00
|
|
|
/**
|
|
|
|
* Update the timeline with received statuses
|
|
|
|
*
|
|
|
|
* @param statusListReceived - List<Status> Statuses received
|
|
|
|
*/
|
2022-10-01 17:00:03 +02:00
|
|
|
private int updateStatusListWith(List<Status> statusListReceived) {
|
|
|
|
int insertedStatus = 0;
|
2022-06-11 18:19:37 +02:00
|
|
|
if (statusListReceived != null && statusListReceived.size() > 0) {
|
|
|
|
for (Status statusReceived : statusListReceived) {
|
2022-09-26 17:09:28 +02:00
|
|
|
int position = 0;
|
2022-09-27 11:19:49 +02:00
|
|
|
if (timelineStatuses != null) {
|
2022-09-26 17:09:28 +02:00
|
|
|
//First we refresh statuses
|
2022-09-27 11:19:49 +02:00
|
|
|
statusAdapter.notifyItemRangeChanged(0, timelineStatuses.size());
|
2022-09-26 17:09:28 +02:00
|
|
|
//We loop through messages already in the timeline
|
2022-09-27 11:19:49 +02:00
|
|
|
for (Status statusAlreadyPresent : timelineStatuses) {
|
2022-09-26 17:09:28 +02:00
|
|
|
//We compare the id of each status and we only add status having an id greater than the another, it is inserted at this position
|
|
|
|
//Pinned messages are ignored because their date can be older
|
|
|
|
if (statusReceived.id.compareTo(statusAlreadyPresent.id) > 0) {
|
|
|
|
//We add the status to a list of id - thus we know it is already in the timeline
|
2022-09-27 11:19:49 +02:00
|
|
|
if (!timelineStatuses.contains(statusReceived) && !statusReceived.pinned && timelineType != Timeline.TimeLineEnum.ACCOUNT_TIMELINE) {
|
|
|
|
timelineStatuses.add(position, statusReceived);
|
2022-09-26 17:09:28 +02:00
|
|
|
statusAdapter.notifyItemInserted(position);
|
2022-10-02 12:18:13 +02:00
|
|
|
if (!statusReceived.cached) {
|
|
|
|
insertedStatus++;
|
|
|
|
}
|
2022-09-26 17:09:28 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
position++;
|
2022-06-18 18:39:55 +02:00
|
|
|
}
|
2022-09-26 17:09:28 +02:00
|
|
|
//Statuses added at the bottom
|
2022-09-27 11:19:49 +02:00
|
|
|
if (position == timelineStatuses.size() && !timelineStatuses.contains(statusReceived)) {
|
2022-09-26 17:09:28 +02:00
|
|
|
//We add the status to a list of id - thus we know it is already in the timeline
|
2022-09-27 11:19:49 +02:00
|
|
|
timelineStatuses.add(position, statusReceived);
|
2022-09-26 17:09:28 +02:00
|
|
|
statusAdapter.notifyItemInserted(position);
|
2022-06-18 18:39:55 +02:00
|
|
|
}
|
2022-06-18 12:00:27 +02:00
|
|
|
}
|
2022-06-11 18:19:37 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-01 17:00:03 +02:00
|
|
|
return insertedStatus;
|
2022-06-11 18:19:37 +02:00
|
|
|
}
|
|
|
|
|
2022-10-01 17:00:03 +02:00
|
|
|
/**
|
|
|
|
* Update view and pagination when scrolling down
|
|
|
|
*
|
|
|
|
* @param fetched_statuses Statuses
|
|
|
|
*/
|
|
|
|
private synchronized void dealWithPagination(Statuses fetched_statuses, DIRECTION direction, boolean fetchingMissing) {
|
|
|
|
dealWithPagination(fetched_statuses, direction, fetchingMissing, null);
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
2022-09-24 15:27:57 +02:00
|
|
|
/**
|
2022-09-24 17:26:44 +02:00
|
|
|
* Router for common timelines that can have the same treatments
|
|
|
|
* - HOME / LOCAL / PUBLIC / LIST / TAG
|
2022-09-24 15:27:57 +02:00
|
|
|
*
|
|
|
|
* @param direction - DIRECTION null if first call, then is set to TOP or BOTTOM depending of scroll
|
|
|
|
*/
|
2022-09-29 17:59:53 +02:00
|
|
|
private void routeCommon(DIRECTION direction, boolean fetchingMissing, Status status) {
|
2022-10-22 16:39:43 +02:00
|
|
|
if (direction == null && !isViewInitialized && slug != null && slugOfFirstFragment != null) {
|
2022-10-21 16:33:57 +02:00
|
|
|
isViewInitialized = slug.compareTo(slugOfFirstFragment) == 0;
|
|
|
|
}
|
2022-09-24 15:27:57 +02:00
|
|
|
if (binding == null || getActivity() == null || !isAdded()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Initialize with default params
|
2022-09-27 11:19:49 +02:00
|
|
|
timelineParams = new TimelinesVM.TimelineParams(timelineType, direction, ident);
|
2022-09-24 15:27:57 +02:00
|
|
|
timelineParams.limit = MastodonHelper.statusesPerCall(requireActivity());
|
2022-10-03 17:48:12 +02:00
|
|
|
if (direction == DIRECTION.REFRESH || direction == DIRECTION.SCROLL_TOP || direction == DIRECTION.FETCH_NEW) {
|
2022-09-26 10:17:25 +02:00
|
|
|
timelineParams.maxId = null;
|
|
|
|
timelineParams.minId = null;
|
|
|
|
} else if (direction == DIRECTION.BOTTOM) {
|
|
|
|
timelineParams.maxId = fetchingMissing ? max_id_fetch_more : max_id;
|
|
|
|
timelineParams.minId = null;
|
2022-09-26 17:09:28 +02:00
|
|
|
} else if (direction == DIRECTION.TOP) {
|
2022-09-26 10:17:25 +02:00
|
|
|
timelineParams.minId = fetchingMissing ? min_id_fetch_more : min_id;
|
|
|
|
timelineParams.maxId = null;
|
2022-09-26 17:09:28 +02:00
|
|
|
} else {
|
|
|
|
timelineParams.maxId = max_id;
|
2022-09-26 10:17:25 +02:00
|
|
|
}
|
2022-09-24 15:27:57 +02:00
|
|
|
timelineParams.fetchingMissing = fetchingMissing;
|
2022-10-06 10:19:59 +02:00
|
|
|
|
2022-09-24 15:27:57 +02:00
|
|
|
switch (timelineType) {
|
|
|
|
case LOCAL:
|
|
|
|
timelineParams.local = true;
|
|
|
|
timelineParams.remote = false;
|
|
|
|
break;
|
|
|
|
case PUBLIC:
|
|
|
|
timelineParams.local = false;
|
|
|
|
timelineParams.remote = true;
|
|
|
|
break;
|
|
|
|
case LIST:
|
|
|
|
timelineParams.listId = list_id;
|
|
|
|
break;
|
|
|
|
case TAG:
|
|
|
|
if (tagTimeline == null) {
|
|
|
|
tagTimeline = new TagTimeline();
|
|
|
|
tagTimeline.name = search;
|
|
|
|
}
|
|
|
|
timelineParams.onlyMedia = timelineType == Timeline.TimeLineEnum.ART;
|
|
|
|
timelineParams.none = tagTimeline.none;
|
|
|
|
timelineParams.all = tagTimeline.all;
|
|
|
|
timelineParams.any = tagTimeline.any;
|
|
|
|
timelineParams.hashtagTrim = tagTimeline.name;
|
2022-10-06 10:19:59 +02:00
|
|
|
if (timelineParams.hashtagTrim != null && timelineParams.hashtagTrim.startsWith("#")) {
|
|
|
|
timelineParams.hashtagTrim = tagTimeline.name.substring(1);
|
|
|
|
}
|
2022-09-24 15:27:57 +02:00
|
|
|
break;
|
2022-09-26 17:09:28 +02:00
|
|
|
case REMOTE:
|
|
|
|
timelineParams.instance = remoteInstance;
|
|
|
|
timelineParams.token = null;
|
|
|
|
break;
|
2022-09-24 15:27:57 +02:00
|
|
|
}
|
2022-09-24 17:26:44 +02:00
|
|
|
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
|
|
|
|
boolean useCache = sharedpreferences.getBoolean(getString(R.string.SET_USE_CACHE), true);
|
2022-10-20 17:29:04 +02:00
|
|
|
|
|
|
|
Handler handler = new Handler();
|
|
|
|
//The action for fetching new messages is delayed for other timelines than HOME
|
2022-10-21 16:33:57 +02:00
|
|
|
if (slugOfFirstFragment == null) {
|
|
|
|
slugOfFirstFragment = slug;
|
|
|
|
}
|
|
|
|
|
2022-10-20 17:29:04 +02:00
|
|
|
handler.postDelayed(() -> {
|
|
|
|
if (useCache && direction != DIRECTION.SCROLL_TOP && direction != DIRECTION.FETCH_NEW) {
|
|
|
|
getCachedStatus(direction, fetchingMissing, timelineParams);
|
|
|
|
} else {
|
|
|
|
getLiveStatus(direction, fetchingMissing, timelineParams, status);
|
|
|
|
}
|
2022-10-21 16:33:57 +02:00
|
|
|
}, slug.compareTo(slugOfFirstFragment) == 0 ? 0 : 1000);
|
2022-09-24 17:26:44 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-01 17:00:03 +02:00
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
storeMarker();
|
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
//Update last read id for home timeline
|
|
|
|
if (isAdded()) {
|
|
|
|
storeMarker();
|
|
|
|
}
|
2022-10-22 11:53:09 +02:00
|
|
|
try {
|
|
|
|
LocalBroadcastManager.getInstance(requireActivity()).unregisterReceiver(receive_action);
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2022-10-01 17:00:03 +02:00
|
|
|
super.onDestroyView();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void storeMarker() {
|
2022-10-05 17:26:15 +02:00
|
|
|
if (mLayoutManager != null) {
|
2022-10-01 17:00:03 +02:00
|
|
|
int position = mLayoutManager.findFirstVisibleItemPosition();
|
|
|
|
if (timelineStatuses != null && timelineStatuses.size() > position) {
|
|
|
|
try {
|
|
|
|
Status status = timelineStatuses.get(position);
|
|
|
|
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(getString(R.string.SET_INNER_MARKER) + BaseMainActivity.currentUserID + BaseMainActivity.currentInstance + slug, status.id);
|
|
|
|
editor.apply();
|
2022-10-05 17:26:15 +02:00
|
|
|
if (timelineType == Timeline.TimeLineEnum.HOME) {
|
|
|
|
timelinesVM.addMarker(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id, null);
|
|
|
|
}
|
2022-10-01 17:00:03 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void router(DIRECTION direction) {
|
|
|
|
if (networkAvailable == BaseMainActivity.status.UNKNOWN) {
|
|
|
|
new Thread(() -> {
|
|
|
|
if (networkAvailable == BaseMainActivity.status.UNKNOWN) {
|
|
|
|
networkAvailable = Helper.isConnectedToInternet(requireActivity(), BaseMainActivity.currentInstance);
|
|
|
|
}
|
|
|
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
|
|
Runnable myRunnable = () -> route(direction, false);
|
|
|
|
mainHandler.post(myRunnable);
|
|
|
|
}).start();
|
|
|
|
} else {
|
|
|
|
route(direction, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 17:26:44 +02:00
|
|
|
private void getCachedStatus(DIRECTION direction, boolean fetchingMissing, TimelinesVM.TimelineParams timelineParams) {
|
2022-10-21 15:29:21 +02:00
|
|
|
if (getView() == null) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-24 17:26:44 +02:00
|
|
|
if (direction == null) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelinesVM.getTimelineCache(timelineStatuses, timelineParams)
|
2022-09-24 17:26:44 +02:00
|
|
|
.observe(getViewLifecycleOwner(), statusesCached -> {
|
2022-10-21 16:33:57 +02:00
|
|
|
initialStatuses = statusesCached;
|
2022-09-24 17:26:44 +02:00
|
|
|
if (statusesCached == null || statusesCached.statuses == null || statusesCached.statuses.size() == 0) {
|
2022-09-29 17:59:53 +02:00
|
|
|
getLiveStatus(null, fetchingMissing, timelineParams, null);
|
2022-09-24 17:26:44 +02:00
|
|
|
} else {
|
|
|
|
initializeStatusesCommonView(statusesCached);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (direction == DIRECTION.BOTTOM) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelinesVM.getTimelineCache(timelineStatuses, timelineParams)
|
2022-09-24 17:26:44 +02:00
|
|
|
.observe(getViewLifecycleOwner(), statusesCachedBottom -> {
|
|
|
|
if (statusesCachedBottom == null || statusesCachedBottom.statuses == null || statusesCachedBottom.statuses.size() == 0) {
|
2022-09-29 17:59:53 +02:00
|
|
|
getLiveStatus(DIRECTION.BOTTOM, fetchingMissing, timelineParams, null);
|
2022-09-24 17:26:44 +02:00
|
|
|
} else {
|
|
|
|
dealWithPagination(statusesCachedBottom, DIRECTION.BOTTOM, fetchingMissing);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (direction == DIRECTION.TOP) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelinesVM.getTimelineCache(timelineStatuses, timelineParams)
|
2022-09-24 17:26:44 +02:00
|
|
|
.observe(getViewLifecycleOwner(), statusesCachedTop -> {
|
|
|
|
if (statusesCachedTop == null || statusesCachedTop.statuses == null || statusesCachedTop.statuses.size() == 0) {
|
2022-09-29 17:59:53 +02:00
|
|
|
getLiveStatus(DIRECTION.TOP, fetchingMissing, timelineParams, null);
|
2022-09-24 17:26:44 +02:00
|
|
|
} else {
|
|
|
|
dealWithPagination(statusesCachedTop, DIRECTION.TOP, fetchingMissing);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2022-10-01 17:00:03 +02:00
|
|
|
} else if (direction == DIRECTION.REFRESH) {
|
|
|
|
timelinesVM.getTimelineCache(timelineStatuses, timelineParams)
|
2022-09-24 17:26:44 +02:00
|
|
|
.observe(getViewLifecycleOwner(), statusesRefresh -> {
|
2022-09-26 17:09:28 +02:00
|
|
|
if (statusesRefresh == null || statusesRefresh.statuses == null || statusesRefresh.statuses.size() == 0) {
|
2022-10-01 17:00:03 +02:00
|
|
|
getLiveStatus(direction, fetchingMissing, timelineParams, null);
|
2022-09-24 17:26:44 +02:00
|
|
|
} else {
|
2022-09-26 17:09:28 +02:00
|
|
|
if (statusAdapter != null) {
|
|
|
|
dealWithPagination(statusesRefresh, direction, true);
|
|
|
|
} else {
|
|
|
|
initializeStatusesCommonView(statusesRefresh);
|
|
|
|
}
|
2022-09-24 17:26:44 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 17:59:53 +02:00
|
|
|
private void getLiveStatus(DIRECTION direction, boolean fetchingMissing, TimelinesVM.TimelineParams timelineParams, Status status) {
|
2022-10-21 16:33:57 +02:00
|
|
|
if (getView() == null) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-24 15:27:57 +02:00
|
|
|
if (direction == null) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelinesVM.getTimeline(timelineStatuses, timelineParams)
|
2022-10-21 16:33:57 +02:00
|
|
|
.observe(getViewLifecycleOwner(), statuses -> {
|
|
|
|
initialStatuses = statuses;
|
|
|
|
initializeStatusesCommonView(statuses);
|
|
|
|
});
|
2022-09-24 15:27:57 +02:00
|
|
|
} else if (direction == DIRECTION.BOTTOM) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelinesVM.getTimeline(timelineStatuses, timelineParams)
|
2022-09-29 17:59:53 +02:00
|
|
|
.observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM, fetchingMissing, status));
|
2022-09-24 15:27:57 +02:00
|
|
|
} else if (direction == DIRECTION.TOP) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelinesVM.getTimeline(timelineStatuses, timelineParams)
|
2022-09-29 17:59:53 +02:00
|
|
|
.observe(getViewLifecycleOwner(), statusesTop -> dealWithPagination(statusesTop, DIRECTION.TOP, fetchingMissing, status));
|
2022-10-01 17:00:03 +02:00
|
|
|
} else if (direction == DIRECTION.REFRESH || direction == DIRECTION.SCROLL_TOP || direction == DIRECTION.FETCH_NEW) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelinesVM.getTimeline(timelineStatuses, timelineParams)
|
2022-09-24 15:27:57 +02:00
|
|
|
.observe(getViewLifecycleOwner(), statusesRefresh -> {
|
|
|
|
if (statusAdapter != null) {
|
2022-09-29 17:59:53 +02:00
|
|
|
dealWithPagination(statusesRefresh, direction, true, status);
|
2022-09-24 15:27:57 +02:00
|
|
|
} else {
|
|
|
|
initializeStatusesCommonView(statusesRefresh);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 17:59:53 +02:00
|
|
|
|
2022-10-01 17:00:03 +02:00
|
|
|
public enum DIRECTION {
|
|
|
|
TOP,
|
|
|
|
BOTTOM,
|
|
|
|
REFRESH,
|
|
|
|
SCROLL_TOP,
|
|
|
|
FETCH_NEW
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
/**
|
|
|
|
* Router for timelines
|
|
|
|
*
|
|
|
|
* @param direction - DIRECTION null if first call, then is set to TOP or BOTTOM depending of scroll
|
|
|
|
*/
|
2022-06-02 07:40:49 +02:00
|
|
|
private void route(DIRECTION direction, boolean fetchingMissing) {
|
2022-09-29 17:59:53 +02:00
|
|
|
route(direction, fetchingMissing, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Router for timelines
|
|
|
|
*
|
|
|
|
* @param direction - DIRECTION null if first call, then is set to TOP or BOTTOM depending of scroll
|
|
|
|
*/
|
|
|
|
private void route(DIRECTION direction, boolean fetchingMissing, Status statusToUpdate) {
|
2022-09-25 11:23:08 +02:00
|
|
|
if (binding == null || getActivity() == null || !isAdded()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// --- HOME TIMELINE ---
|
|
|
|
if (timelineType == Timeline.TimeLineEnum.HOME) {
|
|
|
|
//for more visibility it's done through loadHomeStrategy method
|
2022-09-29 17:59:53 +02:00
|
|
|
routeCommon(direction, fetchingMissing, statusToUpdate);
|
2022-09-25 11:23:08 +02:00
|
|
|
} else if (timelineType == Timeline.TimeLineEnum.LOCAL) { //LOCAL TIMELINE
|
2022-09-29 17:59:53 +02:00
|
|
|
routeCommon(direction, fetchingMissing, statusToUpdate);
|
2022-09-25 11:23:08 +02:00
|
|
|
} else if (timelineType == Timeline.TimeLineEnum.PUBLIC) { //PUBLIC TIMELINE
|
2022-09-29 17:59:53 +02:00
|
|
|
routeCommon(direction, fetchingMissing, statusToUpdate);
|
2022-09-25 11:23:08 +02:00
|
|
|
} else if (timelineType == Timeline.TimeLineEnum.REMOTE) { //REMOTE TIMELINE
|
|
|
|
//NITTER TIMELINES
|
|
|
|
if (pinnedTimeline != null && pinnedTimeline.remoteInstance.type == RemoteInstance.InstanceType.NITTER) {
|
|
|
|
if (direction == null) {
|
|
|
|
timelinesVM.getNitter(pinnedTimeline.remoteInstance.host, null)
|
|
|
|
.observe(getViewLifecycleOwner(), this::initializeStatusesCommonView);
|
|
|
|
} else if (direction == DIRECTION.BOTTOM) {
|
|
|
|
timelinesVM.getNitter(pinnedTimeline.remoteInstance.host, max_id)
|
|
|
|
.observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM, false));
|
|
|
|
} else if (direction == DIRECTION.TOP) {
|
|
|
|
flagLoading = false;
|
|
|
|
} else if (direction == DIRECTION.REFRESH || direction == DIRECTION.SCROLL_TOP) {
|
|
|
|
timelinesVM.getNitter(pinnedTimeline.remoteInstance.host, null)
|
|
|
|
.observe(getViewLifecycleOwner(), statusesRefresh -> {
|
|
|
|
if (statusAdapter != null) {
|
|
|
|
dealWithPagination(statusesRefresh, direction, true);
|
|
|
|
} else {
|
|
|
|
initializeStatusesCommonView(statusesRefresh);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} //GNU TIMELINES
|
|
|
|
else if (pinnedTimeline != null && pinnedTimeline.remoteInstance.type == RemoteInstance.InstanceType.GNU) {
|
|
|
|
|
|
|
|
}//MISSKEY TIMELINES
|
|
|
|
else if (pinnedTimeline != null && pinnedTimeline.remoteInstance.type == RemoteInstance.InstanceType.MISSKEY) {
|
|
|
|
if (direction == null) {
|
|
|
|
timelinesVM.getMisskey(remoteInstance, null, MastodonHelper.statusesPerCall(requireActivity()))
|
|
|
|
.observe(getViewLifecycleOwner(), this::initializeStatusesCommonView);
|
|
|
|
} else if (direction == DIRECTION.BOTTOM) {
|
|
|
|
timelinesVM.getMisskey(remoteInstance, max_id, MastodonHelper.statusesPerCall(requireActivity()))
|
|
|
|
.observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM, false));
|
|
|
|
} else if (direction == DIRECTION.TOP) {
|
|
|
|
flagLoading = false;
|
|
|
|
} else if (direction == DIRECTION.REFRESH || direction == DIRECTION.SCROLL_TOP) {
|
|
|
|
timelinesVM.getMisskey(remoteInstance, null, MastodonHelper.statusesPerCall(requireActivity()))
|
|
|
|
.observe(getViewLifecycleOwner(), statusesRefresh -> {
|
|
|
|
if (statusAdapter != null) {
|
|
|
|
dealWithPagination(statusesRefresh, direction, true);
|
|
|
|
} else {
|
|
|
|
initializeStatusesCommonView(statusesRefresh);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} //PEERTUBE TIMELINES
|
|
|
|
else if (pinnedTimeline != null && pinnedTimeline.remoteInstance.type == RemoteInstance.InstanceType.PEERTUBE) {
|
|
|
|
if (direction == null) {
|
|
|
|
timelinesVM.getPeertube(remoteInstance, null, MastodonHelper.statusesPerCall(requireActivity()))
|
|
|
|
.observe(getViewLifecycleOwner(), this::initializeStatusesCommonView);
|
|
|
|
} else if (direction == DIRECTION.BOTTOM) {
|
2022-09-27 11:19:49 +02:00
|
|
|
timelinesVM.getPeertube(remoteInstance, String.valueOf(timelineStatuses.size()), MastodonHelper.statusesPerCall(requireActivity()))
|
2022-09-25 11:23:08 +02:00
|
|
|
.observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM, false));
|
|
|
|
} else if (direction == DIRECTION.TOP) {
|
|
|
|
flagLoading = false;
|
|
|
|
} else if (direction == DIRECTION.REFRESH || direction == DIRECTION.SCROLL_TOP) {
|
|
|
|
timelinesVM.getPeertube(remoteInstance, null, MastodonHelper.statusesPerCall(requireActivity()))
|
|
|
|
.observe(getViewLifecycleOwner(), statusesRefresh -> {
|
|
|
|
if (statusAdapter != null) {
|
|
|
|
dealWithPagination(statusesRefresh, direction, true);
|
|
|
|
} else {
|
|
|
|
initializeStatusesCommonView(statusesRefresh);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else { //Other remote timelines
|
2022-09-29 17:59:53 +02:00
|
|
|
routeCommon(direction, fetchingMissing, statusToUpdate);
|
2022-09-24 15:27:57 +02:00
|
|
|
}
|
2022-09-25 11:23:08 +02:00
|
|
|
} else if (timelineType == Timeline.TimeLineEnum.LIST) { //LIST TIMELINE
|
2022-09-29 17:59:53 +02:00
|
|
|
routeCommon(direction, fetchingMissing, statusToUpdate);
|
2022-09-25 11:23:08 +02:00
|
|
|
} else if (timelineType == Timeline.TimeLineEnum.TAG || timelineType == Timeline.TimeLineEnum.ART) { //TAG TIMELINE
|
2022-09-29 17:59:53 +02:00
|
|
|
routeCommon(direction, fetchingMissing, statusToUpdate);
|
2022-09-25 11:23:08 +02:00
|
|
|
} else if (timelineType == Timeline.TimeLineEnum.ACCOUNT_TIMELINE) { //PROFILE TIMELINES
|
|
|
|
if (direction == null) {
|
|
|
|
if (show_pinned) {
|
|
|
|
//Fetch pinned statuses to display them at the top
|
|
|
|
accountsVM.getAccountStatuses(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, null, null, null, null, null, false, true, MastodonHelper.statusesPerCall(requireActivity()))
|
|
|
|
.observe(getViewLifecycleOwner(), pinnedStatuses -> accountsVM.getAccountStatuses(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, null, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity()))
|
|
|
|
.observe(getViewLifecycleOwner(), otherStatuses -> {
|
|
|
|
if (otherStatuses != null && otherStatuses.statuses != null && pinnedStatuses != null && pinnedStatuses.statuses != null) {
|
|
|
|
for (Status status : pinnedStatuses.statuses) {
|
|
|
|
status.pinned = true;
|
2022-06-27 18:16:41 +02:00
|
|
|
}
|
2022-09-25 11:23:08 +02:00
|
|
|
otherStatuses.statuses.addAll(0, pinnedStatuses.statuses);
|
|
|
|
initializeStatusesCommonView(otherStatuses);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
accountsVM.getAccountStatuses(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, null, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity()))
|
|
|
|
.observe(getViewLifecycleOwner(), this::initializeStatusesCommonView);
|
|
|
|
}
|
|
|
|
} else if (direction == DIRECTION.BOTTOM) {
|
|
|
|
accountsVM.getAccountStatuses(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, max_id, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity()))
|
|
|
|
.observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM, false));
|
|
|
|
} else {
|
|
|
|
flagLoading = false;
|
|
|
|
}
|
|
|
|
} else if (search != null) {
|
|
|
|
SearchVM searchVM = new ViewModelProvider(FragmentMastodonTimeline.this).get(viewModelKey, SearchVM.class);
|
|
|
|
searchVM.search(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, search.trim(), null, null, false, true, false, 0, null, null, MastodonHelper.STATUSES_PER_CALL)
|
|
|
|
.observe(getViewLifecycleOwner(), results -> {
|
|
|
|
if (results != null) {
|
|
|
|
Statuses statuses = new Statuses();
|
|
|
|
statuses.statuses = results.statuses;
|
|
|
|
statuses.pagination = new Pagination();
|
|
|
|
initializeStatusesCommonView(statuses);
|
2022-05-06 19:18:53 +02:00
|
|
|
} else {
|
2022-09-25 11:23:08 +02:00
|
|
|
Toasty.error(requireActivity(), getString(R.string.toast_error), Toasty.LENGTH_LONG).show();
|
2022-05-06 19:18:53 +02:00
|
|
|
}
|
2022-09-25 11:23:08 +02:00
|
|
|
});
|
|
|
|
} else if (searchCache != null) {
|
|
|
|
SearchVM searchVM = new ViewModelProvider(FragmentMastodonTimeline.this).get(viewModelKey, SearchVM.class);
|
|
|
|
searchVM.searchCache(BaseMainActivity.currentInstance, BaseMainActivity.currentUserID, searchCache.trim())
|
|
|
|
.observe(getViewLifecycleOwner(), results -> {
|
|
|
|
if (results != null) {
|
|
|
|
Statuses statuses = new Statuses();
|
|
|
|
statuses.statuses = results.statuses;
|
|
|
|
statuses.pagination = new Pagination();
|
|
|
|
initializeStatusesCommonView(statuses);
|
2022-05-06 19:18:53 +02:00
|
|
|
} else {
|
2022-09-25 11:23:08 +02:00
|
|
|
Toasty.error(requireActivity(), getString(R.string.toast_error), Toasty.LENGTH_LONG).show();
|
2022-05-06 19:18:53 +02:00
|
|
|
}
|
2022-09-25 11:23:08 +02:00
|
|
|
});
|
|
|
|
} else if (timelineType == Timeline.TimeLineEnum.FAVOURITE_TIMELINE) {
|
|
|
|
if (direction == null) {
|
|
|
|
accountsVM.getFavourites(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, String.valueOf(MastodonHelper.statusesPerCall(requireActivity())), null, null)
|
|
|
|
.observe(getViewLifecycleOwner(), this::initializeStatusesCommonView);
|
|
|
|
} else if (direction == DIRECTION.BOTTOM) {
|
|
|
|
accountsVM.getFavourites(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, String.valueOf(MastodonHelper.statusesPerCall(requireActivity())), null, max_id)
|
|
|
|
.observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM, false));
|
|
|
|
} else {
|
|
|
|
flagLoading = false;
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
2022-09-25 11:23:08 +02:00
|
|
|
} else if (timelineType == Timeline.TimeLineEnum.BOOKMARK_TIMELINE) {
|
|
|
|
if (direction == null) {
|
|
|
|
accountsVM.getBookmarks(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, String.valueOf(MastodonHelper.statusesPerCall(requireActivity())), null, null, null)
|
|
|
|
.observe(getViewLifecycleOwner(), this::initializeStatusesCommonView);
|
|
|
|
} else if (direction == DIRECTION.BOTTOM) {
|
|
|
|
accountsVM.getBookmarks(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, String.valueOf(MastodonHelper.statusesPerCall(requireActivity())), max_id, null, null)
|
|
|
|
.observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM, false));
|
|
|
|
} else {
|
|
|
|
flagLoading = false;
|
|
|
|
}
|
|
|
|
} else if (timelineType == Timeline.TimeLineEnum.TREND_MESSAGE) {
|
|
|
|
if (direction == null) {
|
|
|
|
timelinesVM.getStatusTrends(BaseMainActivity.currentToken, BaseMainActivity.currentInstance)
|
|
|
|
.observe(getViewLifecycleOwner(), statusesTrends -> {
|
|
|
|
Statuses statuses = new Statuses();
|
|
|
|
statuses.statuses = new ArrayList<>();
|
|
|
|
if (statusesTrends != null) {
|
|
|
|
statuses.statuses.addAll(statusesTrends);
|
|
|
|
}
|
|
|
|
statuses.pagination = new Pagination();
|
|
|
|
initializeStatusesCommonView(statuses);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-05-06 19:18:53 +02:00
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
2022-05-10 17:21:22 +02:00
|
|
|
|
2022-09-29 17:59:53 +02:00
|
|
|
|
|
|
|
|
2022-05-10 17:21:22 +02:00
|
|
|
/**
|
|
|
|
* Refresh status in list
|
|
|
|
*/
|
|
|
|
public void refreshAllAdapters() {
|
2022-09-27 11:19:49 +02:00
|
|
|
if (statusAdapter != null && timelineStatuses != null) {
|
|
|
|
statusAdapter.notifyItemRangeChanged(0, timelineStatuses.size());
|
2022-05-10 17:21:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 07:40:49 +02:00
|
|
|
@Override
|
2022-09-29 17:59:53 +02:00
|
|
|
public void onClickMinId(String min_id, Status statusToUpdate) {
|
2022-06-02 07:40:49 +02:00
|
|
|
//Fetch more has been pressed
|
|
|
|
min_id_fetch_more = min_id;
|
2022-09-29 17:59:53 +02:00
|
|
|
route(DIRECTION.TOP, true, statusToUpdate);
|
2022-06-02 07:40:49 +02:00
|
|
|
}
|
|
|
|
|
2022-07-08 19:02:18 +02:00
|
|
|
@Override
|
2022-09-29 17:59:53 +02:00
|
|
|
public void onClickMaxId(String max_id, Status statusToUpdate) {
|
2022-07-08 19:02:18 +02:00
|
|
|
max_id_fetch_more = max_id;
|
2022-09-29 17:59:53 +02:00
|
|
|
route(DIRECTION.BOTTOM, true, statusToUpdate);
|
2022-07-08 19:02:18 +02:00
|
|
|
}
|
|
|
|
|
2022-10-01 17:00:03 +02:00
|
|
|
public interface UpdateCounters {
|
|
|
|
void onUpdate(int count, Timeline.TimeLineEnum type, String slug);
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
}
|