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 { enum ConversationAction: Codable {
case makeConversationActive(chat: Chat) case makeConversationActive(chat: Chat, roster: Roster?)
case messageForCurrentConversationReceived(Message) case messageForCurrentConversationReceived(Message)
case sendMessage(from: String, to: String, body: String) 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> { func middleware(state: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
switch action { switch action {
case .chatsAction(.chatStarted(let chat)): 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): case .conversationAction(.makeConversationActive):
return Just(AppAction.changeFlow(.conversation)).eraseToAnyPublisher() return Just(AppAction.changeFlow(.conversation)).eraseToAnyPublisher()
@ -14,7 +19,7 @@ final class ConversationMiddleware {
case .xmppAction(.xmppMessageReceived(let message)): case .xmppAction(.xmppMessageReceived(let message)):
return Future<AppAction, Never> { promise in return Future<AppAction, Never> { promise in
let currentChat = state.conversationsState.currentChat 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)))) promise(.success(.conversationAction(.messageForCurrentConversationReceived(message))))
} else { } else {
promise(.success(.empty)) promise(.success(.empty))
@ -22,6 +27,14 @@ final class ConversationMiddleware {
} }
.eraseToAnyPublisher() .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: default:
return Empty().eraseToAnyPublisher() return Empty().eraseToAnyPublisher()
} }

View file

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

View file

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

View file

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