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

331 lines
15 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;
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;
import android.support.annotation.NonNull;
2017-05-05 16:36:04 +02:00
import android.support.v4.app.Fragment;
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;
import android.widget.Toast;
2017-05-05 16:36:04 +02:00
import java.util.ArrayList;
import java.util.List;
2017-09-13 08:47:59 +02:00
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;
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.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.asynctasks.RetrieveNotificationsAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
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;
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-09-13 08:47:59 +02:00
private String lastReadNotifications;
2017-09-13 15:21:03 +02:00
private String userId;
2017-10-24 14:30:07 +02:00
LinearLayoutManager mLayoutManager;
2017-08-24 15:57:35 +02:00
public DisplayNotificationsFragment(){
}
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-08-24 15:57:35 +02:00
2017-10-24 14:30:07 +02:00
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
2017-08-25 10:25:13 +02:00
final SharedPreferences 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);
lastReadNotifications = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, 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;
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, 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
}
}
}
});
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;
if( context != null)
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, null, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-05-05 16:36:04 +02:00
}
});
2017-08-13 11:30:28 +02:00
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
R.color.mastodonC2,
R.color.mastodonC3);
2017-11-03 10:18:47 +01:00
if( context != null)
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if( context != null)
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}, 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;
}
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 onRetrieveNotifications(APIResponse apiResponse, String acct, String userId, boolean refreshData) {
2017-08-04 14:38:18 +02:00
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
2017-06-03 18:18:27 +02:00
if( apiResponse.getError() != null){
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();
flag_loading = false;
swipeRefreshLayout.setRefreshing(false);
swiped = false;
return;
}
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){
if( lastReadNotifications != null && Long.parseLong(tmpNotification.getId()) > Long.parseLong(lastReadNotifications)) {
2017-09-13 08:47:59 +02:00
MainActivity.countNewNotifications++;
}
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
MainActivity.lastNotificationId = notifications.get(0).getId();
2017-12-28 11:25:12 +01:00
if (notifications.size() > 0)
updateNotificationLastId(sharedpreferences, this.userId, 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-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);
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){
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
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if (visible && notifications != null && notifications.size() > 0) {
updateNotificationLastId(sharedpreferences, this.userId, 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
//Store last notification id to avoid to notify for those that have been already seen
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
2017-12-28 11:25:12 +01:00
if (this.notifications != null && this.notifications.size() > 0) {
updateNotificationLastId(sharedpreferences, this.userId, 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;
MainActivity.countNewNotifications = 0;
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, null, 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
2017-11-15 19:02:33 +01:00
if( notification.getId() != null && notifications != null && notifications.size() > 0 && notifications.get(0)!= null
&& Long.parseLong(notification.getId()) > Long.parseLong(notifications.get(0).getId())) {
//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) {
if( notifications != null && notifications.size() > 0) {
for (int i = notifications.size()-1 ; i >= 0 ; i--) {
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-12-28 11:25:12 +01:00
/**
* Records the id of the notification only if its greater than the previous one.
* @param sharedPreferences SharedPreferences
* @param userId String current logged user
* @param notificationId String current notification id to check
*/
private void updateNotificationLastId(SharedPreferences sharedPreferences, String userId, String notificationId){
2017-12-28 11:25:12 +01:00
String lastNotif = sharedPreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
2017-12-28 11:25:12 +01:00
if( lastNotif == null || Long.parseLong(notificationId) > Long.parseLong(lastNotif)){
this.lastReadNotifications = notificationId;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notificationId);
editor.apply();
}
}
2017-05-05 16:36:04 +02:00
}