From 83c5670e51c7822e674804f8c0a378da1dadcf0c Mon Sep 17 00:00:00 2001 From: Rehan Date: Mon, 16 Sep 2024 14:56:59 +0500 Subject: [PATCH] dispatch using manager --- Sources/MessagingInApp/Gist/Gist.swift | 19 +++---------- .../State/InAppMessageMiddleware.swift | 27 ++++++++++++------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Sources/MessagingInApp/Gist/Gist.swift b/Sources/MessagingInApp/Gist/Gist.swift index 7720c35a4..691bc0816 100644 --- a/Sources/MessagingInApp/Gist/Gist.swift +++ b/Sources/MessagingInApp/Gist/Gist.swift @@ -129,22 +129,11 @@ public class Gist { } func fetchUserMessagesFromStoreState() { - logger.logWithModuleTag("Attempting to fetch user messages from local store", level: .info) - inAppMessageManager.fetchState { [self] state in - let messages = state.messagesInQueue - guard !messages.isEmpty else { return } - - // Switch to main thread before checking application state - threadUtil.runMain { - // Skip fetching messages from local store if application is in background - // This is to prevent showing messages when the app was moved to background - guard UIApplication.shared.applicationState != .background else { - self.logger.logWithModuleTag("Application in background, skipping local queue check.", level: .info) - return - } + logger.logWithModuleTag("[DEV] Attempting to fetch user messages from local store", level: .info) + inAppMessageManager.fetchState { [weak self] state in + guard let self else { return } - self.inAppMessageManager.dispatch(action: .processMessageQueue(messages: Array(messages))) - } + setupPollingAndFetch(skipMessageFetch: false, pollingInterval: state.pollInterval) } } diff --git a/Sources/MessagingInApp/State/InAppMessageMiddleware.swift b/Sources/MessagingInApp/State/InAppMessageMiddleware.swift index 44ec95b5c..44345ea9f 100644 --- a/Sources/MessagingInApp/State/InAppMessageMiddleware.swift +++ b/Sources/MessagingInApp/State/InAppMessageMiddleware.swift @@ -7,11 +7,23 @@ import Foundation private func middleware( completion: @escaping MiddlewareCompletion ) -> InAppMessageMiddleware { - { dispatch, getState in { next in { action in - let getStateOrDefault = { getState() ?? InAppMessageState() } - completion(dispatch, getStateOrDefault, next, action) - } - } + { _, + // swiftlint:disable:next closure_parameter_position + getState in { + // swiftlint:disable:next closure_parameter_position + next in { + // swiftlint:disable:next closure_parameter_position + action in + let getStateOrDefault = { getState() ?? InAppMessageState() } + // Use dispatch from InAppMessageManager so that next action is queued in correct order and dispatched + // only after the current action is processed + // Ideally, we should have dispatch as a parameter with completion, but we'll keep it simple for now + let dispatch: (InAppMessageAction) -> Void = { action in + DIGraphShared.shared.inAppMessageManager.dispatch(action: action) + } + completion(dispatch, getStateOrDefault, next, action) + } + } } } @@ -136,16 +148,13 @@ func messageQueueProcessorMiddleware(logger: Logger) -> InAppMessageMiddleware { } let state = getState() - let activeModalMessage = state.currentMessageState.activeModalMessage // Filter out messages with valid queueId that have not been shown yet and sort by priority let notShownMessages = messages .filter { message in guard let queueId = message.queueId else { return false } // Filter out messages that have been shown already, or if the message is embedded - return !state.shownMessageQueueIds.contains(queueId) && - activeModalMessage?.queueId != queueId && - message.gistProperties.elementId == nil + return !state.shownMessageQueueIds.contains(queueId) && message.gistProperties.elementId == nil } .reduce(into: [Message]()) { result, message in if !result.contains(where: { $0.queueId == message.queueId }) {