wip
This commit is contained in:
parent
f8a38b0fdd
commit
0c8df19d55
|
@ -2,4 +2,9 @@ enum ChatsAction: Codable {
|
||||||
case chatsListUpdated(chats: [Chat])
|
case chatsListUpdated(chats: [Chat])
|
||||||
|
|
||||||
case startChat(accountJid: String, participantJid: String)
|
case startChat(accountJid: String, participantJid: String)
|
||||||
|
case chatStarted(chat: Chat)
|
||||||
|
|
||||||
|
case createNewChat(accountJid: String, participantJid: String)
|
||||||
|
case chatCreated(chat: Chat)
|
||||||
|
case chatCreationFailed(reason: String)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,14 +11,24 @@ final class ChatsMiddleware {
|
||||||
|
|
||||||
case .chatsAction(.startChat(accountJid: let accountJid, participantJid: let participantJid)):
|
case .chatsAction(.startChat(accountJid: let accountJid, participantJid: let participantJid)):
|
||||||
return Future<AppAction, Never> { promise in
|
return Future<AppAction, Never> { promise in
|
||||||
// find existing chat
|
if let exist = state.chatsState.chats.first(where: { $0.account == accountJid && $0.participant == participantJid }) {
|
||||||
let exist = state.chatsState.chats.first {
|
// open existing chat
|
||||||
$0.account == accountJid && $0.participant == participantJid
|
promise(.success(.chatsAction(.chatStarted(chat: exist))))
|
||||||
|
} else {
|
||||||
|
// create new chat
|
||||||
|
promise(.success(.chatsAction(.createNewChat(accountJid: accountJid, participantJid: participantJid))))
|
||||||
}
|
}
|
||||||
promise(.success(.empty))
|
|
||||||
}
|
}
|
||||||
.eraseToAnyPublisher()
|
.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:
|
default:
|
||||||
return Empty().eraseToAnyPublisher()
|
return Empty().eraseToAnyPublisher()
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,6 +125,31 @@ final class DatabaseMiddleware {
|
||||||
}
|
}
|
||||||
.eraseToAnyPublisher()
|
.eraseToAnyPublisher()
|
||||||
|
|
||||||
|
case .chatsAction(.createNewChat(let accountJid, let participantJid)):
|
||||||
|
return Future<AppAction, Never> { 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:
|
default:
|
||||||
return Empty().eraseToAnyPublisher()
|
return Empty().eraseToAnyPublisher()
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,9 @@ extension ChatsState {
|
||||||
case .chatsListUpdated(let chats):
|
case .chatsListUpdated(let chats):
|
||||||
state.chats = chats
|
state.chats = chats
|
||||||
|
|
||||||
|
case .chatStarted(let chat):
|
||||||
|
state.currentChat = chat
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
struct ChatsState: Stateable {
|
struct ChatsState: Stateable {
|
||||||
var chats: [Chat]
|
var chats: [Chat]
|
||||||
|
var currentChat: Chat?
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Init
|
// MARK: Init
|
||||||
|
|
Loading…
Reference in a new issue