mv-experiment #1
|
@ -2,7 +2,7 @@ import Foundation
|
||||||
import GRDB
|
import GRDB
|
||||||
import Martin
|
import Martin
|
||||||
|
|
||||||
final class RosterManager: Martin.RosterManager {
|
final class ClientMartinRosterManager: Martin.RosterManager {
|
||||||
func clear(for context: Martin.Context) {
|
func clear(for context: Martin.Context) {
|
||||||
do {
|
do {
|
||||||
try Database.shared.dbQueue.write { db in
|
try Database.shared.dbQueue.write { db in
|
|
@ -19,7 +19,7 @@ final class Client: ObservableObject {
|
||||||
private var connection: XMPPClient
|
private var connection: XMPPClient
|
||||||
private var connectionCancellable: AnyCancellable?
|
private var connectionCancellable: AnyCancellable?
|
||||||
|
|
||||||
private var rosterManager = RosterManager()
|
private var rosterManager = ClientMartinRosterManager()
|
||||||
|
|
||||||
init(credentials: Credentials) {
|
init(credentials: Credentials) {
|
||||||
self.credentials = credentials
|
self.credentials = credentials
|
||||||
|
|
|
@ -6,16 +6,32 @@ import GRDB
|
||||||
final class RostersStore: ObservableObject {
|
final class RostersStore: ObservableObject {
|
||||||
@Published private(set) var rosters: [Roster] = []
|
@Published private(set) var rosters: [Roster] = []
|
||||||
|
|
||||||
init() {
|
private var cancellable: AnyCancellable?
|
||||||
// Task {
|
|
||||||
// let observation = ValueObservation.tracking(Roster.fetchAll)
|
init(clientsPublisher: Published<[Client]>.Publisher) {
|
||||||
// do {
|
subscribeToClientsStore(clientsPublisher: clientsPublisher)
|
||||||
// for try await credentials in observation.values(in: Database.shared.dbQueue) {
|
}
|
||||||
// processCredentials(credentials)
|
|
||||||
// ready = true
|
private func subscribeToClientsStore(clientsPublisher: Published<[Client]>.Publisher) {
|
||||||
// print("Fetched \(credentials.count) credentials")
|
let rostersPublisher = ValueObservation.tracking(Roster.fetchAll)
|
||||||
// }
|
.publisher(in: Database.shared.dbQueue)
|
||||||
// } catch {}
|
.receive(on: DispatchQueue.main)
|
||||||
// }
|
.catch { _ in Just([]) }
|
||||||
|
|
||||||
|
cancellable = clientsPublisher
|
||||||
|
.flatMap { clients in
|
||||||
|
Publishers.MergeMany(clients.map { $0.$state })
|
||||||
|
.prepend(clients.map { $0.state })
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
.combineLatest(rostersPublisher)
|
||||||
|
.sink { [weak self] clientStates, rosters in
|
||||||
|
self?.handleUpdates(clientStates: clientStates, rosters: rosters)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func handleUpdates(clientStates: [ClientState], rosters: [Roster]) {
|
||||||
|
self.rosters = rosters
|
||||||
|
print("Client States: \(clientStates.count), Rosters: \(rosters.count)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ import SwiftUI
|
||||||
|
|
||||||
struct ContactsScreen: View {
|
struct ContactsScreen: View {
|
||||||
@EnvironmentObject var clientsStore: ClientsStore
|
@EnvironmentObject var clientsStore: ClientsStore
|
||||||
|
@StateObject var rostersStore = RostersStore(clientsPublisher: ClientsStore.shared.$clients)
|
||||||
// @State private var addPanelPresented = false
|
// @State private var addPanelPresented = false
|
||||||
// @State private var isErrorAlertPresented = false
|
// @State private var isErrorAlertPresented = false
|
||||||
// @State private var errorAlertMessage = ""
|
// @State private var errorAlertMessage = ""
|
||||||
|
|
Loading…
Reference in a new issue