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
@@ -3,7 +3,7 @@ import SwiftUI
@MainActor
public struct ConnectionListView: View {
@Environment(\.scenePhase) private var scenePhase
@EnvironmentObject private var commandClient: CommandClient
@StateObject private var viewModel = ConnectionListViewModel()
public init() {}
@@ -56,18 +56,12 @@ public struct ConnectionListView: View {
#endif
.alertBinding($viewModel.alert)
.onAppear {
viewModel.setCommandClient(commandClient)
viewModel.connect()
}
.onDisappear {
viewModel.disconnect()
}
.onChangeCompat(of: scenePhase) { newValue in
if newValue == .active {
viewModel.connect()
} else {
viewModel.disconnect()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
#if os(iOS)
.background(Color(uiColor: .systemGroupedBackground))
@@ -11,8 +11,8 @@ public class ConnectionListViewModel: ObservableObject {
@Published public var alert: Alert?
@Published public var connectionStateFilter: ConnectionStateFilter {
didSet {
commandClient.connectionStateFilter = connectionStateFilter
commandClient.filterConnectionsNow()
commandClient?.connectionStateFilter = connectionStateFilter
commandClient?.filterConnectionsNow()
saveStateFilterTask?.cancel()
saveStateFilterTask = Task {
await SharedPreferences.connectionStateFilter.set(connectionStateFilter.rawValue)
@@ -22,8 +22,8 @@ public class ConnectionListViewModel: ObservableObject {
@Published public var connectionSort: ConnectionSort {
didSet {
commandClient.connectionSort = connectionSort
commandClient.filterConnectionsNow()
commandClient?.connectionSort = connectionSort
commandClient?.filterConnectionsNow()
saveSortTask?.cancel()
saveSortTask = Task {
await SharedPreferences.connectionSort.set(connectionSort.rawValue)
@@ -31,7 +31,7 @@ public class ConnectionListViewModel: ObservableObject {
}
}
private let commandClient = CommandClient(.connections)
private var commandClient: CommandClient?
private var cancellables = Set<AnyCancellable>()
private var connectTask: Task<Void, Never>?
private var saveStateFilterTask: Task<Void, Never>?
@@ -40,8 +40,11 @@ public class ConnectionListViewModel: ObservableObject {
public init() {
connectionStateFilter = .active
connectionSort = .byDate
}
commandClient.$connections
public func setCommandClient(_ client: CommandClient) {
commandClient = client
client.$connections
.compactMap { $0 }
.sink { [weak self] goConnections in
self?.setConnections(goConnections)
@@ -60,7 +63,6 @@ public class ConnectionListViewModel: ObservableObject {
guard let self else { return }
await self.loadPreferences()
if Task.isCancelled { return }
self.commandClient.connect()
self.connectTask = nil
}
}
@@ -79,7 +81,6 @@ public class ConnectionListViewModel: ObservableObject {
saveStateFilterTask = nil
saveSortTask?.cancel()
saveSortTask = nil
commandClient.disconnect()
}
public func closeAllConnections() {
@@ -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)
}
@@ -2,7 +2,7 @@ import Library
import SwiftUI
public struct GroupListView: View {
@Environment(\.scenePhase) private var scenePhase
@EnvironmentObject private var commandClient: CommandClient
@StateObject private var viewModel = GroupListViewModel()
public init() {}
@@ -23,17 +23,8 @@ public struct GroupListView: View {
}
}
.onAppear {
viewModel.setCommandClient(commandClient)
viewModel.connect()
}
.onDisappear {
viewModel.disconnect()
}
.onChangeCompat(of: scenePhase) { newValue in
if newValue == .active {
viewModel.connect()
} else {
viewModel.disconnect()
}
}
}
}
@@ -8,11 +8,14 @@ public class GroupListViewModel: ObservableObject {
@Published public var isLoading = true
@Published public var groups: [OutboundGroup] = []
private let commandClient = CommandClient(.groups)
private var commandClient: CommandClient?
private var cancellables = Set<AnyCancellable>()
public init() {
commandClient.$groups
public init() {}
public func setCommandClient(_ client: CommandClient) {
commandClient = client
client.$groups
.compactMap { $0 }
.sink { [weak self] goGroups in
self?.setGroups(goGroups)
@@ -34,15 +37,9 @@ public class GroupListViewModel: ObservableObject {
}),
]
isLoading = false
} else {
commandClient.connect()
}
}
public func disconnect() {
commandClient.disconnect()
}
private func setGroups(_ goGroups: [LibboxOutboundGroup]) {
var groups = [OutboundGroup]()
for goGroup in goGroups {