1
1
import SwiftData
2
2
import SwiftUI
3
3
4
- struct LiteralHeadersSection : View {
5
- @AppStorage ( " UseLiteralHeaders " ) private var useLiteralHeaders = false
6
- @Environment ( \ . modelContext ) private var modelContext
4
+ struct LiteralHeadersSection < VPN : VPNService > : View {
5
+ @EnvironmentObject var vpn : VPN
6
+ @EnvironmentObject var settings : Settings
7
7
8
- @State private var selectedHeader : PersistentIdentifier ?
8
+ @State private var selectedHeader : LiteralHeader . ID ?
9
9
@State private var editingHeader : LiteralHeader ?
10
10
@State private var addingNewHeader = false
11
- @Query private var headers : [ LiteralHeader ]
11
+
12
+ let inspection = Inspection < Self > ( )
12
13
13
14
var body : some View {
14
15
Section {
15
- Toggle ( isOn: $useLiteralHeaders) {
16
+ Toggle ( isOn: settings . $useLiteralHeaders) {
16
17
Text ( " HTTP Headers " )
17
18
Text ( " When enabled, these headers will be included on all outgoing HTTP requests. " )
19
+ if vpn. state != . disabled { Text ( " Cannot be modified while Coder VPN is enabled. " ) }
18
20
}
19
21
. controlSize ( . large)
20
22
21
- Table ( headers , selection: $selectedHeader) {
23
+ Table ( settings . literalHeaders , selection: $selectedHeader) {
22
24
TableColumn ( " Header " , value: \. header)
23
25
TableColumn ( " Value " , value: \. value)
24
- } . opacity ( useLiteralHeaders ? 1 : 0.5 )
26
+ } . opacity ( settings . useLiteralHeaders ? 1 : 0.5 )
25
27
. frame ( minWidth: 400 , minHeight: 200 )
26
28
. padding ( . bottom, 25 )
27
29
. overlay ( alignment: . bottom) {
@@ -36,7 +38,8 @@ struct LiteralHeadersSection: View {
36
38
}
37
39
Divider ( )
38
40
Button {
39
- removeSelection ( )
41
+ settings. literalHeaders. removeAll { $0. id == selectedHeader }
42
+ selectedHeader = nil
40
43
} label: {
41
44
Image ( systemName: " minus " )
42
45
. frame ( width: 24 , height: 24 )
@@ -48,13 +51,13 @@ struct LiteralHeadersSection: View {
48
51
. fixedSize ( horizontal: false , vertical: true )
49
52
}
50
53
. background ( . primary. opacity ( 0.04 ) )
51
- . contextMenu ( forSelectionType: PersistentIdentifier . self, menu: { _ in } ,
54
+ . contextMenu ( forSelectionType: LiteralHeader . ID . self, menu: { _ in } ,
52
55
primaryAction: { selectedHeaders in
53
56
if let firstHeader = selectedHeaders. first {
54
- editingHeader = headers . first ( where: { $0. id == firstHeader } )
57
+ editingHeader = settings . literalHeaders . first ( where: { $0. id == firstHeader } )
55
58
}
56
59
} )
57
- . disabled ( !useLiteralHeaders)
60
+ . disabled ( !settings . useLiteralHeaders)
58
61
}
59
62
. sheet ( isPresented: $addingNewHeader) {
60
63
LiteralHeaderModal ( )
@@ -63,13 +66,7 @@ struct LiteralHeadersSection: View {
63
66
LiteralHeaderModal ( existingHeader: header)
64
67
} . onTapGesture {
65
68
selectedHeader = nil
66
- }
67
- }
68
-
69
- func removeSelection( ) {
70
- if let selectedHeader, let header = headers. first ( where: { $0. id == selectedHeader } ) {
71
- modelContext. delete ( header)
72
- self . selectedHeader = nil
73
- }
69
+ } . disabled ( vpn. state != . disabled)
70
+ . onReceive ( inspection. notice) { self . inspection. visit ( self , $0) } // ViewInspector
74
71
}
75
72
}
0 commit comments