diff --git a/ConversationsClassic/AppCore/Actions/ConversationActions.swift b/ConversationsClassic/AppCore/Actions/ConversationActions.swift index 8b1b744..838f835 100644 --- a/ConversationsClassic/AppCore/Actions/ConversationActions.swift +++ b/ConversationsClassic/AppCore/Actions/ConversationActions.swift @@ -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]) } diff --git a/ConversationsClassic/AppCore/Middlewares/ConversationMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/ConversationMiddleware.swift index 2bc44e9..bfdafc7 100644 --- a/ConversationsClassic/AppCore/Middlewares/ConversationMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/ConversationMiddleware.swift @@ -6,7 +6,12 @@ final class ConversationMiddleware { func middleware(state: AppState, action: AppAction) -> AnyPublisher { switch action { case .chatsAction(.chatStarted(let chat)): - return Just(AppAction.conversationAction(.makeConversationActive(chat: chat))).eraseToAnyPublisher() + return Future { 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 { 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 { promise in + var currentMessages = state.conversationsState.currentMessages + currentMessages.append(message) + promise(.success(.conversationAction(.messagesUpdated(messages: currentMessages)))) + } + .eraseToAnyPublisher() + default: return Empty().eraseToAnyPublisher() } diff --git a/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift b/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift index 78aa23a..164afb0 100644 --- a/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift +++ b/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift @@ -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 diff --git a/ConversationsClassic/AppCore/State/ConversationState.swift b/ConversationsClassic/AppCore/State/ConversationState.swift index 39a9810..9f3146e 100644 --- a/ConversationsClassic/AppCore/State/ConversationState.swift +++ b/ConversationsClassic/AppCore/State/ConversationState.swift @@ -1,5 +1,6 @@ struct ConversationState: Stateable { var currentChat: Chat? + var currentRoster: Roster? var currentMessages: [Message] } diff --git a/ConversationsClassic/View/Screens/ConversationScreen.swift b/ConversationsClassic/View/Screens/ConversationScreen.swift index b30f88f..a7ffb07 100644 --- a/ConversationsClassic/View/Screens/ConversationScreen.swift +++ b/ConversationsClassic/View/Screens/ConversationScreen.swift @@ -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) }