Refactor Dashboard
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import Library
|
||||
import NetworkExtension
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct StartStopButton: View {
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
|
||||
public init() {}
|
||||
|
||||
public var body: some View {
|
||||
viewBuilder {
|
||||
if ApplicationLibrary.inPreview {
|
||||
Button {} label: {
|
||||
Label("Stop", systemImage: "stop.fill")
|
||||
}
|
||||
.labelStyle(.iconOnly)
|
||||
} else if let profile = environments.extensionProfile {
|
||||
ToggleConnectionButton().environmentObject(profile)
|
||||
} else {
|
||||
Button {} label: {
|
||||
Label("Start", systemImage: "play.fill")
|
||||
}
|
||||
.labelStyle(.iconOnly)
|
||||
.disabled(true)
|
||||
}
|
||||
}
|
||||
.disabled(environments.emptyProfiles)
|
||||
}
|
||||
|
||||
private struct ToggleConnectionButton: View {
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
@EnvironmentObject private var profile: ExtensionProfile
|
||||
@State private var alert: Alert?
|
||||
|
||||
var body: some View {
|
||||
Button {
|
||||
Task {
|
||||
await switchProfile(!profile.status.isConnected)
|
||||
}
|
||||
} label: {
|
||||
if !profile.status.isConnected {
|
||||
Label("Start", systemImage: "play.fill")
|
||||
} else {
|
||||
Label("Stop", systemImage: "stop.fill")
|
||||
}
|
||||
}
|
||||
.labelStyle(.iconOnly)
|
||||
.disabled(!profile.status.isEnabled)
|
||||
.alertBinding($alert)
|
||||
}
|
||||
|
||||
private nonisolated func switchProfile(_ isEnabled: Bool) async {
|
||||
do {
|
||||
if isEnabled {
|
||||
try await profile.start()
|
||||
await environments.commandClient.connect()
|
||||
} else {
|
||||
try await profile.stop()
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user