diff --git a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/InAppMessagesManager.kt b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/InAppMessagesManager.kt index 6513e3f54..4c3f1e5d8 100644 --- a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/InAppMessagesManager.kt +++ b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/InAppMessagesManager.kt @@ -79,7 +79,7 @@ internal class InAppMessagesManager( // IAMs loaded remotely from on_session // If on_session won't be called this will be loaded from cache - private var messages: List = listOf() + private var messages: MutableList = mutableListOf() // IAMs that have been dismissed by the user // This mean they have already displayed to the user @@ -255,7 +255,7 @@ internal class InAppMessagesManager( val newMessages = _backend.listInAppMessages(appId, subscriptionId) if (newMessages != null) { - this.messages = newMessages + this.messages = newMessages as MutableList evaluateInAppMessages() } } @@ -265,16 +265,22 @@ internal class InAppMessagesManager( */ private suspend fun evaluateInAppMessages() { Logging.debug("InAppMessagesManager.evaluateInAppMessages()") - - for (message in messages) { - // Make trigger evaluation first, dynamic trigger might change "trigger changed" flag value for redisplay messages - if (_triggerController.evaluateMessageTriggers(message)) { - setDataForRedisplay(message) - if (!dismissedMessages.contains(message.messageId) && !message.isFinished) { - queueMessageForDisplay(message) + val messagesToQueue = mutableListOf() + + synchronized(messages) { + for (message in messages) { + if (_triggerController.evaluateMessageTriggers(message)) { + setDataForRedisplay(message) + if (!dismissedMessages.contains(message.messageId) && !message.isFinished) { + messagesToQueue.add(message) + } } } } + + for (message in messagesToQueue) { + queueMessageForDisplay(message) + } } /** @@ -390,6 +396,7 @@ internal class InAppMessagesManager( queueMessageForDisplay(messageToDisplay!!) } else if (result == false) { _state.inAppMessageIdShowing = null + messages.remove(messageToDisplay) messageWasDismissed(messageToDisplay!!, true) } } @@ -457,13 +464,17 @@ internal class InAppMessagesManager( newTriggersKeys: Collection, isNewTriggerAdded: Boolean, ) { - for (message in messages) { - val isMessageDisplayed = redisplayedInAppMessages.contains(message) - val isTriggerOnMessage = _triggerController.isTriggerOnMessage(message, newTriggersKeys) - val isOnlyDynamicTriggers = _triggerController.messageHasOnlyDynamicTriggers(message) - if (!message.isTriggerChanged && isMessageDisplayed && (isTriggerOnMessage || isNewTriggerAdded && isOnlyDynamicTriggers)) { - Logging.debug("InAppMessagesManager.makeRedisplayMessagesAvailableWithTriggers: Trigger changed for message: $message") - message.isTriggerChanged = true + synchronized(messages) { + for (message in messages) { + val isMessageDisplayed = redisplayedInAppMessages.contains(message) + val isTriggerOnMessage = + _triggerController.isTriggerOnMessage(message, newTriggersKeys) + val isOnlyDynamicTriggers = + _triggerController.messageHasOnlyDynamicTriggers(message) + if (!message.isTriggerChanged && isMessageDisplayed && (isTriggerOnMessage || isNewTriggerAdded && isOnlyDynamicTriggers)) { + Logging.debug("InAppMessagesManager.makeRedisplayMessagesAvailableWithTriggers: Trigger changed for message: $message") + message.isTriggerChanged = true + } } } }