Files
sing-box-for-apple/ApplicationLibrary/Views/Connections/ConnectionListView.swift
T
2025-11-27 16:10:55 +08:00

88 lines
3.0 KiB
Swift

import Library
import SwiftUI
@MainActor
public struct ConnectionListView: View {
@EnvironmentObject private var environments: ExtensionEnvironments
@StateObject private var viewModel = ConnectionListViewModel()
public init() {}
public var body: some View {
VStack {
if viewModel.isLoading {
Text("Loading...")
} else {
if viewModel.connections.isEmpty {
Text("Empty connections")
} else {
ScrollView {
LazyVGrid(columns: [GridItem(.flexible())], alignment: .leading) {
ForEach(viewModel.filteredConnections(), id: \.id) { it in
ConnectionView(it)
}
}
}
.padding()
}
}
}
#if !os(tvOS)
.toolbar {
ToolbarItem {
Menu {
Picker("State", selection: $viewModel.connectionStateFilter) {
ForEach(ConnectionStateFilter.allCases) { state in
Text(state.name)
}
}
Picker("Sort By", selection: $viewModel.connectionSort) {
ForEach(ConnectionSort.allCases, id: \.self) { sortBy in
Text(sortBy.name)
}
}
Button("Close All Connections", role: .destructive) {
viewModel.closeAllConnections()
}
} label: {
Label("Filter", systemImage: "line.3.horizontal.circle")
}
}
}
#endif
#if os(macOS)
.searchable(text: $viewModel.searchText)
#endif
.alertBinding($viewModel.alert)
.onAppear {
viewModel.connect()
}
.onReceive(environments.commandClient.$connections) { connections in
viewModel.setConnections(connections)
}
.onChangeCompat(of: viewModel.connectionStateFilter) { filter in
environments.commandClient.connectionStateFilter = filter
environments.commandClient.filterConnectionsNow()
}
.onChangeCompat(of: viewModel.connectionSort) { sort in
environments.commandClient.connectionSort = sort
environments.commandClient.filterConnectionsNow()
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
#if os(iOS)
.background(Color(uiColor: .systemGroupedBackground))
#endif
}
private var backgroundColor: Color {
#if os(iOS)
return Color(uiColor: .secondarySystemGroupedBackground)
#elseif os(macOS)
return Color(nsColor: .textBackgroundColor)
#elseif os(tvOS)
return Color(uiColor: .black)
#endif
}
}