diff --git a/ConversationsClassic/AppData/Client/Client.swift b/ConversationsClassic/AppData/Client/Client.swift index 13398c5..1de5ad3 100644 --- a/ConversationsClassic/AppData/Client/Client.swift +++ b/ConversationsClassic/AppData/Client/Client.swift @@ -100,6 +100,12 @@ extension Client { _ = try await connection.module(.roster).removeItem(jid: JID(roster.contactBareJid)) } + func setActive(_ active: Bool) { + Task { + try? await credentials.setActive(flag: active) + } + } + func connect() async { guard credentials.isActive, state == .enabled(.disconnected) else { return diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index 0eda5a6..2d65d4a 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -71,8 +71,16 @@ extension ClientsStore { Task { await withTaskGroup(of: Void.self) { taskGroup in for client in clients { - taskGroup.addTask { - await client.connect() + if client.credentials.isActive && client.state != .enabled(.connected) { + taskGroup.addTask { + await client.connect() + } + } + + if !client.credentials.isActive && client.state == .enabled(.connected) { + taskGroup.addTask { + client.disconnect() + } } } } diff --git a/ConversationsClassic/View/Main/Settings/SettingsScreen.swift b/ConversationsClassic/View/Main/Settings/SettingsScreen.swift index 413573b..f83bb9c 100644 --- a/ConversationsClassic/View/Main/Settings/SettingsScreen.swift +++ b/ConversationsClassic/View/Main/Settings/SettingsScreen.swift @@ -26,13 +26,12 @@ struct SettingsScreen: View { SharedListRow( iconType: .charCircle(client.credentials.bareJid), text: client.credentials.bareJid, - controlType: .none - // controlType: .switcher(isOn: Binding( - // get: { client.credentials.isActive }, - // set: { new in - // client.setActive(new) - // } - // )) + controlType: .switcher(isOn: Binding( + get: { client.credentials.isActive }, + set: { new in + client.setActive(new) + } + )) ) .onTapGesture { print("Tapped account \(client.credentials.bareJid)") diff --git a/ConversationsClassic/View/RootView.swift b/ConversationsClassic/View/RootView.swift index 61b3638..3205073 100644 --- a/ConversationsClassic/View/RootView.swift +++ b/ConversationsClassic/View/RootView.swift @@ -7,7 +7,7 @@ struct RootView: View { var body: some View { Group { if clientsStore.ready { - if clientsStore.clients.isEmpty { + if clientsStore.clients.isEmpty || clientsStore.clients.allSatisfy({ !$0.credentials.isActive }) { RouterView { _ in WelcomeScreen() }