gitbay/Views/Repos/FileEditSheet.swift

v1.6.0
gitbay-ios/gitbay/Views/Repos/FileEditSheet.swift history · blame · raw

71 lines · 2639 bytes

 1import SwiftUI
 2
 3/// Edit one file and commit it. The commit is unsigned — the server
 4/// authors it — so a repository requiring signatures refuses, and the
 5/// refusal is what the sheet shows.
 6struct FileEditSheet: View {
 7
 8    let model: FileViewModel
 9    let branch: String
10    @Binding var content: String
11    let onCommitted: () -> Void
12
13    @Environment(\.dismiss) private var dismiss
14    @State private var message = ""
15
16    var body: some View {
17        NavigationStack {
18            Form {
19                Section {
20                    TextEditor(text: $content)
21                        .font(.gbMono(.caption))
22                        .frame(minHeight: 260)
23                        .autocorrectionDisabled()
24                        .textInputAutocapitalization(.never)
25                        .accessibilityIdentifier("file-edit-content")
26                } header: {
27                    Text(model.fileName)
28                } footer: {
29                    Text("Commits to \(branch) as an unsigned commit.")
30                }
31                if let format = MarkupPreview.format(forFile: model.fileName) {
32                    Section {
33                        MarkupPreview(text: content, format: format, identifier: "file-edit-preview")
34                    }
35                }
36                Section("Message") {
37                    TextField("edit \(model.fileName)", text: $message)
38                        .autocorrectionDisabled()
39                        .accessibilityIdentifier("file-edit-message")
40                }
41                if let error = model.actionError {
42                    Section {
43                        GBNotice(error, .gbWarn)
44                    }
45                }
46            }
47            .navigationTitle("Edit")
48            .navigationBarTitleDisplayMode(.inline)
49            .toolbar {
50                ToolbarItem(placement: .cancellationAction) {
51                    Button("Cancel") { dismiss() }
52                }
53                ToolbarItem(placement: .confirmationAction) {
54                    if model.working {
55                        ProgressView()
56                    } else {
57                        Button("Commit") {
58                            Task {
59                                if await model.commit(content: content, message: message) {
60                                    onCommitted()
61                                }
62                            }
63                        }
64                        .accessibilityIdentifier("file-edit-commit")
65                    }
66                }
67            }
68            .interactiveDismissDisabled(model.working)
69        }
70    }
71}