another.im-ios/ConversationsClassic/AppCore/Middlewares/ConversationMiddleware.swift
2024-06-26 11:29:30 +02:00

24 lines
867 B
Swift

import Combine
final class ConversationMiddleware {
static let shared = ConversationMiddleware()
func middleware(state: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
switch action {
case .chatsAction(.chatStarted(let chat)):
return Future<AppAction, Never> { promise in
let roster = state.rostersState.rosters
.first { $0.bareJid == chat.account && $0.contactBareJid == chat.participant }
promise(.success(.conversationAction(.makeConversationActive(chat: chat, roster: roster))))
}
.eraseToAnyPublisher()
case .conversationAction(.makeConversationActive):
return Just(AppAction.changeFlow(.conversation)).eraseToAnyPublisher()
default:
return Empty().eraseToAnyPublisher()
}
}
}