Files
Kymo/kymo/Features/MenuBar/PanelPublicIPRefresh.swift

25 lines
1.2 KiB
Swift

import SwiftUI
/// The mounted overview owns public-IP work and cancels it when demand ends.
struct PanelPublicIPRefresh: ViewModifier {
let metrics: MetricsStore
@Binding var address: String?
private var route: String {
let snapshot = metrics.snapshot
return "\(snapshot.defaultNetworkInterface ?? ""):\(String(describing: snapshot.networkPath)):\(snapshot.network?.filter { $0.active }.map { $0.addresses.joined(separator: ",") }.joined(separator: ";") ?? "")"
}
func body(content: Content) -> some View {
content.task(id: "\(route):\(metrics.isPaused):\(metrics.isSystemSleeping):\(metrics.isDisplaySleeping):\(metrics.isPanelVisible)") {
guard !metrics.isPaused, !metrics.isSystemSleeping, !metrics.isDisplaySleeping, metrics.isPanelVisible else { return }
address = nil
guard metrics.snapshot.networkPath == .available else { return }
await PublicIPAddressService.shared.refreshContinuously(route: route) { value in
await MainActor.run {
guard !Task.isCancelled else { return }
address = value
}
}
}
}
}