From 0ec74b37851c178b8a9b8f231e8695a6c58464cd Mon Sep 17 00:00:00 2001
From: ialokim <ialokim@mailbox.org>
Date: Tue, 25 Oct 2022 11:23:35 +0200
Subject: [PATCH] add notification cool-down phase to prevent flood of
 notifications

---
 .../briarproject/briar/desktop/ui/BriarUi.kt   | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/ui/BriarUi.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/ui/BriarUi.kt
index 44dc403110..2036067ea2 100644
--- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/ui/BriarUi.kt
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/ui/BriarUi.kt
@@ -65,6 +65,7 @@ import java.awt.event.WindowFocusListener
 import javax.annotation.concurrent.Immutable
 import javax.inject.Inject
 import javax.inject.Singleton
+import kotlin.time.Duration.Companion.seconds
 
 enum class Screen {
     STARTUP,
@@ -136,6 +137,9 @@ constructor(
 
             DisposableEffect(Unit) {
 
+                val notificationCoolDown = 5.seconds.inWholeMilliseconds
+                var lastNotification = 0L
+
                 val eventListener = EventListener { e ->
                     when (e) {
                         is LifecycleEvent ->
@@ -150,15 +154,21 @@ constructor(
 
                     override fun windowLostFocus(e: WindowEvent?) {
                         focusState.focused = false
+                        // reset notification cool-down
+                        lastNotification = 0
                     }
                 }
                 val messageCounterListener: MessageCounterListener = { (total, contacts) ->
                     if (total > 0 && !focusState.focused) {
                         window.iconImage = iconBadge
-                        if (configuration.visualNotifications)
-                            visualNotificationProvider.notifyPrivateMessages(total, contacts)
-                        if (configuration.soundNotifications)
-                            soundNotificationProvider.notifyPrivateMessages(total, contacts)
+                        val currentTime = System.currentTimeMillis()
+                        if (currentTime - lastNotification > notificationCoolDown) {
+                            if (configuration.visualNotifications)
+                                visualNotificationProvider.notifyPrivateMessages(total, contacts)
+                            if (configuration.soundNotifications)
+                                soundNotificationProvider.notifyPrivateMessages(total, contacts)
+                            lastNotification = currentTime
+                        }
                     }
                 }
 
-- 
GitLab