mirror of
https://github.com/yJason/ClashX-Dashboard.git
synced 2026-03-01 00:35:19 +08:00
fix: spm
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// OverviewTopItemView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct OverviewTopItemView: View {
|
||||
|
||||
@State var name: String
|
||||
@Binding var value: String
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
HStack {
|
||||
Text(name)
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
Spacer()
|
||||
}
|
||||
Text(value)
|
||||
.font(.system(size: 16))
|
||||
}
|
||||
.frame(width: 125)
|
||||
.padding(EdgeInsets(top: 10, leading: 13, bottom: 10, trailing: 13))
|
||||
.background(Color(nsColor: .textBackgroundColor))
|
||||
.cornerRadius(10)
|
||||
}
|
||||
}
|
||||
|
||||
struct OverviewTopItemView_Previews: PreviewProvider {
|
||||
@State static var value: String = "Value"
|
||||
static var previews: some View {
|
||||
OverviewTopItemView(name: "Name", value: $value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// OverviewView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import DSFSparkline
|
||||
|
||||
struct OverviewView: View {
|
||||
|
||||
@EnvironmentObject var data: ClashOverviewData
|
||||
|
||||
@State private var columnCount: Int = 4
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 25) {
|
||||
|
||||
|
||||
|
||||
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: columnCount)) {
|
||||
|
||||
OverviewTopItemView(name: "Upload", value: $data.uploadString)
|
||||
OverviewTopItemView(name: "Download", value: $data.downloadString)
|
||||
OverviewTopItemView(name: "Upload Total", value: $data.uploadTotal)
|
||||
OverviewTopItemView(name: "Download Total", value: $data.downloadTotal)
|
||||
|
||||
OverviewTopItemView(name: "Active Connections", value: $data.activeConns)
|
||||
OverviewTopItemView(name: "Memory Usage", value: $data.memory)
|
||||
}
|
||||
|
||||
.background {
|
||||
GeometryReader { geometry in
|
||||
Rectangle()
|
||||
.fill(.clear)
|
||||
.frame(height: 1)
|
||||
.onChange(of: geometry.size.width) { newValue in
|
||||
updateColumnCount(newValue)
|
||||
}
|
||||
.onAppear {
|
||||
updateColumnCount(geometry.size.width)
|
||||
}
|
||||
}.padding()
|
||||
}
|
||||
|
||||
|
||||
|
||||
HStack {
|
||||
RoundedRectangle(cornerRadius: 2)
|
||||
.fill(Color(nsColor: .systemBlue))
|
||||
.frame(width: 20, height: 13)
|
||||
Text("Down")
|
||||
|
||||
RoundedRectangle(cornerRadius: 2)
|
||||
.fill(Color(nsColor: .systemGreen))
|
||||
.frame(width: 20, height: 13)
|
||||
Text("Up")
|
||||
}
|
||||
|
||||
|
||||
TrafficGraphView(values: $data.downloadHistories,
|
||||
graphColor: .systemBlue)
|
||||
|
||||
TrafficGraphView(values: $data.uploadHistories,
|
||||
graphColor: .systemGreen)
|
||||
|
||||
}.padding()
|
||||
}
|
||||
|
||||
func updateColumnCount(_ width: Double) {
|
||||
let v = Int(Int(width) / 155)
|
||||
let new = v == 0 ? 1 : v
|
||||
|
||||
if new != columnCount {
|
||||
columnCount = new
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//struct OverviewView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// OverviewView()
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,148 @@
|
||||
//
|
||||
// TrafficGraphView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import DSFSparkline
|
||||
|
||||
fileprivate let labelsCount = 4
|
||||
|
||||
struct TrafficGraphView: View {
|
||||
@Binding var values: [CGFloat]
|
||||
@State var graphColor: DSFColor
|
||||
|
||||
init(values: Binding<[CGFloat]>,
|
||||
graphColor: DSFColor) {
|
||||
self._values = values
|
||||
self.graphColor = graphColor
|
||||
}
|
||||
|
||||
|
||||
@State private var labels = [String]()
|
||||
@State private var dataSource = DSFSparkline.DataSource()
|
||||
@State private var currentMaxValue: CGFloat = 0
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
VStack {
|
||||
ForEach(labels, id: \.self) {
|
||||
Text($0)
|
||||
.font(.system(size: 11, weight: .light))
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
graphView
|
||||
}
|
||||
.onAppear {
|
||||
updateChart(values)
|
||||
}
|
||||
.onChange(of: values) { newValue in
|
||||
updateChart(newValue)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var graphView: some View {
|
||||
ZStack {
|
||||
DSFSparklineLineGraphView.SwiftUI(
|
||||
dataSource: dataSource,
|
||||
graphColor: graphColor,
|
||||
interpolated: false,
|
||||
showZeroLine: false
|
||||
)
|
||||
|
||||
DSFSparklineSurface.SwiftUI([
|
||||
gridOverlay
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
let gridOverlay: DSFSparklineOverlay = {
|
||||
let grid = DSFSparklineOverlay.GridLines()
|
||||
grid.dataSource = .init(values: [1], range: 0...1)
|
||||
|
||||
|
||||
var floatValues = [CGFloat]()
|
||||
for i in 0...labelsCount {
|
||||
floatValues.append(CGFloat(i) / CGFloat(labelsCount))
|
||||
}
|
||||
let _ = floatValues.removeFirst()
|
||||
|
||||
grid.floatValues = floatValues.reversed()
|
||||
|
||||
grid.strokeColor = DSFColor.systemGray.withAlphaComponent(0.3).cgColor
|
||||
grid.strokeWidth = 0.5
|
||||
grid.dashStyle = [2, 2]
|
||||
|
||||
return grid
|
||||
}()
|
||||
|
||||
|
||||
func updateChart(_ values: [CGFloat]) {
|
||||
let max = values.max() ?? CGFloat(labelsCount) * 1000
|
||||
|
||||
if currentMaxValue != 0 && currentMaxValue == max {
|
||||
self.dataSource.set(values: values)
|
||||
return
|
||||
} else {
|
||||
currentMaxValue = max
|
||||
}
|
||||
|
||||
let byte = Int64(max)
|
||||
let kb = byte / 1000
|
||||
|
||||
var v1: Double = 0
|
||||
var v2 = ""
|
||||
var v3: Double = 1
|
||||
|
||||
switch kb {
|
||||
case 0..<Int64(labelsCount):
|
||||
v1 = Double(labelsCount)
|
||||
v2 = "KB/s"
|
||||
case Int64(labelsCount)..<100:
|
||||
// 0 - 99 KB/s
|
||||
v1 = Double(kb)
|
||||
v2 = "KB/s"
|
||||
case 100..<100_000:
|
||||
// 0.1 - 99MB/s
|
||||
v1 = Double(kb) / 1_000
|
||||
v2 = "MB/s"
|
||||
v3 = 1_000
|
||||
default:
|
||||
// case 10_000..<100_000:
|
||||
// 0.1 - 10GB/s
|
||||
v1 = Double(kb) / 1_000_000
|
||||
v2 = "GB/s"
|
||||
v3 = 1_000_000
|
||||
}
|
||||
|
||||
let vv = Double(labelsCount) / 10
|
||||
|
||||
if v1.truncatingRemainder(dividingBy: vv) != 0 {
|
||||
v1 = Double((Int(v1 / vv) + 1)) * vv
|
||||
}
|
||||
|
||||
var re = [String]()
|
||||
|
||||
for i in 0...labelsCount {
|
||||
let s = String(format: "%.1f%@", v1 * Double(i) / Double(labelsCount), v2)
|
||||
re.append(s)
|
||||
}
|
||||
re = re.reversed()
|
||||
let _ = re.removeLast()
|
||||
|
||||
let upperBound = CGFloat(v1*v3*1000)
|
||||
|
||||
self.dataSource.set(values: values)
|
||||
self.dataSource.setRange(lowerBound: 0, upperBound: upperBound)
|
||||
self.labels = re
|
||||
}
|
||||
}
|
||||
|
||||
//struct TrafficGraphView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// TrafficGraphView()
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user