Refactor to use consolidated CommandClient

This commit is contained in:
世界
2025-11-26 22:25:48 +08:00
parent b71cbae382
commit 39b10d707e
11 changed files with 79 additions and 99 deletions
@@ -74,6 +74,31 @@ public struct ActiveDashboardView: View {
OverviewView($viewModel.profileList, $viewModel.selectedProfileID, $viewModel.systemProxyAvailable, $viewModel.systemProxyEnabled)
#endif
}
.environmentObject(viewModel.dashboardClient)
.onAppear {
if ApplicationLibrary.inPreview || profile.status.isConnected {
viewModel.dashboardClient.connect()
}
}
.onDisappear {
viewModel.dashboardClient.disconnect()
}
.onChangeCompat(of: scenePhase) { newPhase in
if newPhase == .active {
if profile.status.isConnected {
viewModel.dashboardClient.connect()
}
} else {
viewModel.dashboardClient.disconnect()
}
}
.onChangeCompat(of: profile.status) { newStatus in
if newStatus.isConnected {
viewModel.dashboardClient.connect()
} else {
viewModel.dashboardClient.disconnect()
}
}
.onReceive(environments.profileUpdate) { _ in
Task {
await viewModel.reload()
@@ -12,6 +12,7 @@ final class ActiveDashboardViewModel: ObservableObject {
@Published var selection = DashboardPage.overview
@Published var systemProxyAvailable = false
@Published var systemProxyEnabled = false
@Published var dashboardClient = CommandClient([.status, .groups, .clashMode, .connections])
var onEmptyProfilesChange: ((Bool) -> Void)?
@@ -4,7 +4,7 @@ import SwiftUI
@MainActor
public struct ClashModeView: View {
@Environment(\.scenePhase) private var scenePhase
@EnvironmentObject private var commandClient: CommandClient
@StateObject private var viewModel = ClashModeViewModel()
public init() {}
@@ -29,13 +29,7 @@ public struct ClashModeView: View {
}
.padding([.leading, .trailing])
.onAppear {
viewModel.connect()
}
.onDisappear {
viewModel.disconnect()
}
.onChangeCompat(of: scenePhase) { newValue in
viewModel.handleScenePhase(newValue)
viewModel.setCommandClient(commandClient)
}
.alertBinding($viewModel.alert)
}
@@ -7,37 +7,22 @@ final class ClashModeViewModel: ObservableObject {
@Published var clashMode = ""
@Published var alert: Alert?
private let commandClient = CommandClient(.clashMode)
var commandClient: CommandClient?
var clashModeList: [String] {
commandClient.clashModeList
commandClient?.clashModeList ?? []
}
var shouldShowPicker: Bool {
commandClient.clashModeList.count > 1
(commandClient?.clashModeList.count ?? 0) > 1
}
init() {
commandClient.$clashMode
func setCommandClient(_ client: CommandClient) {
commandClient = client
client.$clashMode
.assign(to: &$clashMode)
}
func connect() {
commandClient.connect()
}
func disconnect() {
commandClient.disconnect()
}
func handleScenePhase(_ phase: ScenePhase) {
if phase == .active {
connect()
} else {
disconnect()
}
}
nonisolated func setClashMode(_ newMode: String) async {
do {
try LibboxNewStandaloneCommandClient()!.setClashMode(newMode)
@@ -6,8 +6,7 @@ import SwiftUI
#endif
public struct ExtensionStatusView: View {
@Environment(\.scenePhase) private var scenePhase
@StateObject private var commandClient = CommandClient(.status)
@EnvironmentObject private var commandClient: CommandClient
@State private var columnCount: Int = 4
@State private var alert: Alert?
@@ -92,19 +91,6 @@ public struct ExtensionStatusView: View {
.frame(alignment: .topLeading)
.padding([.top, .leading, .trailing])
}
.onAppear {
commandClient.connect()
}
.onDisappear {
commandClient.disconnect()
}
.onChangeCompat(of: scenePhase) { newValue in
if newValue == .active {
commandClient.connect()
} else {
commandClient.disconnect()
}
}
.alertBinding($alert)
}