From 87a5b69ba029d57d8c01359c8535775821cd7428 Mon Sep 17 00:00:00 2001
From: Thomas <tschneider.ac@gmail.com>
Date: Mon, 9 May 2022 19:21:50 +0200
Subject: [PATCH] Fix #19 / #31 - Notifications are displayed several times and
 are not correct

---
 .../fedilab/android/helper/NotificationsHelper.java | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java b/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java
index 6b66d43f..c2b98bc4 100644
--- a/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java
+++ b/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java
@@ -74,9 +74,12 @@ public class NotificationsHelper {
         if (accountDb == null) {
             return;
         }
-        String last_notifid = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + slug, null);
+        String last_notifid;
         if (since_ids.containsKey(slug)) {
             last_notifid = since_ids.get(slug);
+        } else {
+            last_notifid = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + slug, null);
+            since_ids.put(slug, last_notifid);
         }
 
         //Check which notifications the user wants to see
@@ -321,13 +324,14 @@ public class NotificationsHelper {
 
                         @Override
                         public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
-                            notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(),
-                                    R.mipmap.ic_launcher), finalNotifType, finalTitle, finalMessage1);
                             String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null);
                             if (lastNotif == null || notifications.get(0).id.compareTo(lastNotif) > 0) {
                                 SharedPreferences.Editor editor = prefs.edit();
+                                since_ids.put(account.user_id + "@" + account.instance, lastNotif);
                                 editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id);
                                 editor.apply();
+                                notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(),
+                                        R.mipmap.ic_launcher), finalNotifType, finalTitle, finalMessage1);
                             }
                             return false;
                         }
@@ -335,12 +339,13 @@ public class NotificationsHelper {
                     .into(new CustomTarget<Bitmap>() {
                         @Override
                         public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
-                            notify_user(context, account, intent, resource, finalNotifType, finalTitle, finalMessage);
                             String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null);
                             if (lastNotif == null || notifications.get(0).id.compareTo(lastNotif) > 0) {
                                 SharedPreferences.Editor editor = prefs.edit();
                                 editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id);
                                 editor.apply();
+                                since_ids.put(account.user_id + "@" + account.instance, lastNotif);
+                                notify_user(context, account, intent, resource, finalNotifType, finalTitle, finalMessage);
                             }
                         }