another.im-ios/ConversationsClassic/AppData/Stores/ClientsStore.swift

62 lines
1.8 KiB
Swift
Raw Normal View History

2024-08-11 00:28:01 +00:00
import Combine
import Foundation
import GRDB
@MainActor
final class ClientsStore: ObservableObject {
static let shared = ClientsStore()
@Published private(set) var ready = false
@Published private(set) var clients: [Client] = []
2024-08-11 19:01:48 +00:00
private let observation = ValueObservation.tracking(Credentials.fetchAll)
2024-08-11 00:28:01 +00:00
func startFetching() {
Task {
do {
for try await credentials in observation.values(in: Database.shared.dbQueue) {
processCredentials(credentials)
ready = true
print("Fetched \(credentials.count) credentials")
}
} catch {}
}
}
2024-08-11 19:01:48 +00:00
private func processCredentials(_ credentials: [Credentials]) {
let existsJids = Set(clients.map { $0.credentials.bareJid })
let credentialsJids = Set(credentials.map { $0.bareJid })
let forAdd = credentials.filter { !existsJids.contains($0.bareJid) }
let newClients = forAdd.map { Client(credentials: $0) }
let forRemove = clients.filter { !credentialsJids.contains($0.credentials.bareJid) }
forRemove.forEach { $0.disconnect() }
var updatedClients = clients.filter { credentialsJids.contains($0.credentials.bareJid) }
updatedClients.append(contentsOf: newClients)
clients = updatedClients
}
}
extension ClientsStore {
2024-08-11 00:28:01 +00:00
func addNewClient(_ client: Client) {
clients.append(client)
Task(priority: .background) {
try? await client.credentials.save()
}
}
2024-08-11 19:01:48 +00:00
func reconnectAll() {
Task {
await withTaskGroup(of: Void.self) { taskGroup in
for client in clients {
taskGroup.addTask {
await client.connect()
}
}
}
}
2024-08-11 00:28:01 +00:00
}
}