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;
|
2017-05-25 17:07:56 +02:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2017-08-24 17:35:39 +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;
|
2017-08-24 15:57:35 +02:00
|
|
|
import android.widget.TextView;
|
2017-05-30 19:31:44 +02:00
|
|
|
import android.widget.Toast;
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2017-08-24 15:57:35 +02:00
|
|
|
import java.util.Date;
|
2017-05-05 16:36:04 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-08-24 15:22:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
2017-06-03 18:18:27 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.APIResponse;
|
2017-05-25 17:07:56 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
2017-05-05 16:36:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
|
|
|
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
2017-05-25 17:07:56 +02:00
|
|
|
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
|
|
|
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
2017-05-05 16:36:04 +02:00
|
|
|
import mastodon.etalab.gouv.fr.mastodon.R;
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public class DisplayNotificationsFragment extends Fragment implements OnRetrieveNotificationsInterface {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-08-25 10:25:13 +02:00
|
|
|
private List<Notification> notifications, notificationsTmp;
|
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-06-16 18:54:46 +02:00
|
|
|
private ListView lv_notifications;
|
2017-08-24 15:22:04 +02:00
|
|
|
private DisplayNotificationsFragment displayNotificationsFragment;
|
2017-08-24 15:57:35 +02:00
|
|
|
private TextView new_data;
|
|
|
|
|
|
|
|
|
|
|
|
public DisplayNotificationsFragment(){
|
|
|
|
displayNotificationsFragment = this;
|
|
|
|
}
|
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_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-08-24 15:57:35 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
swipeRefreshLayout = (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-06-16 18:54:46 +02:00
|
|
|
lv_notifications = (ListView) rootView.findViewById(R.id.lv_notifications);
|
2017-08-24 15:57:35 +02:00
|
|
|
new_data = (TextView) rootView.findViewById(R.id.new_data);
|
2017-05-05 16:36:04 +02:00
|
|
|
mainLoader = (RelativeLayout) rootView.findViewById(R.id.loader);
|
|
|
|
nextElementLoader = (RelativeLayout) rootView.findViewById(R.id.loading_next_notifications);
|
|
|
|
textviewNoAction = (RelativeLayout) rootView.findViewById(R.id.no_action);
|
|
|
|
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);
|
|
|
|
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments,this.notifications);
|
2017-05-05 16:36:04 +02:00
|
|
|
lv_notifications.setAdapter(notificationsListAdapter);
|
|
|
|
lv_notifications.setOnScrollListener(new AbsListView.OnScrollListener() {
|
|
|
|
@Override
|
|
|
|
public void onScrollStateChanged(AbsListView view, int scrollState) {
|
|
|
|
|
|
|
|
}
|
|
|
|
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
|
|
|
|
|
|
|
|
if(firstVisibleItem + visibleItemCount == totalItemCount ) {
|
|
|
|
if(!flag_loading ) {
|
|
|
|
flag_loading = true;
|
2017-05-28 17:16:31 +02:00
|
|
|
asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null,DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-05-05 16:36:04 +02:00
|
|
|
nextElementLoader.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
|
|
|
max_id = null;
|
|
|
|
notifications = new ArrayList<>();
|
|
|
|
firstLoad = true;
|
|
|
|
flag_loading = true;
|
2017-06-11 17:14:54 +02:00
|
|
|
swiped = true;
|
2017-05-28 17:16:31 +02:00
|
|
|
asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, 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-05-05 16:36:04 +02:00
|
|
|
|
2017-05-28 17:16:31 +02:00
|
|
|
asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-08-24 15:57:35 +02:00
|
|
|
|
|
|
|
new_data.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-08-25 10:25:13 +02:00
|
|
|
if( notificationsTmp != null){
|
|
|
|
boolean isOnWifi = Helper.isOnWIFI(context);
|
|
|
|
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
|
|
|
|
notifications = new ArrayList<>();
|
|
|
|
for(Notification notification: notificationsTmp){
|
|
|
|
notifications.add(notification);
|
|
|
|
}
|
|
|
|
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments, notifications);
|
|
|
|
lv_notifications.setAdapter(notificationsListAdapter);
|
2017-08-24 15:57:35 +02:00
|
|
|
}
|
2017-08-25 10:25:13 +02:00
|
|
|
new_data.setVisibility(View.GONE);
|
|
|
|
notificationsTmp = new ArrayList<>();
|
2017-08-24 15:57:35 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-08-24 15:22:04 +02:00
|
|
|
public void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId, boolean refreshData) {
|
2017-08-25 12:45:42 +02:00
|
|
|
|
2017-08-04 14:38:18 +02:00
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
2017-05-30 19:31:44 +02:00
|
|
|
mainLoader.setVisibility(View.GONE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2017-06-03 18:18:27 +02:00
|
|
|
if( apiResponse.getError() != null){
|
2017-05-30 19:31:44 +02:00
|
|
|
boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true);
|
|
|
|
if( show_error_messages)
|
2017-06-14 17:19:16 +02:00
|
|
|
Toast.makeText(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-08-24 15:57:35 +02:00
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
2017-08-25 10:25:13 +02:00
|
|
|
editor.putString(Helper.LAST_BUBBLE_REFRESH_NOTIF+ userId,Helper.dateToString(context, new Date()));
|
2017-08-24 15:57:35 +02:00
|
|
|
editor.apply();
|
|
|
|
|
2017-08-25 10:25:13 +02:00
|
|
|
String bubble_max_id = sharedpreferences.getString(Helper.LAST_MAX_ID_BUBBLE_NOTIF + userId, null);
|
2017-08-25 12:45:42 +02:00
|
|
|
|
2017-06-03 18:18:27 +02:00
|
|
|
List<Notification> notifications = apiResponse.getNotifications();
|
2017-08-25 10:25:13 +02:00
|
|
|
max_id = apiResponse.getMax_id();
|
2017-08-25 12:45:42 +02:00
|
|
|
if( refreshData ) {
|
|
|
|
manageNotifications(notifications, max_id);
|
2017-08-25 10:25:13 +02:00
|
|
|
if( apiResponse.getSince_id() != null) {
|
|
|
|
editor.putString(Helper.LAST_MAX_ID_BUBBLE_NOTIF + userId, apiResponse.getSince_id());
|
|
|
|
editor.apply();
|
2017-08-24 15:22:04 +02:00
|
|
|
}
|
|
|
|
}else {
|
2017-08-25 12:45:42 +02:00
|
|
|
int countData = 0;
|
|
|
|
if( bubble_max_id != null) {
|
|
|
|
for (Notification nt : notifications) {
|
|
|
|
if (nt.getId().trim().equals(bubble_max_id.trim()))
|
|
|
|
break;
|
|
|
|
countData++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !displayNotificationsFragment.getUserVisibleHint() ){
|
|
|
|
|
|
|
|
((MainActivity)context).updateNotifCounter(countData);
|
2017-08-25 10:25:13 +02:00
|
|
|
max_id = null;
|
|
|
|
firstLoad = true;
|
|
|
|
notificationsTmp = new ArrayList<>();
|
|
|
|
for(Notification tmpNotification: notifications){
|
|
|
|
this.notificationsTmp.add(tmpNotification);
|
|
|
|
}
|
2017-08-25 12:45:42 +02:00
|
|
|
if( apiResponse.getSince_id() != null) {
|
|
|
|
editor.putString(Helper.LAST_MAX_ID_BUBBLE_NOTIF + userId, apiResponse.getSince_id());
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
if( notifications != null && notifications.size() > 0 && countData > 0) {
|
|
|
|
max_id = null;
|
|
|
|
firstLoad = true;
|
|
|
|
notificationsTmp = new ArrayList<>();
|
|
|
|
for(Notification tmpNotification: notifications){
|
|
|
|
this.notificationsTmp.add(tmpNotification);
|
|
|
|
}
|
|
|
|
new_data.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2017-08-25 10:25:13 +02:00
|
|
|
}
|
2017-08-24 15:22:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void manageNotifications(List<Notification> notifications, String max_id){
|
2017-08-24 19:28:03 +02:00
|
|
|
flag_loading = (max_id == null );
|
2017-08-24 15:22:04 +02:00
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
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-08-04 14:38:18 +02:00
|
|
|
boolean isOnWifi = Helper.isOnWIFI(context);
|
|
|
|
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
|
|
|
|
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments, this.notifications);
|
2017-06-16 18:54:46 +02:00
|
|
|
lv_notifications.setAdapter(notificationsListAdapter);
|
|
|
|
swiped = false;
|
|
|
|
}
|
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){
|
|
|
|
this.notifications.add(tmpNotification);
|
|
|
|
}
|
|
|
|
notificationsListAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-05-25 17:07:56 +02:00
|
|
|
//Store last notification id to avoid to notify for those that have been already seen
|
|
|
|
if( notifications != null && notifications.size() > 0) {
|
2017-05-28 14:58:04 +02:00
|
|
|
//acct is null as userId when used in Fragment, data need to be retrieved via shared preferences and db
|
2017-08-24 15:22:04 +02:00
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
2017-05-25 17:07:56 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
Account currentAccount = new AccountDAO(context, db).getAccountByID(userId);
|
2017-07-22 11:02:50 +02:00
|
|
|
if( currentAccount != null && firstLoad){
|
2017-05-25 17:07:56 +02:00
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
2017-08-25 15:30:35 +02:00
|
|
|
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + currentAccount.getId(), max_id);
|
2017-05-25 17:07:56 +02:00
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
}
|
2017-07-22 11:02:50 +02:00
|
|
|
firstLoad = false;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2017-08-17 15:48:36 +02:00
|
|
|
|
|
|
|
public void scrollToTop(){
|
|
|
|
if( lv_notifications != null)
|
|
|
|
lv_notifications.setAdapter(notificationsListAdapter);
|
|
|
|
}
|
2017-08-24 15:22:04 +02:00
|
|
|
|
|
|
|
public void update(){
|
2017-08-25 10:25:13 +02:00
|
|
|
if( context != null){
|
2017-08-25 12:45:42 +02:00
|
|
|
asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, null, null, null, false, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-08-25 10:25:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void refreshData(){
|
2017-08-25 12:45:42 +02:00
|
|
|
if(context != null && this.notificationsTmp != null && this.notificationsTmp.size() > 0){
|
2017-08-25 10:25:13 +02:00
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean isOnWifi = Helper.isOnWIFI(context);
|
|
|
|
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
|
|
|
|
notifications = new ArrayList<>();
|
|
|
|
for(Notification notification: this.notificationsTmp){
|
|
|
|
notifications.add(notification);
|
|
|
|
}
|
|
|
|
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments, notifications);
|
|
|
|
lv_notifications.setAdapter(notificationsListAdapter);
|
|
|
|
this.notificationsTmp = new ArrayList<>();
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|