diff --git a/ConversationsClassic/AppCore/Actions/ConversationActions.swift b/ConversationsClassic/AppCore/Actions/ConversationActions.swift index 09efa13..8b1b744 100644 --- a/ConversationsClassic/AppCore/Actions/ConversationActions.swift +++ b/ConversationsClassic/AppCore/Actions/ConversationActions.swift @@ -1,4 +1,5 @@ enum ConversationAction: Codable { case makeConversationActive(chat: Chat) case messageForCurrentConversationReceived(Message) + case sendMessage(from: String, to: String, body: String) } diff --git a/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift index fa61a10..eaa20af 100644 --- a/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift @@ -86,6 +86,14 @@ final class XMPPMiddleware { } .eraseToAnyPublisher() + case .conversationAction(.sendMessage(let from, let to, let body)): + return Future { [weak self] promise in + // TODO: handle errors + self?.service.sendMessage(from: from, to: to, body: body) + promise(.success(.empty)) + } + .eraseToAnyPublisher() + default: return Empty().eraseToAnyPublisher() } diff --git a/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift b/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift index 4f167df..78aa23a 100644 --- a/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift +++ b/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift @@ -1,3 +1,4 @@ +// TODO: implement complex merging list of messages extension ConversationState { static func reducer(state: inout ConversationState, action: ConversationAction) { switch action { diff --git a/ConversationsClassic/AppCore/XMPP/XMPPService.swift b/ConversationsClassic/AppCore/XMPP/XMPPService.swift index ecd6f9c..4cfc3b6 100644 --- a/ConversationsClassic/AppCore/XMPP/XMPPService.swift +++ b/ConversationsClassic/AppCore/XMPP/XMPPService.swift @@ -103,4 +103,17 @@ final class XMPPService: ObservableObject { func getClient(for jid: String) -> XMPPClient? { clients.first { $0.connectionConfiguration.userJid.stringValue == jid } } + + // TODO: add handler + func sendMessage(from: String, to: String, body: String) { + guard let client = getClient(for: from) else { return } + let message = Martin.Message() + // message.from = client.connectionConfiguration.userJid + message.to = JID(to) + message.body = body + + client.writer.write(message) { res in + print(res) + } + } } diff --git a/ConversationsClassic/View/Screens/ConversationScreen.swift b/ConversationsClassic/View/Screens/ConversationScreen.swift index e97ed31..b30f88f 100644 --- a/ConversationsClassic/View/Screens/ConversationScreen.swift +++ b/ConversationsClassic/View/Screens/ConversationScreen.swift @@ -66,6 +66,13 @@ struct ConversationScreenTextInput: View { .padding(.trailing, 8) .tappablePadding(.symmetric(8)) { if !messageStr.isEmpty { + guard let acc = store.state.conversationsState.currentChat?.account else { return } + guard let contact = store.state.conversationsState.currentChat?.participant else { return } + store.dispatch(.conversationAction(.sendMessage( + from: acc, + to: contact, + body: messageStr + ))) messageStr = "" UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) }