Files
iOS/Sources/App/Notifications/NotificationManagerLocalPushInterfaceDirect.swift
Bruno Pantaleão Gonçalves 1bf28ec458 Improve local push reconnection and add logs (#4702)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->
- Added button to retry local push reconnection
- Improves reconnection logic
- Added logs to debug in case of issues
## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
2026-06-04 21:42:33 +00:00

66 lines
2.2 KiB
Swift

import Foundation
import HAKit
import Shared
class NotificationManagerLocalPushInterfaceDirect: NotificationManagerLocalPushInterface {
func status(for server: Server) -> NotificationManagerLocalPushStatus {
.allowed(localPushManagers[server].state)
}
private var localPushManagers: PerServerContainer<LocalPushManager>!
weak var localPushDelegate: LocalPushManagerDelegate?
init(delegate: LocalPushManagerDelegate) {
self.localPushDelegate = delegate
self.localPushManagers = .init { [weak self] server in
let manager = LocalPushManager(server: server)
manager.delegate = self?.localPushDelegate
let token = NotificationCenter.default.addObserver(
forName: LocalPushManager.stateDidChange,
object: manager,
queue: .main,
using: { [weak self] _ in
self?.pushManagerStateDidChange(server: server)
}
)
return .init(manager) { _, _ in
NotificationCenter.default.removeObserver(token)
}
}
}
func addObserver(
for server: Server,
handler: @escaping (NotificationManagerLocalPushStatus) -> Void
) -> HACancellable {
let observer = Observer(identifier: UUID(), server: server, handler: handler)
observers.append(observer)
return HABlockCancellable { [weak self] in
self?.observers.removeAll(where: { $0.identifier == observer.identifier })
}
}
func retryLocalPush(for server: Server?, reason: LocalPushRetryReason) {}
func scheduleAppOpenLocalPushRetries() {}
private struct Observer: Equatable {
let identifier: UUID
let server: Server
let handler: (NotificationManagerLocalPushStatus) -> Void
static func == (lhs: Observer, rhs: Observer) -> Bool {
lhs.identifier == rhs.identifier
}
}
private var observers = [Observer]()
private func pushManagerStateDidChange(server: Server) {
for observer in observers where observer.server == server {
observer.handler(status(for: server))
}
}
}