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()
// }
//}
@@ -8,25 +8,29 @@ import SwiftUI
struct RuleProviderView: View {
@State var ruleProvider: ClashRuleProvider
@State var provider: DBRuleProvider
var body: some View {
VStack(alignment: .leading) {
HStack {
Text(ruleProvider.name)
Text(provider.name)
.font(.title)
.fontWeight(.medium)
Text(ruleProvider.type)
Text(ruleProvider.behavior)
Text(provider.type)
Text(provider.behavior)
Spacer()
}
HStack {
Text("\(ruleProvider.ruleCount) rules")
if let date = ruleProvider.updatedAt {
Text("\(provider.ruleCount) rules")
if let date = provider.updatedAt {
Text("Updated \(RelativeDateTimeFormatter().localizedString(for: date, relativeTo: .now))")
}
Spacer()
}
.font(.system(size: 12))
.foregroundColor(.secondary)
}
}
}
@@ -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()
// }
//}
@@ -27,7 +27,6 @@ struct ProxyGroupView: View {
}
.padding()
}
.background {
GeometryReader { geometry in
Rectangle()
@@ -1,19 +0,0 @@
//
// ProxyListView.swift
// ClashX Dashboard
//
//
import SwiftUI
struct ProxyListView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
}
}
struct ProxyListView_Previews: PreviewProvider {
static var previews: some View {
ProxyListView()
}
}
@@ -1,207 +0,0 @@
//
// ProxyProviderGroupView.swift
// ClashX Dashboard
//
//
import SwiftUI
//struct ProxyProviderGroupView: View {
// @Binding var columnCount: Int
//
// @Binding var providerInfo: ClashProvider
//
// @State private var proxyItems: [ProxyItemData]
//
// @State private var trafficInfo: String
// @State private var expireDate: String
// @State private var updateAt: String
//
//
// @State private var isListExpanded = false
// @State private var isTesting = false
// @State private var isUpdating = false
//
// @EnvironmentObject var searchString: ProxiesSearchString
//
// init(columnCount: Binding<Int>,
// providerInfo: Binding<ClashProvider>) {
// self._columnCount = columnCount
// self._providerInfo = providerInfo
//
// let info = providerInfo.wrappedValue
//
// self.proxyItems = info.proxies.map(ProxyItemData.init)
//
// if let info = info.subscriptionInfo {
// let used = info.download + info.upload
// let total = info.total
//
// let formatter = ByteCountFormatter()
//
// trafficInfo = formatter.string(fromByteCount: used)
// + " / "
// + formatter.string(fromByteCount: total)
// + " ( \(String(format: "%.2f", Double(used)/Double(total/100)))% )"
//
//
// let expire = info.expire
//
// expireDate = "Expire: "
// + Date(timeIntervalSince1970: TimeInterval(expire))
// .formatted()
// } else {
// trafficInfo = ""
// expireDate = ""
// }
//
// if let updatedAt = info.updatedAt {
// let formatter = RelativeDateTimeFormatter()
// self.updateAt = formatter.localizedString(for: updatedAt, relativeTo: .now)
// } else {
// self.updateAt = ""
// }
// }
//
//
// var body: some View {
// Section {
// providerListView
// .background {
// Rectangle()
// .frame(width: 2, height: listHeight(columnCount))
// .foregroundColor(.clear)
// }
// .show(isVisible: !isListExpanded)
//
// } header: {
// providerInfoView
// } footer: {
// HStack {
// Button {
// update()
// } label: {
// Label("Update", systemImage: "arrow.clockwise")
// }
//
// .disabled(isUpdating)
//
// Button {
// startBenchmark()
// } label: {
// Label("Benchmark", systemImage: "bolt.fill")
// }
// .disabled(isTesting)
// }
// }
// }
//
// var providerInfoView: some View {
// VStack(alignment: .leading) {
// HStack {
// Text(providerInfo.name)
// .font(.title)
// .fontWeight(.medium)
// Text(providerInfo.vehicleType.rawValue)
// .fontWeight(.regular)
// Text("\(providerInfo.proxies.count)")
// Button() {
// isListExpanded = !isListExpanded
// } label: {
// Image(systemName: isListExpanded ? "chevron.up" : "chevron.down")
// }
// Button() {
// update()
// } label: {
// Image(systemName: "arrow.clockwise")
// }
// .disabled(isUpdating)
//
// Button() {
// startBenchmark()
// } label: {
// Image(systemName: "bolt.fill")
// }
// .disabled(isTesting)
// }
//
// HStack {
// if trafficInfo != "" {
// Text(trafficInfo)
// .fontWeight(.regular)
// }
// if expireDate != "" {
// Text(expireDate)
// .fontWeight(.regular)
// }
// }
// if updateAt != "" {
// Text("Updated \(updateAt)")
// .fontWeight(.regular)
// }
// }
// }
//
// var providerListView: some View {
// LazyVGrid(columns: Array(repeating: GridItem(.flexible()),
// count: columnCount)) {
// ForEach($proxyItems, id: \.id) { item in
// ProxyItemView(
// proxy: item,
// selectable: false
// )
// .background(.white)
// .cornerRadius(8)
//// .onTapGesture {
//// let item = item.wrappedValue
//// updateSelect(item.name)
//// }
// .show(isVisible: {
// if searchString.string.isEmpty {
// return true
// } else {
// return item.wrappedValue.name.lowercased().contains(searchString.string.lowercased())
// }
// }())
// }
// }
// }
//
// func listHeight(_ columnCount: Int) -> Double {
// let lineCount = ceil(Double(providerInfo.proxies.count) / Double(columnCount))
// return lineCount * 60 + (lineCount - 1) * 8
// }
//
// func startBenchmark() {
// isTesting = true
// let name = providerInfo.name
// ApiRequest.healthCheck(proxy: name) {
// ApiRequest.requestProxyProviderList {
// isTesting = false
//
// guard let provider = $0.allProviders[name] else { return }
// self.proxyItems = provider.proxies.map(ProxyItemData.init)
// }
// }
// }
//
// func update() {
// isUpdating = true
// let name = providerInfo.name
// ApiRequest.updateProvider(for: .proxy, name: name) { _ in
// ApiRequest.requestProxyProviderList {
// isUpdating = false
// guard let provider = $0.allProviders[name] else { return }
// self.providerInfo = provider
// }
// }
// }
//
//
//}
//struct ProviderGroupView_Previews: PreviewProvider {
// static var previews: some View {
// ProviderGroupView()
// }
//}
@@ -8,21 +8,11 @@ import SwiftUI
struct RulesView: View {
@State var ruleProviders = [ClashRuleProvider]()
@State var ruleItems = [ClashRule]()
@State private var searchString: String = ""
var providers: [ClashRuleProvider] {
if searchString.isEmpty {
return ruleProviders
} else {
return ruleProviders.filtered(searchString, for: ["name", "behavior", "type"])
}
}
var rules: [EnumeratedSequence<[ClashRule]>.Element] {
if searchString.isEmpty {
return Array(ruleItems.enumerated())
@@ -34,10 +24,6 @@ struct RulesView: View {
var body: some View {
List {
ForEach(providers, id: \.self) {
RuleProviderView(ruleProvider: $0)
}
ForEach(rules, id: \.element.id) {
RuleItemView(index: $0.offset, rule: $0.element)
}
@@ -48,14 +34,6 @@ struct RulesView: View {
ApiRequest.getRules {
ruleItems = $0
}
ApiRequest.requestRuleProviderList {
ruleProviders = $0.allProviders.map {
$0.value
}.sorted {
$0.name < $1.name
}
}
}
}
}
@@ -24,6 +24,10 @@ struct SidebarView: View {
icon: "globe.asia.australia",
view: AnyView(ProxiesView())),
SidebarItem(name: "Providers",
icon: "link.icloud",
view: AnyView(ProvidersView())),
SidebarItem(name: "Rules",
icon: "waveform.and.magnifyingglass",
view: AnyView(RulesView())),