fedilab-Android-App/app/src/main/java/app/fedilab/android/fragments/DisplayStatusFragment.java

1332 lines
65 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-05-05 16:36:04 +02:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-05-05 16:36:04 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-05-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
2018-11-17 14:31:03 +01:00
2018-10-20 14:39:24 +02:00
import android.app.Activity;
2019-01-10 11:51:25 +01:00
import android.content.BroadcastReceiver;
2017-05-05 16:36:04 +02:00
import android.content.Context;
2017-08-26 19:39:11 +02:00
import android.content.Intent;
2019-01-10 11:51:25 +01:00
import android.content.IntentFilter;
2017-05-05 16:36:04 +02:00
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
2017-05-05 16:36:04 +02:00
import android.os.Bundle;
2017-11-03 10:18:47 +01:00
import android.os.Handler;
import android.os.Looper;
2017-05-05 16:36:04 +02:00
import android.view.LayoutInflater;
2020-03-10 19:00:32 +01:00
import android.view.MenuItem;
2017-05-05 16:36:04 +02:00
import android.view.View;
import android.view.ViewGroup;
2019-11-13 15:59:50 +01:00
import android.view.inputmethod.InputMethodManager;
2017-05-05 16:36:04 +02:00
import android.widget.RelativeLayout;
2019-01-12 17:55:18 +01:00
import android.widget.TextView;
import android.widget.Toast;
2018-11-17 14:31:03 +01:00
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
2020-02-09 10:43:26 +01:00
import androidx.annotation.Nullable;
2019-11-15 16:32:25 +01:00
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
2020-02-09 10:43:26 +01:00
import com.bumptech.glide.Glide;
import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import com.bumptech.glide.util.FixedPreloadSizeProvider;
2020-03-10 19:00:32 +01:00
import com.google.android.material.navigation.NavigationView;
2020-02-09 10:43:26 +01:00
2020-03-07 08:14:15 +01:00
import org.jetbrains.annotations.NotNull;
2017-05-05 16:36:04 +02:00
import java.util.ArrayList;
2020-02-09 10:43:26 +01:00
import java.util.Collections;
2019-01-22 08:51:36 +01:00
import java.util.Date;
2018-11-21 18:04:10 +01:00
import java.util.Iterator;
2017-05-05 16:36:04 +02:00
import java.util.List;
2017-09-13 08:47:59 +02:00
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.activities.BaseMainActivity;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.asynctasks.ManageListsAsyncTask;
2020-03-10 19:00:32 +01:00
import app.fedilab.android.asynctasks.PostActionAsyncTask;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.asynctasks.RetrieveFeedsAfterBookmarkAsyncTask;
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
import app.fedilab.android.asynctasks.RetrieveMissingFeedsAsyncTask;
import app.fedilab.android.asynctasks.RetrievePeertubeSearchAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
2020-03-10 19:00:32 +01:00
import app.fedilab.android.client.API;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
2020-03-10 19:00:32 +01:00
import app.fedilab.android.client.Entities.Announcement;
2020-02-09 10:43:26 +01:00
import app.fedilab.android.client.Entities.Attachment;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Conversation;
2020-03-10 19:00:32 +01:00
import app.fedilab.android.client.Entities.Error;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Peertube;
import app.fedilab.android.client.Entities.RemoteInstance;
2019-05-25 12:37:50 +02:00
import app.fedilab.android.client.Entities.RetrieveFeedsParam;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Status;
2020-03-07 08:46:48 +01:00
import app.fedilab.android.client.Entities.StatusDrawerParams;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.TagTimeline;
import app.fedilab.android.drawers.ArtListAdapter;
import app.fedilab.android.drawers.PeertubeAdapter;
import app.fedilab.android.drawers.PixelfedListAdapter;
import app.fedilab.android.drawers.StatusListAdapter;
import app.fedilab.android.helper.Helper;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.interfaces.OnListActionInterface;
2020-03-10 19:00:32 +01:00
import app.fedilab.android.interfaces.OnPostActionInterface;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.interfaces.OnRetrieveFeedsAfterBookmarkInterface;
import app.fedilab.android.interfaces.OnRetrieveFeedsInterface;
import app.fedilab.android.interfaces.OnRetrieveMissingFeedsInterface;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.services.StreamingFederatedTimelineService;
import app.fedilab.android.services.StreamingLocalTimelineService;
import app.fedilab.android.sqlite.InstancesDAO;
import app.fedilab.android.sqlite.SearchDAO;
import app.fedilab.android.sqlite.Sqlite;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2017-05-05 16:36:04 +02:00
2021-02-28 11:46:34 +01:00
import static app.fedilab.android.helper.BaseHelper.NOTIF_PUSH;
import static app.fedilab.android.helper.BaseHelper.liveNotifType;
2017-05-05 16:36:04 +02:00
/**
* Created by Thomas on 24/04/2017.
* Fragment to display content related to status
*/
2020-03-10 19:00:32 +01:00
public class DisplayStatusFragment extends Fragment implements OnPostActionInterface, OnRetrieveFeedsInterface, OnRetrieveMissingFeedsInterface, OnRetrieveFeedsAfterBookmarkInterface, OnListActionInterface {
2017-05-05 16:36:04 +02:00
2020-03-07 08:14:15 +01:00
private LinearLayoutManager mLayoutManager;
2017-05-05 16:36:04 +02:00
private boolean flag_loading;
private Context context;
private StatusListAdapter statusListAdapter;
2018-10-06 15:59:00 +02:00
private PeertubeAdapter peertubeAdapater;
2019-01-14 18:16:45 +01:00
private ArtListAdapter artListAdapter;
private PixelfedListAdapter pixelfedListAdapter;
2017-05-05 16:36:04 +02:00
private String max_id;
private List<Status> statuses;
2018-10-06 15:59:00 +02:00
private List<Peertube> peertubes;
private RetrieveFeedsAsyncTask.Type type;
2017-05-05 16:36:04 +02:00
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout;
private String targetedId;
private String tag;
2017-10-14 14:36:48 +02:00
private RecyclerView lv_status;
2018-09-05 10:19:07 +02:00
private boolean showMediaOnly, showPinned, showReply;
2019-05-12 09:21:42 +02:00
private Intent streamingFederatedIntent, streamingLocalIntent;
2019-08-18 17:17:47 +02:00
private boolean firstTootsLoaded;
2017-12-28 17:25:36 +01:00
private String userId, instance;
2017-12-28 15:33:02 +01:00
private SharedPreferences sharedpreferences;
2018-04-25 14:03:15 +02:00
private boolean isSwipped;
2018-08-20 19:00:20 +02:00
private String remoteInstance;
2018-10-06 15:59:00 +02:00
private String instanceType;
2018-10-22 19:19:39 +02:00
private String search_peertube, remote_channel_name;
2018-11-25 16:29:38 +01:00
private String initialBookMark;
2018-12-15 17:13:06 +01:00
private TagTimeline tagTimeline;
2018-12-19 15:12:46 +01:00
private String updatedBookMark;
private String lastReadToot;
2019-01-12 17:55:18 +01:00
private TextView textviewNoActionText;
2019-01-03 18:59:44 +01:00
private boolean ischannel;
2019-01-07 19:16:15 +01:00
private boolean ownVideos;
2019-01-10 11:51:25 +01:00
private BroadcastReceiver receive_action;
2019-09-06 17:55:14 +02:00
private BroadcastReceiver receive_data;
2019-01-22 08:51:36 +01:00
private Date lastReadTootDate, initialBookMarkDate, updatedBookMarkDate;
2019-04-26 15:50:26 +02:00
private int timelineId;
2019-05-25 12:37:50 +02:00
private String currentfilter;
2020-04-08 12:42:15 +02:00
private View rootView;
2019-09-06 17:55:14 +02:00
public DisplayStatusFragment() {
2017-08-24 15:57:35 +02:00
}
2017-05-05 16:36:04 +02:00
2019-12-15 18:38:24 +01:00
2017-05-05 16:36:04 +02:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2020-04-08 12:42:15 +02:00
rootView = inflater.inflate(R.layout.fragment_status, container, false);
2017-05-26 17:20:36 +02:00
statuses = new ArrayList<>();
2018-10-06 15:59:00 +02:00
peertubes = new ArrayList<>();
2017-05-05 16:36:04 +02:00
context = getContext();
Bundle bundle = this.getArguments();
showMediaOnly = false;
2017-12-09 07:34:36 +01:00
//Will allow to load first toots if bookmark != null
2018-12-19 15:12:46 +01:00
firstTootsLoaded = false;
showPinned = false;
2018-09-05 10:19:07 +02:00
showReply = false;
2018-12-15 17:13:06 +01:00
tagTimeline = null;
2017-05-05 16:36:04 +02:00
if (bundle != null) {
type = (RetrieveFeedsAsyncTask.Type) bundle.get("type");
2019-01-03 18:59:44 +01:00
targetedId = bundle.getString("targetedid", null);
2019-01-07 19:16:15 +01:00
ownVideos = bundle.getBoolean("ownvideos", false); //Peetube account watching its videos
2017-05-05 16:36:04 +02:00
tag = bundle.getString("tag", null);
2019-09-06 17:55:14 +02:00
showMediaOnly = bundle.getBoolean("showMediaOnly", false);
showPinned = bundle.getBoolean("showPinned", false);
showReply = bundle.getBoolean("showReply", false);
2018-08-20 19:00:20 +02:00
remoteInstance = bundle.getString("remote_instance", "");
2018-10-20 12:22:35 +02:00
search_peertube = bundle.getString("search_peertube", null);
2018-10-22 19:19:39 +02:00
remote_channel_name = bundle.getString("remote_channel_name", null);
2019-09-06 17:55:14 +02:00
instanceType = bundle.getString("instanceType", "MASTODON");
ischannel = bundle.getBoolean("ischannel", false);
2019-04-26 15:50:26 +02:00
timelineId = bundle.getInt("timelineId");
2019-05-25 12:37:50 +02:00
currentfilter = bundle.getString("currentfilter", null);
2017-05-05 16:36:04 +02:00
}
2020-04-25 19:20:42 +02:00
if (ischannel) {
2019-01-03 18:59:44 +01:00
type = RetrieveFeedsAsyncTask.Type.CHANNEL;
2020-04-25 19:20:42 +02:00
}
2019-01-03 18:59:44 +01:00
assert context != null;
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-01-03 14:23:13 +01:00
//instanceType should not be null only for Peertube accounts
2019-04-21 18:50:56 +02:00
2019-09-06 17:55:14 +02:00
if (remoteInstance != null && !remoteInstance.equals("") && instanceType == null) {
2018-10-06 15:59:00 +02:00
List<RemoteInstance> remoteInstanceObj = new InstancesDAO(context, db).getInstanceByName(remoteInstance);
2019-09-06 17:55:14 +02:00
if (remoteInstanceObj != null && remoteInstanceObj.size() > 0)
2018-10-06 15:59:00 +02:00
instanceType = remoteInstanceObj.get(0).getType();
}
2018-04-25 14:03:15 +02:00
isSwipped = false;
2017-05-05 16:36:04 +02:00
max_id = null;
flag_loading = true;
firstLoad = true;
2018-12-19 15:12:46 +01:00
initialBookMark = null;
assert context != null;
2017-12-28 15:33:02 +01:00
sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean isOnWifi = Helper.isOnWIFI(context);
2017-10-21 17:15:52 +02:00
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
2019-11-12 21:27:02 +01:00
int c1 = getResources().getColor(R.color.cyanea_accent);
int c2 = getResources().getColor(R.color.cyanea_primary_dark);
int c3 = getResources().getColor(R.color.cyanea_primary);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(c3);
2019-11-12 19:08:05 +01:00
swipeRefreshLayout.setColorSchemeColors(
2019-11-12 21:27:02 +01:00
c1, c2, c1
2019-11-12 19:08:05 +01:00
);
2017-10-21 17:15:52 +02:00
lv_status = rootView.findViewById(R.id.lv_status);
2020-02-09 10:43:26 +01:00
ListPreloader.PreloadSizeProvider sizeProvider =
new FixedPreloadSizeProvider(640, 480);
ListPreloader.PreloadModelProvider modelProvider = new MyPreloadModelProvider();
2020-03-07 08:14:15 +01:00
RecyclerViewPreloader preloader =
2020-03-08 10:29:06 +01:00
new RecyclerViewPreloader<>(Glide.with(context), modelProvider, sizeProvider, 20);
2020-02-09 10:43:26 +01:00
lv_status.addOnScrollListener(preloader);
2020-02-01 10:54:45 +01:00
2019-09-06 17:55:14 +02:00
mainLoader = rootView.findViewById(R.id.loader);
2017-10-21 17:15:52 +02:00
nextElementLoader = rootView.findViewById(R.id.loading_next_status);
2019-09-06 17:55:14 +02:00
textviewNoAction = rootView.findViewById(R.id.no_action);
2019-01-12 17:55:18 +01:00
textviewNoActionText = rootView.findViewById(R.id.no_action_text);
2017-05-05 16:36:04 +02:00
mainLoader.setVisibility(View.VISIBLE);
nextElementLoader.setVisibility(View.GONE);
2018-10-20 16:26:45 +02:00
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2019-09-06 17:55:14 +02:00
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, context != null ? Helper.getLiveInstance(context) : null);
2019-08-18 17:17:47 +02:00
2018-09-26 13:45:13 +02:00
2018-12-19 15:12:46 +01:00
//For Home timeline, fetch stored values for bookmark and last read toot
2019-09-29 18:18:20 +02:00
if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) {
2018-12-19 15:12:46 +01:00
initialBookMark = sharedpreferences.getString(Helper.BOOKMARK_ID + userId + instance, null);
2019-09-06 17:55:14 +02:00
initialBookMarkDate = Helper.stringToDate(context, sharedpreferences.getString(Helper.BOOKMARK_DATE + userId + instance, null));
2018-12-19 15:12:46 +01:00
lastReadToot = sharedpreferences.getString(Helper.LAST_READ_TOOT_ID + userId + instance, null);
2019-09-06 17:55:14 +02:00
lastReadTootDate = Helper.stringToDate(context, sharedpreferences.getString(Helper.LAST_READ_TOOT_DATE + userId + instance, null));
2018-12-19 15:12:46 +01:00
}
2019-12-08 16:54:47 +01:00
if (instanceType == null || instanceType.equals("MASTODON") || instanceType.equals("MISSKEY") || instanceType.equals("GNU") || instanceType.equals("NITTER")) {
2019-09-06 17:55:14 +02:00
if (type == RetrieveFeedsAsyncTask.Type.TAG && tag != null) {
2019-01-14 18:16:45 +01:00
BaseMainActivity.displayPeertube = null;
List<TagTimeline> tagTimelines = new SearchDAO(context, db).getTimelineInfo(tag);
2019-09-06 17:55:14 +02:00
if (tagTimelines != null && tagTimelines.size() > 0) {
2019-01-14 18:16:45 +01:00
tagTimeline = tagTimelines.get(0);
2020-03-07 08:46:48 +01:00
StatusDrawerParams statusDrawerParams = new StatusDrawerParams();
statusDrawerParams.setTagTimeline(tagTimelines.get(0));
statusDrawerParams.setTargetedId(targetedId);
statusDrawerParams.setOnWifi(isOnWifi);
2020-03-07 12:10:52 +01:00
statusDrawerParams.setType(type);
2020-03-07 08:46:48 +01:00
statusDrawerParams.setStatuses(this.statuses);
statusListAdapter = new StatusListAdapter(statusDrawerParams);
2019-01-14 18:16:45 +01:00
lv_status.setAdapter(statusListAdapter);
}
2019-09-06 17:55:14 +02:00
} else {
2019-01-14 18:16:45 +01:00
BaseMainActivity.displayPeertube = null;
2020-03-07 08:46:48 +01:00
StatusDrawerParams statusDrawerParams = new StatusDrawerParams();
statusDrawerParams.setType(type);
statusDrawerParams.setInstanceType(instanceType);
statusDrawerParams.setTargetedId(targetedId);
statusDrawerParams.setOnWifi(isOnWifi);
statusDrawerParams.setStatuses(this.statuses);
2020-03-07 12:10:52 +01:00
statusDrawerParams.setType(type);
2020-03-07 08:46:48 +01:00
statusListAdapter = new StatusListAdapter(statusDrawerParams);
2018-12-15 17:13:06 +01:00
lv_status.setAdapter(statusListAdapter);
}
2019-09-06 17:55:14 +02:00
} else if (instanceType.equals("PEERTUBE")) {
if (remoteInstance != null && MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE) //if it's a Peertube account connected
2019-08-19 12:06:21 +02:00
remoteInstance = instance;
2018-10-20 14:39:24 +02:00
BaseMainActivity.displayPeertube = remoteInstance;
2019-08-19 09:11:23 +02:00
peertubeAdapater = new PeertubeAdapter(remoteInstance, ownVideos, this.peertubes);
2018-10-06 15:59:00 +02:00
lv_status.setAdapter(peertubeAdapater);
2019-09-06 17:55:14 +02:00
} else if (instanceType.equals("PIXELFED")) {
if (remoteInstance != null && MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) //if it's a Peertube account connected
2019-08-19 12:06:21 +02:00
remoteInstance = instance;
2021-01-29 18:02:32 +01:00
StatusDrawerParams statusDrawerParams = new StatusDrawerParams();
statusDrawerParams.setOnWifi(isOnWifi);
statusDrawerParams.setStatuses(this.statuses);
statusDrawerParams.setType(type);
pixelfedListAdapter = new PixelfedListAdapter(statusDrawerParams);
2019-01-14 18:16:45 +01:00
lv_status.setAdapter(pixelfedListAdapter);
2019-09-06 17:55:14 +02:00
} else if (instanceType.equals("ART")) {
if (type == RetrieveFeedsAsyncTask.Type.TAG) {
2019-01-24 09:57:55 +01:00
List<TagTimeline> tagTimelines = new SearchDAO(context, db).getTimelineInfo(tag);
if (tagTimelines != null && tagTimelines.size() > 0) {
tagTimeline = tagTimelines.get(0);
}
2019-01-24 09:54:26 +01:00
}
2019-08-19 09:11:23 +02:00
artListAdapter = new ArtListAdapter(this.statuses);
2019-01-14 18:16:45 +01:00
lv_status.setAdapter(artListAdapter);
2018-10-06 15:59:00 +02:00
}
2017-10-24 14:06:28 +02:00
mLayoutManager = new LinearLayoutManager(context);
2020-03-07 12:10:52 +01:00
2017-10-24 14:06:28 +02:00
lv_status.setLayoutManager(mLayoutManager);
2018-01-05 09:46:59 +01:00
2018-01-10 14:39:01 +01:00
2019-01-14 18:16:45 +01:00
//Manage broadcast receiver for Mastodon timelines
2019-09-06 17:55:14 +02:00
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.GNU || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
if (receive_action != null)
2019-01-10 11:51:25 +01:00
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_action);
receive_action = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
assert b != null;
Status status = b.getParcelable("status");
2019-07-09 18:08:04 +02:00
String delete_statuses_from = b.getString("delete_statuses_for_id", null);
2019-09-06 17:55:14 +02:00
if (status != null && statusListAdapter != null) {
2019-11-22 19:33:32 +01:00
statusListAdapter.notifyStatusChanged(status);
2019-09-06 17:55:14 +02:00
} else if (delete_statuses_from != null) {
2019-07-09 18:08:04 +02:00
List<Status> statusesToRemove = new ArrayList<>();
2019-09-06 17:55:14 +02:00
for (Status status_temp : statuses) {
if (status_temp.getAccount().getId().equals(delete_statuses_from))
2019-07-09 18:08:04 +02:00
statusesToRemove.add(status);
}
statuses.removeAll(statusesToRemove);
statusListAdapter.notifyDataSetChanged();
2019-01-10 11:51:25 +01:00
}
}
};
LocalBroadcastManager.getInstance(context).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_ACTION));
2019-01-11 16:39:01 +01:00
2019-09-29 18:18:20 +02:00
if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME || type == RetrieveFeedsAsyncTask.Type.LOCAL || type == RetrieveFeedsAsyncTask.Type.PUBLIC) {
2019-01-11 16:39:01 +01:00
if (receive_data != null)
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_data);
receive_data = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
assert b != null;
String userIdService = b.getString("userIdService", null);
if (userIdService != null && userIdService.equals(userId)) {
Status status = b.getParcelable("data");
refresh(status);
}
}
};
}
2019-09-06 17:55:14 +02:00
if (type == RetrieveFeedsAsyncTask.Type.PUBLIC)
2019-01-11 16:39:01 +01:00
LocalBroadcastManager.getInstance(context).registerReceiver(receive_data, new IntentFilter(Helper.RECEIVE_FEDERATED_DATA));
2019-09-29 18:18:20 +02:00
else if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME)
2019-01-11 16:39:01 +01:00
LocalBroadcastManager.getInstance(context).registerReceiver(receive_data, new IntentFilter(Helper.RECEIVE_HOME_DATA));
2019-09-06 17:55:14 +02:00
else if (type == RetrieveFeedsAsyncTask.Type.LOCAL)
2019-01-11 16:39:01 +01:00
LocalBroadcastManager.getInstance(context).registerReceiver(receive_data, new IntentFilter(Helper.RECEIVE_LOCAL_DATA));
2019-01-10 11:51:25 +01:00
}
2017-05-26 17:20:36 +02:00
2019-09-06 17:55:14 +02:00
if (type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE && search_peertube != null)
((Activity) context).setTitle(remoteInstance + " - " + search_peertube);
if (type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE && remote_channel_name != null)
((Activity) context).setTitle(remote_channel_name + " - " + remoteInstance);
if (type != RetrieveFeedsAsyncTask.Type.POVERVIEW) //No paginations for Peertube Overviews (it's a fixed size content
2019-01-14 18:16:45 +01:00
lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() {
2019-09-06 17:55:14 +02:00
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (type != RetrieveFeedsAsyncTask.Type.ART && context instanceof BaseMainActivity) {
if (dy < 0 && !((BaseMainActivity) context).getFloatingVisibility())
2019-01-14 18:16:45 +01:00
((BaseMainActivity) context).manageFloatingButton(true);
2019-09-06 17:55:14 +02:00
if (dy > 0 && ((BaseMainActivity) context).getFloatingVisibility())
2019-01-14 18:16:45 +01:00
((BaseMainActivity) context).manageFloatingButton(false);
}
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
2019-09-06 17:55:14 +02:00
if (dy > 0) {
2019-01-14 18:16:45 +01:00
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
2019-09-06 17:55:14 +02:00
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
if (!flag_loading) {
2019-01-14 18:16:45 +01:00
flag_loading = true;
manageAsyncTask(true);
nextElementLoader.setVisibility(View.VISIBLE);
}
2019-01-14 18:16:45 +01:00
} else {
nextElementLoader.setVisibility(View.GONE);
2017-11-03 10:18:47 +01:00
}
2019-01-14 18:16:45 +01:00
}
2019-11-15 16:32:25 +01:00
if ((type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) && statuses != null && statuses.size() > firstVisibleItem && firstVisibleItem >= 0) {
2019-01-22 08:51:36 +01:00
Date bookmarkL = statuses.get(firstVisibleItem).getCreated_at();
updatedBookMark = statuses.get(firstVisibleItem).getId();
updatedBookMarkDate = statuses.get(firstVisibleItem).getCreated_at();
2019-09-06 17:55:14 +02:00
if (lastReadTootDate == null || (bookmarkL != null && bookmarkL.after(lastReadTootDate))) {
//Last read toot, only incremented if the id of the toot is greater than the recorded one
2019-01-22 08:51:36 +01:00
lastReadTootDate = bookmarkL;
2019-01-30 17:04:20 +01:00
}
2017-11-03 10:18:47 +01:00
}
2017-12-09 07:42:46 +01:00
}
2019-01-14 18:16:45 +01:00
});
2020-02-01 10:40:41 +01:00
if (instanceType == null || (!instanceType.equals("PEERTUBE") && !instanceType.equals("NITTER"))) {
2020-03-07 08:14:15 +01:00
swipeRefreshLayout.setOnRefreshListener(() -> {
if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME)
MainActivity.countNewStatus = 0;
isSwipped = true;
if (type != RetrieveFeedsAsyncTask.Type.CONVERSATION)
retrieveMissingToots(null);
else {
if (statuses.size() > 0)
retrieveMissingToots(statuses.get(0).getId());
else
2018-11-22 17:53:09 +01:00
retrieveMissingToots(null);
2018-03-16 18:52:43 +01:00
}
2020-03-07 08:14:15 +01:00
2018-10-07 14:48:51 +02:00
});
2020-03-08 10:29:06 +01:00
} else if (instanceType.equals("PEERTUBE")) {
2020-03-07 08:14:15 +01:00
swipeRefreshLayout.setOnRefreshListener(() -> {
if (peertubes.size() > 0) {
int size = peertubes.size();
isSwipped = true;
peertubes.clear();
peertubes = new ArrayList<>();
max_id = "0";
peertubeAdapater.notifyItemRangeRemoved(0, size);
if (search_peertube == null) { //Not a Peertube search
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, remoteInstance, "0", DisplayStatusFragment.this);
2020-03-07 08:14:15 +01:00
} else {
2021-01-19 17:43:51 +01:00
new RetrievePeertubeSearchAsyncTask(context, remoteInstance, search_peertube, DisplayStatusFragment.this);
2018-10-07 14:48:51 +02:00
}
}
});
2020-03-08 10:29:06 +01:00
} else {
2020-03-07 08:14:15 +01:00
swipeRefreshLayout.setOnRefreshListener(() -> {
if (statuses.size() > 0) {
int size = statuses.size();
isSwipped = true;
statuses.clear();
statuses = new ArrayList<>();
max_id = null;
statusListAdapter.notifyItemRangeRemoved(0, size);
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this);
2020-02-01 10:40:41 +01:00
}
});
2019-06-25 18:29:08 +02:00
}
2019-09-06 17:55:14 +02:00
if (context != null) {
2019-01-14 18:16:45 +01:00
//Load data depending of the value
manageAsyncTask(false);
2019-09-06 17:55:14 +02:00
} else {
2020-03-07 08:14:15 +01:00
new Handler(Looper.getMainLooper()).postDelayed(() -> {
if (context != null) {
manageAsyncTask(false);
}
}, 500);
2017-05-26 17:20:36 +02:00
}
2017-05-05 16:36:04 +02:00
return rootView;
}
2018-12-19 15:12:46 +01:00
@Override
2019-09-06 17:55:14 +02:00
public void onPause() {
2018-12-19 15:12:46 +01:00
super.onPause();
2019-09-06 17:55:14 +02:00
if (swipeRefreshLayout != null) {
2019-08-18 12:56:24 +02:00
swipeRefreshLayout.setEnabled(false);
swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.clearAnimation();
}
2018-12-19 15:12:46 +01:00
//Store bookmark on pause
2019-11-15 16:32:25 +01:00
if (context instanceof BaseMainActivity && (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME)) {
2018-12-19 15:12:46 +01:00
SharedPreferences.Editor editor = sharedpreferences.edit();
2019-09-06 17:55:14 +02:00
if (updatedBookMark != null)
2018-12-19 15:12:46 +01:00
editor.putString(Helper.BOOKMARK_ID + userId + instance, updatedBookMark);
2019-09-06 17:55:14 +02:00
if (updatedBookMarkDate != null)
2019-01-22 08:51:36 +01:00
editor.putString(Helper.BOOKMARK_DATE + userId + instance, Helper.dateToString(updatedBookMarkDate));
2019-09-06 17:55:14 +02:00
if (lastReadToot != null)
2018-12-19 15:12:46 +01:00
editor.putString(Helper.LAST_READ_TOOT_ID + userId + instance, lastReadToot);
2019-09-06 17:55:14 +02:00
if (lastReadTootDate != null)
2019-01-30 17:04:20 +01:00
editor.putString(Helper.LAST_READ_TOOT_DATE + userId + instance, Helper.dateToString(lastReadTootDate));
editor.apply();
2018-12-19 15:12:46 +01:00
}
2019-11-15 16:32:25 +01:00
if (getActivity() != null) {
2019-11-13 15:59:50 +01:00
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
2019-11-15 16:32:25 +01:00
if (imm != null && getView() != null) {
2019-11-13 15:59:50 +01:00
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}
}
2018-12-19 15:12:46 +01:00
}
2017-05-05 16:36:04 +02:00
@Override
2019-09-06 17:55:14 +02:00
public void onCreate(Bundle saveInstance) {
2017-05-05 16:36:04 +02:00
super.onCreate(saveInstance);
}
@Override
2020-03-07 08:14:15 +01:00
public void onAttach(@NotNull Context context) {
2017-05-05 16:36:04 +02:00
super.onAttach(context);
this.context = context;
}
2019-08-17 11:10:31 +02:00
@Override
2019-09-06 17:55:14 +02:00
public void onStop() {
2019-08-17 11:10:31 +02:00
super.onStop();
2019-09-06 17:55:14 +02:00
if (statusListAdapter != null) {
2019-08-17 11:10:31 +02:00
statusListAdapter.storeToot();
}
2019-09-06 17:55:14 +02:00
if (type == RetrieveFeedsAsyncTask.Type.PUBLIC && streamingFederatedIntent != null) {
2019-08-17 11:10:31 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + userId + instance, false);
editor.apply();
context.stopService(streamingFederatedIntent);
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.LOCAL && streamingLocalIntent != null) {
2019-08-17 11:10:31 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL + userId + instance, false);
editor.apply();
context.stopService(streamingLocalIntent);
}
}
@Override
2019-09-06 17:55:14 +02:00
public void onDestroy() {
super.onDestroy();
2019-09-06 17:55:14 +02:00
if (receive_action != null)
2019-01-10 11:51:25 +01:00
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_action);
2019-09-06 17:55:14 +02:00
if (receive_data != null)
2019-01-11 16:39:01 +01:00
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_data);
2017-05-05 16:36:04 +02:00
}
@Override
public void onRetrieveFeeds(APIResponse apiResponse) {
2018-12-19 15:12:46 +01:00
//hide loaders
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
2018-12-19 15:12:46 +01:00
//handle other API error but discards 404 - error which can often happen due to toots which have been deleted
2020-01-18 18:23:09 +01:00
if (this.peertubes == null || this.statuses == null || apiResponse == null || (apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404 && apiResponse.getError().getStatusCode() != 501)) {
2019-09-06 17:55:14 +02:00
if (apiResponse == null)
Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2019-09-30 18:14:46 +02:00
else {
2019-11-15 16:32:25 +01:00
if (apiResponse.getError().getError().length() < 100) {
2019-09-30 18:14:46 +02:00
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2019-11-15 16:32:25 +01:00
} else {
Toasty.error(context, getString(R.string.long_api_error, "\ud83d\ude05"), Toast.LENGTH_LONG).show();
2019-09-30 18:14:46 +02:00
}
}
swipeRefreshLayout.setRefreshing(false);
2017-08-19 19:10:43 +02:00
flag_loading = false;
return;
}
2018-12-19 15:12:46 +01:00
//For remote Peertube remote instances
2019-09-06 17:55:14 +02:00
if (instanceType.equals("PEERTUBE")) {
2018-10-07 10:45:34 +02:00
int previousPosition = this.peertubes.size();
2019-09-06 17:55:14 +02:00
if (max_id == null)
2018-10-07 10:45:34 +02:00
max_id = "0";
2018-12-19 15:12:46 +01:00
//max_id needs to work like an offset
2019-08-31 16:41:29 +02:00
int tootPerPage = sharedpreferences.getInt(Helper.SET_TOOT_PER_PAGE, Helper.TOOTS_PER_PAGE);
2020-03-07 08:14:15 +01:00
max_id = String.valueOf(Integer.parseInt(max_id) + tootPerPage);
2019-09-06 17:55:14 +02:00
if (apiResponse.getPeertubes() == null) {
2019-01-10 16:05:48 +01:00
return;
}
2018-10-07 10:45:34 +02:00
this.peertubes.addAll(apiResponse.getPeertubes());
2018-12-19 15:12:46 +01:00
//If no item were inserted previously the adapter is created
2019-09-06 17:55:14 +02:00
if (previousPosition == 0) {
2019-08-19 09:11:23 +02:00
peertubeAdapater = new PeertubeAdapter(remoteInstance, ownVideos, this.peertubes);
2018-10-07 14:48:51 +02:00
lv_status.setAdapter(peertubeAdapater);
2019-09-06 17:55:14 +02:00
} else
2018-10-07 14:48:51 +02:00
peertubeAdapater.notifyItemRangeInserted(previousPosition, apiResponse.getPeertubes().size());
2018-12-19 15:12:46 +01:00
//remove handlers
2018-10-07 10:45:34 +02:00
swipeRefreshLayout.setRefreshing(false);
2020-02-01 10:40:41 +01:00
textviewNoAction.setVisibility(View.GONE);
2019-09-06 17:55:14 +02:00
if (firstLoad && (apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0)) {
textviewNoActionText.setText(R.string.no_video_to_display);
2019-01-12 17:55:18 +01:00
textviewNoAction.setVisibility(View.VISIBLE);
}
2018-10-07 11:57:15 +02:00
flag_loading = false;
2019-01-12 17:55:18 +01:00
firstLoad = false;
2020-03-08 10:29:06 +01:00
} else if (instanceType.equals("NITTER")) {
2020-04-08 19:35:43 +02:00
max_id = apiResponse.getMax_id();
2020-02-01 10:40:41 +01:00
int previousPosition = this.statuses.size();
//max_id needs to work like an offset
this.statuses.addAll(apiResponse.getStatuses());
//If no item were inserted previously the adapter is created
if (previousPosition == 0) {
boolean isOnWifi = Helper.isOnWIFI(context);
2020-03-07 08:46:48 +01:00
StatusDrawerParams statusDrawerParams = new StatusDrawerParams();
statusDrawerParams.setInstanceType(instanceType);
statusDrawerParams.setType(type);
statusDrawerParams.setTargetedId(targetedId);
statusDrawerParams.setOnWifi(isOnWifi);
statusDrawerParams.setStatuses(this.statuses);
statusListAdapter = new StatusListAdapter(statusDrawerParams);
2020-02-01 10:40:41 +01:00
lv_status.setAdapter(statusListAdapter);
} else {
statusListAdapter.notifyItemRangeInserted(previousPosition, apiResponse.getStatuses().size());
}
textviewNoAction.setVisibility(View.GONE);
//remove handlers
swipeRefreshLayout.setRefreshing(false);
2020-02-02 12:12:40 +01:00
if (firstLoad && (apiResponse.getStatuses() == null || apiResponse.getStatuses().size() == 0)) {
2020-02-01 10:40:41 +01:00
textviewNoAction.setVisibility(View.VISIBLE);
}
flag_loading = false;
firstLoad = false;
2019-09-06 17:55:14 +02:00
} else {
2019-01-14 19:27:15 +01:00
2018-12-19 15:12:46 +01:00
//When Mastodon statuses have been fetched.
2019-09-06 17:55:14 +02:00
if (type == RetrieveFeedsAsyncTask.Type.CONVERSATION) { //Conversation timeline
2018-12-19 15:12:46 +01:00
//this timeline is dealt differently because it is embedded in Conversation entity and not directly in statuses
2018-10-29 15:49:13 +01:00
List<Conversation> conversations = apiResponse.getConversations();
2018-12-19 15:12:46 +01:00
//Statuses from conversation entity are retrieved
2018-10-29 15:49:13 +01:00
List<Status> statusesConversations = new ArrayList<>();
2019-09-06 17:55:14 +02:00
if (conversations != null) {
2018-11-22 07:47:43 +01:00
for (Conversation conversation : conversations) {
2019-08-07 09:14:20 +02:00
Status status = conversation.getLast_status();
if (status != null) {
status.setConversationId(conversation.getId());
List<String> ppConversation = new ArrayList<>();
for (Account account : conversation.getAccounts())
ppConversation.add(account.getAvatar());
status.setConversationProfilePicture(ppConversation);
statusesConversations.add(status);
2018-11-22 07:47:43 +01:00
}
}
2018-10-29 18:32:55 +01:00
}
2018-10-29 15:49:13 +01:00
apiResponse.setStatuses(statusesConversations);
2020-04-08 12:42:15 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.ANNOUNCEMENTS) {
if (apiResponse.getAnnouncements() != null && apiResponse.getAnnouncements().size() > 0) {
2020-03-10 19:00:32 +01:00
List<Status> statusesAnnouncements = new ArrayList<>();
2020-04-08 12:42:15 +02:00
for (Announcement announcement : apiResponse.getAnnouncements()) {
statusesAnnouncements.add(0, announcement);
if (!announcement.isRead()) {
2021-01-19 17:43:51 +01:00
new PostActionAsyncTask(context, API.StatusAction.DISMISS_ANNOUNCEMENT, announcement.getId(), DisplayStatusFragment.this);
2020-03-10 19:00:32 +01:00
}
}
2020-04-11 15:37:26 +02:00
apiResponse.setStatuses(statusesAnnouncements);
2020-03-10 19:00:32 +01:00
}
2020-04-11 15:37:26 +02:00
2020-04-08 12:42:15 +02:00
final NavigationView navigationView = ((MainActivity) context).findViewById(R.id.nav_view);
2020-03-10 19:00:32 +01:00
MenuItem item = navigationView.getMenu().findItem(R.id.nav_announcements);
TextView actionView = item.getActionView().findViewById(R.id.counter);
2020-04-08 12:42:15 +02:00
if (actionView != null) {
2020-03-10 19:00:32 +01:00
actionView.setVisibility(View.GONE);
2020-03-09 18:34:39 +01:00
}
2018-10-29 15:49:13 +01:00
}
2018-10-07 11:57:15 +02:00
int previousPosition = this.statuses.size();
2019-03-31 14:25:40 +02:00
List<Status> statuses;
2019-09-06 17:55:14 +02:00
if (apiResponse.getResults() != null && apiResponse.getResults().getStatuses() != null)
2019-03-31 14:25:40 +02:00
statuses = apiResponse.getResults().getStatuses();
else
statuses = apiResponse.getStatuses();
2020-04-11 15:37:26 +02:00
2018-12-19 15:12:46 +01:00
//At this point all statuses are in "List<Status> statuses"
2018-12-26 15:43:36 +01:00
//Pagination for Pixelfed
2021-01-26 18:47:15 +01:00
if (type == RetrieveFeedsAsyncTask.Type.SEARCH && !tag.contains("_cache_")) {
2019-09-06 17:55:14 +02:00
if (max_id == null)
2019-03-31 15:15:46 +02:00
max_id = "0";
2020-03-07 08:14:15 +01:00
max_id = String.valueOf(Integer.parseInt(max_id) + 20);
2019-09-06 17:55:14 +02:00
} else {
2018-12-26 15:43:36 +01:00
max_id = apiResponse.getMax_id();
}
2019-03-31 15:15:46 +02:00
2018-12-19 15:12:46 +01:00
//while max_id is different from null, there are some more toots to load when scrolling
2019-09-06 17:55:14 +02:00
flag_loading = (max_id == null);
2018-12-19 15:12:46 +01:00
//If it's the first load and the reply doesn't contain any toots, a message is displayed.
2019-09-06 17:55:14 +02:00
if (firstLoad && (statuses == null || statuses.size() == 0)) {
2018-10-07 11:57:15 +02:00
textviewNoAction.setVisibility(View.VISIBLE);
2018-12-19 15:12:46 +01:00
lv_status.setVisibility(View.GONE);
2019-09-06 17:55:14 +02:00
} else {
2018-12-19 15:12:46 +01:00
lv_status.setVisibility(View.VISIBLE);
2018-10-07 11:57:15 +02:00
textviewNoAction.setVisibility(View.GONE);
2018-12-19 15:12:46 +01:00
}
2018-10-07 11:57:15 +02:00
//First toot are loaded as soon as the bookmark has been retrieved
2018-12-19 15:12:46 +01:00
//Only for the Home timeline
2019-11-15 16:32:25 +01:00
if ((type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) && !firstTootsLoaded) {
2019-05-25 18:58:31 +02:00
boolean remember_position_home = sharedpreferences.getBoolean(Helper.SET_REMEMBER_POSITION_HOME, true);
2019-09-06 17:55:14 +02:00
if (remember_position_home)
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAfterBookmarkAsyncTask(context, null, false, DisplayStatusFragment.this);
2018-10-07 11:57:15 +02:00
firstTootsLoaded = true;
}
2018-12-19 15:12:46 +01:00
//Let's deal with statuses
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0) {
2020-03-08 10:29:06 +01:00
if (statusListAdapter != null && (instanceType.equals("MASTODON") || instanceType.equals("MISSKEY") || instanceType.equals("GNU"))) {
2019-01-14 19:27:15 +01:00
this.statuses.addAll(statuses);
statusListAdapter.notifyItemRangeInserted(previousPosition, statuses.size());
2019-09-06 17:55:14 +02:00
} else if (artListAdapter != null && instanceType.equals("ART")) {
2018-12-31 17:30:51 +01:00
boolean show_nsfw = sharedpreferences.getBoolean(Helper.SET_ART_WITH_NSFW, false);
2019-09-06 17:55:14 +02:00
if (!show_nsfw) {
2018-12-31 17:30:51 +01:00
ArrayList<Status> safeStatuses = new ArrayList<>();
2019-09-06 17:55:14 +02:00
for (Status status : statuses) {
if (!status.isSensitive())
2018-12-31 17:30:51 +01:00
safeStatuses.add(status);
}
this.statuses.addAll(safeStatuses);
2019-01-14 19:27:15 +01:00
artListAdapter.notifyItemRangeInserted(previousPosition, safeStatuses.size());
2019-09-06 17:55:14 +02:00
} else {
2018-12-15 17:13:06 +01:00
this.statuses.addAll(statuses);
2019-01-14 19:27:15 +01:00
artListAdapter.notifyItemRangeInserted(previousPosition, statuses.size());
2018-12-15 17:13:06 +01:00
}
2019-01-21 18:49:02 +01:00
2019-09-06 17:55:14 +02:00
} else if (pixelfedListAdapter != null && instanceType.equals("PIXELFED")) {
2019-01-14 19:27:15 +01:00
this.statuses.addAll(statuses);
pixelfedListAdapter.notifyItemRangeInserted(previousPosition, statuses.size());
2018-10-07 11:57:15 +02:00
}
2017-09-24 18:44:42 +02:00
}
2018-10-07 11:57:15 +02:00
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
}
2017-05-05 16:36:04 +02:00
}
2017-09-27 17:52:23 +02:00
/**
* Deals with new status coming from the streaming api
2019-09-06 17:55:14 +02:00
*
2017-09-27 17:52:23 +02:00
* @param status Status
*/
2019-09-06 17:55:14 +02:00
public void refresh(Status status) {
2017-09-06 15:26:58 +02:00
//New data are available
if (context == null)
return;
2019-09-06 17:55:14 +02:00
if (status.getId() != null && statuses != null && statuses.size() > 0 && statuses.get(0) != null
&& status.getCreated_at().after(statuses.get(0).getCreated_at())) {
2020-03-07 08:14:15 +01:00
List<Status> tempTootResult = new ArrayList<>();
2018-11-10 08:52:17 +01:00
tempTootResult.add(status);
2019-09-06 17:55:14 +02:00
if (tempTootResult.size() > 0)
2018-09-26 13:45:13 +02:00
status = tempTootResult.get(0);
2019-11-15 16:32:25 +01:00
if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) {
2017-12-28 15:33:02 +01:00
//Makes sure the status is not already displayed
2019-09-06 17:55:14 +02:00
if (!statuses.contains(status)) {
2017-12-28 15:33:02 +01:00
//Update the id of the last toot retrieved
MainActivity.lastHomeId = status.getId();
statuses.add(0, status);
2019-08-17 19:07:35 +02:00
statusListAdapter.notifyItemInserted(0);
2018-12-19 15:12:46 +01:00
try {
((MainActivity) context).updateHomeCounter();
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
2017-12-28 15:33:02 +01:00
}
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.PUBLIC || type == RetrieveFeedsAsyncTask.Type.LOCAL || type == RetrieveFeedsAsyncTask.Type.DIRECT || type == RetrieveFeedsAsyncTask.Type.GNU_DM) {
2017-10-21 19:19:11 +02:00
status.setNew(false);
2017-10-24 11:03:36 +02:00
statuses.add(0, status);
2017-10-24 14:06:28 +02:00
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (firstVisibleItem > 0)
2017-10-24 14:06:28 +02:00
statusListAdapter.notifyItemInserted(0);
else
statusListAdapter.notifyDataSetChanged();
if (textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE);
}
2017-08-28 19:12:40 +02:00
}
}
2019-08-18 17:17:47 +02:00
@Override
public void onDestroyView() {
2019-09-06 17:55:14 +02:00
if (lv_status != null) {
2019-10-20 18:03:10 +02:00
try {
lv_status.setAdapter(null);
2020-04-09 18:57:12 +02:00
} catch (Exception ignored) {
}
2019-08-18 17:17:47 +02:00
}
super.onDestroyView();
2020-04-08 12:42:15 +02:00
rootView = null;
2019-08-18 17:17:47 +02:00
}
@Override
2019-09-06 17:55:14 +02:00
public void onResume() {
super.onResume();
2019-08-17 19:17:48 +02:00
swipeRefreshLayout.setEnabled(true);
2021-02-28 11:46:34 +01:00
boolean liveNotifications = liveNotifType(context) == NOTIF_PUSH;
2019-11-15 16:32:25 +01:00
if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) {
2020-04-25 19:20:42 +02:00
if (this.isVisible()) {
2021-01-19 17:43:51 +01:00
if (statuses != null && statuses.size() > 0) {
2018-12-19 10:54:26 +01:00
retrieveMissingToots(statuses.get(0).getId());
2019-02-06 17:53:49 +01:00
}
2018-12-19 10:54:26 +01:00
}
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.PUBLIC) {
2020-04-25 19:20:42 +02:00
if (isVisible()) {
SharedPreferences.Editor editor = sharedpreferences.edit();
2017-12-28 17:39:02 +01:00
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + userId + instance, true);
editor.apply();
2019-09-06 17:55:14 +02:00
if (liveNotifications) {
streamingFederatedIntent = new Intent(context, StreamingFederatedTimelineService.class);
2018-09-04 07:29:42 +02:00
try {
context.startService(streamingFederatedIntent);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
}
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0)
2017-10-07 08:34:50 +02:00
retrieveMissingToots(statuses.get(0).getId());
}
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.LOCAL) {
2020-04-25 19:20:42 +02:00
if (isVisible()) {
2017-09-30 09:00:00 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
2017-12-28 17:39:02 +01:00
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL + userId + instance, true);
2017-09-30 09:00:00 +02:00
editor.apply();
2019-09-06 17:55:14 +02:00
if (liveNotifications) {
streamingLocalIntent = new Intent(context, StreamingLocalTimelineService.class);
2018-09-04 07:29:42 +02:00
try {
context.startService(streamingLocalIntent);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
}
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0)
2017-10-07 08:34:50 +02:00
retrieveMissingToots(statuses.get(0).getId());
2017-09-30 09:00:00 +02:00
}
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.DIRECT || type == RetrieveFeedsAsyncTask.Type.GNU_DM) {
2020-04-25 19:20:42 +02:00
if (isVisible()) {
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0)
2018-10-26 17:00:38 +02:00
retrieveMissingToots(statuses.get(0).getId());
}
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.CONVERSATION) {
2020-04-25 19:20:42 +02:00
if (isVisible()) {
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0)
2018-11-18 17:24:12 +01:00
retrieveMissingToots(statuses.get(0).getId());
}
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.TAG) {
2020-04-25 19:20:42 +02:00
if (isVisible()) {
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0)
2018-11-21 18:04:10 +01:00
retrieveMissingToots(statuses.get(0).getId());
}
}
}
2017-09-27 17:52:23 +02:00
/**
* Called from main activity in onResume to retrieve missing toots (home timeline)
2019-09-06 17:55:14 +02:00
*
2017-09-27 17:52:23 +02:00
* @param sinceId String
*/
2019-09-06 17:55:14 +02:00
private void retrieveMissingToots(String sinceId) {
2018-12-23 10:43:45 +01:00
2021-01-19 17:43:51 +01:00
// if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME)
// new RetrieveFeedsAfterBookmarkAsyncTask(context, null, false, DisplayStatusFragment.this);
2019-12-08 18:48:52 +01:00
if (type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE && (instanceType == null || instanceType.compareTo("NITTER") != 0))
2021-01-19 17:43:51 +01:00
new RetrieveMissingFeedsAsyncTask(context, remoteInstance, sinceId, type, DisplayStatusFragment.this);
2019-09-06 17:55:14 +02:00
else if (type == RetrieveFeedsAsyncTask.Type.TAG)
2021-01-19 17:43:51 +01:00
new RetrieveMissingFeedsAsyncTask(context, tag, sinceId, type, DisplayStatusFragment.this);
2020-03-31 17:28:09 +02:00
else if (type == RetrieveFeedsAsyncTask.Type.LIST)
2021-01-19 17:43:51 +01:00
new ManageListsAsyncTask(context, targetedId, null, sinceId, DisplayStatusFragment.this);
2018-08-24 15:47:16 +02:00
else
2021-01-19 17:43:51 +01:00
new RetrieveMissingFeedsAsyncTask(context, sinceId, type, DisplayStatusFragment.this);
2018-08-24 15:47:16 +02:00
2017-09-27 17:52:23 +02:00
}
2019-05-09 19:03:02 +02:00
2019-09-06 17:55:14 +02:00
public void retrieveMissingHome() {
if (statusListAdapter != null && statuses != null && lv_status != null && mLayoutManager != null) {
2019-05-09 19:03:02 +02:00
int firstVisible = mLayoutManager.findFirstVisibleItemPosition();
Iterator<Status> s = statuses.iterator();
int i = 0;
while (s.hasNext() && i < firstVisible) {
2020-03-07 08:14:15 +01:00
s.next();
2019-05-09 19:03:02 +02:00
s.remove();
statusListAdapter.notifyItemRemoved(0);
statusListAdapter.notifyItemChanged(0);
i++;
}
2019-09-06 17:55:14 +02:00
if (statuses.size() > 0)
2019-05-13 07:21:09 +02:00
initialBookMarkDate = statuses.get(0).getCreated_at();
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAfterBookmarkAsyncTask(context, null, false, DisplayStatusFragment.this);
2019-05-09 19:03:02 +02:00
}
}
2017-09-27 17:52:23 +02:00
/**
* When tab comes visible, first displayed toot is defined as read
2019-09-06 17:55:14 +02:00
*
2017-09-27 17:52:23 +02:00
* @param visible boolean
*/
2017-09-13 15:21:03 +02:00
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
2019-09-06 17:55:14 +02:00
if (context == null)
2017-09-13 15:21:03 +02:00
return;
2021-02-28 11:46:34 +01:00
int liveNotifications = liveNotifType(context);
2017-09-13 15:21:03 +02:00
//Store last toot id for home timeline to avoid to notify for those that have been already seen
2019-11-15 16:32:25 +01:00
if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) {
2018-12-19 10:54:26 +01:00
if (visible) {
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0) {
2018-12-19 10:54:26 +01:00
retrieveMissingToots(statuses.get(0).getId());
2019-02-06 17:53:49 +01:00
}
2018-12-19 10:54:26 +01:00
}
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.PUBLIC) {
if (visible) {
2017-09-29 14:08:46 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
2017-12-28 17:39:02 +01:00
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + userId + instance, true);
2017-09-29 14:08:46 +02:00
editor.apply();
2019-09-14 18:53:23 +02:00
if (liveNotifications != 2) {
streamingFederatedIntent = new Intent(context, StreamingFederatedTimelineService.class);
2018-09-04 07:29:42 +02:00
try {
context.startService(streamingFederatedIntent);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
}
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0)
2017-10-07 08:34:50 +02:00
retrieveMissingToots(statuses.get(0).getId());
2019-09-06 17:55:14 +02:00
} else {
if (streamingFederatedIntent != null) {
2017-09-29 14:08:46 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
2017-12-28 17:39:02 +01:00
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + userId + instance, false);
2017-09-29 14:08:46 +02:00
editor.apply();
context.stopService(streamingFederatedIntent);
}
}
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.LOCAL) {
2017-09-30 09:00:00 +02:00
if (visible) {
SharedPreferences.Editor editor = sharedpreferences.edit();
2017-12-28 17:39:02 +01:00
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL + userId + instance, true);
2017-09-30 09:00:00 +02:00
editor.apply();
2019-09-14 18:53:23 +02:00
if (liveNotifications != 2) {
streamingLocalIntent = new Intent(context, StreamingLocalTimelineService.class);
2018-09-04 07:29:42 +02:00
try {
context.startService(streamingLocalIntent);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
}
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0)
2017-10-07 08:34:50 +02:00
retrieveMissingToots(statuses.get(0).getId());
2019-09-06 17:55:14 +02:00
} else {
if (streamingLocalIntent != null) {
2017-09-30 09:00:00 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
2017-12-28 17:39:02 +01:00
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL + userId + instance, false);
2017-09-30 09:00:00 +02:00
editor.apply();
context.stopService(streamingLocalIntent);
}
}
}
2017-09-13 15:21:03 +02:00
}
2019-08-17 11:10:31 +02:00
2019-09-06 17:55:14 +02:00
public void scrollToTop() {
2020-03-08 10:29:06 +01:00
if (mLayoutManager != null) {
2019-11-28 18:42:16 +01:00
mLayoutManager.scrollToPositionWithOffset(0, 0);
}
}
2018-11-10 08:52:17 +01:00
/**
* Refresh status in list
*/
2019-09-06 17:55:14 +02:00
public void refreshFilter() {
2019-01-14 19:27:15 +01:00
2019-09-06 17:55:14 +02:00
if (statusListAdapter != null) {
2019-08-18 19:04:16 +02:00
statusListAdapter.notifyDataSetChanged();
2019-09-06 17:55:14 +02:00
} else if (pixelfedListAdapter != null) {
2019-08-18 19:04:16 +02:00
pixelfedListAdapter.notifyDataSetChanged();
2019-09-06 17:55:14 +02:00
} else if (artListAdapter != null) {
2019-08-18 19:04:16 +02:00
artListAdapter.notifyDataSetChanged();
2019-08-18 17:17:47 +02:00
}
2018-11-10 08:52:17 +01:00
}
2017-09-27 17:52:23 +02:00
@Override
2018-11-10 08:52:17 +01:00
public void onRetrieveMissingFeeds(List<Status> statuses) {
2019-09-06 17:55:14 +02:00
if (swipeRefreshLayout == null)
2018-11-04 10:01:16 +01:00
return;
//Clean label new
2018-03-10 12:18:31 +01:00
swipeRefreshLayout.setRefreshing(false);
2019-09-06 17:55:14 +02:00
if (isSwipped && this.statuses != null && this.statuses.size() > 0) {
for (Status status : this.statuses) {
status.setNew(false);
}
switch (instanceType) {
case "MASTODON":
case "MISSKEY":
2019-12-08 16:54:47 +01:00
case "NITTER":
case "GNU":
statusListAdapter.notifyItemRangeChanged(0, this.statuses.size());
break;
case "PIXELFED":
pixelfedListAdapter.notifyItemRangeChanged(0, this.statuses.size());
break;
case "ART":
artListAdapter.notifyItemRangeChanged(0, this.statuses.size());
break;
}
}
2018-04-25 14:03:15 +02:00
isSwipped = false;
2019-01-26 12:26:55 +01:00
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0) {
if (textviewNoAction.getVisibility() == View.VISIBLE) {
2019-01-26 12:26:55 +01:00
textviewNoAction.setVisibility(View.GONE);
lv_status.setVisibility(View.VISIBLE);
}
2017-10-24 14:06:28 +02:00
int inserted = 0;
2018-11-22 19:15:31 +01:00
int insertedConversation = 0;
2019-09-06 17:55:14 +02:00
if (type == RetrieveFeedsAsyncTask.Type.CONVERSATION) { //Remove conversation already displayed if new messages
2018-11-21 18:04:10 +01:00
int position = 0;
2018-11-23 19:34:44 +01:00
insertedConversation = statuses.size();
2019-09-06 17:55:14 +02:00
if (this.statuses != null) {
2018-11-21 18:04:10 +01:00
for (Iterator<Status> it = this.statuses.iterator(); it.hasNext(); ) {
Status status = it.next();
for (Status status1 : statuses) {
if (status.getConversationId() != null && status.getConversationId().equals(status1.getConversationId())) {
2020-03-07 08:14:15 +01:00
switch (instanceType) {
case "MASTODON":
case "MISSKEY":
case "NITTER":
case "GNU":
statusListAdapter.notifyItemRemoved(position);
break;
case "PIXELFED":
pixelfedListAdapter.notifyItemRemoved(position);
break;
case "ART":
artListAdapter.notifyItemRemoved(position);
break;
}
2018-11-21 18:04:10 +01:00
it.remove();
}
}
position++;
}
}
}
2017-10-24 11:03:36 +02:00
for (int i = statuses.size() - 1; i >= 0; i--) {
2019-09-06 17:55:14 +02:00
if (this.statuses != null) {
2019-09-29 18:18:20 +02:00
if (type != RetrieveFeedsAsyncTask.Type.HOME && type != RetrieveFeedsAsyncTask.Type.PF_HOME) {
2019-09-06 17:55:14 +02:00
if (tagTimeline != null && instanceType.equals("ART") && !tagTimeline.isNSFW()) {
if (!statuses.get(i).isSensitive()) {
2019-01-24 09:54:26 +01:00
this.statuses.add(0, statuses.get(i));
inserted++;
}
2019-09-06 17:55:14 +02:00
} else {
2019-01-22 08:51:36 +01:00
if (this.statuses.size() == 0 || statuses.get(i).getCreated_at().after(this.statuses.get(0).getCreated_at())) {
2018-12-28 12:09:06 +01:00
inserted++;
this.statuses.add(0, statuses.get(i));
}
2018-12-27 17:49:50 +01:00
}
2019-09-06 17:55:14 +02:00
} else {
if (lastReadTootDate != null && statuses.get(i).getCreated_at().after(lastReadTootDate)) {
if (!this.statuses.contains(statuses.get(i))) {
2018-12-28 15:21:36 +01:00
statuses.get(i).setNew(true);
MainActivity.countNewStatus++;
inserted++;
this.statuses.add(0, statuses.get(i));
}
2018-12-22 11:08:04 +01:00
}
2018-09-26 13:45:13 +02:00
}
2017-10-07 13:00:08 +02:00
}
2017-09-27 17:52:23 +02:00
}
2019-12-08 16:54:47 +01:00
if (statusListAdapter != null && (instanceType.equals("MASTODON") || instanceType.equals("MISSKEY") || instanceType.equals("NITTER") || instanceType.equals("GNU")))
2019-01-14 19:27:15 +01:00
statusListAdapter.notifyItemRangeInserted(0, inserted);
2019-09-06 17:55:14 +02:00
else if (pixelfedListAdapter != null && instanceType.equals("PIXELFED"))
2019-01-14 19:27:15 +01:00
pixelfedListAdapter.notifyItemRangeInserted(0, inserted);
2019-09-06 17:55:14 +02:00
else if (artListAdapter != null && instanceType.equals("ART"))
2019-01-14 19:27:15 +01:00
artListAdapter.notifyItemRangeInserted(0, inserted);
2017-09-27 17:52:23 +02:00
try {
2019-11-15 16:32:25 +01:00
if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME)
2017-10-25 13:45:22 +02:00
((MainActivity) context).updateHomeCounter();
2018-11-22 19:15:31 +01:00
else {
2019-09-06 17:55:14 +02:00
if (type != RetrieveFeedsAsyncTask.Type.CONVERSATION)
2019-04-21 09:12:10 +02:00
((MainActivity) context).manageTab(type, inserted);
2018-11-22 19:15:31 +01:00
else
2019-04-21 09:12:10 +02:00
((MainActivity) context).manageTab(type, insertedConversation);
2018-11-22 19:15:31 +01:00
}
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
2017-09-27 17:52:23 +02:00
}
}
2017-12-06 16:56:59 +01:00
2019-09-06 17:55:14 +02:00
public void fetchMore(String max_id) {
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAfterBookmarkAsyncTask(context, max_id, true, DisplayStatusFragment.this);
2018-12-21 18:49:02 +01:00
}
@Override
public void onRetrieveFeedsAfterBookmark(APIResponse apiResponse) {
2019-09-29 18:18:20 +02:00
if (statusListAdapter == null && pixelfedListAdapter == null)
2019-02-06 17:53:49 +01:00
return;
2020-01-18 18:23:09 +01:00
if (apiResponse == null || (apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404 && apiResponse.getError().getStatusCode() != 501)) {
2019-09-06 17:55:14 +02:00
if (apiResponse == null)
Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2019-11-15 16:32:25 +01:00
else if (apiResponse.getError().getError().length() < 100) {
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
} else {
Toasty.error(context, getString(R.string.long_api_error, "\ud83d\ude05"), Toast.LENGTH_LONG).show();
}
2018-12-21 18:49:02 +01:00
swipeRefreshLayout.setRefreshing(false);
flag_loading = false;
return;
}
List<Status> statuses = apiResponse.getStatuses();
2019-09-06 17:55:14 +02:00
if (statuses == null || statuses.size() == 0 || this.statuses == null)
2018-12-24 09:51:02 +01:00
return;
2018-12-21 18:49:02 +01:00
//Find the position of toots between those already present
int position = 0;
2019-09-06 17:55:14 +02:00
if (position < this.statuses.size() && statuses.get(0).getCreated_at() != null && this.statuses.get(position).getCreated_at() != null) {
2019-05-13 19:38:48 +02:00
while (position < this.statuses.size() && statuses.get(0).getCreated_at().before(this.statuses.get(position).getCreated_at())) {
position++;
}
2018-12-21 18:49:02 +01:00
}
ArrayList<Status> tmpStatuses = new ArrayList<>();
2020-06-06 14:22:51 +02:00
if (this.statuses.size() > 0) {
for (Status tmpStatus : statuses) {
//Put the toot at its place in the list (id desc)
if (!apiResponse.isFetchmore() && this.statuses.size() > 0 && !this.statuses.contains(tmpStatus) && tmpStatus.getCreated_at() != null && this.statuses.get(0).getCreated_at() != null && tmpStatus.getCreated_at().after(this.statuses.get(0).getCreated_at())) { //Element not already added
//Mark status at new ones when their id is greater than the last read toot id
if ((type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) && lastReadTootDate != null && tmpStatus.getCreated_at().after(lastReadTootDate)) {
tmpStatus.setNew(true);
MainActivity.countNewStatus++;
}
tmpStatuses.add(tmpStatus);
} else if (apiResponse.isFetchmore() && !this.statuses.contains(tmpStatus)) { //Element not already added
//Mark status at new ones when their id is greater than the last read toot id
if ((type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) && lastReadTootDate != null && tmpStatus.getCreated_at().after(lastReadTootDate)) {
tmpStatus.setNew(true);
MainActivity.countNewStatus++;
}
tmpStatuses.add(tmpStatus);
2018-12-21 18:49:02 +01:00
}
}
2020-06-06 14:22:51 +02:00
} else {
this.statuses.addAll(statuses);
tmpStatuses.addAll(statuses);
2018-12-21 18:49:02 +01:00
}
2020-06-06 14:22:51 +02:00
2018-12-21 19:15:35 +01:00
try {
((MainActivity) context).updateHomeCounter();
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
2019-08-31 16:41:29 +02:00
int tootPerPage = sharedpreferences.getInt(Helper.SET_TOOT_PER_PAGE, Helper.TOOTS_PER_PAGE);
2018-12-21 18:49:02 +01:00
//Display the fetch more toot button
2019-09-06 17:55:14 +02:00
if (tmpStatuses.size() >= tootPerPage) {
if (initialBookMarkDate != null && tmpStatuses.size() > 0 && tmpStatuses.get(tmpStatuses.size() - 1).getCreated_at().after(initialBookMarkDate)) {
if (StatusListAdapter.fetch_all_more && statuses.size() > 0) {
fetchMore(tmpStatuses.get(tmpStatuses.size() - 1).getId());
2019-09-06 17:55:14 +02:00
} else {
tmpStatuses.get(tmpStatuses.size() - 1).setFetchMore(true);
StatusListAdapter.fetch_all_more = false;
}
2018-12-21 18:49:02 +01:00
}
2019-09-06 17:55:14 +02:00
} else {
StatusListAdapter.fetch_all_more = false;
2018-12-21 18:49:02 +01:00
}
this.statuses.addAll(position, tmpStatuses);
2019-11-15 16:32:25 +01:00
if (statusListAdapter != null)
2019-09-29 18:18:20 +02:00
statusListAdapter.notifyItemRangeInserted(position, tmpStatuses.size());
2019-11-15 16:32:25 +01:00
if (pixelfedListAdapter != null)
2019-09-29 18:18:20 +02:00
pixelfedListAdapter.notifyItemRangeInserted(position, tmpStatuses.size());
2019-01-22 16:31:54 +01:00
boolean display_content_after_fetch_more = sharedpreferences.getBoolean(Helper.SET_DISPLAY_CONTENT_AFTER_FM, true);
2019-09-06 17:55:14 +02:00
if (position > 0 && display_content_after_fetch_more)
2019-01-22 16:31:54 +01:00
lv_status.scrollToPosition(position + tmpStatuses.size());
2019-09-06 17:55:14 +02:00
if (textviewNoAction.getVisibility() == View.VISIBLE && tmpStatuses.size() > 0) {
2018-12-26 16:18:13 +01:00
textviewNoAction.setVisibility(View.GONE);
lv_status.setVisibility(View.VISIBLE);
}
2017-12-06 16:56:59 +01:00
}
//Update last read toots value when pressing tab button
2019-09-06 17:55:14 +02:00
public void updateLastReadToot() {
2019-09-29 18:18:20 +02:00
if ((type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) && this.statuses != null && this.statuses.size() > 0) {
lastReadToot = this.statuses.get(0).getId();
}
}
2019-01-10 11:51:25 +01:00
2019-04-22 13:16:35 +02:00
private void manageAsyncTask(boolean pagination) {
2019-01-14 18:16:45 +01:00
//Message for an account
if (type == RetrieveFeedsAsyncTask.Type.USER || type == RetrieveFeedsAsyncTask.Type.CHANNEL)
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, showReply, DisplayStatusFragment.this);
2019-04-22 13:16:35 +02:00
//Tag timelines
2019-04-26 15:50:26 +02:00
else if (type == RetrieveFeedsAsyncTask.Type.SEARCH)
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this);
2019-09-06 17:55:14 +02:00
else if (type == RetrieveFeedsAsyncTask.Type.TAG)
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, timelineId, max_id, DisplayStatusFragment.this);
2019-04-22 13:16:35 +02:00
else if (type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE) {
2019-01-14 18:16:45 +01:00
//Remote instances
2019-04-22 13:16:35 +02:00
if (search_peertube == null) { //Not a Peertube search
if (remote_channel_name == null) { //Not a channel
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this);
2019-04-22 13:16:35 +02:00
} else
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, remoteInstance, remote_channel_name, null, DisplayStatusFragment.this);
2019-04-22 13:16:35 +02:00
} else
2021-01-19 17:43:51 +01:00
new RetrievePeertubeSearchAsyncTask(context, remoteInstance, search_peertube, DisplayStatusFragment.this);
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE_FILTERED) {
2019-05-25 12:37:50 +02:00
RetrieveFeedsParam retrieveFeedsParam = new RetrieveFeedsParam();
retrieveFeedsParam.setAction(type);
retrieveFeedsParam.setCurrentfilter(currentfilter);
retrieveFeedsParam.setRemoteInstance(remoteInstance);
retrieveFeedsParam.setMax_id(max_id);
retrieveFeedsParam.setSocial(instanceType);
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, retrieveFeedsParam, DisplayStatusFragment.this);
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveFeedsAsyncTask.Type.LIST) {
2021-01-19 17:43:51 +01:00
new ManageListsAsyncTask(context, targetedId, max_id, null, DisplayStatusFragment.this);
2019-09-06 17:55:14 +02:00
} else {
if (!pagination) {
2019-09-29 18:18:20 +02:00
if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) {
2019-01-14 18:16:45 +01:00
if (context instanceof BaseMainActivity) {
2019-05-25 18:58:31 +02:00
boolean remember_position_home = sharedpreferences.getBoolean(Helper.SET_REMEMBER_POSITION_HOME, true);
2019-09-06 17:55:14 +02:00
if (remember_position_home)
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, initialBookMark, false, DisplayStatusFragment.this);
2019-05-25 18:58:31 +02:00
else
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, null, DisplayStatusFragment.this);
2019-01-14 18:16:45 +01:00
}
} else { //Most classical search will be done by this call
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, null, DisplayStatusFragment.this);
2019-01-14 18:16:45 +01:00
}
2019-09-06 17:55:14 +02:00
} else {
2019-09-29 18:18:20 +02:00
if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME) {
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this);
2019-09-06 17:55:14 +02:00
} else {//Most classical search will be done by this call for pagination
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this);
2019-01-14 18:16:45 +01:00
}
}
}
}
2019-04-22 13:16:35 +02:00
@Override
public void onActionDone(ManageListsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
//Discards 404 - error which can often happen due to toots which have been deleted
2019-11-15 16:32:25 +01:00
if (context == null) {
2019-11-06 10:43:07 +01:00
return;
}
2019-04-22 13:16:35 +02:00
if (apiResponse.getError() != null) {
2020-03-08 10:29:06 +01:00
if (!apiResponse.getError().getError().startsWith("404 -") && !apiResponse.getError().getError().startsWith("501 -"))
2019-11-15 16:32:25 +01:00
if (apiResponse.getError().getError().length() < 100) {
2019-09-30 18:14:46 +02:00
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2019-11-15 16:32:25 +01:00
} else {
Toasty.error(context, getString(R.string.long_api_error, "\ud83d\ude05"), Toast.LENGTH_LONG).show();
2019-09-30 18:14:46 +02:00
}
2019-04-22 13:16:35 +02:00
swipeRefreshLayout.setRefreshing(false);
isSwipped = false;
flag_loading = false;
return;
}
2019-09-06 17:55:14 +02:00
if (actionType == ManageListsAsyncTask.action.GET_LIST_TIMELINE) {
2019-04-22 13:16:35 +02:00
int previousPosition = this.statuses.size();
List<Status> statuses = apiResponse.getStatuses();
max_id = apiResponse.getMax_id();
flag_loading = (max_id == null);
if (!isSwipped && firstLoad && (statuses == null || statuses.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
if (isSwipped) {
if (previousPosition > 0) {
2020-03-07 08:14:15 +01:00
this.statuses.subList(0, previousPosition).clear();
2019-04-22 13:16:35 +02:00
statusListAdapter.notifyItemRangeRemoved(0, previousPosition);
}
isSwipped = false;
}
if (statuses != null && statuses.size() > 0) {
this.statuses.addAll(statuses);
statusListAdapter.notifyItemRangeInserted(previousPosition, statuses.size());
}
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
}
}
2019-08-14 18:55:45 +02:00
public void pullToRefresh() {
2019-09-06 17:55:14 +02:00
if (swipeRefreshLayout != null) {
2019-08-14 18:55:45 +02:00
swipeRefreshLayout.setRefreshing(true);
2019-09-06 17:55:14 +02:00
} else {
2019-08-14 18:55:45 +02:00
return;
}
2019-09-06 17:55:14 +02:00
if (instanceType == null || !instanceType.equals("PEERTUBE")) {
2019-09-29 18:18:20 +02:00
if (type == RetrieveFeedsAsyncTask.Type.HOME || type == RetrieveFeedsAsyncTask.Type.PF_HOME)
2019-08-14 18:55:45 +02:00
MainActivity.countNewStatus = 0;
isSwipped = true;
2019-08-15 17:50:11 +02:00
int lenght = statuses.size();
statuses.clear();
2020-03-07 08:14:15 +01:00
assert instanceType != null;
2019-08-15 17:50:11 +02:00
switch (instanceType) {
case "MASTODON":
case "MISSKEY":
2019-12-08 16:54:47 +01:00
case "NITTER":
2019-08-15 17:50:11 +02:00
case "GNU":
statusListAdapter.notifyItemRangeRemoved(0, lenght);
break;
case "PIXELFED":
pixelfedListAdapter.notifyItemRangeRemoved(0, lenght);
break;
case "ART":
artListAdapter.notifyItemRangeRemoved(0, lenght);
break;
2019-08-14 18:55:45 +02:00
}
2019-08-15 17:50:11 +02:00
max_id = null;
manageAsyncTask(false);
2019-09-06 17:55:14 +02:00
} else if (peertubes != null) {
2019-08-14 18:55:45 +02:00
if (peertubes.size() > 0) {
int size = peertubes.size();
isSwipped = true;
peertubes.clear();
peertubes = new ArrayList<>();
max_id = "0";
peertubeAdapater.notifyItemRangeRemoved(0, size);
if (search_peertube == null) { //Not a Peertube search
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, type, remoteInstance, "0", DisplayStatusFragment.this);
2019-08-14 18:55:45 +02:00
} else {
2021-01-19 17:43:51 +01:00
new RetrievePeertubeSearchAsyncTask(context, remoteInstance, search_peertube, DisplayStatusFragment.this);
2019-08-14 18:55:45 +02:00
}
}
}
}
2020-02-09 10:43:26 +01:00
2020-03-10 19:00:32 +01:00
@Override
public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) {
}
2020-02-09 10:43:26 +01:00
private class MyPreloadModelProvider implements ListPreloader.PreloadModelProvider<String> {
@Override
@NonNull
public List<String> getPreloadItems(int position) {
2020-03-08 10:29:06 +01:00
if (statuses == null || statuses.size() == 0) {
2020-02-09 10:43:26 +01:00
return Collections.emptyList();
}
Status status = statuses.get(position);
2020-03-08 10:29:06 +01:00
if (status.getMedia_attachments() == null || status.getMedia_attachments().size() == 0) {
2020-02-09 10:43:26 +01:00
return Collections.emptyList();
}
List<String> preloaded_urls = new ArrayList<>();
2020-03-08 10:29:06 +01:00
for (Attachment attachment : status.getMedia_attachments()) {
2020-02-09 10:43:26 +01:00
preloaded_urls.add(attachment.getPreview_url());
}
2020-03-08 10:29:06 +01:00
return preloaded_urls;
2020-02-09 10:43:26 +01:00
}
@Nullable
@Override
public RequestBuilder<?> getPreloadRequestBuilder(@NonNull String url) {
return Glide.with(context)
.load(url)
2020-03-14 08:41:21 +01:00
.thumbnail(0.1f)
2020-02-09 10:43:26 +01:00
.override(640, 480);
}
}
2017-05-05 16:36:04 +02:00
}