|
| 1 | +import AIModel |
| 2 | +import Foundation |
| 3 | +import SwiftUI |
| 4 | + |
| 5 | +struct CustomHeaderSettingsView: View { |
| 6 | + @Binding var headers: [ChatModel.Info.CustomHeaderInfo.HeaderField] |
| 7 | + @Environment(\.dismiss) var dismiss |
| 8 | + @State private var newKey = "" |
| 9 | + @State private var newValue = "" |
| 10 | + |
| 11 | + var body: some View { |
| 12 | + VStack { |
| 13 | + List { |
| 14 | + ForEach(headers.indices, id: \.self) { index in |
| 15 | + HStack { |
| 16 | + TextField("Key", text: Binding( |
| 17 | + get: { headers[index].key }, |
| 18 | + set: { newKey in |
| 19 | + headers[index].key = newKey |
| 20 | + } |
| 21 | + )) |
| 22 | + TextField("Value", text: Binding( |
| 23 | + get: { headers[index].value }, |
| 24 | + set: { headers[index].value = $0 } |
| 25 | + )) |
| 26 | + Button(action: { |
| 27 | + headers.remove(at: index) |
| 28 | + }) { |
| 29 | + Image(systemName: "trash") |
| 30 | + .foregroundColor(.red) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + HStack { |
| 36 | + TextField("New Key", text: $newKey) |
| 37 | + TextField("New Value", text: $newValue) |
| 38 | + Button(action: { |
| 39 | + if !newKey.isEmpty { |
| 40 | + headers.append(ChatModel.Info.CustomHeaderInfo.HeaderField( |
| 41 | + key: newKey, |
| 42 | + value: newValue |
| 43 | + )) |
| 44 | + newKey = "" |
| 45 | + newValue = "" |
| 46 | + } |
| 47 | + }) { |
| 48 | + Image(systemName: "plus.circle.fill") |
| 49 | + .foregroundColor(.green) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + HStack { |
| 55 | + Spacer() |
| 56 | + Button("Done") { |
| 57 | + dismiss() |
| 58 | + } |
| 59 | + }.padding() |
| 60 | + } |
| 61 | + .frame(height: 500) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#Preview { |
| 66 | + struct V: View { |
| 67 | + @State var headers: [ChatModel.Info.CustomHeaderInfo.HeaderField] = [ |
| 68 | + .init(key: "key", value: "value"), |
| 69 | + .init(key: "key2", value: "value2"), |
| 70 | + ] |
| 71 | + var body: some View { |
| 72 | + CustomHeaderSettingsView(headers: $headers) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return V() |
| 77 | +} |
| 78 | + |
0 commit comments