Skip to content

Commit

Permalink
dispatch using manager
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Sep 16, 2024
1 parent 946a08b commit 83c5670
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
19 changes: 4 additions & 15 deletions Sources/MessagingInApp/Gist/Gist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
27 changes: 18 additions & 9 deletions Sources/MessagingInApp/State/InAppMessageMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

Expand Down Expand Up @@ -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 }) {
Expand Down

0 comments on commit 83c5670

Please sign in to comment.