mirror of
https://github.com/yJason/ClashX-Dashboard.git
synced 2026-03-01 00:35:19 +08:00
init
This commit is contained in:
33
ClashX Dashboard/Views/SidebarView/SidebarItem.swift
Normal file
33
ClashX Dashboard/Views/SidebarView/SidebarItem.swift
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// SidebarItem.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import SwiftUI
|
||||
|
||||
|
||||
class SidebarItems: ObservableObject, Identifiable {
|
||||
let id = UUID()
|
||||
@Published var items: [SidebarItem]
|
||||
@Published var selectedIndex = 0
|
||||
|
||||
init(_ items: [SidebarItem]) {
|
||||
self.items = items
|
||||
}
|
||||
}
|
||||
|
||||
class SidebarItem: ObservableObject {
|
||||
let id = UUID()
|
||||
let name: String
|
||||
let icon: String
|
||||
let view: AnyView
|
||||
|
||||
|
||||
init(name: String, icon: String, view: AnyView) {
|
||||
self.name = name
|
||||
self.icon = icon
|
||||
self.view = view
|
||||
}
|
||||
}
|
||||
28
ClashX Dashboard/Views/SidebarView/SidebarItemView.swift
Normal file
28
ClashX Dashboard/Views/SidebarView/SidebarItemView.swift
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// SidebarItemView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct SidebarItemView: View {
|
||||
|
||||
@State var item: SidebarItem
|
||||
|
||||
@Binding var selectionName: String?
|
||||
|
||||
var body: some View {
|
||||
NavigationLink(destination: item.view, tag: item.name, selection: $selectionName) {
|
||||
Label(item.name, systemImage: item.icon)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//struct SidebarItemView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// SidebarItemView(item: .init(name: "Overview",
|
||||
// icon: "chart.bar.xaxis",
|
||||
// view: AnyView(OverviewView())))
|
||||
// }
|
||||
//}
|
||||
86
ClashX Dashboard/Views/SidebarView/SidebarView.swift
Normal file
86
ClashX Dashboard/Views/SidebarView/SidebarView.swift
Normal file
@@ -0,0 +1,86 @@
|
||||
//
|
||||
// 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: String? = "Overview"
|
||||
|
||||
@State private var sidebarItems = [
|
||||
SidebarItem(name: "Overview",
|
||||
icon: "chart.bar.xaxis",
|
||||
view: AnyView(OverviewView())),
|
||||
|
||||
SidebarItem(name: "Proxies",
|
||||
icon: "globe.asia.australia",
|
||||
view: AnyView(ProxiesView())),
|
||||
|
||||
SidebarItem(name: "Rules",
|
||||
icon: "waveform.and.magnifyingglass",
|
||||
view: AnyView(RulesView())),
|
||||
|
||||
SidebarItem(name: "Conns",
|
||||
icon: "app.connected.to.app.below.fill",
|
||||
view: AnyView(ConnectionsView())),
|
||||
|
||||
SidebarItem(name: "Config",
|
||||
icon: "slider.horizontal.3",
|
||||
view: AnyView(ConfigView())),
|
||||
|
||||
SidebarItem(name: "Logs",
|
||||
icon: "wand.and.stars.inverse",
|
||||
view: AnyView(LogsView()))
|
||||
]
|
||||
|
||||
var body: some View {
|
||||
ScrollViewReader { scrollViewProxy in
|
||||
List(sidebarItems, id: \.id) { item in
|
||||
SidebarItemView(item: item, selectionName: $sidebarSelectionName)
|
||||
}
|
||||
.listStyle(.sidebar)
|
||||
}
|
||||
.environmentObject(clashApiDatasStorage.overviewData)
|
||||
.environmentObject(clashApiDatasStorage.logStorage)
|
||||
.environmentObject(clashApiDatasStorage.connsStorage)
|
||||
.onAppear {
|
||||
ConfigManager.selectLoggingApiLevel = .debug
|
||||
clashApiDatasStorage.resetStreamApi()
|
||||
|
||||
connsQueue.sync {
|
||||
clashApiDatasStorage.connsStorage.conns
|
||||
.removeAll()
|
||||
}
|
||||
|
||||
updateConnections()
|
||||
}
|
||||
.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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//struct SidebarView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// SidebarView()
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user