Files
evgeniyChepelev 315283822c iOS home screen widget (#78)
* Add Home Screen widget with VPN toggle and refactor app activation logic

- Add NetBirdWidgetExtension target with small/medium widget sizes
- Support direct connect/disconnect from widget via interactive buttons (iOS 17+)
- Detect missing VPN config or login-required state and open app via deep link
- Poll for stable VPN state after toggle to prevent loader getting stuck
- Add widget shared state keys to GlobalConstants and sync status from MainViewModel
- Fix false "authentication required" alert on app resume after widget disconnect
- Deduplicate app activation logic into shared startActivation/stopActivation
- Extract polling helpers: updateDetailsIfChanged, updatePeersIfChanged, applyExtensionStatus
2026-04-14 10:30:08 +02:00

38 lines
1.2 KiB
Swift

import SwiftUI
import AppIntents
/// Resolves what the widget's action area should show based on VPN state.
/// Extracts the shared transitioning / needsSetup / normal branching logic
/// so Small and Medium views stay focused on layout.
@available(iOS 17.0, *)
struct WidgetActionButton<TransitionContent: View, Label: View>: View {
let entry: VPNStatusEntry
let transitionContent: () -> TransitionContent
let label: (_ isConnected: Bool) -> Label
var body: some View {
if entry.status.isTransitioning {
transitionContent()
} else if entry.needsAppSetup && !entry.isConnected {
openAppLink {
label(false)
}
} else {
Button(intent: ToggleVPNIntent(action: entry.isConnected ? "disconnect" : "connect")) {
label(entry.isConnected)
}
.buttonStyle(.plain)
}
}
@ViewBuilder
private func openAppLink<Content: View>(@ViewBuilder content: () -> Content) -> some View {
let url = entry.loginRequired ? WidgetConstants.deepLinkLogin : WidgetConstants.deepLinkConnect
if let url {
Link(destination: url, label: content)
} else {
content()
}
}
}