burrow/Apple/App/TunnelView.swift
Conrad Kramer b3a540fc48 Add support for starting and stopping the tunnel
This commit introduces the Tunnel view model object which has
support for asking for permission, starting and stopping the
tunnel. It automatically updates its state and publishes
changes as an ObservableObject.
2023-05-09 23:01:36 -04:00

37 lines
811 B
Swift

import SwiftUI
struct TunnelView: View {
@ObservedObject
var tunnel: Tunnel
var body: some View {
VStack {
Text(verbatim: tunnel.status.description)
switch tunnel.status {
case .connected(_):
Button("Disconnect", action: stop)
case .permissionRequired:
Button("Allow", action: configure)
case .disconnected:
Button("Start", action: start)
default:
EmptyView()
}
}
.task { await tunnel.update() }
.padding()
}
private func start() {
try? tunnel.start()
}
private func stop() {
tunnel.stop()
}
private func configure() {
Task { try await tunnel.configure() }
}
}