@@ -2,39 +2,77 @@ import SwiftUI
22
33struct MGConfigurationView : View {
44
5- @EnvironmentObject private var tunnel : MGPacketTunnelManager
5+ @EnvironmentObject private var packetTunnelManager : MGPacketTunnelManager
66
77 @EnvironmentObject private var configurationListManager : MGConfigurationListManager
8-
9- @State private var isPresented = false
10-
8+
9+ @State private var isConfigurationListExpanded = true
10+
1111 let current : Binding < String >
1212
1313 var body : some View {
14- LabeledContent {
15- Button ( " 切换 " ) {
16- isPresented. toggle ( )
17- }
18- . sheet ( isPresented: $isPresented) {
19- MGConfigurationListView ( current: current)
20- . environmentObject ( configurationListManager)
14+ DisclosureGroup ( isExpanded: $isConfigurationListExpanded) {
15+ if configurationListManager. configurations. isEmpty {
16+ HStack {
17+ Spacer ( )
18+ VStack ( spacing: 20 ) {
19+ Image ( systemName: " doc.text.magnifyingglass " )
20+ . font ( . largeTitle)
21+ Text ( " 暂无配置 " )
22+ }
23+ . foregroundColor ( . secondary)
24+ . padding ( )
25+ . padding ( . trailing, 25 )
26+ Spacer ( )
27+ }
28+ } else {
29+ ForEach ( configurationListManager. configurations) { configuration in
30+ Button {
31+ guard current. wrappedValue != configuration. id else {
32+ return
33+ }
34+ current. wrappedValue = configuration. id
35+ guard let status = packetTunnelManager. status, status == . connected else {
36+ return
37+ }
38+ packetTunnelManager. stop ( )
39+ Task ( priority: . userInitiated) {
40+ do {
41+ try await Task . sleep ( for: . milliseconds( 500 ) )
42+ try await packetTunnelManager. start ( )
43+ } catch { }
44+ }
45+ } label: {
46+ HStack {
47+ Image ( systemName: " checkmark " )
48+ . foregroundColor ( . accentColor)
49+ . opacity ( current. wrappedValue == configuration. id ? 1.0 : 0.0 )
50+ Text ( configuration. attributes. alias)
51+ . foregroundColor ( . primary)
52+ }
53+ }
54+ }
2155 }
2256 } label: {
23- Label {
24- Text ( title)
25- . lineLimit ( 1 )
26- } icon: {
27- Image ( systemName: " doc.plaintext " )
57+ LabeledContent {
58+ Text ( isConfigurationListExpanded ? " " : currentConfigurationName)
59+ } label: {
60+ Label {
61+ Text ( " 配置 " )
62+ } icon: {
63+ Image ( systemName: " doc.plaintext " )
64+ }
2865 }
2966 }
67+ . listRowSeparator ( configurationListManager. configurations. isEmpty ? . hidden : . automatic)
3068 . onAppear {
3169 configurationListManager. reload ( )
3270 }
3371 }
3472
35- private var title : String {
73+ private var currentConfigurationName : String {
3674 guard let configuration = configurationListManager. configurations. first ( where: { $0. id == current. wrappedValue } ) else {
37- return " 未选择 "
75+ return " 无 "
3876 }
3977 return configuration. attributes. alias
4078 }
0 commit comments