From 0c8df19d553bf65534986bc58b9bfed3eeec8dfa Mon Sep 17 00:00:00 2001 From: fmodf Date: Thu, 20 Jun 2024 07:43:49 +0200 Subject: [PATCH] wip --- .../AppCore/Actions/ChatsActions.swift | 5 ++++ .../AppCore/Middlewares/ChatsMiddleware.swift | 18 ++++++++++--- .../Middlewares/DatabaseMiddleware.swift | 25 +++++++++++++++++++ .../AppCore/Reducers/ChatsReducer.swift | 3 +++ .../AppCore/State/ChatsState.swift | 1 + 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/ConversationsClassic/AppCore/Actions/ChatsActions.swift b/ConversationsClassic/AppCore/Actions/ChatsActions.swift index debeb1e..b247cd2 100644 --- a/ConversationsClassic/AppCore/Actions/ChatsActions.swift +++ b/ConversationsClassic/AppCore/Actions/ChatsActions.swift @@ -2,4 +2,9 @@ enum ChatsAction: Codable { case chatsListUpdated(chats: [Chat]) case startChat(accountJid: String, participantJid: String) + case chatStarted(chat: Chat) + + case createNewChat(accountJid: String, participantJid: String) + case chatCreated(chat: Chat) + case chatCreationFailed(reason: String) } diff --git a/ConversationsClassic/AppCore/Middlewares/ChatsMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/ChatsMiddleware.swift index 2e76ba1..a3cfd82 100644 --- a/ConversationsClassic/AppCore/Middlewares/ChatsMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/ChatsMiddleware.swift @@ -11,14 +11,24 @@ final class ChatsMiddleware { case .chatsAction(.startChat(accountJid: let accountJid, participantJid: let participantJid)): return Future { promise in - // find existing chat - let exist = state.chatsState.chats.first { - $0.account == accountJid && $0.participant == participantJid + if let exist = state.chatsState.chats.first(where: { $0.account == accountJid && $0.participant == participantJid }) { + // open existing chat + promise(.success(.chatsAction(.chatStarted(chat: exist)))) + } else { + // create new chat + promise(.success(.chatsAction(.createNewChat(accountJid: accountJid, participantJid: participantJid)))) } - promise(.success(.empty)) } .eraseToAnyPublisher() + case .chatsAction(.chatCreated(let chat)): + return Just(.chatsAction(.chatStarted(chat: chat))) + .eraseToAnyPublisher() + + case .chatsAction(.chatStarted(let chat)): + // TODO: make transition to chat screen + return Empty().eraseToAnyPublisher() + default: return Empty().eraseToAnyPublisher() } diff --git a/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift index abef39f..362236a 100644 --- a/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift @@ -125,6 +125,31 @@ final class DatabaseMiddleware { } .eraseToAnyPublisher() + case .chatsAction(.createNewChat(let accountJid, let participantJid)): + return Future { promise in + Task(priority: .background) { [weak self] in + guard let database = self?.database else { + promise(.success(.chatsAction(.chatCreationFailed(reason: L10n.Global.Error.genericDbError)))) + return + } + do { + try database._db.write { db in + let chat = Chat( + id: UUID().uuidString, + account: accountJid, + participant: participantJid, + type: .chat + ) + try chat.insert(db) + promise(.success(.chatsAction(.chatCreated(chat: chat)))) + } + } catch { + promise(.success(.chatsAction(.chatCreationFailed(reason: L10n.Global.Error.genericDbError)))) + } + } + } + .eraseToAnyPublisher() + default: return Empty().eraseToAnyPublisher() } diff --git a/ConversationsClassic/AppCore/Reducers/ChatsReducer.swift b/ConversationsClassic/AppCore/Reducers/ChatsReducer.swift index 9fadaab..393c61c 100644 --- a/ConversationsClassic/AppCore/Reducers/ChatsReducer.swift +++ b/ConversationsClassic/AppCore/Reducers/ChatsReducer.swift @@ -4,6 +4,9 @@ extension ChatsState { case .chatsListUpdated(let chats): state.chats = chats + case .chatStarted(let chat): + state.currentChat = chat + default: break } diff --git a/ConversationsClassic/AppCore/State/ChatsState.swift b/ConversationsClassic/AppCore/State/ChatsState.swift index d83fd39..469aef8 100644 --- a/ConversationsClassic/AppCore/State/ChatsState.swift +++ b/ConversationsClassic/AppCore/State/ChatsState.swift @@ -1,5 +1,6 @@ struct ChatsState: Stateable { var chats: [Chat] + var currentChat: Chat? } // MARK: Init