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

509 lines
22 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-05-05 16:36:04 +02:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-05-05 16:36:04 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-05-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
2018-11-25 10:45:16 +01:00
2020-04-08 19:10:32 +02:00
import android.app.NotificationManager;
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;
2019-09-14 18:33:58 +02:00
import android.database.sqlite.SQLiteDatabase;
2017-05-05 16:36:04 +02:00
import android.os.Bundle;
2017-11-03 10:18:47 +01:00
import android.os.Handler;
import android.os.Looper;
2019-11-15 16:32:25 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
2019-09-06 17:55:14 +02:00
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2019-11-15 16:32:25 +01:00
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
2020-03-08 10:29:06 +01:00
2019-11-08 18:39:55 +01:00
import org.jetbrains.annotations.NotNull;
2020-03-08 10:29:06 +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
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.asynctasks.RetrieveMissingNotificationsAsyncTask;
import app.fedilab.android.asynctasks.RetrieveNotificationsAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Notification;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.drawers.NotificationsListAdapter;
import app.fedilab.android.helper.Helper;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.interfaces.OnRetrieveMissingNotificationsInterface;
import app.fedilab.android.interfaces.OnRetrieveNotificationsInterface;
2019-09-14 18:33:58 +02:00
import app.fedilab.android.services.LiveNotificationDelayedService;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2017-05-05 16:36:04 +02:00
2019-09-14 18:33:58 +02:00
import static android.content.Context.MODE_PRIVATE;
2019-07-19 18:04:47 +02:00
import static app.fedilab.android.activities.BaseMainActivity.countNewNotifications;
2017-05-05 16:36:04 +02:00
/**
* 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
2020-03-07 16:53:34 +01:00
private LinearLayoutManager mLayoutManager;
private Type type;
2017-05-05 16:36:04 +02:00
private boolean flag_loading;
private Context context;
private NotificationsListAdapter notificationsListAdapter;
private String max_id;
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;
2019-02-06 17:53:49 +01:00
private BroadcastReceiver receive_action;
2019-08-18 12:56:24 +02:00
private BroadcastReceiver receive_data;
2020-04-08 12:42:15 +02:00
private View rootView;
2017-08-24 15:57:35 +02:00
2019-09-06 17:55:14 +02:00
public DisplayNotificationsFragment() {
2017-08-24 15:57:35 +02:00
}
2017-05-05 16:36:04 +02:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2017-05-05 16:36:04 +02:00
2020-04-08 12:42:15 +02:00
rootView = inflater.inflate(R.layout.fragment_notifications, container, false);
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);
2019-11-12 21:27:02 +01:00
int c1 = getResources().getColor(R.color.cyanea_accent);
int c2 = getResources().getColor(R.color.cyanea_primary_dark);
int c3 = getResources().getColor(R.color.cyanea_primary);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(c3);
swipeRefreshLayout.setColorSchemeColors(
c1, c2, c1
);
2017-12-28 15:33:02 +01:00
sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
2019-02-03 12:07:43 +01:00
Bundle bundle = this.getArguments();
if (bundle != null) {
type = (Type) bundle.get("type");
}
2017-10-24 14:30:07 +02:00
lv_notifications = rootView.findViewById(R.id.lv_notifications);
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);
2019-09-06 17:55:14 +02:00
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, context != null ? Helper.getLiveInstance(context) : null);
notificationsListAdapter = new NotificationsListAdapter(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() {
2020-03-07 16:53:34 +01:00
public void onScrolled(@NotNull RecyclerView recyclerView, int dx, int dy) {
2019-09-06 17:55:14 +02:00
if (dy > 0) {
2017-10-24 14:30:07 +02:00
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
2017-10-24 14:30:07 +02:00
if (!flag_loading) {
flag_loading = true;
2021-01-19 17:43:51 +01:00
new RetrieveNotificationsAsyncTask(context, type, true, null, max_id, DisplayNotificationsFragment.this);
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-02-06 17:53:49 +01:00
2019-09-06 17:55:14 +02:00
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
2019-02-04 18:00:12 +01:00
2019-09-06 17:55:14 +02:00
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");
2020-04-16 16:01:13 +02:00
if (notificationsListAdapter != null && status != null) {
2019-09-06 17:55:14 +02:00
notificationsListAdapter.notifyNotificationWithActionChanged(status);
2020-04-16 16:01:13 +02:00
}
2019-09-06 17:55:14 +02:00
}
};
LocalBroadcastManager.getInstance(context).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_ACTION));
if (type == Type.ALL) {
if (receive_data != null)
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_data);
receive_data = new BroadcastReceiver() {
2019-02-04 18:00:12 +01:00
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
assert b != null;
2019-09-06 17:55:14 +02:00
String userIdService = b.getString("userIdService", null);
if (userIdService != null && userIdService.equals(userId)) {
Notification notification = b.getParcelable("data");
refresh(notification);
if (context instanceof MainActivity && type == Type.ALL)
((MainActivity) context).updateNotifCounter();
}
2019-01-11 14:55:53 +01:00
}
2019-02-04 18:00:12 +01:00
};
2019-09-06 17:55:14 +02:00
LocalBroadcastManager.getInstance(context).registerReceiver(receive_data, new IntentFilter(Helper.RECEIVE_DATA));
}
2019-01-11 14:55:53 +01:00
}
2020-03-07 16:53:34 +01:00
swipeRefreshLayout.setOnRefreshListener(() -> {
flag_loading = true;
swiped = true;
if (type == Type.ALL) {
countNewNotifications = 0;
try {
((MainActivity) context).updateNotifCounter();
} catch (Exception ignored) {
2019-05-18 14:58:54 +02:00
}
2017-05-05 16:36:04 +02:00
}
2020-03-07 16:53:34 +01:00
String sinceId = null;
if (notifications != null && notifications.size() > 0)
sinceId = notifications.get(0).getId();
if (context != null)
2021-01-19 17:43:51 +01:00
new RetrieveMissingNotificationsAsyncTask(context, type, sinceId, DisplayNotificationsFragment.this);
2017-05-05 16:36:04 +02:00
});
2019-09-06 17:55:14 +02:00
if (context != null)
2021-01-19 17:43:51 +01:00
new RetrieveNotificationsAsyncTask(context, type, true, null, max_id, DisplayNotificationsFragment.this);
2017-11-03 10:18:47 +01:00
else
2020-03-07 16:53:34 +01:00
new Handler(Looper.getMainLooper()).postDelayed(() -> {
if (context != null)
2021-01-19 17:43:51 +01:00
new RetrieveNotificationsAsyncTask(context, type, true, null, max_id, DisplayNotificationsFragment.this);
2017-11-03 10:18:47 +01:00
}, 500);
2017-05-05 16:36:04 +02:00
return rootView;
}
2020-04-08 12:42:15 +02:00
2017-05-05 16:36:04 +02:00
@Override
2019-09-06 17:55:14 +02:00
public void onCreate(Bundle saveInstance) {
2017-05-05 16:36:04 +02:00
super.onCreate(saveInstance);
}
@Override
2019-11-08 18:39:55 +01:00
public void onAttach(@NotNull Context context) {
2017-05-05 16:36:04 +02:00
super.onAttach(context);
this.context = context;
}
2019-01-27 17:06:18 +01:00
@Override
public void onDestroy() {
super.onDestroy();
2019-09-06 17:55:14 +02:00
if (receive_action != null)
2019-01-11 14:55:53 +01:00
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_action);
2019-09-06 17:55:14 +02:00
if (receive_data != null)
2019-01-11 16:39:01 +01:00
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_data);
2019-01-13 11:34:32 +01:00
2017-05-05 16:36:04 +02:00
}
2019-08-18 17:17:47 +02:00
@Override
public void onDestroyView() {
2019-09-06 17:55:14 +02:00
if (lv_notifications != null) {
2019-08-18 17:17:47 +02:00
lv_notifications.setAdapter(null);
}
super.onDestroyView();
2020-04-08 12:42:15 +02:00
rootView = null;
2019-08-18 17:17:47 +02:00
}
2019-08-17 19:17:48 +02:00
@Override
public void onResume() {
super.onResume();
swipeRefreshLayout.setEnabled(true);
2019-09-14 18:33:58 +02:00
if (context == null)
return;
2020-04-25 19:20:42 +02:00
if (isVisible()) {
2020-04-08 19:10:32 +02:00
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert mNotificationManager != null;
mNotificationManager.cancelAll();
}
2020-04-25 19:20:42 +02:00
if (isVisible() && notifications != null && notifications.size() > 0) {
2019-09-14 18:33:58 +02:00
retrieveMissingNotifications(notifications.get(0).getId());
updateNotificationLastId(notifications.get(0).getId());
}
2019-08-17 19:17:48 +02:00
}
@Override
public void onPause() {
super.onPause();
2019-09-06 17:55:14 +02:00
if (swipeRefreshLayout != null) {
2019-08-18 12:56:24 +02:00
swipeRefreshLayout.setEnabled(false);
swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.clearAnimation();
}
2019-08-17 19:17:48 +02:00
}
2017-05-05 16:36:04 +02:00
@Override
public void onRetrieveNotifications(APIResponse apiResponse, Account account, boolean refreshData) {
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);
2019-09-06 17:55:14 +02:00
if (apiResponse.getError() != null) {
2019-01-30 14:07:37 +01:00
2019-11-15 16:32:25 +01:00
if (apiResponse.getError().getError().length() < 100) {
2019-09-30 18:14:46 +02:00
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2019-11-15 16:32:25 +01:00
} else {
Toasty.error(context, getString(R.string.long_api_error, "\ud83d\ude05"), Toast.LENGTH_LONG).show();
2019-09-30 18:14:46 +02:00
}
flag_loading = false;
swipeRefreshLayout.setRefreshing(false);
swiped = false;
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
2019-09-06 17:55:14 +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);
2019-09-06 17:55:14 +02:00
if (swiped) {
2017-11-20 16:27:40 +01:00
if (previousPosition > 0) {
2020-03-07 16:53:34 +01:00
this.notifications.subList(0, previousPosition).clear();
2017-11-20 16:27:40 +01:00
notificationsListAdapter.notifyItemRangeRemoved(0, previousPosition);
}
2017-06-16 18:54:46 +02:00
swiped = false;
}
2019-09-06 17:55:14 +02:00
if (notifications != null && notifications.size() > 0) {
2019-09-14 18:33:58 +02:00
if (type == Type.ALL) {
if (lastReadNotifications != null && notifications.get(0).getId().compareTo(lastReadNotifications) > 0) {
countNewNotifications++;
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(context));
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-09-14 18:33:58 +02:00
Account accountdb = new AccountDAO(context, db).getUniqAccount(userId, instance);
LiveNotificationDelayedService.since_ids.put(accountdb.getAcct() + "@" + accountdb.getInstance(), notifications.get(0).getId());
}
}
2019-09-06 17:55:14 +02:00
for (Notification tmpNotification : notifications) {
2018-01-02 14:47:13 +01:00
2019-09-06 17:55:14 +02:00
if (type == Type.ALL) {
2019-08-31 10:22:40 +02:00
if (lastReadNotifications != null && tmpNotification.getId().compareTo(lastReadNotifications) > 0) {
2019-07-19 18:04:47 +02:00
countNewNotifications++;
2019-05-18 14:58:54 +02:00
}
2019-09-14 18:33:58 +02:00
2019-05-18 14:58:54 +02:00
try {
((MainActivity) context).updateNotifCounter();
} catch (Exception ignored) {
}
}
2017-09-13 08:47:59 +02:00
this.notifications.add(tmpNotification);
2017-05-05 16:36:04 +02:00
}
2019-09-06 17:55:14 +02:00
if (firstLoad && type == Type.ALL) {
2017-09-27 17:52:23 +02:00
//Update the id of the last notification retrieved
2019-09-06 17:55:14 +02:00
if (MainActivity.lastNotificationId == null || notifications.get(0).getId().compareTo(MainActivity.lastNotificationId) > 0) {
2017-12-28 15:33:02 +01:00
MainActivity.lastNotificationId = notifications.get(0).getId();
2019-07-14 18:35:31 +02:00
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());
2019-09-06 17:55:14 +02:00
} else {
if (firstLoad)
2017-12-28 15:33:02 +01:00
textviewNoAction.setVisibility(View.VISIBLE);
2017-05-05 16:36:04 +02:00
}
2019-09-06 17:55:14 +02:00
if (firstLoad && type == Type.ALL)
((MainActivity) context).updateNotifCounter();
2017-05-05 16:36:04 +02:00
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
2017-09-24 18:44:42 +02:00
//The initial call comes from a classic tab refresh
2019-09-06 17:55:14 +02:00
flag_loading = (max_id == null);
2017-05-05 16:36:04 +02:00
}
2017-09-27 17:52:23 +02:00
/**
* Called from main activity in onResume to retrieve missing notifications
2019-09-06 17:55:14 +02:00
*
2017-09-27 17:52:23 +02:00
* @param sinceId String
*/
2019-09-06 17:55:14 +02:00
void retrieveMissingNotifications(String sinceId) {
2021-01-19 17:43:51 +01:00
new RetrieveMissingNotificationsAsyncTask(context, type, sinceId, DisplayNotificationsFragment.this);
2017-09-27 17:52:23 +02:00
}
2017-09-13 15:21:03 +02:00
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
2019-09-06 17:55:14 +02:00
if (context == null)
2017-09-13 15:21:03 +02:00
return;
//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
}
}
2019-09-06 17:55:14 +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
}
}
2020-03-07 16:53:34 +01:00
void refreshAll() {
2019-09-06 17:55:14 +02:00
if (context == null)
2017-10-01 10:32:51 +02:00
return;
max_id = null;
firstLoad = true;
flag_loading = true;
swiped = true;
2019-09-06 17:55:14 +02:00
if (type == Type.ALL) {
2019-07-19 18:04:47 +02:00
countNewNotifications = 0;
2019-05-18 14:58:54 +02:00
}
2021-01-19 17:43:51 +01:00
new RetrieveNotificationsAsyncTask(context, type, true, null, null, DisplayNotificationsFragment.this);
2017-10-01 10:32:51 +02:00
}
2019-09-06 17:55:14 +02:00
public void refresh(Notification notification) {
if (context == null)
return;
2019-09-06 17:55:14 +02:00
if (notification != null) {
//Makes sure the notifications is not already displayed
2019-09-06 17:55:14 +02:00
if (!this.notifications.contains(notification)) {
//Update the id of the last notification retrieved
2019-07-18 13:45:39 +02:00
notifications.add(0, notification);
2019-09-06 17:55:14 +02:00
if (type == Type.ALL) {
2019-07-18 13:45:39 +02:00
MainActivity.lastNotificationId = notification.getId();
2019-07-19 18:04:47 +02:00
countNewNotifications++;
2019-05-18 14:58:54 +02:00
try {
((MainActivity) context).updateNotifCounter();
} catch (Exception ignored) {
}
}
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
@Override
public void onRetrieveMissingNotifications(List<Notification> notifications) {
2019-01-02 13:50:11 +01:00
flag_loading = false;
swipeRefreshLayout.setRefreshing(false);
2019-09-06 17:55:14 +02:00
if (this.notifications != null && this.notifications.size() > 0 && swiped) {
2019-01-30 14:07:37 +01:00
swiped = false;
2019-09-06 17:55:14 +02:00
notificationsListAdapter.notifyItemRangeChanged(0, this.notifications.size());
2019-01-24 10:17:43 +01:00
}
2019-09-06 17:55:14 +02:00
if (notifications != null && notifications.size() > 0) {
if (type == Type.ALL) {
2019-07-14 18:35:31 +02:00
//Update the id of the last notification retrieved
2019-09-06 17:55:14 +02:00
if (MainActivity.lastNotificationId == null || notifications.get(0).getId().compareTo(MainActivity.lastNotificationId) >= 0) {
2019-07-14 18:35:31 +02:00
MainActivity.lastNotificationId = notifications.get(0).getId();
updateNotificationLastId(notifications.get(0).getId());
}
2019-09-14 18:33:58 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(context));
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-09-14 18:33:58 +02:00
Account account = new AccountDAO(context, db).getUniqAccount(userId, instance);
if (MainActivity.lastNotificationId != null && notifications.get(0).getId().compareTo(MainActivity.lastNotificationId) > 0) {
LiveNotificationDelayedService.since_ids.put(account.getAcct() + "@" + account.getInstance(), notifications.get(0).getId());
}
2019-07-14 18:35:31 +02:00
}
2019-09-06 17:55:14 +02:00
if (textviewNoAction.getVisibility() == View.VISIBLE) {
2019-06-09 18:07:06 +02:00
textviewNoAction.setVisibility(View.GONE);
lv_notifications.setVisibility(View.VISIBLE);
}
2019-01-24 10:17:43 +01:00
int inserted = 0;
2019-09-06 17:55:14 +02:00
for (int i = notifications.size() - 1; i >= 0; i--) {
2019-01-24 10:17:43 +01:00
if (this.notifications != null && this.notifications.size() == 0 ||
2019-08-31 10:22:40 +02:00
notifications.get(i).getId().compareTo(this.notifications.get(0).getId()) > 0) {
2019-07-19 18:04:47 +02:00
countNewNotifications++;
2017-09-27 17:52:23 +02:00
this.notifications.add(0, notifications.get(i));
2019-01-24 10:17:43 +01:00
inserted++;
2017-09-27 17:52:23 +02:00
}
}
2019-09-06 17:55:14 +02:00
notificationsListAdapter.notifyItemRangeInserted(0, inserted);
2017-09-27 17:52:23 +02:00
try {
((MainActivity) context).updateNotifCounter();
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
2017-09-27 17:52:23 +02:00
}
}
/**
* Records the id of the notification only if its greater than the previous one.
2019-09-06 17:55:14 +02:00
*
* @param notificationId String current notification id to check
*/
2019-09-06 17:55:14 +02:00
private void updateNotificationLastId(String notificationId) {
if (type == Type.ALL) {
2019-02-04 18:00:12 +01:00
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, null);
2019-08-31 10:22:40 +02:00
if (lastNotif == null || notificationId.compareTo(lastNotif) >= 0) {
2019-07-19 18:04:47 +02:00
countNewNotifications = 0;
2019-02-04 18:00:12 +01:00
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, notificationId);
editor.apply();
}
}
}
2019-08-31 10:22:40 +02:00
/**
* Records the id of the notification only if its greater than the previous one.
*/
2019-09-06 17:55:14 +02:00
public void updateNotificationRead() {
if (type == Type.ALL) {
2019-08-31 10:22:40 +02:00
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, null);
countNewNotifications = 0;
if (lastNotif != null && this.notifications != null && this.notifications.size() > 0 && this.notifications.get(0).getId().compareTo(lastNotif) > 0) {
2019-08-31 10:22:40 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, this.notifications.get(0).getId());
editor.apply();
}
}
}
2019-11-15 16:32:25 +01:00
public enum Type {
ALL,
MENTION,
FAVORITE,
BOOST,
STATUS,
2019-11-15 16:32:25 +01:00
POLL,
FOLLOW
}
2017-05-05 16:36:04 +02:00
}