Skip to content

Commit

Permalink
Add a repeat check for trigger combination evaluated to true
Browse files Browse the repository at this point in the history
Summary of changes:
  1. When a time-based trigger is evaluated and 'onTriggerCompleted()' is called, we no longer make redisplay messages available to avoid duplicate redisplay of a same message.
  2. Modify the logic of 'makeRedisplayMessagesAvailableWithTriggers()' so that a message can be redisplayed only if at least one trigger has changed or a new trigger is added.
  3. 'onTriggerConditionChanged()' now accepts a new parameter 'triggerId' that indicates the time-based trigger that fires.
  • Loading branch information
jinliu9508 committed Jul 18, 2024
1 parent 36e6824 commit 7582adf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,16 @@ internal class InAppMessagesManager(
*
* Make all messages with redisplay available if:
* - Already displayed
* - At least one Trigger has changed
* - At least one existing Trigger has changed OR a new trigger is added
*/
private fun makeRedisplayMessagesAvailableWithTriggers(newTriggersKeys: Collection<String>) {
private fun makeRedisplayMessagesAvailableWithTriggers(
newTriggersKeys: Collection<String>,
isNewTriggerAdded: Boolean,
) {
for (message in messages) {
if (!message.isTriggerChanged && redisplayedInAppMessages.contains(message) &&
_triggerController.isTriggerOnMessage(message, newTriggersKeys)
if (!message.isTriggerChanged &&
redisplayedInAppMessages.contains(message) &&
(_triggerController.isTriggerOnMessage(message, newTriggersKeys) || isNewTriggerAdded)
) {
Logging.debug("InAppMessagesManager.makeRedisplayMessagesAvailableWithTriggers: Trigger changed for message: $message")
message.isTriggerChanged = true
Expand Down Expand Up @@ -643,7 +647,6 @@ internal class InAppMessagesManager(
Logging.debug("InAppMessagesManager.onTriggerCompleted: called with triggerId: $triggerId")
val triggerIds: MutableSet<String> = HashSet()
triggerIds.add(triggerId)
makeRedisplayMessagesAvailableWithTriggers(triggerIds)
}

/**
Expand All @@ -653,9 +656,11 @@ internal class InAppMessagesManager(
*
* @see OSInAppMessageController.setDataForRedisplay
*/
override fun onTriggerConditionChanged() {
override fun onTriggerConditionChanged(triggerId: String) {
Logging.debug("InAppMessagesManager.onTriggerConditionChanged()")

makeRedisplayMessagesAvailableWithTriggers(listOf(triggerId), false)

suspendifyOnThread {
// This method is called when a time-based trigger timer fires, meaning the message can
// probably be shown now. So the current message conditions should be re-evaluated
Expand All @@ -666,7 +671,7 @@ internal class InAppMessagesManager(
override fun onTriggerChanged(newTriggerKey: String) {
Logging.debug("InAppMessagesManager.onTriggerChanged(newTriggerKey: $newTriggerKey)")

makeRedisplayMessagesAvailableWithTriggers(listOf(newTriggerKey))
makeRedisplayMessagesAvailableWithTriggers(listOf(newTriggerKey), true)

suspendifyOnThread {
// This method is called when a time-based trigger timer fires, meaning the message can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal interface ITriggerHandler {
/**
* Called when a time-based trigger (dynamic trigger) will now evaluate to true.
*/
fun onTriggerConditionChanged()
fun onTriggerConditionChanged(triggerId: String)

/**
* Called when a new trigger has been added, or an existing trigger's value has been
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ internal class DynamicTriggerController(
object : TimerTask() {
override fun run() {
scheduledMessages.remove(triggerId)
events.fire { it.onTriggerConditionChanged() }
events.fire { it.onTriggerConditionChanged(triggerId) }
}
},
triggerId,
Expand Down

0 comments on commit 7582adf

Please sign in to comment.