From ac5615497b792b48a695dcaeac214e7c44dee02c Mon Sep 17 00:00:00 2001 From: FineFindus <63370021+FineFindus@users.noreply.github.com> Date: Wed, 1 Mar 2023 22:00:49 +0100 Subject: [PATCH] refactor(notifications-tab/badge): use improved implementation Removes the now unecessary network call, by hooking into the notification reciever instead. --- .../android/GlobalUserPreferences.java | 6 ++--- .../android/PushNotificationReceiver.java | 2 ++ .../android/fragments/HomeFragment.java | 22 +++---------------- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java index cf88ad534..fec865394 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java +++ b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java @@ -47,7 +47,7 @@ public class GlobalUserPreferences{ public static boolean collapseLongPosts; public static boolean spectatorMode; public static boolean autoHideFab; - public static long lastNotificationOpenedTime; + public static boolean unreadNotifications; public static String publishButtonText; public static ThemePreference theme; public static ColorPreference color; @@ -102,7 +102,7 @@ public class GlobalUserPreferences{ collapseLongPosts=prefs.getBoolean("collapseLongPosts", true); spectatorMode=prefs.getBoolean("spectatorMode", false); autoHideFab=prefs.getBoolean("autoHideFab", true); - lastNotificationOpenedTime=prefs.getLong("lastNotificationOpenedTime", 0); + unreadNotifications=prefs.getBoolean("unreadNotifications", false); publishButtonText=prefs.getString("publishButtonText", ""); theme=ThemePreference.values()[prefs.getInt("theme", 0)]; recentLanguages=fromJson(prefs.getString("recentLanguages", "{}"), recentLanguagesType, new HashMap<>()); @@ -152,7 +152,7 @@ public class GlobalUserPreferences{ .putBoolean("collapseLongPosts", collapseLongPosts) .putBoolean("spectatorMode", spectatorMode) .putBoolean("autoHideFab", autoHideFab) - .putLong("lastNotificationOpenedTime", lastNotificationOpenedTime) + .putBoolean("unreadNotifications", unreadNotifications) .putString("publishButtonText", publishButtonText) .putBoolean("bottomEncoding", bottomEncoding) .putInt("theme", theme.ordinal()) diff --git a/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java b/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java index 44e01fd02..749aef6d5 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java +++ b/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java @@ -76,6 +76,8 @@ public class PushNotificationReceiver extends BroadcastReceiver{ @Override public void onSuccess(org.joinmastodon.android.model.Notification result){ MastodonAPIController.runInBackground(()->PushNotificationReceiver.this.notify(context, pn, accountID, result)); + GlobalUserPreferences.unreadNotifications = true; + GlobalUserPreferences.save(); } @Override diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeFragment.java index 81915a575..c1e0f5757 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeFragment.java @@ -64,7 +64,6 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene private View tabBarWrap; private ImageView tabBarAvatar; private ImageView notificationTabIcon; - private boolean notificationBadged = false; @IdRes private int currentTab=R.id.tab_home; @@ -131,21 +130,7 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene ViewImageLoader.load(tabBarAvatar, null, new UrlImageLoaderRequest(self.avatar, V.dp(28), V.dp(28))); notificationTabIcon=content.findViewById(R.id.tab_notifications); - - AccountSessionManager.getInstance() - .getAccount(accountID).getCacheController() - .getNotifications(null, 1, false, false, true, new Callback<>() { - @Override - public void onSuccess(PaginatedResponse> result) { - notificationBadged = result.items.get(0).createdAt.isAfter(Instant.ofEpochMilli(GlobalUserPreferences.lastNotificationOpenedTime)); - setNotificationBadge(); - } - - @Override - public void onError(ErrorResponse error) { - error.showToast(getContext()); - } - }); + setNotificationBadge(); if(savedInstanceState==null){ getChildFragmentManager().beginTransaction() @@ -287,8 +272,7 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene } if(tab == R.id.tab_notifications){ - notificationBadged=false; - GlobalUserPreferences.lastNotificationOpenedTime = System.currentTimeMillis(); + GlobalUserPreferences.unreadNotifications = false; GlobalUserPreferences.save(); setNotificationBadge(); } @@ -367,6 +351,6 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene } private void setNotificationBadge() { - notificationTabIcon.setImageDrawable(getContext().getDrawable(notificationBadged ? R.drawable.ic_notifications_tab_badged : R.drawable.ic_fluent_alert_28_selector)); + notificationTabIcon.setImageDrawable(getContext().getDrawable(GlobalUserPreferences.unreadNotifications ? R.drawable.ic_notifications_tab_badged : R.drawable.ic_fluent_alert_28_selector)); } }