From 03fb9f45b27ff507520ea8fa0926586dd6354e19 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 11 Mar 2017 19:20:10 +0100 Subject: [PATCH] Prevent pull notifications from notifying about things already loaded in fragment Dismiss notifications when viewing fragment (moved to onResume) --- .../tusky/NotificationsFragment.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java b/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java index 7d3f84ed..08460a08 100644 --- a/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java @@ -17,6 +17,7 @@ package com.keylesspalace.tusky; import android.app.NotificationManager; import android.content.Context; +import android.content.SharedPreferences; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.Nullable; @@ -58,9 +59,6 @@ public class NotificationsFragment extends SFragment implements @Override public void onCreate(@Nullable Bundle savedInstanceState) { - NotificationManager notificationManager = - (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.cancel(PullNotificationService.NOTIFY_ID); super.onCreate(savedInstanceState); } @@ -119,6 +117,15 @@ public class NotificationsFragment extends SFragment implements return rootView; } + @Override + public void onResume() { + super.onResume(); + + // When we view this fragment, dismiss the notifications + NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.cancel(PullNotificationService.NOTIFY_ID); + } + @Override public void onDestroyView() { TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout); @@ -165,6 +172,13 @@ public class NotificationsFragment extends SFragment implements if (notifications.size() > 0 && !findNotification(notifications, fromId)) { setFetchTimelineState(FooterViewHolder.State.LOADING); adapter.addItems(notifications); + + // Set last update id for pull notifications so that we don't get notified + // about things we already loaded here + SharedPreferences preferences = getActivity().getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + editor.putString("lastUpdateId", notifications.get(0).id); + editor.apply(); } else { setFetchTimelineState(FooterViewHolder.State.END_OF_TIMELINE); }