Fix pull to refresh for notifications
This commit is contained in:
parent
82384b48a5
commit
005ad3363c
|
@ -16,8 +16,8 @@ package fr.gouv.etalab.mastodon.asynctasks;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
||||||
|
@ -37,8 +37,8 @@ public class RetrieveMissingNotificationsAsyncTask extends AsyncTask<Void, Void,
|
||||||
|
|
||||||
private String since_id;
|
private String since_id;
|
||||||
private OnRetrieveMissingNotificationsInterface listener;
|
private OnRetrieveMissingNotificationsInterface listener;
|
||||||
private List<Notification> notifications = new ArrayList<>();
|
|
||||||
private WeakReference<Context> contextReference;
|
private WeakReference<Context> contextReference;
|
||||||
|
private List<Notification> notifications;
|
||||||
|
|
||||||
public RetrieveMissingNotificationsAsyncTask(Context context, String since_id, OnRetrieveMissingNotificationsInterface onRetrieveMissingNotifications){
|
public RetrieveMissingNotificationsAsyncTask(Context context, String since_id, OnRetrieveMissingNotificationsInterface onRetrieveMissingNotifications){
|
||||||
this.contextReference = new WeakReference<>(context);
|
this.contextReference = new WeakReference<>(context);
|
||||||
|
@ -49,20 +49,10 @@ public class RetrieveMissingNotificationsAsyncTask extends AsyncTask<Void, Void,
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params) {
|
protected Void doInBackground(Void... params) {
|
||||||
int loopInc = 0;
|
|
||||||
API api = new API(this.contextReference.get());
|
API api = new API(this.contextReference.get());
|
||||||
List<Notification> tempNotifications;
|
APIResponse apiResponse = api.getNotificationsSince(since_id, 40, false);
|
||||||
while (loopInc < 10){
|
since_id = apiResponse.getSince_id();
|
||||||
APIResponse apiResponse = api.getNotificationsSince(since_id, 40, false);
|
notifications = apiResponse.getNotifications();
|
||||||
String max_id = apiResponse.getMax_id();
|
|
||||||
since_id = apiResponse.getSince_id();
|
|
||||||
tempNotifications = apiResponse.getNotifications();
|
|
||||||
if( notifications != null && tempNotifications != null)
|
|
||||||
notifications.addAll(0, tempNotifications);
|
|
||||||
loopInc++;
|
|
||||||
if( max_id == null || max_id.equals(since_id))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if( notifications != null && notifications.size() > 0) {
|
if( notifications != null && notifications.size() > 0) {
|
||||||
MainActivity.lastNotificationId = notifications.get(0).getId();
|
MainActivity.lastNotificationId = notifications.get(0).getId();
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,8 +133,11 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
|
||||||
flag_loading = true;
|
flag_loading = true;
|
||||||
swiped = true;
|
swiped = true;
|
||||||
MainActivity.countNewNotifications = 0;
|
MainActivity.countNewNotifications = 0;
|
||||||
|
String sinceId = null;
|
||||||
|
if( notifications != null && notifications.size() > 0 )
|
||||||
|
sinceId = notifications.get(0).getId();
|
||||||
if( context != null)
|
if( context != null)
|
||||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
asyncTask = new RetrieveMissingNotificationsAsyncTask(context, sinceId, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||||
|
@ -317,7 +320,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRetrieveMissingNotifications(List<Notification> notifications) {
|
public void onRetrieveMissingNotifications(List<Notification> notifications) {
|
||||||
|
flag_loading = false;
|
||||||
|
swipeRefreshLayout.setRefreshing(false);
|
||||||
if( notifications != null && notifications.size() > 0) {
|
if( notifications != null && notifications.size() > 0) {
|
||||||
for (int i = notifications.size()-1 ; i >= 0 ; i--) {
|
for (int i = notifications.size()-1 ; i >= 0 ; i--) {
|
||||||
if (this.notifications.size() == 0 ||
|
if (this.notifications.size() == 0 ||
|
||||||
|
|
Loading…
Reference in New Issue