This commit is contained in:
mrFq1
2023-06-05 23:39:23 +08:00
parent e5860ead2c
commit f488f41f8d
72 changed files with 247 additions and 45 deletions

View File

@@ -0,0 +1,18 @@
//
// SidebarItem.swift
// ClashX Dashboard
//
//
import Cocoa
import SwiftUI
enum SidebarItem: String {
case overview = "Overview"
case proxies = "Proxies"
case providers = "Providers"
case rules = "Rules"
case conns = "Conns"
case config = "Config"
case logs = "Logs"
}

View File

@@ -0,0 +1,83 @@
//
// SidebarListView.swift
// ClashX Dashboard
//
//
import SwiftUI
import Introspect
struct SidebarListView: View {
@Binding var selectionName: SidebarItem?
@State private var reloadID = UUID().uuidString
var body: some View {
List {
NavigationLink(destination: OverviewView(),
tag: SidebarItem.overview,
selection: $selectionName) {
Label(SidebarItem.overview.rawValue, systemImage: "chart.bar.xaxis")
}
NavigationLink(destination: ProxiesView(),
tag: SidebarItem.proxies,
selection: $selectionName) {
Label(SidebarItem.proxies.rawValue, systemImage: "globe.asia.australia")
}
NavigationLink(destination: ProvidersView(),
tag: SidebarItem.providers,
selection: $selectionName) {
Label(SidebarItem.providers.rawValue, systemImage: "link.icloud")
}
NavigationLink(destination: RulesView(),
tag: SidebarItem.rules,
selection: $selectionName) {
Label(SidebarItem.rules.rawValue, systemImage: "waveform.and.magnifyingglass")
}
NavigationLink(destination: ConnectionsView(),
tag: SidebarItem.conns,
selection: $selectionName) {
Label(SidebarItem.conns.rawValue, systemImage: "app.connected.to.app.below.fill")
}
NavigationLink(destination: ConfigView(),
tag: SidebarItem.config,
selection: $selectionName) {
Label(SidebarItem.config.rawValue, systemImage: "slider.horizontal.3")
}
NavigationLink(destination: LogsView(),
tag: SidebarItem.logs,
selection: $selectionName) {
Label(SidebarItem.logs.rawValue, systemImage: "wand.and.stars.inverse")
}
}
.introspectTableView {
if selectionName == nil {
selectionName = SidebarItem.overview
$0.allowsEmptySelection = false
if $0.selectedRow == -1 {
$0.selectRowIndexes(.init(integer: 0), byExtendingSelection: false)
}
}
}
.listStyle(.sidebar)
.id(reloadID)
.onReceive(NotificationCenter.default.publisher(for: .reloadDashboard)) { _ in
reloadID = UUID().uuidString
}
}
}
//struct SidebarListView_Previews: PreviewProvider {
// static var previews: some View {
// SidebarListView()
// }
//}

View File

@@ -0,0 +1,69 @@
//
// SidebarView.swift
// ClashX Dashboard
//
//
import SwiftUI
struct SidebarView: View {
@StateObject var clashApiDatasStorage = ClashApiDatasStorage()
private let connsQueue = DispatchQueue(label: "thread-safe-connsQueue", attributes: .concurrent)
private let timer = Timer.publish(every: 1, on: .main, in: .default).autoconnect()
@State private var sidebarSelectionName: SidebarItem?
var body: some View {
Group {
SidebarListView(selectionName: $sidebarSelectionName)
}
.environmentObject(clashApiDatasStorage.overviewData)
.environmentObject(clashApiDatasStorage.logStorage)
.environmentObject(clashApiDatasStorage.connsStorage)
.onAppear {
if ConfigManager.selectLoggingApiLevel == .unknow {
ConfigManager.selectLoggingApiLevel = .info
}
clashApiDatasStorage.resetStreamApi()
connsQueue.sync {
clashApiDatasStorage.connsStorage.conns
.removeAll()
}
updateConnections()
}
.onChange(of: sidebarSelectionName) { newValue in
sidebarItemChanged(newValue)
}
.onReceive(timer, perform: { _ in
updateConnections()
})
}
func updateConnections() {
ApiRequest.getConnections { snap in
connsQueue.sync {
clashApiDatasStorage.overviewData.upTotal = snap.uploadTotal
clashApiDatasStorage.overviewData.downTotal = snap.downloadTotal
clashApiDatasStorage.overviewData.activeConns = "\(snap.connections.count)"
clashApiDatasStorage.connsStorage.conns = snap.connections
}
}
}
func sidebarItemChanged(_ item: SidebarItem?) {
guard let item else { return }
NotificationCenter.default.post(name: .sidebarItemChanged, object: nil, userInfo: ["item": item])
}
}
//struct SidebarView_Previews: PreviewProvider {
// static var previews: some View {
// SidebarView()
// }
//}