diff --git a/Sources/App/AppDelegate.swift b/Sources/App/AppDelegate.swift index e1d3292c8..c9c503044 100644 --- a/Sources/App/AppDelegate.swift +++ b/Sources/App/AppDelegate.swift @@ -50,6 +50,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } + private var shouldRefreshTitleSubscription = false + private var watchCommunicatorService: WatchCommunicatorService? func application( @@ -133,9 +135,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { #if targetEnvironment(macCatalyst) titleSubscription = manager.subscribeStatusItemTitle( - existing: titleSubscription, + existing: shouldRefreshTitleSubscription ? nil : titleSubscription, update: Current.macBridge.configureStatusItem(title:) ) + shouldRefreshTitleSubscription = false #endif } } @@ -400,12 +403,27 @@ class AppDelegate: UIResponder, UIApplicationDelegate { name: SettingsStore.menuRelatedSettingDidChange, object: nil ) + NotificationCenter.default.addObserver( + self, + selector: #selector(apiDidConnect(_:)), + name: HomeAssistantAPI.didConnectNotification, + object: nil + ) } @objc private func menuRelatedSettingDidChange(_ note: Notification) { UIMenuSystem.main.setNeedsRebuild() } + @objc private func apiDidConnect(_ note: Notification) { + // When API reconnects, rebuild the menu to refresh the status item title subscription + // Force refresh by setting the flag that will cause the subscription to be recreated + #if targetEnvironment(macCatalyst) + shouldRefreshTitleSubscription = true + #endif + UIMenuSystem.main.setNeedsRebuild() + } + private func setupUIApplicationShortcutItems() { if Current.isCatalyst { UIApplication.shared.shortcutItems = [.init( diff --git a/Sources/App/Utilities/MenuManager.swift b/Sources/App/Utilities/MenuManager.swift index 15da6b6fd..4ec39925e 100644 --- a/Sources/App/Utilities/MenuManager.swift +++ b/Sources/App/Utilities/MenuManager.swift @@ -81,6 +81,9 @@ class MenuManager { return existing } + // Cancel the old subscription before creating a new one + existing?.cancel() + // if we know it's going to change, reset it for now so it doesn't show the old value update("")