From 3b0386714b8da875515af1c08e67626f6987d2a3 Mon Sep 17 00:00:00 2001 From: Mirzamehdi Date: Fri, 6 Dec 2024 17:09:14 +0100 Subject: [PATCH] Adding setListener to set one listener or to remove existing listeners --- .../mmk/kmpnotifier/notification/NotifierManager.kt | 12 ++++++++++-- .../kmpnotifier/notification/NotifierManagerImpl.kt | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/NotifierManager.kt b/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/NotifierManager.kt index 850d730..097c474 100644 --- a/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/NotifierManager.kt +++ b/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/NotifierManager.kt @@ -31,12 +31,20 @@ public object NotifierManager { } /** - * For listening updates such as push notification token changes + * For listening updates such as push notification token changes. This will add new listener to listener list. */ public fun addListener(listener: Listener) { NotifierManagerImpl.addListener(listener) } + /** + * For listening updates such as push notification token changes. This will set one listener only. + * You can set null to remove listener. + */ + public fun setListener(listener: Listener?) { + NotifierManagerImpl.setListener(listener) + } + /** * * Returns permission util that can be used to check and ask notification permission @@ -72,7 +80,7 @@ public object NotifierManager { * @param title Notification title * @param body Notification body message */ - public fun onPushNotification(title:String?,body:String?) {} + public fun onPushNotification(title: String?, body: String?) {} /** * Called when notification is clicked diff --git a/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/NotifierManagerImpl.kt b/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/NotifierManagerImpl.kt index f67c5c8..b5a6c83 100644 --- a/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/NotifierManagerImpl.kt +++ b/kmpnotifier/src/commonMain/kotlin/com/mmk/kmpnotifier/notification/NotifierManagerImpl.kt @@ -34,6 +34,11 @@ internal object NotifierManagerImpl : KMPKoinComponent() { listeners.add(listener) } + fun setListener(listener: NotifierManager.Listener?) { + listeners.clear() + listener?.let { listeners.add(it) } + } + fun onNewToken(token: String) { listeners.forEach { it.onNewToken(token) } }