Skip to content

Commit fd1dd04

Browse files
committed
Update UI
1 parent bb1f3f2 commit fd1dd04

3 files changed

Lines changed: 74 additions & 61 deletions

File tree

Mango/Home/MGConfigurationView.swift

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,77 @@ import SwiftUI
22

33
struct 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
}

Mango/Home/MGHomeView.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ struct MGHomeView: View {
44

55
@Environment(\.colorScheme) private var colorScheme
66
@EnvironmentObject private var packetTunnelManager: MGPacketTunnelManager
7-
7+
88
let current: Binding<String>
99

1010
var body: some View {
@@ -19,17 +19,15 @@ struct MGHomeView: View {
1919
.ignoresSafeArea()
2020
} else {
2121
Form {
22-
Section {
23-
MGConfigurationView(current: current)
24-
} header: {
25-
Text("配置")
26-
}
2722
Section {
2823
MGControlView()
2924
MGConnectedDurationView()
3025
} header: {
3126
Text("状态")
3227
}
28+
Section {
29+
MGConfigurationView(current: current)
30+
}
3331
}
3432
.environmentObject(packetTunnelManager)
3533
}

Mango/Subscribe/List/MGConfigurationListView.swift

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,21 @@ struct MGConfigurationListView: View {
1818
var body: some View {
1919
NavigationStack {
2020
List(configurationListManager.configurations) { configuration in
21-
Button {
22-
guard current.wrappedValue != configuration.id else {
23-
return
24-
}
25-
current.wrappedValue = configuration.id
26-
dismiss()
27-
guard let status = packetTunnelManager.status, status == .connected else {
28-
return
29-
}
30-
packetTunnelManager.stop()
31-
Task(priority: .userInitiated) {
32-
do {
33-
try await Task.sleep(for: .milliseconds(500))
34-
try await packetTunnelManager.start()
35-
} catch {}
21+
HStack(alignment: .center, spacing: 8) {
22+
VStack(alignment: .leading, spacing: 4) {
23+
Text(configuration.attributes.alias)
24+
.lineLimit(1)
25+
.foregroundColor(.primary)
26+
.fontWeight(.medium)
27+
Text(configuration.attributes.leastUpdated.formatted(.relative(presentation: .named)))
28+
.lineLimit(1)
29+
.foregroundColor(.secondary)
30+
.font(.callout)
31+
.fontWeight(.light)
3632
}
37-
} label: {
38-
HStack(alignment: .center, spacing: 8) {
39-
VStack(alignment: .leading, spacing: 4) {
40-
Text(configuration.attributes.alias)
41-
.lineLimit(1)
42-
.foregroundColor(.primary)
43-
.fontWeight(.medium)
44-
Text(configuration.attributes.leastUpdated.formatted(.relative(presentation: .named)))
45-
.lineLimit(1)
46-
.foregroundColor(.secondary)
47-
.font(.callout)
48-
.fontWeight(.light)
49-
}
50-
Spacer()
51-
if configurationListManager.downloadingConfigurationIDs.contains(configuration.id) {
52-
ProgressView()
53-
}
54-
if current.wrappedValue == configuration.id {
55-
Image(systemName: "checkmark")
56-
.foregroundColor(.accentColor)
57-
.fontWeight(.medium)
58-
}
33+
Spacer()
34+
if configurationListManager.downloadingConfigurationIDs.contains(configuration.id) {
35+
ProgressView()
5936
}
6037
}
6138
.swipeActions(edge: .trailing, allowsFullSwipe: false) {

0 commit comments

Comments
 (0)