Refactor command client
This commit is contained in:
@@ -52,11 +52,14 @@ public struct DashboardView: View {
|
||||
#endif
|
||||
|
||||
struct DashboardView0: View {
|
||||
@Environment(\.extensionProfile) private var extensionProfile
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
|
||||
var body: some View {
|
||||
if ApplicationLibrary.inPreview {
|
||||
ActiveDashboardView()
|
||||
} else if let profile = extensionProfile.wrappedValue {
|
||||
} else if environments.extensionProfileLoading {
|
||||
ProgressView()
|
||||
} else if let profile = environments.extensionProfile {
|
||||
DashboardView1().environmentObject(profile)
|
||||
} else {
|
||||
FormView {
|
||||
@@ -67,13 +70,13 @@ public struct DashboardView: View {
|
||||
}
|
||||
|
||||
struct DashboardView1: View {
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
@EnvironmentObject private var profile: ExtensionProfile
|
||||
@State private var alert: Alert?
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
ActiveDashboardView()
|
||||
.environmentObject(profile)
|
||||
}
|
||||
.alertBinding($alert)
|
||||
.onChangeCompat(of: profile.status) { newValue in
|
||||
|
||||
@@ -3,16 +3,13 @@ import Library
|
||||
import SwiftUI
|
||||
|
||||
public struct ExtensionStatusView: View {
|
||||
@State private var commandClient: LibboxCommandClient?
|
||||
@State private var message: LibboxStatusMessage?
|
||||
@State private var connectTask: Task<Void, Error>?
|
||||
@StateObject private var commandClient = CommandClient(.status)
|
||||
@State private var columnCount: Int = 4
|
||||
@State private var alert: Alert?
|
||||
|
||||
private let infoFont = Font.system(.caption, design: .monospaced)
|
||||
|
||||
public init() {}
|
||||
|
||||
public var body: some View {
|
||||
viewBuilder {
|
||||
VStack {
|
||||
@@ -34,7 +31,7 @@ public struct ExtensionStatusView: View {
|
||||
StatusLine("Uplink", "52 MiB")
|
||||
StatusLine("Downlink", "5.6 GiB")
|
||||
}
|
||||
} else if let message {
|
||||
} else if let message = commandClient.status {
|
||||
StatusItem("Status") {
|
||||
StatusLine("Memory", LibboxFormatBytes(message.memory))
|
||||
StatusLine("Goroutines", "\(message.goroutines)")
|
||||
@@ -80,53 +77,15 @@ public struct ExtensionStatusView: View {
|
||||
.frame(alignment: .topLeading)
|
||||
.padding([.top, .leading, .trailing])
|
||||
}
|
||||
.onAppear(perform: doReload)
|
||||
.onAppear {
|
||||
commandClient.connect()
|
||||
}
|
||||
.onDisappear {
|
||||
connectTask?.cancel()
|
||||
if let commandClient {
|
||||
try? commandClient.disconnect()
|
||||
}
|
||||
commandClient = nil
|
||||
commandClient.disconnect()
|
||||
}
|
||||
.alertBinding($alert)
|
||||
}
|
||||
|
||||
private func doReload() {
|
||||
connectTask?.cancel()
|
||||
connectTask = Task.detached {
|
||||
await connect()
|
||||
}
|
||||
}
|
||||
|
||||
private func connect() async {
|
||||
let clientOptions = LibboxCommandClientOptions()
|
||||
clientOptions.command = LibboxCommandStatus
|
||||
clientOptions.statusInterval = Int64(2 * NSEC_PER_SEC)
|
||||
let client = LibboxNewCommandClient(FilePath.sharedDirectory.relativePath, statusHandler(self), clientOptions)!
|
||||
|
||||
do {
|
||||
for i in 0 ..< 10 {
|
||||
try await Task.sleep(nanoseconds: UInt64(Double(100 + (i * 50)) * Double(NSEC_PER_MSEC)))
|
||||
try Task.checkCancellation()
|
||||
let isConnected: Bool
|
||||
do {
|
||||
try client.connect()
|
||||
isConnected = true
|
||||
} catch {
|
||||
isConnected = false
|
||||
}
|
||||
try Task.checkCancellation()
|
||||
if isConnected {
|
||||
commandClient = client
|
||||
return
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
NSLog("failed to connect status: \(error.localizedDescription)")
|
||||
try? client.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
private func updateColumnCount(_ width: Double) {
|
||||
let v = Int(Int(width) / 155)
|
||||
let new = v < 1 ? 1 : (v > 4 ? 4 : (v % 2 == 0 ? v : v - 1))
|
||||
@@ -144,26 +103,6 @@ public struct ExtensionStatusView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private class statusHandler: NSObject, LibboxCommandClientHandlerProtocol {
|
||||
private let statusView: ExtensionStatusView
|
||||
|
||||
init(_ statusView: ExtensionStatusView) {
|
||||
self.statusView = statusView
|
||||
}
|
||||
|
||||
func connected() {}
|
||||
|
||||
func disconnected(_: String?) {}
|
||||
|
||||
func writeLog(_: String?) {}
|
||||
|
||||
func writeStatus(_ message: LibboxStatusMessage?) {
|
||||
statusView.message = message
|
||||
}
|
||||
|
||||
func writeGroups(_: LibboxOutboundGroupIteratorProtocol?) {}
|
||||
}
|
||||
|
||||
private struct StatusItem<T>: View where T: View {
|
||||
private let title: String
|
||||
@ViewBuilder private let content: () -> T
|
||||
|
||||
@@ -2,8 +2,6 @@ import Library
|
||||
import SwiftUI
|
||||
|
||||
public struct InstallProfileButton: View {
|
||||
@Environment(\.extensionProfile) private var extensionProfile
|
||||
|
||||
@State private var alert: Alert?
|
||||
|
||||
public init() {}
|
||||
|
||||
@@ -3,7 +3,7 @@ import NetworkExtension
|
||||
import SwiftUI
|
||||
|
||||
public struct StartStopButton: View {
|
||||
@Environment(\.extensionProfile) private var extensionProfile
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
|
||||
public init() {}
|
||||
|
||||
@@ -20,8 +20,8 @@ public struct StartStopButton: View {
|
||||
})
|
||||
#endif
|
||||
|
||||
} else if let profile = extensionProfile.wrappedValue {
|
||||
Button0(profile)
|
||||
} else if let profile = environments.extensionProfile {
|
||||
Button0().environmentObject(profile)
|
||||
} else {
|
||||
#if os(iOS) || os(tvOS)
|
||||
Toggle(isOn: .constant(false)) {
|
||||
@@ -39,14 +39,10 @@ public struct StartStopButton: View {
|
||||
}
|
||||
|
||||
private struct Button0: View {
|
||||
@Environment(\.logClient) private var logClient
|
||||
@ObservedObject private var profile: ExtensionProfile
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
@EnvironmentObject private var profile: ExtensionProfile
|
||||
@State private var alert: Alert?
|
||||
|
||||
init(_ profile: ExtensionProfile) {
|
||||
self.profile = profile
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
viewBuilder {
|
||||
#if os(iOS) || os(tvOS)
|
||||
@@ -81,7 +77,7 @@ public struct StartStopButton: View {
|
||||
do {
|
||||
if isEnabled {
|
||||
try await profile.start()
|
||||
logClient.wrappedValue?.reconnect()
|
||||
environments.logClient.connect()
|
||||
} else {
|
||||
profile.stop()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user