From 841387aab7e96f05b4c78a75cbd8674b91ca2c5c Mon Sep 17 00:00:00 2001 From: fmodf Date: Tue, 22 Oct 2024 19:57:50 +0200 Subject: [PATCH] wip --- ConversationsClassic/AppData/Client/Client.swift | 3 +++ .../AppData/Store/ClientsStore.swift | 6 ++++++ ConversationsClassic/ConversationsClassicApp.swift | 13 +++++++++++++ 3 files changed, 22 insertions(+) diff --git a/ConversationsClassic/AppData/Client/Client.swift b/ConversationsClassic/AppData/Client/Client.swift index 2856562..0794b3b 100644 --- a/ConversationsClassic/AppData/Client/Client.swift +++ b/ConversationsClassic/AppData/Client/Client.swift @@ -82,6 +82,9 @@ extension Client { try? await credentials.setActive(flag: isActive) if isActive { self.state = .enabled(.disconnected) + } else { + self.state = .disabled + self.disconnect() } } } diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index ca8b5cf..9e845ac 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -236,6 +236,12 @@ private extension ClientsStore { } } +extension ClientsStore { + func reconnectOnActiveState() { + reconnectNeeded() + } +} + // MARK: - Remove all data for debug #if DEBUG extension ClientsStore { diff --git a/ConversationsClassic/ConversationsClassicApp.swift b/ConversationsClassic/ConversationsClassicApp.swift index bf24165..53fe0ac 100644 --- a/ConversationsClassic/ConversationsClassicApp.swift +++ b/ConversationsClassic/ConversationsClassicApp.swift @@ -4,6 +4,7 @@ import SwiftUI @main @MainActor struct ConversationsClassic: App { + @Environment(\.scenePhase) private var scenePhase private let clientsStore = ClientsStore.shared init() { @@ -16,6 +17,18 @@ struct ConversationsClassic: App { WindowGroup { RootView() .environmentObject(clientsStore) + .onChange(of: scenePhase) { phase in + switch phase { + case .active: + clientsStore.reconnectOnActiveState() + + case .inactive, .background: + fallthrough + + @unknown default: + break + } + } } } }