|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +struct AddUserDefaultEditor: View { |
| 4 | + |
| 5 | + let value: UserDefaultValue |
| 6 | + |
| 7 | + let model: UserDefaultsModel |
| 8 | + |
| 9 | + @Environment(\.dismiss) private var dismiss |
| 10 | + |
| 11 | + @State private var key: String = "example.key" |
| 12 | + |
| 13 | + var body: some View { |
| 14 | + NavigationView { |
| 15 | + VStack { |
| 16 | + VStack(alignment: .leading) { |
| 17 | + Text("Key") |
| 18 | + .font(.caption) |
| 19 | + .foregroundStyle(.secondary) |
| 20 | + TextField("", text: $key) |
| 21 | + .labelsHidden() |
| 22 | + .keyboardType(.numbersAndPunctuation) |
| 23 | + .padding(4) |
| 24 | + .background(Color(white: 0, opacity: 0.05)) |
| 25 | + .cornerRadius(4) |
| 26 | + } |
| 27 | + .padding() |
| 28 | + switch value { |
| 29 | + |
| 30 | + case .boolean(let value): |
| 31 | + AddBoolEditor(value: value) { value in |
| 32 | + let key = key.trimmingCharacters(in: .whitespacesAndNewlines) |
| 33 | + model.actions.setValue(value, forKey: key) |
| 34 | + dismiss() |
| 35 | + } |
| 36 | + |
| 37 | + case .integer(let value): |
| 38 | + IntValueEditor(value: value) { value in |
| 39 | + let key = key.trimmingCharacters(in: .whitespacesAndNewlines) |
| 40 | + model.actions.setValue(value, forKey: key) |
| 41 | + dismiss() |
| 42 | + } |
| 43 | + |
| 44 | + case .float(let value): |
| 45 | + FloatValueEditor(value: value) { value in |
| 46 | + let key = key.trimmingCharacters(in: .whitespacesAndNewlines) |
| 47 | + model.actions.setValue(value, forKey: key) |
| 48 | + dismiss() |
| 49 | + } |
| 50 | + |
| 51 | + case .double(let value): |
| 52 | + DoubleValueEditor(value: value) { value in |
| 53 | + let key = key.trimmingCharacters(in: .whitespacesAndNewlines) |
| 54 | + model.actions.setValue(value, forKey: key) |
| 55 | + dismiss() |
| 56 | + } |
| 57 | + |
| 58 | + case .string(let value): |
| 59 | + StringValueEditor(text: value) { text in |
| 60 | + let key = key.trimmingCharacters(in: .whitespacesAndNewlines) |
| 61 | + model.actions.setValue(text, forKey: key) |
| 62 | + dismiss() |
| 63 | + } |
| 64 | + |
| 65 | + case .other(_): |
| 66 | + Text("Type is not editable") |
| 67 | + } |
| 68 | + |
| 69 | + Spacer() |
| 70 | + } |
| 71 | + .navigationTitle("Add User Default") |
| 72 | + .navigationBarTitleDisplayMode(.inline) |
| 73 | + .toolbar { |
| 74 | + ToolbarItem(placement: .navigationBarTrailing) { |
| 75 | + Button { |
| 76 | + dismiss() |
| 77 | + } label: { |
| 78 | + Image(systemName: "xmark") |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +private struct AddBoolEditor: View { |
| 87 | + |
| 88 | + @State private var value: Bool |
| 89 | + |
| 90 | + private var apply: (Bool) -> Void |
| 91 | + |
| 92 | + init(value: Bool, apply: @escaping (Bool) -> Void) { |
| 93 | + _value = State(initialValue: value) |
| 94 | + self.apply = apply |
| 95 | + } |
| 96 | + |
| 97 | + var body: some View { |
| 98 | + VStack { |
| 99 | + Toggle("", isOn: $value) |
| 100 | + .labelsHidden() |
| 101 | + Button { |
| 102 | + apply(value) |
| 103 | + } label: { |
| 104 | + Text("Apply") |
| 105 | + } |
| 106 | + .buttonStyle(.borderedProminent) |
| 107 | + .padding(.bottom) |
| 108 | + Spacer() |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments