2017-05-05 16:36:04 +02:00
|
|
|
package fr.gouv.etalab.mastodon.fragments;
|
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2017-07-10 10:33:24 +02:00
|
|
|
* This file is a part of Mastalab
|
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.
|
|
|
|
*
|
2017-07-10 10:33:24 +02:00
|
|
|
* Mastalab 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.
|
|
|
|
*
|
2017-08-04 11:11:27 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
2017-05-05 16:36:04 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2018-10-20 14:39:24 +02:00
|
|
|
import android.app.Activity;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.content.Context;
|
2017-08-26 19:39:11 +02:00
|
|
|
import android.content.Intent;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.content.SharedPreferences;
|
2018-01-04 17:39:16 +01:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2017-11-03 10:18:47 +01:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2017-11-11 07:43:11 +01:00
|
|
|
import android.support.annotation.NonNull;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.support.v4.app.Fragment;
|
2018-05-11 16:33:16 +02:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2017-10-14 14:36:48 +02:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.RelativeLayout;
|
2017-05-30 19:31:44 +02:00
|
|
|
import android.widget.Toast;
|
2017-05-05 16:36:04 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2017-09-13 08:47:59 +02:00
|
|
|
|
2017-11-12 08:33:24 +01:00
|
|
|
import fr.gouv.etalab.mastodon.R;
|
2017-12-06 16:45:07 +01:00
|
|
|
import fr.gouv.etalab.mastodon.activities.BaseMainActivity;
|
2017-09-13 08:47:59 +02:00
|
|
|
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
2017-09-27 17:52:23 +02:00
|
|
|
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingFeedsAsyncTask;
|
2018-10-20 12:22:35 +02:00
|
|
|
import fr.gouv.etalab.mastodon.asynctasks.RetrievePeertubeSearchAsyncTask;
|
2017-06-03 18:18:27 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.APIResponse;
|
2018-01-05 09:46:59 +01:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
2018-10-29 15:49:13 +01:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Conversation;
|
2018-10-06 15:59:00 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Peertube;
|
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.RemoteInstance;
|
|
|
|
import fr.gouv.etalab.mastodon.drawers.PeertubeAdapter;
|
2017-05-05 16:36:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
|
|
|
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
2017-09-27 17:52:23 +02:00
|
|
|
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingFeedsInterface;
|
2017-09-29 07:19:30 +02:00
|
|
|
import fr.gouv.etalab.mastodon.services.StreamingFederatedTimelineService;
|
2017-09-30 09:00:00 +02:00
|
|
|
import fr.gouv.etalab.mastodon.services.StreamingLocalTimelineService;
|
2017-05-05 16:36:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
|
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Status;
|
|
|
|
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
|
2018-01-05 09:46:59 +01:00
|
|
|
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
2018-10-06 15:59:00 +02:00
|
|
|
import fr.gouv.etalab.mastodon.sqlite.InstancesDAO;
|
2018-01-04 17:39:16 +01:00
|
|
|
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
|
|
|
import fr.gouv.etalab.mastodon.sqlite.TempMuteDAO;
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 24/04/2017.
|
|
|
|
* Fragment to display content related to status
|
|
|
|
*/
|
2017-10-28 10:10:50 +02:00
|
|
|
public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsInterface, OnRetrieveMissingFeedsInterface {
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
private boolean flag_loading;
|
|
|
|
private Context context;
|
|
|
|
private AsyncTask<Void, Void, Void> asyncTask;
|
|
|
|
private StatusListAdapter statusListAdapter;
|
2018-10-06 15:59:00 +02:00
|
|
|
private PeertubeAdapter peertubeAdapater;
|
2017-05-05 16:36:04 +02:00
|
|
|
private String max_id;
|
2017-09-13 08:38:08 +02:00
|
|
|
private List<Status> statuses;
|
2018-10-20 16:26:45 +02:00
|
|
|
private List<Status> notFilteredStatuses;
|
2018-10-06 15:59:00 +02:00
|
|
|
private List<Peertube> peertubes;
|
2017-06-19 15:59:34 +02:00
|
|
|
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;
|
2017-09-30 09:00:00 +02:00
|
|
|
private Intent streamingFederatedIntent, streamingLocalIntent;
|
2017-10-24 14:06:28 +02:00
|
|
|
LinearLayoutManager mLayoutManager;
|
2017-12-09 07:34:36 +01:00
|
|
|
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-09-26 13:45:13 +02:00
|
|
|
private List<String> mutedAccount;
|
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-10-20 12:22:35 +02:00
|
|
|
|
2017-08-24 15:57:35 +02:00
|
|
|
public DisplayStatusFragment(){
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
@Override
|
2017-11-11 07:43:11 +01:00
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2017-05-05 16:36:04 +02:00
|
|
|
View rootView = inflater.inflate(R.layout.fragment_status, container, false);
|
2017-05-26 17:20:36 +02:00
|
|
|
statuses = new ArrayList<>();
|
2018-10-20 16:26:45 +02:00
|
|
|
notFilteredStatuses = 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();
|
2017-08-03 14:38:47 +02:00
|
|
|
showMediaOnly = false;
|
2017-12-09 07:34:36 +01:00
|
|
|
//Will allow to load first toots if bookmark != null
|
|
|
|
firstTootsLoaded = true;
|
2017-09-15 19:51:41 +02:00
|
|
|
showPinned = false;
|
2018-09-05 10:19:07 +02:00
|
|
|
showReply = false;
|
2017-05-05 16:36:04 +02:00
|
|
|
if (bundle != null) {
|
|
|
|
type = (RetrieveFeedsAsyncTask.Type) bundle.get("type");
|
|
|
|
targetedId = bundle.getString("targetedId", null);
|
|
|
|
tag = bundle.getString("tag", null);
|
2017-08-03 14:38:47 +02:00
|
|
|
showMediaOnly = bundle.getBoolean("showMediaOnly",false);
|
2017-09-15 19:51:41 +02:00
|
|
|
showPinned = bundle.getBoolean("showPinned",false);
|
2018-09-05 10:19:07 +02:00
|
|
|
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);
|
2018-10-22 16:18:11 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2018-10-06 15:59:00 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
if( !remoteInstance.equals("")){
|
|
|
|
List<RemoteInstance> remoteInstanceObj = new InstancesDAO(context, db).getInstanceByName(remoteInstance);
|
|
|
|
if( remoteInstanceObj != null && remoteInstanceObj.size() > 0)
|
|
|
|
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;
|
2017-11-15 09:44:12 +01:00
|
|
|
assert context != null;
|
2017-12-28 15:33:02 +01:00
|
|
|
sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
2017-12-06 16:45:07 +01:00
|
|
|
boolean isOnWifi = Helper.isOnWIFI(context);
|
|
|
|
int positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
|
2017-10-21 17:15:52 +02:00
|
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
|
2017-12-06 16:45:07 +01:00
|
|
|
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
|
2017-10-21 17:15:52 +02:00
|
|
|
lv_status = rootView.findViewById(R.id.lv_status);
|
2017-10-24 10:30:49 +02:00
|
|
|
mainLoader = rootView.findViewById(R.id.loader);
|
2017-10-21 17:15:52 +02:00
|
|
|
nextElementLoader = rootView.findViewById(R.id.loading_next_status);
|
2017-10-24 10:30:49 +02:00
|
|
|
textviewNoAction = rootView.findViewById(R.id.no_action);
|
2017-05-05 16:36:04 +02:00
|
|
|
mainLoader.setVisibility(View.VISIBLE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2018-01-05 09:46:59 +01:00
|
|
|
|
2018-10-20 16:26:45 +02:00
|
|
|
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
2018-09-26 13:45:13 +02:00
|
|
|
Account account = new AccountDAO(context, db).getAccountByID(userId);
|
|
|
|
mutedAccount = new TempMuteDAO(context, db).getAllTimeMuted(account);
|
|
|
|
|
2018-10-20 12:22:35 +02:00
|
|
|
if( search_peertube == null && (instanceType == null || instanceType.equals("MASTODON"))) {
|
2018-10-20 14:39:24 +02:00
|
|
|
BaseMainActivity.displayPeertube = null;
|
2018-10-06 15:59:00 +02:00
|
|
|
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses);
|
|
|
|
lv_status.setAdapter(statusListAdapter);
|
|
|
|
}else {
|
2018-10-20 14:39:24 +02:00
|
|
|
BaseMainActivity.displayPeertube = remoteInstance;
|
2018-10-07 10:45:34 +02:00
|
|
|
peertubeAdapater = new PeertubeAdapter(context, remoteInstance, this.peertubes);
|
2018-10-06 15:59:00 +02:00
|
|
|
lv_status.setAdapter(peertubeAdapater);
|
|
|
|
}
|
2017-10-24 14:06:28 +02:00
|
|
|
mLayoutManager = new LinearLayoutManager(context);
|
|
|
|
lv_status.setLayoutManager(mLayoutManager);
|
2018-01-05 09:46:59 +01:00
|
|
|
|
2018-01-10 14:39:01 +01:00
|
|
|
|
2017-12-28 17:25:36 +01:00
|
|
|
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, context!=null?Helper.getLiveInstance(context):null);
|
2017-05-26 17:20:36 +02:00
|
|
|
|
2018-10-20 14:39:24 +02:00
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE && search_peertube != null)
|
|
|
|
((Activity)context).setTitle(remoteInstance + " - " + search_peertube);
|
2018-10-22 19:19:39 +02:00
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE && remote_channel_name != null)
|
|
|
|
((Activity)context).setTitle(remote_channel_name + " - " + remoteInstance);
|
2017-11-15 09:44:12 +01:00
|
|
|
lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
2018-10-07 11:57:15 +02:00
|
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy)
|
2017-11-15 09:44:12 +01:00
|
|
|
{
|
2017-12-09 07:42:46 +01:00
|
|
|
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
|
2017-11-15 09:44:12 +01:00
|
|
|
if(dy > 0){
|
|
|
|
int visibleItemCount = mLayoutManager.getChildCount();
|
|
|
|
int totalItemCount = mLayoutManager.getItemCount();
|
|
|
|
if(firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
|
|
|
|
if(!flag_loading ) {
|
|
|
|
flag_loading = true;
|
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.USER)
|
2018-09-05 10:19:07 +02:00
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, showReply, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-11-15 09:44:12 +01:00
|
|
|
else if( type == RetrieveFeedsAsyncTask.Type.TAG)
|
2017-11-03 10:18:47 +01:00
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2018-10-20 12:22:35 +02:00
|
|
|
else if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE ) {
|
2018-10-22 19:19:39 +02:00
|
|
|
if( search_peertube == null) {
|
|
|
|
if( remote_channel_name == null)
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
else
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, remoteInstance, remote_channel_name, null,DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
2018-10-20 12:22:35 +02:00
|
|
|
else
|
|
|
|
asyncTask = new RetrievePeertubeSearchAsyncTask(context, remoteInstance, search_peertube, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}else
|
2017-11-03 10:18:47 +01:00
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-11-15 09:44:12 +01:00
|
|
|
|
|
|
|
nextElementLoader.setVisibility(View.VISIBLE);
|
2017-11-03 10:18:47 +01:00
|
|
|
}
|
2017-11-15 09:44:12 +01:00
|
|
|
} else {
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2017-11-03 10:18:47 +01:00
|
|
|
}
|
2017-12-09 07:42:46 +01:00
|
|
|
}
|
2018-01-10 15:56:13 +01:00
|
|
|
if(type == RetrieveFeedsAsyncTask.Type.HOME && statuses != null && statuses.size() > firstVisibleItem && firstVisibleItem >= 0)
|
2017-12-08 19:36:26 +01:00
|
|
|
if( context instanceof BaseMainActivity){
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
Long bookmarkL = Long.parseLong(statuses.get(firstVisibleItem).getId()) + 1;
|
2017-12-28 17:25:36 +01:00
|
|
|
editor.putString(Helper.BOOKMARK_ID + userId + instance, String.valueOf(bookmarkL));
|
2017-12-08 19:36:26 +01:00
|
|
|
editor.apply();
|
|
|
|
}
|
2017-11-15 09:44:12 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-10-07 14:48:51 +02:00
|
|
|
if( instanceType == null || instanceType.equals("MASTODON"))
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
|
|
|
if( statuses.size() > 0) {
|
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.HOME)
|
|
|
|
MainActivity.countNewStatus = 0;
|
|
|
|
isSwipped = true;
|
|
|
|
retrieveMissingToots(null);
|
|
|
|
}
|
2018-03-16 18:52:43 +01:00
|
|
|
}
|
2018-10-07 14:48:51 +02:00
|
|
|
});
|
|
|
|
else
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
|
|
|
if( peertubes.size() > 0) {
|
|
|
|
int size = peertubes.size();
|
|
|
|
isSwipped = true;
|
|
|
|
peertubes.clear();
|
|
|
|
peertubes = new ArrayList<>();
|
|
|
|
max_id = "0";
|
|
|
|
peertubeAdapater.notifyItemRangeRemoved(0, size);
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, remoteInstance, "0", DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-05-11 16:33:16 +02:00
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
|
|
|
|
switch (theme){
|
|
|
|
case Helper.THEME_LIGHT:
|
|
|
|
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
|
|
|
|
R.color.mastodonC2,
|
|
|
|
R.color.mastodonC3);
|
|
|
|
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.white));
|
|
|
|
break;
|
|
|
|
case Helper.THEME_DARK:
|
|
|
|
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4__,
|
|
|
|
R.color.mastodonC4,
|
|
|
|
R.color.mastodonC4);
|
|
|
|
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.mastodonC1_));
|
|
|
|
break;
|
|
|
|
case Helper.THEME_BLACK:
|
|
|
|
swipeRefreshLayout.setColorSchemeResources(R.color.dark_icon,
|
|
|
|
R.color.mastodonC2,
|
|
|
|
R.color.mastodonC3);
|
2018-05-12 12:06:43 +02:00
|
|
|
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.black_3));
|
2018-05-11 16:33:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-11-15 09:44:12 +01:00
|
|
|
if( context != null) {
|
|
|
|
if (type == RetrieveFeedsAsyncTask.Type.USER)
|
2018-09-05 10:19:07 +02:00
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, showReply,DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-11-15 09:44:12 +01:00
|
|
|
else if (type == RetrieveFeedsAsyncTask.Type.TAG)
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2018-10-20 12:22:35 +02:00
|
|
|
else if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE ) {
|
2018-10-22 19:19:39 +02:00
|
|
|
if( search_peertube == null) {
|
|
|
|
if( remote_channel_name == null)
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
else
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, remoteInstance, remote_channel_name, null,DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
2018-10-20 12:22:35 +02:00
|
|
|
else
|
|
|
|
asyncTask = new RetrievePeertubeSearchAsyncTask(context, remoteInstance, search_peertube, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}else {
|
2017-12-06 18:20:20 +01:00
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.HOME ){
|
|
|
|
String bookmark;
|
|
|
|
if( context instanceof BaseMainActivity){
|
|
|
|
bookmark = ((BaseMainActivity) context).getBookmark();
|
2017-12-10 10:23:23 +01:00
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, bookmark, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
firstTootsLoaded = false;
|
|
|
|
|
2017-12-06 18:20:20 +01:00
|
|
|
}
|
2017-12-09 12:40:53 +01:00
|
|
|
}else {
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, null, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-12-06 18:20:20 +01:00
|
|
|
}
|
2017-09-29 16:10:11 +02:00
|
|
|
}
|
2017-05-26 17:20:36 +02:00
|
|
|
}else {
|
2017-11-15 09:44:12 +01:00
|
|
|
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if( context != null){
|
|
|
|
if (type == RetrieveFeedsAsyncTask.Type.USER)
|
2018-09-05 10:19:07 +02:00
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, showReply,DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-11-15 09:44:12 +01:00
|
|
|
else if (type == RetrieveFeedsAsyncTask.Type.TAG)
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2018-10-20 12:22:35 +02:00
|
|
|
else if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE ) {
|
2018-10-22 19:19:39 +02:00
|
|
|
if( search_peertube == null) {
|
|
|
|
if( remote_channel_name == null)
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
else
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, remoteInstance, remote_channel_name, null,DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
2018-10-20 12:22:35 +02:00
|
|
|
else
|
|
|
|
asyncTask = new RetrievePeertubeSearchAsyncTask(context, remoteInstance, search_peertube, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}else {
|
2017-12-06 18:20:20 +01:00
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.HOME ){
|
|
|
|
String bookmark;
|
|
|
|
if( context instanceof BaseMainActivity){
|
|
|
|
bookmark = ((BaseMainActivity) context).getBookmark();
|
2017-12-10 10:23:23 +01:00
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, bookmark, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
firstTootsLoaded = false;
|
2017-12-06 18:20:20 +01:00
|
|
|
}
|
2017-12-09 12:40:53 +01:00
|
|
|
}else {
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, null, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-12-06 18:20:20 +01:00
|
|
|
}
|
2017-11-15 09:44:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 500);
|
2017-05-26 17:20:36 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle saveInstance)
|
|
|
|
{
|
|
|
|
super.onCreate(saveInstance);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(Context context) {
|
|
|
|
super.onAttach(context);
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:12:50 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroy (){
|
|
|
|
super.onDestroy();
|
2017-05-05 16:36:04 +02:00
|
|
|
if(asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
|
|
|
|
asyncTask.cancel(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-09-29 16:10:11 +02:00
|
|
|
public void onRetrieveFeeds(APIResponse apiResponse) {
|
2017-05-30 19:31:44 +02:00
|
|
|
mainLoader.setVisibility(View.GONE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2017-06-07 15:47:05 +02:00
|
|
|
//Discards 404 - error which can often happen due to toots which have been deleted
|
2018-10-10 08:33:36 +02:00
|
|
|
if( apiResponse == null || (apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404) ){
|
|
|
|
if( apiResponse == null)
|
|
|
|
Toast.makeText(context, R.string.toast_error,Toast.LENGTH_LONG).show();
|
|
|
|
else
|
|
|
|
Toast.makeText(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
|
2017-06-17 07:49:47 +02:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-08-19 19:10:43 +02:00
|
|
|
flag_loading = false;
|
2017-05-30 19:31:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-10-29 15:49:13 +01:00
|
|
|
|
2018-10-07 11:57:15 +02:00
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE && peertubeAdapater != null){
|
2018-10-07 10:45:34 +02:00
|
|
|
int previousPosition = this.peertubes.size();
|
|
|
|
if( max_id == null)
|
|
|
|
max_id = "0";
|
|
|
|
max_id = String.valueOf(Integer.valueOf(max_id) + 50);
|
|
|
|
this.peertubes.addAll(apiResponse.getPeertubes());
|
2018-10-07 14:48:51 +02:00
|
|
|
if( previousPosition == 0) {
|
|
|
|
peertubeAdapater = new PeertubeAdapter(context, remoteInstance, this.peertubes);
|
|
|
|
lv_status.setAdapter(peertubeAdapater);
|
|
|
|
}else
|
|
|
|
peertubeAdapater.notifyItemRangeInserted(previousPosition, apiResponse.getPeertubes().size());
|
2018-10-07 10:45:34 +02:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
firstLoad = false;
|
2018-10-07 11:57:15 +02:00
|
|
|
flag_loading = false;
|
2017-12-09 12:40:53 +01:00
|
|
|
}else {
|
2018-10-29 15:49:13 +01:00
|
|
|
//Add conversation in status
|
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.CONVERSATION ){
|
|
|
|
List<Conversation> conversations = apiResponse.getConversations();
|
|
|
|
List<Status> statusesConversations = new ArrayList<>();
|
2018-10-29 18:32:55 +01:00
|
|
|
for( Conversation conversation: conversations) {
|
|
|
|
Status status = new Status();
|
|
|
|
status = conversation.getLast_status();
|
|
|
|
List<String> ppConversation = new ArrayList<>();
|
|
|
|
for(Account account: conversation.getAccounts())
|
|
|
|
ppConversation.add(account.getAvatar());
|
|
|
|
status.setConversationProfilePicture(ppConversation);
|
|
|
|
statusesConversations.add(status);
|
|
|
|
}
|
2018-10-29 15:49:13 +01:00
|
|
|
apiResponse.setStatuses(statusesConversations);
|
|
|
|
}
|
2018-10-07 11:57:15 +02:00
|
|
|
int previousPosition = this.statuses.size();
|
|
|
|
List<Status> statuses = Helper.filterToots(context, apiResponse.getStatuses(), mutedAccount, type);
|
2018-10-20 16:26:45 +02:00
|
|
|
List<Status> notFilteredStatuses = apiResponse.getStatuses();
|
2018-10-07 11:57:15 +02:00
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.HOME) {
|
|
|
|
if (max_id == null || (apiResponse.getMax_id() != null && Long.parseLong(max_id) > Long.parseLong(apiResponse.getMax_id())))
|
|
|
|
max_id = apiResponse.getMax_id();
|
|
|
|
}else {
|
|
|
|
max_id = apiResponse.getMax_id();
|
|
|
|
}
|
|
|
|
flag_loading = (max_id == null );
|
|
|
|
if( firstLoad && (statuses == null || statuses.size() == 0))
|
|
|
|
textviewNoAction.setVisibility(View.VISIBLE);
|
|
|
|
else
|
|
|
|
textviewNoAction.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
//First toot are loaded as soon as the bookmark has been retrieved
|
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.HOME && !firstTootsLoaded){
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, null, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
firstTootsLoaded = true;
|
|
|
|
}
|
|
|
|
if( statuses != null && statuses.size() > 0) {
|
|
|
|
if (type == RetrieveFeedsAsyncTask.Type.HOME) {
|
|
|
|
String bookmark = null;
|
|
|
|
if( context instanceof BaseMainActivity){
|
|
|
|
bookmark = ((BaseMainActivity) context).getBookmark();
|
2017-12-08 21:18:39 +01:00
|
|
|
}
|
2018-10-07 11:57:15 +02:00
|
|
|
//Toots are older than the bookmark -> no special treatment with them
|
|
|
|
if( bookmark == null || Long.parseLong(statuses.get(0).getId())+1 < Long.parseLong(bookmark)){
|
|
|
|
this.statuses.addAll(statuses);
|
2018-10-20 16:26:45 +02:00
|
|
|
this.notFilteredStatuses.addAll(notFilteredStatuses);
|
2018-10-07 11:57:15 +02:00
|
|
|
statusListAdapter.notifyItemRangeInserted(previousPosition, statuses.size());
|
|
|
|
}else { //Toots are younger than the bookmark
|
|
|
|
String currentMaxId = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + userId + instance, null);
|
|
|
|
int position = 0;
|
2018-10-20 16:26:45 +02:00
|
|
|
int positionNotFiltered = 0;
|
2018-10-07 11:57:15 +02:00
|
|
|
while (position < this.statuses.size() && Long.parseLong(statuses.get(0).getId()) < Long.parseLong(this.statuses.get(position).getId())) {
|
|
|
|
position++;
|
2017-12-03 18:58:41 +01:00
|
|
|
}
|
2018-10-20 16:26:45 +02:00
|
|
|
while (position < this.notFilteredStatuses.size() && Long.parseLong(statuses.get(0).getId()) < Long.parseLong(this.notFilteredStatuses.get(position).getId())) {
|
|
|
|
positionNotFiltered++;
|
|
|
|
}
|
2018-10-07 11:57:15 +02:00
|
|
|
ArrayList<Status> tmpStatuses = new ArrayList<>();
|
|
|
|
for (Status tmpStatus : statuses) {
|
|
|
|
//Mark status at new ones when their id is greater than the bookmark id / Also increments counter
|
|
|
|
if (currentMaxId != null && Long.parseLong(tmpStatus.getId()) > Long.parseLong(currentMaxId)) {
|
|
|
|
tmpStatus.setNew(true);
|
|
|
|
MainActivity.countNewStatus++;
|
|
|
|
}
|
|
|
|
//Put the toot at its place in the list (id desc)
|
|
|
|
if( !this.statuses.contains(tmpStatus) ) { //Element not already addeds
|
|
|
|
tmpStatuses.add(tmpStatus);
|
|
|
|
}
|
2017-12-06 16:45:07 +01:00
|
|
|
}
|
2018-10-07 11:57:15 +02:00
|
|
|
int tootPerPage = sharedpreferences.getInt(Helper.SET_TOOTS_PER_PAGE, 40);
|
|
|
|
if( tmpStatuses.size() >= tootPerPage) {
|
|
|
|
if (tmpStatuses.size() > 0 && Long.parseLong(tmpStatuses.get(tmpStatuses.size() - 1).getId()) > Long.parseLong(bookmark)) {
|
|
|
|
tmpStatuses.get(tmpStatuses.size() - 1).setFetchMore(true);
|
|
|
|
}
|
2017-12-16 08:26:17 +01:00
|
|
|
}
|
2018-10-07 11:57:15 +02:00
|
|
|
this.statuses.addAll(position, tmpStatuses);
|
2018-10-20 16:26:45 +02:00
|
|
|
this.notFilteredStatuses.addAll(positionNotFiltered, tmpStatuses);
|
2018-10-07 11:57:15 +02:00
|
|
|
statusListAdapter.notifyItemRangeInserted(position, tmpStatuses.size());
|
|
|
|
if( tmpStatuses.size() < 3) //If new toots are only two
|
|
|
|
lv_status.scrollToPosition(0);
|
2018-10-25 14:58:29 +02:00
|
|
|
else {
|
2018-10-30 17:15:29 +01:00
|
|
|
lv_status.scrollToPosition(position + tmpStatuses.size());
|
2018-10-25 14:58:29 +02:00
|
|
|
}
|
2017-12-09 07:34:36 +01:00
|
|
|
}
|
2017-12-06 19:12:52 +01:00
|
|
|
|
2018-10-07 11:57:15 +02:00
|
|
|
}else {
|
|
|
|
this.statuses.addAll(statuses);
|
2018-10-20 16:26:45 +02:00
|
|
|
this.notFilteredStatuses.addAll(statuses);
|
2018-10-07 11:57:15 +02:00
|
|
|
statusListAdapter.notifyItemRangeInserted(previousPosition, statuses.size());
|
|
|
|
}
|
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.HOME ) {
|
|
|
|
//Update the id of the last toot retrieved
|
|
|
|
if( MainActivity.lastHomeId == null || Long.parseLong(statuses.get(0).getId()) > Long.parseLong(MainActivity.lastHomeId))
|
|
|
|
MainActivity.lastHomeId = statuses.get(0).getId();
|
|
|
|
if( firstLoad )
|
|
|
|
updateStatusLastId(statuses.get(0).getId());
|
|
|
|
}
|
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.HOME)
|
|
|
|
//Display new value in counter
|
|
|
|
try {
|
|
|
|
((MainActivity) context).updateHomeCounter();
|
|
|
|
}catch (Exception ignored){}
|
2017-09-24 18:44:42 +02:00
|
|
|
}
|
2018-10-07 11:57:15 +02:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
firstLoad = false;
|
2017-10-28 10:10:50 +02:00
|
|
|
|
2018-10-07 11:57:15 +02:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2017-08-17 15:48:36 +02:00
|
|
|
|
2017-09-27 17:52:23 +02:00
|
|
|
/**
|
|
|
|
* Deals with new status coming from the streaming api
|
|
|
|
* @param status Status
|
|
|
|
*/
|
2017-09-13 08:38:08 +02:00
|
|
|
public void refresh(Status status){
|
2017-09-06 15:26:58 +02:00
|
|
|
//New data are available
|
2017-11-15 09:44:12 +01:00
|
|
|
if (context == null)
|
|
|
|
return;
|
2017-11-15 19:02:33 +01:00
|
|
|
if( status.getId() != null && statuses != null && statuses.size() > 0 && statuses.get(0)!= null
|
2017-11-15 09:44:12 +01:00
|
|
|
&& Long.parseLong(status.getId()) > Long.parseLong(statuses.get(0).getId())) {
|
2018-09-26 13:45:13 +02:00
|
|
|
List<Status> tempToot = new ArrayList();
|
|
|
|
tempToot.add(status);
|
|
|
|
List<Status> tempTootResult = Helper.filterToots(context, tempToot, mutedAccount, type);
|
2018-09-28 07:31:07 +02:00
|
|
|
if( tempTootResult != null && tempTootResult.size() > 0)
|
2018-09-26 13:45:13 +02:00
|
|
|
status = tempTootResult.get(0);
|
2017-11-15 09:44:12 +01:00
|
|
|
if (type == RetrieveFeedsAsyncTask.Type.HOME) {
|
|
|
|
|
2017-12-28 15:33:02 +01:00
|
|
|
//Makes sure the status is not already displayed
|
2018-01-04 15:29:23 +01: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);
|
2018-10-20 16:26:45 +02:00
|
|
|
notFilteredStatuses.add(0, status);
|
2017-12-28 15:33:02 +01:00
|
|
|
if (!status.getAccount().getId().equals(userId))
|
|
|
|
MainActivity.countNewStatus++;
|
|
|
|
statusListAdapter.notifyItemInserted(0);
|
|
|
|
if (textviewNoAction.getVisibility() == View.VISIBLE)
|
|
|
|
textviewNoAction.setVisibility(View.GONE);
|
|
|
|
}
|
2017-11-15 09:44:12 +01:00
|
|
|
|
2018-01-04 15:29:23 +01:00
|
|
|
|
2018-10-10 10:42:30 +02:00
|
|
|
} else if (type == RetrieveFeedsAsyncTask.Type.PUBLIC || type == RetrieveFeedsAsyncTask.Type.LOCAL|| type == RetrieveFeedsAsyncTask.Type.DIRECT) {
|
2017-11-15 09:44:12 +01:00
|
|
|
|
2017-10-21 19:19:11 +02:00
|
|
|
status.setNew(false);
|
2017-10-24 11:03:36 +02:00
|
|
|
statuses.add(0, status);
|
2018-10-20 16:26:45 +02:00
|
|
|
notFilteredStatuses.add(0, status);
|
2017-10-24 14:06:28 +02:00
|
|
|
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
|
2017-11-15 09:44:12 +01:00
|
|
|
if (firstVisibleItem > 0)
|
2017-10-24 14:06:28 +02:00
|
|
|
statusListAdapter.notifyItemInserted(0);
|
|
|
|
else
|
|
|
|
statusListAdapter.notifyDataSetChanged();
|
2017-09-26 19:19:53 +02:00
|
|
|
if (textviewNoAction.getVisibility() == View.VISIBLE)
|
|
|
|
textviewNoAction.setVisibility(View.GONE);
|
2017-11-15 09:44:12 +01:00
|
|
|
|
2017-09-26 19:19:53 +02:00
|
|
|
}
|
2017-08-28 19:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 09:23:32 +02:00
|
|
|
/**
|
|
|
|
* Refresh status in list
|
|
|
|
*/
|
|
|
|
public void refreshFilter(){
|
2018-10-20 16:26:45 +02:00
|
|
|
statuses.clear();
|
|
|
|
statuses.addAll(Helper.filterToots(context, notFilteredStatuses, mutedAccount, type));
|
2017-10-11 09:23:32 +02:00
|
|
|
statusListAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2017-09-29 16:10:11 +02:00
|
|
|
@Override
|
|
|
|
public void onResume(){
|
|
|
|
super.onResume();
|
2017-12-29 11:21:24 +01:00
|
|
|
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
|
2017-09-29 16:10:11 +02:00
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.PUBLIC){
|
2017-09-30 08:29:39 +02:00
|
|
|
|
2017-09-29 19:07:42 +02:00
|
|
|
if( getUserVisibleHint() ){
|
2017-09-29 16:22:36 +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 16:22:36 +02:00
|
|
|
editor.apply();
|
2017-12-29 11:21:24 +01:00
|
|
|
if(liveNotifications) {
|
|
|
|
streamingFederatedIntent = new Intent(context, StreamingFederatedTimelineService.class);
|
2018-09-04 07:29:42 +02:00
|
|
|
try {
|
|
|
|
context.startService(streamingFederatedIntent);
|
|
|
|
}catch (Exception ignored){}
|
2017-12-29 11:21:24 +01:00
|
|
|
}
|
2017-10-07 08:34:50 +02:00
|
|
|
if( statuses != null && statuses.size() > 0)
|
|
|
|
retrieveMissingToots(statuses.get(0).getId());
|
2017-09-29 16:22:36 +02:00
|
|
|
}
|
2017-09-30 09:00:00 +02:00
|
|
|
}else if (type == RetrieveFeedsAsyncTask.Type.LOCAL){
|
2017-09-30 10:57:14 +02:00
|
|
|
|
2017-09-30 09:00:00 +02:00
|
|
|
if( getUserVisibleHint() ){
|
|
|
|
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();
|
2017-12-29 11:21:24 +01:00
|
|
|
if( liveNotifications) {
|
|
|
|
streamingLocalIntent = new Intent(context, StreamingLocalTimelineService.class);
|
2018-09-04 07:29:42 +02:00
|
|
|
try {
|
|
|
|
context.startService(streamingLocalIntent);
|
|
|
|
}catch (Exception ignored){}
|
2017-12-29 11:21:24 +01:00
|
|
|
}
|
2017-10-07 08:34:50 +02:00
|
|
|
if( statuses != null && statuses.size() > 0)
|
|
|
|
retrieveMissingToots(statuses.get(0).getId());
|
2017-09-30 09:00:00 +02:00
|
|
|
}
|
2018-10-26 17:00:38 +02:00
|
|
|
}else if (type == RetrieveFeedsAsyncTask.Type.DIRECT){
|
|
|
|
if( getUserVisibleHint() ){
|
|
|
|
if( statuses != null && statuses.size() > 0)
|
|
|
|
retrieveMissingToots(statuses.get(0).getId());
|
|
|
|
}
|
2018-01-04 17:39:16 +01:00
|
|
|
}else if (type == RetrieveFeedsAsyncTask.Type.HOME){
|
2018-01-05 09:46:59 +01:00
|
|
|
statusListAdapter.updateMuted(mutedAccount);
|
2018-03-16 18:52:43 +01:00
|
|
|
if( statuses != null && statuses.size() > 0)
|
|
|
|
retrieveMissingToots(statuses.get(0).getId());
|
2017-09-29 16:10:11 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 19:19:53 +02:00
|
|
|
|
2017-09-27 17:52:23 +02:00
|
|
|
/**
|
|
|
|
* Called from main activity in onResume to retrieve missing toots (home timeline)
|
|
|
|
* @param sinceId String
|
|
|
|
*/
|
|
|
|
public void retrieveMissingToots(String sinceId){
|
2018-08-24 15:47:16 +02:00
|
|
|
if (type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE)
|
2018-08-23 16:10:57 +02:00
|
|
|
asyncTask = new RetrieveMissingFeedsAsyncTask(context, remoteInstance, sinceId, type, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2018-08-24 15:47:16 +02:00
|
|
|
else
|
|
|
|
asyncTask = new RetrieveMissingFeedsAsyncTask(context, sinceId, type, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
|
2017-09-27 17:52:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When tab comes visible, first displayed toot is defined as read
|
|
|
|
* @param visible boolean
|
|
|
|
*/
|
2017-09-13 15:21:03 +02:00
|
|
|
@Override
|
|
|
|
public void setMenuVisibility(final boolean visible) {
|
|
|
|
super.setMenuVisibility(visible);
|
|
|
|
if( context == null)
|
|
|
|
return;
|
2017-12-29 11:21:24 +01:00
|
|
|
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
|
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
|
2017-09-13 18:59:55 +02:00
|
|
|
if (type == RetrieveFeedsAsyncTask.Type.HOME && visible && statuses != null && statuses.size() > 0) {
|
2017-12-28 15:42:06 +01:00
|
|
|
updateStatusLastId(statuses.get(0).getId());
|
2017-10-07 08:34:50 +02:00
|
|
|
} else if( type == RetrieveFeedsAsyncTask.Type.PUBLIC ){
|
2017-09-29 07:19:30 +02:00
|
|
|
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();
|
2017-12-29 11:21:24 +01:00
|
|
|
if(liveNotifications) {
|
|
|
|
streamingFederatedIntent = new Intent(context, StreamingFederatedTimelineService.class);
|
2018-09-04 07:29:42 +02:00
|
|
|
try {
|
|
|
|
context.startService(streamingFederatedIntent);
|
|
|
|
}catch (Exception ignored){}
|
2017-12-29 11:21:24 +01:00
|
|
|
}
|
2017-10-07 08:34:50 +02:00
|
|
|
if( statuses != null && statuses.size() > 0)
|
|
|
|
retrieveMissingToots(statuses.get(0).getId());
|
2017-09-29 07:19:30 +02:00
|
|
|
}else {
|
2017-11-11 10:05:37 +01:00
|
|
|
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();
|
2017-09-30 08:29:39 +02:00
|
|
|
context.stopService(streamingFederatedIntent);
|
2017-09-29 07:19:30 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-30 09:00:00 +02:00
|
|
|
}else if (type == RetrieveFeedsAsyncTask.Type.LOCAL){
|
|
|
|
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();
|
2017-12-29 11:21:24 +01:00
|
|
|
if( liveNotifications ) {
|
|
|
|
streamingLocalIntent = new Intent(context, StreamingLocalTimelineService.class);
|
2018-09-04 07:29:42 +02:00
|
|
|
try {
|
|
|
|
context.startService(streamingLocalIntent);
|
|
|
|
}catch (Exception ignored){}
|
2017-12-29 11:21:24 +01:00
|
|
|
}
|
2017-10-07 08:34:50 +02:00
|
|
|
if( statuses != null && statuses.size() > 0)
|
|
|
|
retrieveMissingToots(statuses.get(0).getId());
|
2017-09-30 09:00:00 +02:00
|
|
|
}else {
|
2017-11-11 10:05:37 +01:00
|
|
|
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-29 07:19:30 +02:00
|
|
|
}
|
2017-09-13 15:21:03 +02:00
|
|
|
}
|
|
|
|
|
2017-09-29 14:08:46 +02:00
|
|
|
@Override
|
|
|
|
public void onStop(){
|
|
|
|
super.onStop();
|
2017-09-29 16:22:36 +02:00
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.PUBLIC && 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();
|
2017-09-30 08:29:39 +02:00
|
|
|
context.stopService(streamingFederatedIntent);
|
2017-09-30 09:00:00 +02:00
|
|
|
}else if(type == RetrieveFeedsAsyncTask.Type.LOCAL && streamingLocalIntent != null){
|
|
|
|
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-29 14:08:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 15:48:36 +02:00
|
|
|
public void scrollToTop(){
|
2017-09-16 09:56:26 +02:00
|
|
|
if( lv_status != null) {
|
2017-08-17 15:48:36 +02:00
|
|
|
lv_status.setAdapter(statusListAdapter);
|
2017-09-16 09:56:26 +02:00
|
|
|
//Store last toot id for home timeline to avoid to notify for those that have been already seen
|
|
|
|
if (type == RetrieveFeedsAsyncTask.Type.HOME && statuses != null && statuses.size() > 0) {
|
2017-12-28 15:42:06 +01:00
|
|
|
updateStatusLastId(statuses.get(0).getId());
|
2017-09-16 09:56:26 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-17 15:48:36 +02:00
|
|
|
}
|
2017-08-18 17:50:54 +02:00
|
|
|
|
2017-09-27 17:52:23 +02:00
|
|
|
@Override
|
2018-09-26 13:45:13 +02:00
|
|
|
public void onRetrieveMissingFeeds(List<Status> statusesMissing) {
|
2018-03-10 12:18:31 +01:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2018-10-20 16:26:45 +02:00
|
|
|
|
2018-09-26 13:45:13 +02:00
|
|
|
List<Status> statuses = Helper.filterToots(context, statusesMissing, mutedAccount, type);
|
2018-04-25 14:03:15 +02:00
|
|
|
if( isSwipped && this.statuses != null && this.statuses.size() > 0) {
|
2018-04-23 17:53:37 +02:00
|
|
|
for (Status status : this.statuses) {
|
|
|
|
status.setNew(false);
|
|
|
|
}
|
|
|
|
statusListAdapter.notifyItemRangeChanged(0, this.statuses.size());
|
|
|
|
}
|
2018-04-25 14:03:15 +02:00
|
|
|
isSwipped = false;
|
2017-09-27 17:52:23 +02:00
|
|
|
if( statuses != null && statuses.size() > 0) {
|
2017-10-24 14:06:28 +02:00
|
|
|
int inserted = 0;
|
2017-10-24 11:03:36 +02:00
|
|
|
for (int i = statuses.size() - 1; i >= 0; i--) {
|
2018-09-26 13:45:13 +02:00
|
|
|
if( this.statuses != null) {
|
|
|
|
if (this.statuses.size() == 0 ||
|
|
|
|
Long.parseLong(statuses.get(i).getId()) > Long.parseLong(this.statuses.get(0).getId())) {
|
|
|
|
if (type == RetrieveFeedsAsyncTask.Type.HOME)
|
|
|
|
statuses.get(i).setNew(true);
|
|
|
|
inserted++;
|
|
|
|
this.statuses.add(0, statuses.get(i));
|
2018-10-20 16:26:45 +02:00
|
|
|
notFilteredStatuses.add(0, statuses.get(i));
|
2018-09-26 13:45:13 +02:00
|
|
|
if (type == RetrieveFeedsAsyncTask.Type.HOME && !statuses.get(i).getAccount().getId().equals(userId))
|
|
|
|
MainActivity.countNewStatus++;
|
|
|
|
}
|
2017-10-07 13:00:08 +02:00
|
|
|
}
|
2017-09-27 17:52:23 +02:00
|
|
|
}
|
2017-11-15 18:38:53 +01:00
|
|
|
statusListAdapter.notifyItemRangeInserted(0, inserted);
|
2017-09-27 17:52:23 +02:00
|
|
|
try {
|
2017-10-25 13:45:22 +02:00
|
|
|
if( type == RetrieveFeedsAsyncTask.Type.HOME)
|
|
|
|
((MainActivity) context).updateHomeCounter();
|
|
|
|
else
|
|
|
|
((MainActivity) context).updateTimeLine(type, inserted);
|
2017-09-27 17:52:23 +02:00
|
|
|
}catch (Exception ignored){}
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 16:56:59 +01:00
|
|
|
|
|
|
|
public void fetchMore(String max_id){
|
|
|
|
asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
2017-12-08 19:36:26 +01:00
|
|
|
|
2017-12-28 15:33:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Records the id of the status only if its greater than the previous one.
|
|
|
|
* @param statusId String current status id to check
|
|
|
|
*/
|
2017-12-28 15:42:06 +01:00
|
|
|
private void updateStatusLastId(String statusId){
|
2017-12-28 15:33:02 +01:00
|
|
|
|
2017-12-28 17:25:36 +01:00
|
|
|
String lastNotif = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + userId + instance, null);
|
2017-12-28 15:33:02 +01:00
|
|
|
if( lastNotif == null || Long.parseLong(statusId) > Long.parseLong(lastNotif)){
|
|
|
|
MainActivity.countNewStatus = 0;
|
2017-12-08 19:36:26 +01:00
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
2017-12-28 17:25:36 +01:00
|
|
|
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId + instance, statusId);
|
2017-12-08 19:36:26 +01:00
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|