Skip to content

Commit

Permalink
Map foreground/background events to same listener
Browse files Browse the repository at this point in the history
  • Loading branch information
rlepinski committed Apr 19, 2024
1 parent d4805f6 commit ce7353e
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions cordova-airship/src/android/AirshipCordova.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AirshipCordova : CordovaPlugin() {
val callbackContext: CallbackContext
)

private var listeners: MutableMap<EventType, MutableList<Listener>> = mutableMapOf()
private var listeners: MutableMap<String, MutableList<Listener>> = mutableMapOf()

companion object {
private val EVENT_NAME_MAP = mapOf(
Expand All @@ -49,7 +49,7 @@ class AirshipCordova : CordovaPlugin() {
EventType.MESSAGE_CENTER_UPDATED to "airship.event.message_center_updated",
EventType.PUSH_TOKEN_RECEIVED to "airship.event.push_token_received",
EventType.FOREGROUND_PUSH_RECEIVED to "airship.event.push_received",
EventType.BACKGROUND_PUSH_RECEIVED to "airship.event.background_push_received",
EventType.BACKGROUND_PUSH_RECEIVED to "airship.event.push_received",
EventType.NOTIFICATION_STATUS_CHANGED to "airship.event.notification_status_changed"
)
}
Expand Down Expand Up @@ -103,44 +103,30 @@ class AirshipCordova : CordovaPlugin() {
val jsonArgs = JsonValue.wrap(args).requireList()

val eventName = jsonArgs.get(0).requireString()
val event: EventType = EVENT_NAME_MAP.firstNotNullOf {
if (it.value == eventName) {
it.key
} else {
null
}
}

val listener = Listener(
listenerId = jsonArgs.get(1).requireInt(),
callbackContext = callbackContext
)

this.listeners.getOrPut(event) { mutableListOf() }.add(listener)
this.listeners.getOrPut(eventName) { mutableListOf() }.add(listener)
notifyPendingEvents()
}

private fun removeListener(args: JSONArray) {
val jsonArgs = JsonValue.wrap(args).requireList()

val eventName = jsonArgs.get(0).requireString()
val event: EventType = EVENT_NAME_MAP.firstNotNullOf {
if (it.value == eventName) {
it.key
} else {
null
}
}

val listenerId = jsonArgs.get(1).requireInt()
this.listeners[event]?.removeAll {
this.listeners[eventName]?.removeAll {
it.listenerId == listenerId
}
}

private fun notifyPendingEvents() {
EventType.values().forEach { eventType ->
val listeners = this.listeners[eventType]
val listeners = this.listeners[EVENT_NAME_MAP[eventType]]
if (listeners?.isNotEmpty() == true) {
EventEmitter.shared().processPending(listOf(eventType)) { event ->
listeners.forEach { listeners ->
Expand Down

0 comments on commit ce7353e

Please sign in to comment.