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-11-25 10:45:16 +01:00
|
|
|
|
2019-01-11 14:55:53 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.content.Context;
|
2019-01-11 14:55:53 +01:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
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;
|
2019-01-11 14:55:53 +01:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2017-10-29 09:30:08 +01:00
|
|
|
import android.support.v7.widget.DividerItemDecoration;
|
2017-10-24 14:30:07 +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;
|
2018-11-25 10:45:16 +01:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2017-09-13 08:47:59 +02:00
|
|
|
|
2018-11-25 10:45:16 +01:00
|
|
|
import es.dmoral.toasty.Toasty;
|
2017-11-12 08:33:24 +01:00
|
|
|
import fr.gouv.etalab.mastodon.R;
|
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.RetrieveMissingNotificationsAsyncTask;
|
2018-11-25 10:45:16 +01:00
|
|
|
import fr.gouv.etalab.mastodon.asynctasks.RetrieveNotificationsAsyncTask;
|
2019-01-11 14:55:53 +01:00
|
|
|
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
|
2019-01-12 09:26:18 +01:00
|
|
|
import fr.gouv.etalab.mastodon.client.API;
|
2017-06-03 18:18:27 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.APIResponse;
|
2017-12-28 17:32:17 +01:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
2018-11-25 10:45:16 +01:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Notification;
|
2019-01-11 14:55:53 +01:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Status;
|
2017-05-05 16:36:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
|
|
|
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
2017-09-27 17:52:23 +02:00
|
|
|
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingNotificationsInterface;
|
2017-05-05 16:36:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 28/04/2017.
|
|
|
|
* Fragment to display notifications related to accounts
|
|
|
|
*/
|
2017-09-27 17:52:23 +02:00
|
|
|
public class DisplayNotificationsFragment extends Fragment implements OnRetrieveNotificationsInterface, OnRetrieveMissingNotificationsInterface {
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean flag_loading;
|
|
|
|
private Context context;
|
|
|
|
private AsyncTask<Void, Void, Void> asyncTask;
|
|
|
|
private NotificationsListAdapter notificationsListAdapter;
|
2017-06-03 15:54:19 +02:00
|
|
|
private String max_id;
|
2017-09-13 08:38:08 +02:00
|
|
|
private List<Notification> notifications;
|
2017-05-05 16:36:04 +02:00
|
|
|
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
|
|
|
|
private boolean firstLoad;
|
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
2017-06-11 17:14:54 +02:00
|
|
|
private boolean swiped;
|
2017-10-24 14:30:07 +02:00
|
|
|
private RecyclerView lv_notifications;
|
2017-12-28 17:25:36 +01:00
|
|
|
private String userId, instance;
|
2017-12-28 15:33:02 +01:00
|
|
|
private SharedPreferences sharedpreferences;
|
2017-10-24 14:30:07 +02:00
|
|
|
LinearLayoutManager mLayoutManager;
|
2019-01-11 14:55:53 +01:00
|
|
|
private BroadcastReceiver receive_action;
|
2019-01-11 16:39:01 +01:00
|
|
|
private BroadcastReceiver receive_data;
|
2017-08-24 15:57:35 +02:00
|
|
|
|
|
|
|
public DisplayNotificationsFragment(){
|
|
|
|
}
|
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_notifications, container, false);
|
2017-06-03 15:54:19 +02:00
|
|
|
max_id = null;
|
2017-05-05 16:36:04 +02:00
|
|
|
context = getContext();
|
|
|
|
firstLoad = true;
|
|
|
|
flag_loading = true;
|
|
|
|
notifications = new ArrayList<>();
|
2017-06-11 17:14:54 +02:00
|
|
|
swiped = false;
|
2017-10-24 14:30:07 +02:00
|
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
|
2017-12-28 15:33:02 +01:00
|
|
|
sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
|
|
|
|
|
2017-10-24 14:30:07 +02:00
|
|
|
lv_notifications = rootView.findViewById(R.id.lv_notifications);
|
2017-10-29 09:30:08 +01:00
|
|
|
lv_notifications.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
|
2017-10-24 14:30:07 +02:00
|
|
|
mainLoader = rootView.findViewById(R.id.loader);
|
|
|
|
nextElementLoader = rootView.findViewById(R.id.loading_next_notifications);
|
|
|
|
textviewNoAction = rootView.findViewById(R.id.no_action);
|
2017-05-05 16:36:04 +02:00
|
|
|
mainLoader.setVisibility(View.VISIBLE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2017-08-04 14:38:18 +02:00
|
|
|
boolean isOnWifi = Helper.isOnWIFI(context);
|
|
|
|
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
|
2017-09-13 08:47:59 +02:00
|
|
|
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
2017-12-28 17:25:36 +01:00
|
|
|
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, context!=null?Helper.getLiveInstance(context):null);
|
2017-08-04 14:38:18 +02:00
|
|
|
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments,this.notifications);
|
2017-05-05 16:36:04 +02:00
|
|
|
lv_notifications.setAdapter(notificationsListAdapter);
|
2017-10-24 14:30:07 +02:00
|
|
|
mLayoutManager = new LinearLayoutManager(context);
|
|
|
|
lv_notifications.setLayoutManager(mLayoutManager);
|
|
|
|
lv_notifications.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
|
|
|
|
{
|
|
|
|
if(dy > 0) {
|
|
|
|
int visibleItemCount = mLayoutManager.getChildCount();
|
|
|
|
int totalItemCount = mLayoutManager.getItemCount();
|
|
|
|
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
|
2017-11-03 08:38:36 +01:00
|
|
|
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
|
2017-10-24 14:30:07 +02:00
|
|
|
if (!flag_loading) {
|
|
|
|
flag_loading = true;
|
2017-12-28 17:32:17 +01:00
|
|
|
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-10-24 14:30:07 +02:00
|
|
|
nextElementLoader.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-11 14:55:53 +01:00
|
|
|
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
|
|
|
|
|
|
|
|
if( receive_action != null)
|
|
|
|
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-01-12 09:26:18 +01:00
|
|
|
API.StatusAction statusAction = (API.StatusAction) b.getSerializable("action");
|
2019-01-11 14:55:53 +01:00
|
|
|
if( status != null) {
|
2019-01-12 09:26:18 +01:00
|
|
|
notificationsListAdapter.notifyNotificationWithActionChanged(statusAction, status);
|
2019-01-11 14:55:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
LocalBroadcastManager.getInstance(context).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_ACTION));
|
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)) {
|
|
|
|
Notification notification = b.getParcelable("data");
|
|
|
|
refresh(notification);
|
|
|
|
if( context instanceof MainActivity)
|
|
|
|
((MainActivity)context).updateNotifCounter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
LocalBroadcastManager.getInstance(context).registerReceiver(receive_data, new IntentFilter(Helper.RECEIVE_DATA));
|
2019-01-11 14:55:53 +01:00
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
|
|
|
max_id = null;
|
|
|
|
firstLoad = true;
|
|
|
|
flag_loading = true;
|
2017-06-11 17:14:54 +02:00
|
|
|
swiped = true;
|
2017-09-13 18:59:55 +02:00
|
|
|
MainActivity.countNewNotifications = 0;
|
2019-01-05 09:38:57 +01:00
|
|
|
try {
|
|
|
|
((MainActivity) context).updateNotifCounter();
|
|
|
|
}catch (Exception ignored){}
|
2019-01-02 13:50:11 +01:00
|
|
|
String sinceId = null;
|
|
|
|
if( notifications != null && notifications.size() > 0 )
|
|
|
|
sinceId = notifications.get(0).getId();
|
2017-11-03 08:38:36 +01:00
|
|
|
if( context != null)
|
2019-01-02 13:50:11 +01:00
|
|
|
asyncTask = new RetrieveMissingNotificationsAsyncTask(context, sinceId, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
});
|
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-03 10:18:47 +01:00
|
|
|
if( context != null)
|
2017-12-28 17:32:17 +01:00
|
|
|
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-11-03 10:18:47 +01:00
|
|
|
else
|
|
|
|
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if( context != null)
|
2017-12-28 17:32:17 +01:00
|
|
|
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-11-03 10:18:47 +01:00
|
|
|
}
|
|
|
|
}, 500);
|
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
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2017-05-05 16:36:04 +02:00
|
|
|
if(asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
|
|
|
|
asyncTask.cancel(true);
|
2019-01-11 14:55:53 +01:00
|
|
|
if( receive_action != null)
|
|
|
|
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_action);
|
2019-01-11 16:39:01 +01:00
|
|
|
if( receive_data != null)
|
|
|
|
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_data);
|
2019-01-13 11:34:32 +01:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-28 17:32:17 +01:00
|
|
|
public void onRetrieveNotifications(APIResponse apiResponse, Account account, boolean refreshData) {
|
2017-05-30 19:31:44 +02:00
|
|
|
mainLoader.setVisibility(View.GONE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2018-01-09 18:56:23 +01:00
|
|
|
String lastReadNotifications = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, null);
|
2017-06-03 18:18:27 +02:00
|
|
|
if( apiResponse.getError() != null){
|
2018-11-25 10:45:16 +01:00
|
|
|
Toasty.error(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
|
2017-06-17 07:49:47 +02:00
|
|
|
flag_loading = false;
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
swiped = false;
|
2017-05-30 19:31:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-12-28 14:03:08 +01:00
|
|
|
|
2017-11-20 16:05:18 +01:00
|
|
|
int previousPosition = notifications.size();
|
2017-08-25 10:25:13 +02:00
|
|
|
max_id = apiResponse.getMax_id();
|
2017-09-13 15:21:03 +02:00
|
|
|
List<Notification> notifications = apiResponse.getNotifications();
|
2017-09-24 18:44:42 +02:00
|
|
|
|
2017-06-11 17:14:54 +02:00
|
|
|
if( !swiped && firstLoad && (notifications == null || notifications.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-11-20 16:27:40 +01:00
|
|
|
if (previousPosition > 0) {
|
|
|
|
for (int i = 0; i < previousPosition; i++) {
|
|
|
|
this.notifications.remove(0);
|
|
|
|
}
|
|
|
|
notificationsListAdapter.notifyItemRangeRemoved(0, previousPosition);
|
|
|
|
}
|
2017-06-16 18:54:46 +02:00
|
|
|
swiped = false;
|
|
|
|
}
|
2017-09-28 19:34:26 +02:00
|
|
|
|
2017-06-19 15:59:34 +02:00
|
|
|
if( notifications != null && notifications.size() > 0) {
|
2017-05-05 16:36:04 +02:00
|
|
|
for(Notification tmpNotification: notifications){
|
2018-01-02 14:47:13 +01:00
|
|
|
|
2017-09-28 19:34:26 +02:00
|
|
|
if( lastReadNotifications != null && Long.parseLong(tmpNotification.getId()) > Long.parseLong(lastReadNotifications)) {
|
2017-09-13 08:47:59 +02:00
|
|
|
MainActivity.countNewNotifications++;
|
2017-09-28 19:34:26 +02:00
|
|
|
}
|
2017-09-13 08:47:59 +02:00
|
|
|
this.notifications.add(tmpNotification);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2017-09-24 18:44:42 +02:00
|
|
|
if( firstLoad) {
|
2017-09-27 17:52:23 +02:00
|
|
|
//Update the id of the last notification retrieved
|
2017-12-28 15:33:02 +01:00
|
|
|
if( MainActivity.lastNotificationId == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(MainActivity.lastNotificationId))
|
|
|
|
MainActivity.lastNotificationId = notifications.get(0).getId();
|
|
|
|
updateNotificationLastId(notifications.get(0).getId());
|
2017-09-24 18:44:42 +02:00
|
|
|
}
|
2017-11-20 16:05:18 +01:00
|
|
|
notificationsListAdapter.notifyItemRangeInserted(previousPosition, notifications.size());
|
2017-12-28 15:33:02 +01:00
|
|
|
}else {
|
|
|
|
if( firstLoad)
|
|
|
|
textviewNoAction.setVisibility(View.VISIBLE);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2017-09-13 15:21:03 +02:00
|
|
|
if( firstLoad )
|
|
|
|
((MainActivity)context).updateNotifCounter();
|
2017-05-05 16:36:04 +02:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-07-22 11:02:50 +02:00
|
|
|
firstLoad = false;
|
2017-09-24 18:44:42 +02:00
|
|
|
//The initial call comes from a classic tab refresh
|
|
|
|
flag_loading = (max_id == null );
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2017-08-17 15:48:36 +02:00
|
|
|
|
2017-09-27 17:52:23 +02:00
|
|
|
/**
|
|
|
|
* Called from main activity in onResume to retrieve missing notifications
|
|
|
|
* @param sinceId String
|
|
|
|
*/
|
|
|
|
public void retrieveMissingNotifications(String sinceId){
|
|
|
|
asyncTask = new RetrieveMissingNotificationsAsyncTask(context, sinceId, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:21:03 +02:00
|
|
|
@Override
|
|
|
|
public void setMenuVisibility(final boolean visible) {
|
|
|
|
super.setMenuVisibility(visible);
|
|
|
|
if( context == null)
|
|
|
|
return;
|
|
|
|
//Store last notification id to avoid to notify for those that have been already seen
|
|
|
|
if (visible && notifications != null && notifications.size() > 0) {
|
2019-01-07 14:08:29 +01:00
|
|
|
retrieveMissingNotifications(notifications.get(0).getId());
|
2017-12-28 15:33:02 +01:00
|
|
|
updateNotificationLastId(notifications.get(0).getId());
|
2017-09-13 15:21:03 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-28 15:30:15 +02:00
|
|
|
|
2017-08-17 15:48:36 +02:00
|
|
|
public void scrollToTop(){
|
|
|
|
if( lv_notifications != null)
|
|
|
|
lv_notifications.setAdapter(notificationsListAdapter);
|
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
|
2017-12-28 11:25:12 +01:00
|
|
|
if (this.notifications != null && this.notifications.size() > 0) {
|
2017-12-28 15:33:02 +01:00
|
|
|
updateNotificationLastId(this.notifications.get(0).getId());
|
2017-09-16 09:56:26 +02:00
|
|
|
}
|
2017-08-17 15:48:36 +02:00
|
|
|
}
|
2017-08-24 15:22:04 +02:00
|
|
|
|
2017-10-01 10:32:51 +02:00
|
|
|
public void refreshAll(){
|
|
|
|
if( context == null)
|
|
|
|
return;
|
|
|
|
max_id = null;
|
|
|
|
firstLoad = true;
|
|
|
|
flag_loading = true;
|
|
|
|
swiped = true;
|
|
|
|
MainActivity.countNewNotifications = 0;
|
2017-12-28 17:32:17 +01:00
|
|
|
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-10-01 10:32:51 +02:00
|
|
|
}
|
|
|
|
|
2017-08-26 15:50:10 +02:00
|
|
|
|
2017-09-13 08:38:08 +02:00
|
|
|
public void refresh(Notification notification){
|
2017-09-06 16:17:19 +02:00
|
|
|
if( context == null)
|
|
|
|
return;
|
2017-09-13 08:38:08 +02:00
|
|
|
if( notification != null){
|
2017-11-15 09:44:12 +01:00
|
|
|
//Makes sure the notifications is not already displayed
|
2018-01-09 18:56:23 +01:00
|
|
|
if( !this.notifications.contains(notification)) {
|
2017-11-15 09:44:12 +01:00
|
|
|
//Update the id of the last notification retrieved
|
|
|
|
MainActivity.lastNotificationId = notification.getId();
|
|
|
|
notifications.add(0, notification);
|
|
|
|
MainActivity.countNewNotifications++;
|
|
|
|
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
|
|
|
|
if (firstVisibleItem > 0)
|
|
|
|
notificationsListAdapter.notifyItemInserted(0);
|
|
|
|
else
|
|
|
|
notificationsListAdapter.notifyDataSetChanged();
|
|
|
|
if (textviewNoAction.getVisibility() == View.VISIBLE)
|
|
|
|
textviewNoAction.setVisibility(View.GONE);
|
|
|
|
}
|
2017-08-28 19:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-27 17:52:23 +02:00
|
|
|
|
2017-10-28 10:47:04 +02:00
|
|
|
|
|
|
|
|
2017-09-27 17:52:23 +02:00
|
|
|
@Override
|
|
|
|
public void onRetrieveMissingNotifications(List<Notification> notifications) {
|
2019-01-02 13:50:11 +01:00
|
|
|
flag_loading = false;
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-09-27 17:52:23 +02:00
|
|
|
if( notifications != null && notifications.size() > 0) {
|
|
|
|
for (int i = notifications.size()-1 ; i >= 0 ; i--) {
|
2017-11-15 09:44:12 +01:00
|
|
|
if (this.notifications.size() == 0 ||
|
|
|
|
Long.parseLong(notifications.get(i).getId()) > Long.parseLong(this.notifications.get(0).getId())) {
|
2017-09-27 17:52:23 +02:00
|
|
|
MainActivity.countNewNotifications++;
|
|
|
|
this.notifications.add(0, notifications.get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notificationsListAdapter.notifyDataSetChanged();
|
|
|
|
try {
|
|
|
|
((MainActivity) context).updateNotifCounter();
|
|
|
|
}catch (Exception ignored){}
|
|
|
|
}
|
|
|
|
}
|
2017-10-01 11:49:37 +02:00
|
|
|
|
2017-12-28 11:25:12 +01:00
|
|
|
|
|
|
|
|
2017-10-01 11:49:37 +02:00
|
|
|
/**
|
|
|
|
* Records the id of the notification only if its greater than the previous one.
|
|
|
|
* @param notificationId String current notification id to check
|
|
|
|
*/
|
2017-12-28 15:33:02 +01:00
|
|
|
private void updateNotificationLastId(String notificationId){
|
2017-12-28 11:25:12 +01:00
|
|
|
|
2017-12-28 17:25:36 +01:00
|
|
|
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, null);
|
2017-12-28 11:25:12 +01:00
|
|
|
if( lastNotif == null || Long.parseLong(notificationId) > Long.parseLong(lastNotif)){
|
2017-12-28 15:33:02 +01:00
|
|
|
MainActivity.countNewNotifications = 0;
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
2017-12-28 17:25:36 +01:00
|
|
|
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, notificationId);
|
2017-10-01 11:49:37 +02:00
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|