This commit is contained in:
fmodf 2024-06-26 08:51:56 +02:00
parent 2b3e50eb77
commit fc88c50ae8
5 changed files with 31 additions and 18 deletions

View file

@ -1,5 +1,7 @@
enum ConversationAction: Codable {
case makeConversationActive(chat: Chat)
case makeConversationActive(chat: Chat, roster: Roster?)
case messageForCurrentConversationReceived(Message)
case sendMessage(from: String, to: String, body: String)
case messagesUpdated(messages: [Message])
}

View file

@ -6,7 +6,12 @@ final class ConversationMiddleware {
func middleware(state: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
switch action {
case .chatsAction(.chatStarted(let chat)):
return Just(AppAction.conversationAction(.makeConversationActive(chat: chat))).eraseToAnyPublisher()
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()
@ -14,7 +19,7 @@ final class ConversationMiddleware {
case .xmppAction(.xmppMessageReceived(let message)):
return Future<AppAction, Never> { promise in
let currentChat = state.conversationsState.currentChat
if message.from == currentChat?.participant, message.to == currentChat?.account {
if message.from == currentChat?.participant, message.to == currentChat?.account, message.contentType != .typing {
promise(.success(.conversationAction(.messageForCurrentConversationReceived(message))))
} else {
promise(.success(.empty))
@ -22,6 +27,14 @@ final class ConversationMiddleware {
}
.eraseToAnyPublisher()
case .conversationAction(.messageForCurrentConversationReceived(let message)):
return Future<AppAction, Never> { promise in
var currentMessages = state.conversationsState.currentMessages
currentMessages.append(message)
promise(.success(.conversationAction(.messagesUpdated(messages: currentMessages))))
}
.eraseToAnyPublisher()
default:
return Empty().eraseToAnyPublisher()
}

View file

@ -1,12 +1,14 @@
// TODO: implement complex merging list of messages
import SwiftUI
extension ConversationState {
static func reducer(state: inout ConversationState, action: ConversationAction) {
switch action {
case .makeConversationActive(let chat):
case .makeConversationActive(let chat, let roster):
state.currentChat = chat
state.currentRoster = roster
case .messageForCurrentConversationReceived(let message):
state.currentMessages.append(message)
case .messagesUpdated(let messages):
state.currentMessages = messages
default:
break

View file

@ -1,5 +1,6 @@
struct ConversationState: Stateable {
var currentChat: Chat?
var currentRoster: Roster?
var currentMessages: [Message]
}

View file

@ -84,7 +84,7 @@ struct ConversationScreenTextInput: View {
}
private struct ConversationScreenHeader: View {
// @EnvironmentObject var state: AppState
@EnvironmentObject var store: AppStore
var body: some View {
ZStack {
@ -93,11 +93,11 @@ private struct ConversationScreenHeader: View {
.ignoresSafeArea()
// title
// let name = (
// state.activeConversation?.participant?.name ??
// state.activeConversation.participant?.contactBareJid
// ) ?? L10n.Chat.title
Text(L10n.Chat.title)
let name = (
store.state.conversationsState.currentRoster?.name ??
store.state.conversationsState.currentRoster?.contactBareJid
) ?? L10n.Chat.title
Text(name)
.font(.head2)
.foregroundColor(Color.Main.black)
@ -108,11 +108,6 @@ private struct ConversationScreenHeader: View {
store.dispatch(.changeFlow(store.state.previousFlow))
}
Spacer()
// Image(systemName: "plus.viewfinder")
// .foregroundColor(Color.Tango.orangeMedium)
// .tappablePadding(.symmetric(12)) {
// print("Scan QR-code")
// }
}
.padding(.horizontal, 16)
}