summaryrefslogtreecommitdiff
path: root/Hutch/Views/Repositories/RepositorySettingsView.swift
blob: e6beedfc761023ad66d80dcd458165d9bdc85404 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import SwiftUI

struct RepositorySettingsView: View {
    let repository: RepositorySummary
    let branches: [ReferenceDetail]
    let client: SRHTClient
    let onRenamed: (String) -> Void
    let onDeleted: () -> Void

    @Environment(\.dismiss) private var dismiss
    @State private var viewModel: RepositorySettingsViewModel?
    @State private var showDeleteConfirmation = false
    @State private var showRenameConfirmation = false
    @State private var saveResultAlert: SaveResultAlert?

    var body: some View {
        NavigationStack {
            Group {
                if let viewModel {
                    settingsForm(viewModel)
                } else {
                    SRHTLoadingStateView(message: "Loading settings…")
                }
            }
            .navigationTitle("Settings")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Done") { dismiss() }
                }
            }
        }
        .task {
            if viewModel == nil {
                let vm = RepositorySettingsViewModel(
                    repository: repository,
                    branches: branches,
                    client: client
                )
                viewModel = vm
            }
        }
    }

    @ViewBuilder
    private func settingsForm(_ viewModel: RepositorySettingsViewModel) -> some View {
        @Bindable var vm = viewModel

        Form {
            infoSection(viewModel)
            renameSection(viewModel)
            accessSection()
            deleteSection(viewModel)
        }
        .srhtErrorBanner(error: $vm.error)
        .alert(
            "Rename repository to \(viewModel.editedName.trimmingCharacters(in: .whitespacesAndNewlines))?",
            isPresented: $showRenameConfirmation
        ) {
            Button("Cancel", role: .cancel) {
                // Alert dismissal is implicit; no additional action required.
            }
            Button("Rename", role: .destructive) {
                Task {
                    await viewModel.rename()
                    if let newName = viewModel.updatedName {
                        onRenamed(newName)
                        dismiss()
                    }
                }
            }
        } message: {
            Text("This will change the repository URL. Existing clones will be redirected but links may break.")
        }
        .alert(
            "Permanently delete \(repository.owner.canonicalName)/\(repository.name)?",
            isPresented: $showDeleteConfirmation
        ) {
            Button("Cancel", role: .cancel) {
                // Alert dismissal is implicit; no additional action required.
            }
            Button("Delete", role: .destructive) {
                Task {
                    await viewModel.deleteRepository()
                    if viewModel.didDelete {
                        dismiss()
                        onDeleted()
                    }
                }
            }
        } message: {
            Text("This cannot be undone.")
        }
        .alert(item: $saveResultAlert) { alert in
            Alert(
                title: Text(alert.title),
                message: Text(alert.message),
                dismissButton: .default(Text("OK"))
            )
        }
    }

    // MARK: - Info Section

    @ViewBuilder
    private func infoSection(_ viewModel: RepositorySettingsViewModel) -> some View {
        Section("Info") {
            LabeledContent("Name") {
                Text(repository.name)
                    .font(.body.monospaced())
            }

            TextField("Description", text: Bindable(viewModel).editedDescription, axis: .vertical)
                .lineLimit(3...6)

            Picker("Visibility", selection: Bindable(viewModel).editedVisibility) {
                Text("Public").tag(Visibility.public)
                Text("Unlisted").tag(Visibility.unlisted)
                Text("Private").tag(Visibility.private)
            }

            if !viewModel.branches.isEmpty {
                Picker("Default Branch", selection: Bindable(viewModel).editedHead) {
                    ForEach(viewModel.branches, id: \.name) { branch in
                        let name = branch.name.replacingOccurrences(of: "refs/heads/", with: "")
                        Text(name).tag(name)
                    }
                }
            }

            Button {
                Task {
                    let didSave = await viewModel.saveInfo()
                    saveResultAlert = SaveResultAlert(
                        title: didSave ? "Settings Updated" : "Couldn't Update Settings",
                        message: didSave ? "Repository settings were saved." : (viewModel.error ?? "Please try again.")
                    )
                }
            } label: {
                if viewModel.isSavingInfo {
                    ProgressView()
                        .frame(maxWidth: .infinity)
                } else {
                    Text("Save Changes")
                        .frame(maxWidth: .infinity)
                }
            }
            .disabled(viewModel.isSavingInfo)
        }
    }

    // MARK: - Rename Section

    @ViewBuilder
    private func renameSection(_ viewModel: RepositorySettingsViewModel) -> some View {
        Section {
            TextField("New repository name", text: Bindable(viewModel).editedName)
                .autocorrectionDisabled()
                .textInputAutocapitalization(.never)

            Text("This will change the repository URL. Existing clones will be redirected but links may break.")
                .font(.caption)
                .foregroundStyle(.secondary)

            Button {
                showRenameConfirmation = true
            } label: {
                if viewModel.isRenaming {
                    ProgressView()
                        .frame(maxWidth: .infinity)
                } else {
                    Text("Rename Repository")
                        .frame(maxWidth: .infinity)
                }
            }
            .disabled(viewModel.isRenaming || viewModel.editedName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
        } header: {
            Text("Rename")
        }
    }

    // MARK: - Access Section

    @ViewBuilder
    private func accessSection() -> some View {
        Section {
            NavigationLink {
                RepositoryACLView(repository: repository, client: client, showsDoneButton: false)
            } label: {
                Label("Manage Access", systemImage: "person.2")
            }

            Text("Review and update repository access without leaving settings.")
                .font(.caption)
                .foregroundStyle(.secondary)
        } header: {
            Text("Access")
        }
    }

    // MARK: - Delete Section

    @ViewBuilder
    private func deleteSection(_ viewModel: RepositorySettingsViewModel) -> some View {
        Section {
            Button(role: .destructive) {
                showDeleteConfirmation = true
            } label: {
                if viewModel.isDeleting {
                    ProgressView()
                        .frame(maxWidth: .infinity)
                } else {
                    Text("Delete Repository")
                        .frame(maxWidth: .infinity)
                }
            }
            .disabled(viewModel.isDeleting)
        }
    }

    private struct SaveResultAlert: Identifiable {
        let title: String
        let message: String

        var id: String { "\(title)-\(message)" }
    }
}