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

202 lines
8.6 KiB
Java
Raw Normal View History

2017-05-05 16:36:04 +02:00
package fr.gouv.etalab.mastodon.fragments;
/* Copyright 2017 Thomas Schneider
*
* This file is a part of Mastodon Etalab for mastodon.etalab.gouv.fr
*
* 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.
*
* Mastodon Etalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Thomas Schneider; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.SharedPreferences;
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;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
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-06-03 18:18:27 +02:00
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
2017-06-16 18:54:46 +02:00
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.helper.Helper;
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;
private String max_id;
2017-05-05 16:36:04 +02:00
private List<Notification> notifications;
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout;
private int notificationPerPage;
2017-06-11 17:14:54 +02:00
private boolean swiped;
2017-06-16 18:54:46 +02:00
private ListView lv_notifications;
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);
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-05-05 16:36:04 +02:00
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 40);
2017-06-16 18:54:46 +02:00
lv_notifications = (ListView) rootView.findViewById(R.id.lv_notifications);
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);
notificationsListAdapter = new NotificationsListAdapter(context, this.notifications);
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;
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;
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
}
});
swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,
R.color.colorPrimary,
R.color.colorPrimaryDark);
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
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 onStop() {
super.onStop();
if(asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
asyncTask.cancel(true);
}
@Override
2017-06-03 18:18:27 +02:00
public void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId) {
2017-05-05 16:36:04 +02:00
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
2017-06-03 18:18:27 +02:00
if( apiResponse.getError() != null){
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
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();
return;
}
2017-06-03 18:18:27 +02:00
List<Notification> notifications = apiResponse.getNotifications();
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 ){
notificationsListAdapter = new NotificationsListAdapter(context, this.notifications);
lv_notifications.setAdapter(notificationsListAdapter);
swiped = false;
}
2017-06-03 18:18:27 +02:00
max_id = apiResponse.getMax_id();
2017-05-05 16:36:04 +02:00
if( notifications != null) {
for(Notification tmpNotification: notifications){
this.notifications.add(tmpNotification);
}
notificationsListAdapter.notifyDataSetChanged();
}
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
flag_loading = notifications != null && notifications.size() < notificationPerPage;
//Store last notification id to avoid to notify for those that have been already seen
if( notifications != null && notifications.size() > 0) {
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
//acct is null as userId when used in Fragment, data need to be retrieved via shared preferences and db
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account currentAccount = new AccountDAO(context, db).getAccountByID(userId);
if( currentAccount != null){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + currentAccount.getId(), notifications.get(0).getId());
editor.apply();
}
}
2017-05-05 16:36:04 +02:00
}
}