This commit is contained in:
fmodf 2024-06-25 14:20:20 +02:00
parent 5df60bd867
commit 2b3e50eb77
5 changed files with 30 additions and 0 deletions

View file

@ -1,4 +1,5 @@
enum ConversationAction: Codable {
case makeConversationActive(chat: Chat)
case messageForCurrentConversationReceived(Message)
case sendMessage(from: String, to: String, body: String)
}

View file

@ -86,6 +86,14 @@ final class XMPPMiddleware {
}
.eraseToAnyPublisher()
case .conversationAction(.sendMessage(let from, let to, let body)):
return Future<AppAction, Never> { [weak self] promise in
// TODO: handle errors
self?.service.sendMessage(from: from, to: to, body: body)
promise(.success(.empty))
}
.eraseToAnyPublisher()
default:
return Empty().eraseToAnyPublisher()
}

View file

@ -1,3 +1,4 @@
// TODO: implement complex merging list of messages
extension ConversationState {
static func reducer(state: inout ConversationState, action: ConversationAction) {
switch action {

View file

@ -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)
}
}
}

View file

@ -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)
}