feat: provider tab

This commit is contained in:
mrFq1
2023-05-18 14:39:36 +08:00
parent 8e9bf8c6cc
commit 7fb4694f23
14 changed files with 639 additions and 260 deletions
@@ -0,0 +1,158 @@
//
// 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 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($provider.proxies, id: \.id) { proxy in
ProxyItemView(
proxy: proxy,
selectable: false
)
.background(.white)
.cornerRadius(8)
.show(isVisible: {
if searchString.string.isEmpty {
return true
} else {
return proxy.wrappedValue.name.lowercased().contains(searchString.string.lowercased())
}
}())
}
}
}
var buttonsView: some View {
VStack {
Button() {
startHealthCheck()
} label: {
HStack {
if isTesting {
ProgressView()
.controlSize(.small)
.frame(width: 12)
} else {
Image(systemName: "bolt.fill")
.frame(width: 12)
}
Text(isTesting ? "Testing" : "Health Check")
.frame(width: 90)
}
.foregroundColor(isTesting ? .gray : .blue)
}
.disabled(isTesting)
Button() {
startUpdate()
} label: {
HStack {
if isUpdating {
ProgressView()
.controlSize(.small)
.frame(width: 12)
} else {
Image(systemName: "arrow.clockwise")
.frame(width: 12)
}
Text(isUpdating ? "Updating" : "Update")
.frame(width: 90)
}
.foregroundColor(isUpdating ? .gray : .blue)
}
.disabled(isTesting)
}
}
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] {
let new = DBProxyProvider(provider: p)
provider.proxies = new.proxies
provider.updatedAt = new.updatedAt
provider.expireDate = new.expireDate
provider.trafficInfo = new.trafficInfo
provider.trafficPercentage = new.trafficPercentage
}
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,68 @@
//
// ProvidersView.swift
// ClashX Dashboard
//
//
import SwiftUI
struct ProvidersView: View {
@ObservedObject var providerStorage = DBProviderStorage()
@EnvironmentObject var hideProxyNames: HideProxyNames
@State private var searchString = ProxiesSearchString()
var body: some View {
NavigationView {
List {
Section("Providers") {
ProxyProvidersRowView(providerStorage: providerStorage)
RuleProvidersRowView(providerStorage: providerStorage)
}
Text("")
Section("Proxy Provider") {
ForEach(providerStorage.proxyProviders,id: \.id) {
ProviderRowView(proxyProvider: $0)
}
}
}
.introspectTableView {
$0.refusesFirstResponder = true
$0.doubleAction = nil
}
.listStyle(.plain)
EmptyView()
}
.searchable(text: $searchString.string)
.environmentObject(searchString)
.onAppear {
loadProviders()
}
}
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,62 @@
//
// ProxyProviderInfoView.swift
// ClashX Dashboard
//
//
import SwiftUI
struct ProxyProviderInfoView: View {
@ObservedObject var provider: DBProxyProvider
@EnvironmentObject var hideProxyNames: HideProxyNames
var body: some View {
VStack {
header
content
}
}
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)
}
}
//struct ProxyProviderInfoView_Previews: PreviewProvider {
// static var previews: some View {
// ProxyProviderInfoView()
// }
//}
@@ -0,0 +1,82 @@
//
// ProxyProvidersRowView.swift
// ClashX Dashboard
//
//
import SwiftUI
struct ProxyProvidersRowView: View {
@ObservedObject var providerStorage: DBProviderStorage
@State private var isUpdating = false
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: {
Button {
updateAll()
} label: {
HStack {
if isUpdating {
ProgressView()
.controlSize(.small)
.frame(width: 12)
} else {
Image(systemName: "arrow.clockwise")
.frame(width: 12)
}
Text(isUpdating ? "Updating" : "Update All")
.frame(width: 80)
}
.foregroundColor(isUpdating ? .gray : .blue)
}
.disabled(isUpdating)
}
.padding()
}
}
var listView: some View {
ForEach(providerStorage.proxyProviders, id: \.id) {
ProxyProviderInfoView(provider: $0)
}
}
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
@State private var isUpdating = false
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(providerStorage.ruleProviders, id: \.id) {
RuleProviderView(provider: $0)
}
}
} header: {
Button {
updateAll()
} label: {
HStack {
if isUpdating {
ProgressView()
.controlSize(.small)
.frame(width: 12)
} else {
Image(systemName: "arrow.clockwise")
.frame(width: 12)
}
Text(isUpdating ? "Updating" : "Update All")
.frame(width: 80)
}
.foregroundColor(isUpdating ? .gray : .blue)
}
.disabled(isUpdating)
}
.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()
// }
//}