fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java

574 lines
29 KiB
Java
Raw Normal View History

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>. */
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;
import android.os.AsyncTask;
2017-08-26 19:39:11 +02:00
import android.os.Build;
2017-05-05 16:36:04 +02:00
import android.os.Bundle;
import android.os.Handler;
2017-05-26 17:20:36 +02:00
import android.os.Parcelable;
2017-05-05 16:36:04 +02:00
import android.support.v4.app.Fragment;
2017-08-26 19:39:11 +02:00
import android.support.v4.content.LocalBroadcastManager;
2017-08-23 09:56:42 +02:00
import android.support.v4.view.ViewCompat;
2017-05-05 16:36:04 +02:00
import android.support.v4.widget.SwipeRefreshLayout;
2017-09-22 07:22:38 +02:00
import android.util.Log;
2017-05-05 16:36:04 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
2017-05-05 16:36:04 +02:00
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
2017-05-05 16:36:04 +02:00
import java.util.List;
import java.util.concurrent.TimeUnit;
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;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRepliesAsyncTask;
2017-06-03 18:18:27 +02:00
import fr.gouv.etalab.mastodon.client.APIResponse;
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;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRepliesInterface;
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 mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
/**
* Created by Thomas on 24/04/2017.
* Fragment to display content related to status
*/
2017-09-27 17:52:23 +02:00
public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsInterface, OnRetrieveRepliesInterface, 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;
private String max_id;
private List<Status> statuses;
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-06-11 17:14:54 +02:00
private boolean swiped;
2017-06-16 18:54:46 +02:00
private ListView lv_status;
private boolean isOnWifi;
private int behaviorWithAttachments;
private boolean showMediaOnly, showPinned;
2017-08-25 16:08:40 +02:00
private int positionSpinnerTrans;
2017-08-26 19:39:11 +02:00
private boolean hideHeader;
private String instanceValue;
private String lastReadStatus;
2017-09-30 09:00:00 +02:00
private Intent streamingFederatedIntent, streamingLocalIntent;
2017-08-24 15:57:35 +02:00
public DisplayStatusFragment(){
}
2017-05-05 16:36:04 +02:00
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_status, container, false);
2017-05-26 17:20:36 +02:00
statuses = new ArrayList<>();
2017-05-05 16:36:04 +02:00
context = getContext();
Bundle bundle = this.getArguments();
2017-06-11 17:14:54 +02:00
boolean comesFromSearch = false;
2017-08-26 19:39:11 +02:00
hideHeader = false;
showMediaOnly = false;
showPinned = 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-26 19:39:11 +02:00
instanceValue = bundle.getString("hideHeaderValue", null);
2017-05-20 19:40:46 +02:00
hideHeader = bundle.getBoolean("hideHeader", false);
showMediaOnly = bundle.getBoolean("showMediaOnly",false);
showPinned = bundle.getBoolean("showPinned",false);
2017-05-26 17:20:36 +02:00
if( bundle.containsKey("statuses")){
ArrayList<Parcelable> statusesReceived = bundle.getParcelableArrayList("statuses");
assert statusesReceived != null;
for(Parcelable status: statusesReceived){
statuses.add((Status) status);
}
comesFromSearch = true;
}
2017-05-05 16:36:04 +02:00
}
max_id = null;
flag_loading = true;
firstLoad = true;
2017-06-11 17:14:54 +02:00
swiped = false;
2017-08-24 15:57:35 +02:00
2017-08-25 16:08:40 +02:00
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
2017-06-16 18:54:46 +02:00
isOnWifi = Helper.isOnWIFI(context);
2017-08-25 16:08:40 +02:00
positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
2017-05-05 16:36:04 +02:00
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
2017-06-16 18:54:46 +02:00
behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2017-09-24 08:47:25 +02:00
if( type == RetrieveFeedsAsyncTask.Type.HOME)
lastReadStatus = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, null);
2017-06-16 18:54:46 +02:00
lv_status = (ListView) rootView.findViewById(R.id.lv_status);
2017-05-05 16:36:04 +02:00
mainLoader = (RelativeLayout) rootView.findViewById(R.id.loader);
nextElementLoader = (RelativeLayout) rootView.findViewById(R.id.loading_next_status);
textviewNoAction = (RelativeLayout) rootView.findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE);
nextElementLoader.setVisibility(View.GONE);
2017-08-25 16:08:40 +02:00
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses);
2017-05-05 16:36:04 +02:00
lv_status.setAdapter(statusListAdapter);
2017-05-26 17:20:36 +02:00
if( !comesFromSearch){
2017-05-20 19:40:46 +02:00
2017-05-26 17:20:36 +02:00
//Hide account header when scrolling for ShowAccountActivity
2017-08-26 19:39:11 +02:00
if (hideHeader && Build.VERSION.SDK_INT >= 21)
ViewCompat.setNestedScrollingEnabled(lv_status, true);
2017-06-03 18:18:27 +02:00
2017-08-26 19:39:11 +02:00
lv_status.setOnScrollListener(new AbsListView.OnScrollListener() {
int lastFirstVisibleItem = 0;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (hideHeader && Build.VERSION.SDK_INT < 21) {
if(firstVisibleItem == 0 && Helper.listIsAtTop(lv_status)){
Intent intent = new Intent(Helper.HEADER_ACCOUNT+instanceValue);
intent.putExtra("hide", false);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}else if (view.getId() == lv_status.getId() && totalItemCount > visibleItemCount) {
final int currentFirstVisibleItem = lv_status.getFirstVisiblePosition();
if (currentFirstVisibleItem > lastFirstVisibleItem) {
Intent intent = new Intent(Helper.HEADER_ACCOUNT + instanceValue);
intent.putExtra("hide", true);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
} else if (currentFirstVisibleItem < lastFirstVisibleItem) {
Intent intent = new Intent(Helper.HEADER_ACCOUNT + instanceValue);
intent.putExtra("hide", false);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
2017-06-03 18:18:27 +02:00
}
2017-08-26 19:39:11 +02:00
lastFirstVisibleItem = currentFirstVisibleItem;
2017-06-03 18:18:27 +02:00
}
2017-05-26 17:20:36 +02:00
}
2017-08-26 19:39:11 +02:00
if(firstVisibleItem + visibleItemCount == totalItemCount ) {
if(!flag_loading ) {
flag_loading = true;
if( type == RetrieveFeedsAsyncTask.Type.USER)
asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-08-26 19:39:11 +02:00
else if( type == RetrieveFeedsAsyncTask.Type.TAG)
asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
nextElementLoader.setVisibility(View.VISIBLE);
}
} else {
nextElementLoader.setVisibility(View.GONE);
}
}
});
2017-05-26 17:20:36 +02:00
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
max_id = null;
statuses = new ArrayList<>();
firstLoad = true;
flag_loading = true;
2017-06-11 17:14:54 +02:00
swiped = true;
2017-09-13 18:59:55 +02:00
MainActivity.countNewStatus = 0;
2017-05-26 17:20:36 +02:00
if( type == RetrieveFeedsAsyncTask.Type.USER)
asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-05-26 17:20:36 +02:00
else if( type == RetrieveFeedsAsyncTask.Type.TAG)
asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
2017-08-13 11:30:28 +02:00
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
R.color.mastodonC2,
R.color.mastodonC3);
2017-05-26 17:20:36 +02:00
if( type == RetrieveFeedsAsyncTask.Type.USER)
asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-05-26 17:20:36 +02:00
else if( type == RetrieveFeedsAsyncTask.Type.TAG)
asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else {
2017-05-26 17:20:36 +02:00
asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
2017-05-26 17:20:36 +02:00
}else {
statusListAdapter.notifyDataSetChanged();
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
if( statuses == null || statuses.size() == 0 )
textviewNoAction.setVisibility(View.VISIBLE);
}
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;
}
@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
public void onRetrieveFeeds(APIResponse apiResponse) {
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
2017-08-24 15:57:35 +02:00
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
2017-06-07 15:47:05 +02:00
if( apiResponse.getError() != null && !apiResponse.getError().getError().startsWith("404 -")){
boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true);
if( show_error_messages)
Toast.makeText(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
swipeRefreshLayout.setRefreshing(false);
swiped = false;
2017-08-19 19:10:43 +02:00
flag_loading = false;
return;
}
List<Status> statuses = apiResponse.getStatuses();
2017-08-26 15:50:10 +02:00
max_id = apiResponse.getMax_id();
2017-08-24 15:57:35 +02:00
flag_loading = (max_id == null );
2017-06-11 17:14:54 +02:00
if( !swiped && firstLoad && (statuses == null || statuses.size() == 0))
2017-05-05 16:36:04 +02:00
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
2017-06-16 18:54:46 +02:00
if( swiped ){
2017-08-25 16:08:40 +02:00
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses);
2017-06-16 18:54:46 +02:00
lv_status.setAdapter(statusListAdapter);
swiped = false;
}
2017-09-24 19:27:04 +02:00
if( statuses != null && statuses.size() > 0) {
2017-09-24 19:27:04 +02:00
ArrayList<String> knownId = new ArrayList<>();
for(Status st: this.statuses){
knownId.add(st.getId());
}
2017-05-05 16:36:04 +02:00
for(Status tmpStatus: statuses){
2017-09-24 19:27:04 +02:00
if( !knownId.contains(tmpStatus.getId())) {
if( type == RetrieveFeedsAsyncTask.Type.HOME && firstLoad && lastReadStatus != null && Long.parseLong(tmpStatus.getId()) > Long.parseLong(lastReadStatus)){
tmpStatus.setNew(true);
MainActivity.countNewStatus++;
}else {
tmpStatus.setNew(false);
}
this.statuses.add(tmpStatus);
}
2017-05-05 16:36:04 +02:00
}
2017-09-24 18:44:42 +02:00
if( firstLoad && type == RetrieveFeedsAsyncTask.Type.HOME) {
2017-09-27 17:52:23 +02:00
//Update the id of the last toot retrieved
MainActivity.lastHomeId = statuses.get(0).getId();
2017-09-24 18:44:42 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, statuses.get(0).getId());
editor.apply();
lastReadStatus = statuses.get(0).getId();
}
2017-05-05 16:36:04 +02:00
statusListAdapter.notifyDataSetChanged();
2017-09-24 18:44:42 +02:00
if( firstLoad && type == RetrieveFeedsAsyncTask.Type.HOME)
2017-09-24 08:47:25 +02:00
//Display new value in counter
try {
((MainActivity) context).updateHomeCounter();
}catch (Exception ignored){}
2017-05-05 16:36:04 +02:00
}
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
2017-05-20 19:40:46 +02:00
//Retrieves replies
if(statuses != null && statuses.size() > 0 && type == RetrieveFeedsAsyncTask.Type.HOME ) {
boolean showPreview = sharedpreferences.getBoolean(Helper.SET_PREVIEW_REPLIES, false);
//Retrieves attached replies to a toot
if (showPreview) {
new RetrieveRepliesAsyncTask(context, statuses, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
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
* @param status Status
*/
public void refresh(Status status){
2017-09-06 15:26:58 +02:00
//New data are available
if( type == RetrieveFeedsAsyncTask.Type.HOME) {
if (context == null)
return;
if (status != null) {
2017-09-27 17:52:23 +02:00
//Update the id of the last toot retrieved
MainActivity.lastHomeId = status.getId();
int index = lv_status.getFirstVisiblePosition() + 1;
View v = lv_status.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
2017-09-14 19:07:30 +02:00
status.setReplies(new ArrayList<Status>());
statuses.add(0,status);
2017-09-24 19:27:04 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if( !status.getAccount().getId().equals(userId))
MainActivity.countNewStatus++;
statusListAdapter.notifyDataSetChanged();
lv_status.setSelectionFromTop(index, top);
if (textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE);
2017-08-29 08:55:03 +02:00
}
2017-09-30 11:25:56 +02:00
}else if(type == RetrieveFeedsAsyncTask.Type.PUBLIC || type == RetrieveFeedsAsyncTask.Type.LOCAL){
if (context == null)
return;
//Avoids the array to be too big...
if (status != null) {
2017-09-30 11:49:18 +02:00
if (lv_status.getFirstVisiblePosition() == 0) {
status.setReplies(new ArrayList<Status>());
status.setNew(false);
statuses.add(0, status);
statusListAdapter.notifyDataSetChanged();
} else {
status.setReplies(new ArrayList<Status>());
statuses.add(0, status);
}
if (textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE);
}
2017-08-28 19:12:40 +02:00
}
}
@Override
public void onResume(){
super.onResume();
if( type == RetrieveFeedsAsyncTask.Type.PUBLIC){
2017-09-29 19:07:42 +02:00
if( getUserVisibleHint() ){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED+userId, true);
editor.apply();
streamingFederatedIntent = new Intent(context, StreamingFederatedTimelineService.class);
context.startService(streamingFederatedIntent);
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 if (type == RetrieveFeedsAsyncTask.Type.LOCAL){
2017-09-30 09:00:00 +02:00
if( getUserVisibleHint() ){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL+userId, true);
editor.apply();
streamingLocalIntent = new Intent(context, StreamingLocalTimelineService.class);
context.startService(streamingLocalIntent);
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
}
}
}
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){
2017-10-07 08:34:50 +02:00
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;
//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-09-13 15:21:03 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, statuses.get(0).getId());
2017-09-13 18:59:55 +02:00
lastReadStatus = statuses.get(0).getId();
2017-09-13 15:21:03 +02:00
editor.apply();
2017-10-07 08:34:50 +02:00
} else if( type == RetrieveFeedsAsyncTask.Type.PUBLIC ){
if (visible) {
2017-09-29 14:08:46 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED+userId, true);
editor.apply();
streamingFederatedIntent = new Intent(context, StreamingFederatedTimelineService.class);
context.startService(streamingFederatedIntent);
2017-10-07 08:34:50 +02:00
if( statuses != null && statuses.size() > 0)
retrieveMissingToots(statuses.get(0).getId());
}else {
if( streamingFederatedIntent != null){
2017-09-29 14:08:46 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED+userId, false);
editor.apply();
context.stopService(streamingFederatedIntent);
}
}
2017-09-30 09:00:00 +02:00
}else if (type == RetrieveFeedsAsyncTask.Type.LOCAL){
if (visible) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL+userId, true);
editor.apply();
streamingLocalIntent = new Intent(context, StreamingLocalTimelineService.class);
context.startService(streamingLocalIntent);
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 {
if( streamingLocalIntent != null){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL+userId, false);
editor.apply();
context.stopService(streamingLocalIntent);
}
}
}
2017-09-13 15:21:03 +02:00
}
2017-09-29 14:08:46 +02:00
@Override
public void onStop(){
super.onStop();
if( type == RetrieveFeedsAsyncTask.Type.PUBLIC && streamingFederatedIntent != null){
2017-09-29 14:08:46 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED+userId, false);
editor.apply();
context.stopService(streamingFederatedIntent);
2017-09-30 09:00:00 +02:00
}else if(type == RetrieveFeedsAsyncTask.Type.LOCAL && streamingLocalIntent != null){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL+userId, false);
editor.apply();
context.stopService(streamingLocalIntent);
2017-09-29 14:08:46 +02:00
}
}
public void scrollToTop(){
2017-09-16 09:56:26 +02:00
if( lv_status != null) {
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) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, statuses.get(0).getId());
lastReadStatus = statuses.get(0).getId();
editor.apply();
}
}
}
@Override
public void onRetrieveReplies(APIResponse apiResponse) {
if( apiResponse.getError() != null || apiResponse.getStatuses() == null || apiResponse.getStatuses().size() == 0){
return;
}
List<Status> modifiedStatus = apiResponse.getStatuses();
for(Status stmp: modifiedStatus){
for(Status status: statuses){
if( status.getId().equals(stmp.getId()))
2017-09-14 19:07:30 +02:00
if( stmp.getReplies() != null )
status.setReplies(stmp.getReplies());
else
status.setReplies(new ArrayList<Status>());
}
}
statusListAdapter.notifyDataSetChanged();
}
2017-09-27 17:52:23 +02:00
@Override
public void onRetrieveMissingFeeds(List<Status> statuses) {
if( statuses != null && statuses.size() > 0) {
ArrayList<String> knownId = new ArrayList<>();
for (Status st : this.statuses) {
knownId.add(st.getId());
}
2017-10-07 13:00:08 +02:00
if( lv_status.getFirstVisiblePosition() > 1 ) {
int index = lv_status.getFirstVisiblePosition() + statuses.size();
View v = lv_status.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
for (int i = statuses.size() - 1; i >= 0; i--) {
if (!knownId.contains(statuses.get(i).getId())) {
if (type == RetrieveFeedsAsyncTask.Type.HOME)
statuses.get(i).setNew(true);
statuses.get(i).setReplies(new ArrayList<Status>());
this.statuses.add(0, statuses.get(i));
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if (type == RetrieveFeedsAsyncTask.Type.HOME && !statuses.get(i).getAccount().getId().equals(userId))
MainActivity.countNewStatus++;
}
2017-09-27 17:52:23 +02:00
}
2017-10-07 13:00:08 +02:00
statusListAdapter.notifyDataSetChanged();
lv_status.setSelectionFromTop(index, top);
}else {
for (int i = statuses.size() - 1; i >= 0; i--) {
if (!knownId.contains(statuses.get(i).getId())) {
if (type == RetrieveFeedsAsyncTask.Type.HOME)
statuses.get(i).setNew(true);
statuses.get(i).setReplies(new ArrayList<Status>());
this.statuses.add(0, statuses.get(i));
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if (type == RetrieveFeedsAsyncTask.Type.HOME && !statuses.get(i).getAccount().getId().equals(userId))
MainActivity.countNewStatus++;
}
}
statusListAdapter.notifyDataSetChanged();
2017-09-27 17:52:23 +02:00
}
try {
((MainActivity) context).updateHomeCounter();
}catch (Exception ignored){}
}
}
2017-05-05 16:36:04 +02:00
}