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,140 @@
|
||||
//
|
||||
// ProviderProxiesView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ProviderProxiesView: View {
|
||||
|
||||
@ObservedObject var provider: DBProxyProvider
|
||||
@EnvironmentObject var hideProxyNames: HideProxyNames
|
||||
@EnvironmentObject var searchString: ProxiesSearchString
|
||||
|
||||
@State private var columnCount: Int = 3
|
||||
@State private var isTesting = false
|
||||
@State private var isUpdating = false
|
||||
|
||||
var proxies: [DBProxy] {
|
||||
if searchString.string.isEmpty {
|
||||
return provider.proxies
|
||||
} else {
|
||||
return provider.proxies.filter {
|
||||
$0.name.lowercased().contains(searchString.string.lowercased())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
Section {
|
||||
proxyListView
|
||||
} header: {
|
||||
HStack {
|
||||
ProxyProviderInfoView(provider: provider)
|
||||
buttonsView
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
.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()
|
||||
}
|
||||
}
|
||||
|
||||
func updateColumnCount(_ width: Double) {
|
||||
let v = Int(Int(width) / 180)
|
||||
let new = v == 0 ? 1 : v
|
||||
|
||||
if new != columnCount {
|
||||
columnCount = new
|
||||
}
|
||||
}
|
||||
|
||||
var proxyListView: some View {
|
||||
LazyVGrid(columns: Array(repeating: GridItem(.flexible()),
|
||||
count: columnCount)) {
|
||||
ForEach(proxies, id: \.id) { proxy in
|
||||
ProxyItemView(
|
||||
proxy: proxy,
|
||||
selectable: false
|
||||
)
|
||||
.background(Color(nsColor: .textBackgroundColor))
|
||||
.cornerRadius(8)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var buttonsView: some View {
|
||||
VStack {
|
||||
ProgressButton(
|
||||
title: "Health Check",
|
||||
title2: "Testing",
|
||||
iconName: "bolt.fill",
|
||||
inProgress: $isTesting,
|
||||
autoWidth: false) {
|
||||
startHealthCheck()
|
||||
}
|
||||
|
||||
ProgressButton(
|
||||
title: "Update",
|
||||
title2: "Updating",
|
||||
iconName: "arrow.clockwise",
|
||||
inProgress: $isUpdating,
|
||||
autoWidth: false) {
|
||||
startUpdate()
|
||||
}
|
||||
}
|
||||
.frame(width: ProgressButton.width(
|
||||
[
|
||||
"Health Check",
|
||||
"Testing",
|
||||
"Update",
|
||||
"Updating"]
|
||||
))
|
||||
}
|
||||
|
||||
func startHealthCheck() {
|
||||
isTesting = true
|
||||
ApiRequest.healthCheck(proxy: provider.name) {
|
||||
updateProvider {
|
||||
isTesting = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func startUpdate() {
|
||||
isUpdating = true
|
||||
ApiRequest.updateProvider(for: .proxy, name: provider.name) { _ in
|
||||
updateProvider {
|
||||
isUpdating = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func updateProvider(_ completeHandler: (() -> Void)? = nil) {
|
||||
ApiRequest.requestProxyProviderList { resp in
|
||||
if let p = resp.allProviders[provider.name] {
|
||||
provider.updateInfo(DBProxyProvider(provider: p))
|
||||
}
|
||||
completeHandler?()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//struct ProviderProxiesView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// ProviderProxiesView()
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// ProviderRowView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ProviderRowView: View {
|
||||
|
||||
@ObservedObject var proxyProvider: DBProxyProvider
|
||||
@EnvironmentObject var hideProxyNames: HideProxyNames
|
||||
|
||||
var body: some View {
|
||||
NavigationLink {
|
||||
ProviderProxiesView(provider: proxyProvider)
|
||||
} label: {
|
||||
labelView
|
||||
}
|
||||
}
|
||||
|
||||
var labelView: some View {
|
||||
VStack(spacing: 2) {
|
||||
HStack(alignment: .center) {
|
||||
Text(hideProxyNames.hide
|
||||
? String(proxyProvider.id.prefix(8))
|
||||
: proxyProvider.name)
|
||||
.font(.system(size: 15))
|
||||
Spacer()
|
||||
Text(proxyProvider.trafficPercentage)
|
||||
.font(.system(size: 12))
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
HStack {
|
||||
Text(proxyProvider.vehicleType.rawValue)
|
||||
Spacer()
|
||||
Text(proxyProvider.updatedAt)
|
||||
}
|
||||
.font(.system(size: 11))
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
.padding(EdgeInsets(top: 1, leading: 4, bottom: 1, trailing: 4))
|
||||
}
|
||||
}
|
||||
|
||||
//struct ProviderRowView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// ProviderRowView()
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// ProvidersView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ProvidersView: View {
|
||||
@ObservedObject var providerStorage = DBProviderStorage()
|
||||
|
||||
@State private var searchString = ProxiesSearchString()
|
||||
|
||||
@StateObject private var hideProxyNames = HideProxyNames()
|
||||
|
||||
var body: some View {
|
||||
|
||||
NavigationView {
|
||||
listView
|
||||
EmptyView()
|
||||
}
|
||||
.searchable(text: $searchString.string)
|
||||
.onReceive(NotificationCenter.default.publisher(for: .toolbarSearchString)) {
|
||||
guard let string = $0.userInfo?["String"] as? String else { return }
|
||||
searchString.string = string
|
||||
}
|
||||
.onReceive(NotificationCenter.default.publisher(for: .hideNames)) {
|
||||
guard let hide = $0.userInfo?["hide"] as? Bool else { return }
|
||||
hideProxyNames.hide = hide
|
||||
}
|
||||
.environmentObject(searchString)
|
||||
.onAppear {
|
||||
loadProviders()
|
||||
}
|
||||
.environmentObject(hideProxyNames)
|
||||
.toolbar {
|
||||
ToolbarItem {
|
||||
Button {
|
||||
hideProxyNames.hide = !hideProxyNames.hide
|
||||
} label: {
|
||||
Image(systemName: hideProxyNames.hide ? "eyeglasses" : "wand.and.stars")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var listView: some View {
|
||||
List {
|
||||
if providerStorage.proxyProviders.isEmpty,
|
||||
providerStorage.ruleProviders.isEmpty {
|
||||
Text("Empty")
|
||||
.padding()
|
||||
} else {
|
||||
Section("Providers") {
|
||||
if !providerStorage.proxyProviders.isEmpty {
|
||||
ProxyProvidersRowView(providerStorage: providerStorage)
|
||||
}
|
||||
if !providerStorage.ruleProviders.isEmpty {
|
||||
RuleProvidersRowView(providerStorage: providerStorage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !providerStorage.proxyProviders.isEmpty {
|
||||
Text("")
|
||||
|
||||
Section("Proxy Provider") {
|
||||
ForEach(providerStorage.proxyProviders,id: \.id) {
|
||||
ProviderRowView(proxyProvider: $0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.introspectTableView {
|
||||
$0.refusesFirstResponder = true
|
||||
$0.doubleAction = nil
|
||||
}
|
||||
.listStyle(.plain)
|
||||
}
|
||||
|
||||
func loadProviders() {
|
||||
ApiRequest.requestProxyProviderList { resp in
|
||||
providerStorage.proxyProviders = resp.allProviders.values.filter {
|
||||
$0.vehicleType == .HTTP
|
||||
}.sorted {
|
||||
$0.name < $1.name
|
||||
}
|
||||
.map(DBProxyProvider.init)
|
||||
}
|
||||
ApiRequest.requestRuleProviderList { resp in
|
||||
providerStorage.ruleProviders = resp.allProviders.values.sorted {
|
||||
$0.name < $1.name
|
||||
}
|
||||
.map(DBRuleProvider.init)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//struct ProvidersView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// ProvidersView()
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// ProxyProviderInfoView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ProxyProviderInfoView: View {
|
||||
|
||||
@ObservedObject var provider: DBProxyProvider
|
||||
@EnvironmentObject var hideProxyNames: HideProxyNames
|
||||
|
||||
@State var withUpdateButton = false
|
||||
@State var isUpdating = false
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
VStack {
|
||||
header
|
||||
content
|
||||
}
|
||||
|
||||
if withUpdateButton {
|
||||
ProgressButton(
|
||||
title: "",
|
||||
title2: "",
|
||||
iconName: "arrow.clockwise",
|
||||
inProgress: $isUpdating) {
|
||||
update()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var header: some View {
|
||||
HStack() {
|
||||
Text(hideProxyNames.hide
|
||||
? String(provider.id.prefix(8))
|
||||
: provider.name)
|
||||
.font(.system(size: 17))
|
||||
Text(provider.vehicleType.rawValue)
|
||||
.font(.system(size: 13))
|
||||
.foregroundColor(.secondary)
|
||||
Text("\(provider.proxies.count)")
|
||||
.font(.system(size: 11))
|
||||
.padding(EdgeInsets(top: 2, leading: 4, bottom: 2, trailing: 4))
|
||||
.background(Color.gray.opacity(0.5))
|
||||
.cornerRadius(4)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
|
||||
var content: some View {
|
||||
VStack {
|
||||
HStack(spacing: 20) {
|
||||
Text(provider.trafficInfo)
|
||||
Text(provider.expireDate)
|
||||
Spacer()
|
||||
}
|
||||
HStack {
|
||||
Text("Updated \(provider.updatedAt)")
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
.font(.system(size: 12))
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
func update() {
|
||||
isUpdating = true
|
||||
let name = provider.name
|
||||
ApiRequest.updateProvider(for: .proxy, name: name) { _ in
|
||||
ApiRequest.requestProxyProviderList() { resp in
|
||||
if let p = resp.allProviders[provider.name] {
|
||||
provider.updateInfo(DBProxyProvider(provider: p))
|
||||
}
|
||||
isUpdating = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//struct ProxyProviderInfoView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// ProxyProviderInfoView()
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// ProxyProvidersRowView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ProxyProvidersRowView: View {
|
||||
|
||||
@ObservedObject var providerStorage: DBProviderStorage
|
||||
@EnvironmentObject var searchString: ProxiesSearchString
|
||||
|
||||
@State private var isUpdating = false
|
||||
|
||||
var providers: [DBProxyProvider] {
|
||||
if searchString.string.isEmpty {
|
||||
return providerStorage.proxyProviders
|
||||
} else {
|
||||
return providerStorage.proxyProviders.filter {
|
||||
$0.name.lowercased().contains(searchString.string.lowercased())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationLink {
|
||||
contentView
|
||||
} label: {
|
||||
Text("Proxy")
|
||||
.font(.system(size: 15))
|
||||
.padding(EdgeInsets(top: 2, leading: 4, bottom: 2, trailing: 4))
|
||||
}
|
||||
}
|
||||
|
||||
var contentView: some View {
|
||||
ScrollView {
|
||||
Section {
|
||||
VStack(spacing: 16) {
|
||||
listView
|
||||
}
|
||||
} header: {
|
||||
ProgressButton(
|
||||
title: "Update All",
|
||||
title2: "Updating",
|
||||
iconName: "arrow.clockwise", inProgress: $isUpdating) {
|
||||
updateAll()
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
var listView: some View {
|
||||
ForEach(providers, id: \.id) { provider in
|
||||
ProxyProviderInfoView(provider: provider, withUpdateButton: true)
|
||||
}
|
||||
}
|
||||
|
||||
func updateAll() {
|
||||
isUpdating = true
|
||||
|
||||
ApiRequest.updateAllProviders(for: .proxy) { _ in
|
||||
ApiRequest.requestProxyProviderList { resp in
|
||||
providerStorage.proxyProviders = resp.allProviders.values.filter {
|
||||
$0.vehicleType == .HTTP
|
||||
}.sorted {
|
||||
$0.name < $1.name
|
||||
}
|
||||
.map(DBProxyProvider.init)
|
||||
isUpdating = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//struct AllProvidersRowView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// ProxyProvidersRowView()
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// RuleProviderView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct RuleProviderView: View {
|
||||
|
||||
@State var provider: DBRuleProvider
|
||||
|
||||
var body: some View {
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
HStack {
|
||||
Text(provider.name)
|
||||
.font(.title)
|
||||
.fontWeight(.medium)
|
||||
Text(provider.type)
|
||||
Text(provider.behavior)
|
||||
Spacer()
|
||||
}
|
||||
|
||||
HStack {
|
||||
Text("\(provider.ruleCount) rules")
|
||||
if let date = provider.updatedAt {
|
||||
Text("Updated \(RelativeDateTimeFormatter().localizedString(for: date, relativeTo: .now))")
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
.font(.system(size: 12))
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//struct RuleProviderView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// RuleProviderView()
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,75 @@
|
||||
//
|
||||
// RuleProvidersRowView.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct RuleProvidersRowView: View {
|
||||
|
||||
@ObservedObject var providerStorage: DBProviderStorage
|
||||
@EnvironmentObject var searchString: ProxiesSearchString
|
||||
|
||||
@State private var isUpdating = false
|
||||
|
||||
var providers: [DBRuleProvider] {
|
||||
if searchString.string.isEmpty {
|
||||
return providerStorage.ruleProviders
|
||||
} else {
|
||||
return providerStorage.ruleProviders.filter {
|
||||
$0.name.lowercased().contains(searchString.string.lowercased())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationLink {
|
||||
contentView
|
||||
} label: {
|
||||
Text("Rule")
|
||||
.font(.system(size: 15))
|
||||
.padding(EdgeInsets(top: 2, leading: 4, bottom: 2, trailing: 4))
|
||||
}
|
||||
}
|
||||
|
||||
var contentView: some View {
|
||||
ScrollView {
|
||||
Section {
|
||||
VStack(spacing: 12) {
|
||||
ForEach(providers, id: \.id) {
|
||||
RuleProviderView(provider: $0)
|
||||
}
|
||||
}
|
||||
} header: {
|
||||
ProgressButton(
|
||||
title: "Update All",
|
||||
title2: "Updating",
|
||||
iconName: "arrow.clockwise",
|
||||
inProgress: $isUpdating) {
|
||||
updateAll()
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
func updateAll() {
|
||||
isUpdating = true
|
||||
ApiRequest.updateAllProviders(for: .rule) { _ in
|
||||
ApiRequest.requestRuleProviderList { resp in
|
||||
providerStorage.ruleProviders = resp.allProviders.values.sorted {
|
||||
$0.name < $1.name
|
||||
}
|
||||
.map(DBRuleProvider.init)
|
||||
isUpdating = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//struct ProxyProvidersRowView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// RuleProvidersRowView()
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user