Refactor command client

This commit is contained in:
世界
2023-08-15 17:20:44 +08:00
parent 1ac5df7f72
commit dbd8700145
20 changed files with 378 additions and 572 deletions
+3 -1
View File
@@ -5,6 +5,7 @@ import SwiftUI
public struct MacApplication: Scene {
@State private var showMenuBarExtra = false
@State private var isMenuPresented = false
@StateObject private var environments = ExtensionEnvironments()
public init() {}
public var body: some Scene {
@@ -12,10 +13,11 @@ public struct MacApplication: Scene {
MainView()
.onAppear {
Task.detached {
initialize()
await initialize()
}
}
.environment(\.showMenuBarExtra, $showMenuBarExtra)
.environmentObject(environments)
})
.commands {
if showMenuBarExtra {
+26 -68
View File
@@ -5,11 +5,9 @@ import SwiftUI
public struct MainView: View {
@Environment(\.controlActiveState) private var controlActiveState
@EnvironmentObject private var environments: ExtensionEnvironments
@State private var selection = NavigationPage.dashboard
@State private var extensionProfile: ExtensionProfile?
@State private var profileLoading = true
@State private var logClient: LogClient!
@State private var importProfile: LibboxProfileContent?
@State private var importRemoteProfile: LibboxImportRemoteProfile?
@State private var alert: Alert?
@@ -19,55 +17,41 @@ public struct MainView: View {
NavigationSplitView {
SidebarView()
} detail: {
if profileLoading {
ProgressView().onAppear {
Task {
logClient = LogClient(SharedPreferences.maxLogLines)
await loadProfile()
}
}
} else {
selection.contentView
.navigationTitle(selection.title)
}
selection.contentView
.navigationTitle(selection.title)
}
#if !DEBUG
.onAppear {
environments.postReload()
#if !DEBUG
if Variant.useSystemExtension {
Task.detached {
checkApplicationPath()
}
}
#endif
}
.alertBinding($alert)
.toolbar {
ToolbarItem(placement: .navigation) {
StartStopButton()
}
#endif
.alertBinding($alert)
.toolbar {
ToolbarItem(placement: .navigation) {
StartStopButton()
}
}
.onChangeCompat(of: controlActiveState) { newValue in
if newValue != .inactive {
environments.postReload()
}
.onChangeCompat(of: controlActiveState) { newValue in
if newValue != .inactive {
Task {
await loadProfile()
connectLog()
}
}
}
.onChangeCompat(of: selection) { value in
if value == .logs {
environments.connectLog()
}
.onChangeCompat(of: selection) { value in
if value == .logs {
connectLog()
}
}
.formStyle(.grouped)
.environment(\.selection, $selection)
.environment(\.extensionProfile, $extensionProfile)
.environment(\.logClient, $logClient)
.environment(\.importProfile, $importProfile)
.environment(\.importRemoteProfile, $importRemoteProfile)
.handlesExternalEvents(preferring: [], allowing: ["*"])
.onOpenURL(perform: openURL)
}
.formStyle(.grouped)
.environment(\.selection, $selection)
.environment(\.importProfile, $importProfile)
.environment(\.importRemoteProfile, $importRemoteProfile)
.handlesExternalEvents(preferring: [], allowing: ["*"])
.onOpenURL(perform: openURL)
}
private func openURL(url: URL) {
@@ -97,32 +81,6 @@ public struct MainView: View {
}
}
private func loadProfile() async {
defer {
profileLoading = false
}
if let newProfile = try? await ExtensionProfile.load() {
if extensionProfile == nil {
newProfile.register()
extensionProfile = newProfile
}
} else {
extensionProfile = nil
}
}
private func connectLog() {
guard let profile = extensionProfile else {
return
}
guard let logClient else {
return
}
if profile.status.isConnected, !logClient.isConnected {
logClient.reconnect()
}
}
private func checkApplicationPath() {
let directoryName = URL(filePath: Bundle.main.bundlePath).deletingLastPathComponent().pathComponents.last
if directoryName != "Applications" {
+40
View File
@@ -0,0 +1,40 @@
import Libbox
import Library
import SwiftUI
public struct MenuLabel: View {
@EnvironmentObject private var environments: ExtensionEnvirnments
public init() {}
public var body: some View {
// if let profile = environments.extensionProfile {
// MenuLabel0().environmentObject(profile)
// } else {
// }
}
private struct MenuLabel0: View {
@EnvironmentObject private var environments: ExtensionEnvirnments
@EnvironmentObject private var extensionProfile: ExtensionProfile
@StateObject private var commandClient = CommandClient(.status)
var body: some View {
HStack {
if extensionProfile.status.isConnectedStrict, let message = commandClient.status {
Image("MenuIcon")
Text("\(LibboxFormatBytes(message.uplink))/s ↓ \(LibboxFormatBytes(message.downlink))/s")
} else {
Image("MenuIcon")
}
}
.onAppear {
commandClient.connect()
}
.onChangeCompat(of: extensionProfile.status) { newValue in
if newValue.isConnectedStrict {
commandClient.connect()
}
}
}
}
}
+4 -2
View File
@@ -4,12 +4,14 @@ import SwiftUI
public struct SidebarView: View {
@Environment(\.selection) private var selection
@Environment(\.extensionProfile) private var extensionProfile
@EnvironmentObject private var environments: ExtensionEnvironments
public init() {}
public var body: some View {
VStack {
if let profile = extensionProfile.wrappedValue {
if environments.extensionProfileLoading {
ProgressView()
} else if let profile = environments.extensionProfile {
SidebarView0().environmentObject(profile)
} else {
SidebarView1()