From 5689a9e3428d1ec22fae0243906372bc74b0e56d Mon Sep 17 00:00:00 2001 From: fmodf Date: Sat, 19 Oct 2024 17:17:14 +0200 Subject: [PATCH] wip --- .../AppData/Store/ClientsStore.swift | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index a1d7a25..edd9412 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -83,13 +83,19 @@ final class ClientsStore: ObservableObject { // MARK: - Login/Connections extension ClientsStore { func tryLogin(_ jidStr: String, _ pass: String) async throws { - // login with fake timeout - async let sleep: Void? = try? await Task.sleep(nanoseconds: 1 * NSEC_PER_SEC) - async let request = try await Client.tryLogin(with: .init(bareJid: jidStr, pass: pass, isActive: true)) - let client = try await(request, sleep).0 + if let client = clients.first(where: { $0.credentials.bareJid == jidStr }) { + // check if credentials already exist and enable it + // this change will invoke reconnect automatically + try? await client.credentials.setActive(flag: true) + } else { + // new client login with fake timeout + async let sleep: Void? = try? await Task.sleep(nanoseconds: 1 * NSEC_PER_SEC) + async let request = try await Client.tryLogin(with: .init(bareJid: jidStr, pass: pass, isActive: true)) + let client = try await(request, sleep).0 - clients.append(client) - try? await client.credentials.save() + clients.append(client) + try? await client.credentials.save() + } } private func reconnectNeeded() {