| @@ -24,6 +24,7 @@ struct SettingsScreen: View { |
| 24 | 24 | LabeledContent("Folder", value: model.configFolder?.lastPathComponent ?? "None") |
| 25 | 25 | Button(model.configFolder == nil ? "Choose Folder…" : "Choose Another Folder…") { choosing = true } |
| 26 | 26 | if model.configFolder != nil { |
| 27 | NavigationLink("Edit config.toml") { ConfigFileEditor(model: model) } |
| 27 | 28 | Button("Stop Using This Folder", role: .destructive) { model.setConfigFolder(nil) } |
| 28 | 29 | } |
| 29 | 30 | ForEach(model.configProblems, id: \.self) { Text($0).foregroundStyle(.red) } |
| @@ -158,4 +159,76 @@ struct SettingsScreen: View { |
| 158 | 159 | .joined(separator: "; ") |
| 159 | 160 | } |
| 160 | 161 | } |
| 162 | /// The configuration folder's `config.toml` as plain text, saved when you tap Save or leave. |
| 163 | struct ConfigFileEditor: View { |
| 164 | let model: MobileModel |
| 165 | @State private var text = "" |
| 166 | @State private var saved: String? |
| 167 | |
| 168 | var body: some View { |
| 169 | PlainTextView(text: $text) |
| 170 | .navigationTitle("config.toml") |
| 171 | .navigationBarTitleDisplayMode(.inline) |
| 172 | .toolbar { |
| 173 | Button("Save") { save() }.disabled(text == saved) |
| 174 | } |
| 175 | .safeAreaInset(edge: .bottom) { |
| 176 | let _ = model.settingsVersion |
| 177 | if !model.configProblems.isEmpty { |
| 178 | VStack(alignment: .leading, spacing: 4) { |
| 179 | ForEach(model.configProblems, id: \.self) { Text($0) } |
| 180 | } |
| 181 | .font(.footnote) |
| 182 | .foregroundStyle(.red) |
| 183 | .padding() |
| 184 | .frame(maxWidth: .infinity, alignment: .leading) |
| 185 | .background(.bar) |
| 186 | } |
| 187 | } |
| 188 | .onAppear { |
| 189 | guard saved == nil else { return } |
| 190 | text = model.configText() ?? UserConfig.defaultTemplate |
| 191 | saved = text |
| 192 | } |
| 193 | .onDisappear { save() } |
| 194 | } |
| 195 | |
| 196 | private func save() { |
| 197 | guard text != saved else { return } |
| 198 | model.saveConfig(text) |
| 199 | saved = text |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /// A monospaced text view without smart quotes, dashes or autocorrection, which would break TOML. |
| 204 | private struct PlainTextView: UIViewRepresentable { |
| 205 | @Binding var text: String |
| 206 | |
| 207 | func makeUIView(context: Context) -> UITextView { |
| 208 | let view = UITextView() |
| 209 | view.font = .monospacedSystemFont(ofSize: UIFont.preferredFont(forTextStyle: .footnote).pointSize, weight: .regular) |
| 210 | view.adjustsFontForContentSizeCategory = true |
| 211 | view.autocorrectionType = .no |
| 212 | view.autocapitalizationType = .none |
| 213 | view.smartQuotesType = .no |
| 214 | view.smartDashesType = .no |
| 215 | view.smartInsertDeleteType = .no |
| 216 | view.spellCheckingType = .no |
| 217 | view.keyboardDismissMode = .interactive |
| 218 | view.delegate = context.coordinator |
| 219 | return view |
| 220 | } |
| 221 | |
| 222 | func updateUIView(_ view: UITextView, context: Context) { |
| 223 | if view.text != text { view.text = text } |
| 224 | } |
| 225 | |
| 226 | func makeCoordinator() -> Coordinator { Coordinator(text: $text) } |
| 227 | |
| 228 | final class Coordinator: NSObject, UITextViewDelegate { |
| 229 | var text: Binding<String> |
| 230 | init(text: Binding<String>) { self.text = text } |
| 231 | func textViewDidChange(_ view: UITextView) { text.wrappedValue = view.text } |
| 232 | } |
| 233 | } |
| 161 | 234 | #endif |