import Combine import Foundation import Network /// Publishes the current network's metering and data-constrained signals. /// /// This monitor reports path facts only. The parent model owns the policy that /// decides whether those facts should change the effective playback quality. @MainActor final class NetworkQualityMonitor: ObservableObject { @Published private(set) var isExpensive = false @Published private(set) var isConstrained = false @Published private(set) var hasCurrentPath = false private let monitor: NWPathMonitor private let monitorQueue: DispatchQueue init() { let monitor = NWPathMonitor() let monitorQueue = DispatchQueue(label: "com.hydraulophone.network-quality") self.monitor = monitor self.monitorQueue = monitorQueue monitor.pathUpdateHandler = { [weak self] path in Task { @MainActor [weak self] in self?.apply(path) } } monitor.start(queue: monitorQueue) } deinit { monitor.cancel() } private func apply(_ path: NWPath) { let hasCurrentPath = path.status == .satisfied self.hasCurrentPath = hasCurrentPath isExpensive = hasCurrentPath && path.isExpensive isConstrained = hasCurrentPath && path.isConstrained } }