26 lines
924 B
Swift
26 lines
924 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 Deferred {
|
|
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()
|
|
}
|
|
}
|
|
}
|