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

450 lines
20 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
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;
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.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;
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
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;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2019-05-18 11:10:30 +02: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;
import app.fedilab.android.interfaces.OnRetrieveMissingNotificationsInterface;
import app.fedilab.android.interfaces.OnRetrieveNotificationsInterface;
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
private boolean flag_loading;
private Context context;
private AsyncTask<Void, Void, Void> asyncTask;
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;
2017-10-24 14:30:07 +02:00
LinearLayoutManager mLayoutManager;
2019-02-06 17:53:49 +01:00
private BroadcastReceiver receive_action;
2019-01-27 17:06:18 +01:00
private static BroadcastReceiver receive_data;
2017-08-24 15:57:35 +02:00
public DisplayNotificationsFragment(){
}
2017-05-05 16:36:04 +02:00
2019-02-03 12:07:43 +01:00
public enum Type{
ALL,
MENTION,
FAVORITE,
BOOST,
2019-05-04 10:01:38 +02:00
POLL,
2019-02-03 12:07:43 +01:00
FOLLOW
}
Type type;
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
View 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);
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-12-28 15:33:02 +01:00
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);
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();
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
2017-10-24 14:30:07 +02:00
if (!flag_loading) {
flag_loading = true;
2019-02-03 12:07:43 +01:00
asyncTask = new RetrieveNotificationsAsyncTask(context, type,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-02-06 17:53:49 +01:00
2019-02-04 18:00:12 +01:00
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
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-03-30 18:48:38 +01:00
if( notificationsListAdapter != null && status != null)
notificationsListAdapter.notifyNotificationWithActionChanged(status);
2019-01-11 14:55:53 +01:00
}
2019-02-04 18:00:12 +01:00
};
LocalBroadcastManager.getInstance(context).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_ACTION));
2019-02-06 17:53:49 +01:00
if( type == Type.ALL) {
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);
2019-05-18 14:58:54 +02:00
if (context instanceof MainActivity && type == Type.ALL )
2019-02-06 17:53:49 +01:00
((MainActivity) context).updateNotifCounter();
}
2019-02-04 18:00:12 +01:00
}
2019-02-06 17:53:49 +01:00
};
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() {
flag_loading = true;
2017-06-11 17:14:54 +02:00
swiped = true;
2019-05-18 14:58:54 +02:00
if(type == Type.ALL) {
MainActivity.countNewNotifications = 0;
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();
if( context != null)
2019-02-03 12:07:43 +01:00
asyncTask = new RetrieveMissingNotificationsAsyncTask(context, type, 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)
2019-02-03 12:07:43 +01:00
asyncTask = new RetrieveNotificationsAsyncTask(context, type,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)
2019-02-03 12:07:43 +01:00
asyncTask = new RetrieveNotificationsAsyncTask(context, type,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;
}
2019-01-27 17:06:18 +01: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);
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
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);
2017-06-03 18:18:27 +02:00
if( apiResponse.getError() != null){
2019-01-30 14:07:37 +01:00
2018-11-25 10:45:16 +01:00
Toasty.error(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
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
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;
}
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
2019-05-18 14:58:54 +02:00
if(type == Type.ALL) {
if (lastReadNotifications != null && Long.parseLong(tmpNotification.getId()) > Long.parseLong(lastReadNotifications)) {
MainActivity.countNewNotifications++;
}
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-05-18 14:58:54 +02:00
if( firstLoad && type == Type.ALL) {
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
}
2019-05-18 14:58:54 +02:00
if( firstLoad && type == Type.ALL)
2017-09-13 15:21:03 +02:00
((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
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
* @param sinceId String
*/
public void retrieveMissingNotifications(String sinceId){
2019-02-03 12:07:43 +01:00
asyncTask = new RetrieveMissingNotificationsAsyncTask(context, type, sinceId, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
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);
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
}
}
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-10-01 10:32:51 +02:00
public void refreshAll(){
if( context == null)
return;
max_id = null;
firstLoad = true;
flag_loading = true;
swiped = true;
2019-05-18 14:58:54 +02:00
if(type == Type.ALL) {
MainActivity.countNewNotifications = 0;
}
2019-02-03 12:07:43 +01:00
asyncTask = new RetrieveNotificationsAsyncTask(context, type,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
public void refresh(Notification notification){
if( context == null)
return;
if( notification != null){
//Makes sure the notifications is not already displayed
2018-01-09 18:56:23 +01:00
if( !this.notifications.contains(notification)) {
//Update the id of the last notification retrieved
MainActivity.lastNotificationId = notification.getId();
notifications.add(0, notification);
2019-05-18 14:58:54 +02:00
if( type == Type.ALL) {
MainActivity.countNewNotifications++;
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
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);
2019-01-30 14:07:37 +01:00
if( this.notifications != null && this.notifications.size() > 0 && swiped){
swiped = false;
2019-01-24 10:17:43 +01:00
notificationsListAdapter.notifyItemRangeChanged(0,this.notifications.size());
}
2017-09-27 17:52:23 +02:00
if( notifications != null && notifications.size() > 0) {
2019-06-09 18:07:06 +02:00
if( textviewNoAction.getVisibility() == View.VISIBLE){
textviewNoAction.setVisibility(View.GONE);
lv_notifications.setVisibility(View.VISIBLE);
}
2019-01-24 10:17:43 +01:00
int inserted = 0;
2017-09-27 17:52:23 +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 ||
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));
2019-01-24 10:17:43 +01:00
inserted++;
2017-09-27 17:52:23 +02:00
}
}
2019-01-24 10:17:43 +01:00
notificationsListAdapter.notifyItemRangeInserted(0,inserted);
2017-09-27 17:52:23 +02:00
try {
((MainActivity) context).updateNotifCounter();
}catch (Exception ignored){}
}
}
2017-12-28 11:25:12 +01: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){
2019-02-04 18:00:12 +01:00
if( type == Type.ALL) {
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, null);
if (lastNotif == null || Long.parseLong(notificationId) > Long.parseLong(lastNotif)) {
MainActivity.countNewNotifications = 0;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, notificationId);
editor.apply();
}
}
}
2017-05-05 16:36:04 +02:00
}