From 005ad3363c36c242d2579fbe539c6f8c0c8ccd4a Mon Sep 17 00:00:00 2001 From: stom79 Date: Wed, 2 Jan 2019 13:50:11 +0100 Subject: [PATCH] Fix pull to refresh for notifications --- ...RetrieveMissingNotificationsAsyncTask.java | 20 +++++-------------- .../DisplayNotificationsFragment.java | 8 ++++++-- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingNotificationsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingNotificationsAsyncTask.java index e2b04db7e..05d830fad 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingNotificationsAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingNotificationsAsyncTask.java @@ -16,8 +16,8 @@ package fr.gouv.etalab.mastodon.asynctasks; import android.content.Context; import android.os.AsyncTask; + import java.lang.ref.WeakReference; -import java.util.ArrayList; import java.util.List; import fr.gouv.etalab.mastodon.activities.MainActivity; @@ -37,8 +37,8 @@ public class RetrieveMissingNotificationsAsyncTask extends AsyncTask notifications = new ArrayList<>(); private WeakReference contextReference; + private List notifications; public RetrieveMissingNotificationsAsyncTask(Context context, String since_id, OnRetrieveMissingNotificationsInterface onRetrieveMissingNotifications){ this.contextReference = new WeakReference<>(context); @@ -49,20 +49,10 @@ public class RetrieveMissingNotificationsAsyncTask extends AsyncTask tempNotifications; - while (loopInc < 10){ - APIResponse apiResponse = api.getNotificationsSince(since_id, 40, false); - 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; - } + APIResponse apiResponse = api.getNotificationsSince(since_id, 40, false); + since_id = apiResponse.getSince_id(); + notifications = apiResponse.getNotifications(); if( notifications != null && notifications.size() > 0) { MainActivity.lastNotificationId = notifications.get(0).getId(); } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java index 4df037ccc..33a385e92 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java @@ -133,8 +133,11 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve flag_loading = true; swiped = true; MainActivity.countNewNotifications = 0; + String sinceId = null; + if( notifications != null && notifications.size() > 0 ) + sinceId = notifications.get(0).getId(); 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); @@ -317,7 +320,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve @Override public void onRetrieveMissingNotifications(List notifications) { - + flag_loading = false; + swipeRefreshLayout.setRefreshing(false); if( notifications != null && notifications.size() > 0) { for (int i = notifications.size()-1 ; i >= 0 ; i--) { if (this.notifications.size() == 0 ||