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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
import SwiftUI
struct BuildListView: View {
@AppStorage(AppStorageKeys.swipeActionsEnabled, store: .standard) private var swipeActionsEnabled = true
@AppStorage(AppStorageKeys.buildsAutoRefreshInterval) private var autoRefreshRawValue = 0
@AppStorage(AppStorageKeys.buildsRepoFilter) private var savedRepoFilter = ""
@Environment(AppState.self) private var appState
@State private var viewModel: BuildListViewModel?
@State private var showSubmitSheet = false
@State private var submittedJobId: Int?
private var autoRefreshInterval: AutoRefreshInterval {
AutoRefreshInterval(rawValue: autoRefreshRawValue) ?? .off
}
var body: some View {
Group {
if let viewModel {
listContent(viewModel)
} else {
SRHTLoadingStateView(message: "Loading builds…")
}
}
.navigationTitle("Builds")
.toolbar {
if let viewModel {
ToolbarItem(placement: .topBarLeading) {
Menu {
Section("Auto-Refresh") {
ForEach(AutoRefreshInterval.allCases, id: \.self) { interval in
Button {
autoRefreshRawValue = interval.rawValue
viewModel.startAutoRefresh(interval: interval)
} label: {
if interval.rawValue == autoRefreshRawValue {
Label(interval.label, systemImage: "checkmark")
} else {
Text(interval.label)
}
}
}
}
Section("Filter by Tag") {
Button {
savedRepoFilter = ""
viewModel.repoFilter = ""
} label: {
if savedRepoFilter.isEmpty {
Label("All", systemImage: "checkmark")
} else {
Text("All")
}
}
ForEach(viewModel.availableTags, id: \.self) { tag in
Button {
savedRepoFilter = tag
viewModel.repoFilter = tag
} label: {
if savedRepoFilter == tag {
Label(tag, systemImage: "checkmark")
} else {
Text(tag)
}
}
}
}
} label: {
Image(systemName: "line.3.horizontal.decrease.circle")
}
.accessibilityLabel("Build filters")
}
ToolbarItem(placement: .topBarTrailing) {
Button {
showSubmitSheet = true
} label: {
Image(systemName: "plus")
}
.accessibilityLabel("Submit build")
}
}
}
.sheet(isPresented: $showSubmitSheet) {
if let viewModel {
SubmitBuildSheet(viewModel: viewModel) { jobId in
showSubmitSheet = false
submittedJobId = jobId
}
}
}
.navigationDestination(for: JobSummary.self) { job in
BuildDetailView(jobId: job.id)
}
.navigationDestination(isPresented: Binding(
get: { submittedJobId != nil },
set: { isPresented in
if !isPresented {
submittedJobId = nil
}
}
)) {
if let submittedJobId {
BuildDetailView(jobId: submittedJobId)
}
}
.task {
if viewModel == nil {
let vm = BuildListViewModel(client: appState.client, defaults: appState.accountDefaults)
vm.repoFilter = savedRepoFilter
viewModel = vm
await vm.loadJobs()
}
// Restart auto-refresh every time the view (re)appears, since
// onDisappear stops it when navigating away.
viewModel?.startAutoRefresh(interval: autoRefreshInterval)
}
.onDisappear {
viewModel?.stopAutoRefresh()
}
}
@ViewBuilder
private func listContent(_ viewModel: BuildListViewModel) -> some View {
@Bindable var vm = viewModel
List {
Section {
Picker("Filter", selection: $vm.filter) {
ForEach(BuildListFilter.allCases, id: \.self) { filter in
Text(filter.rawValue).tag(filter)
}
}
.pickerStyle(.segmented)
.padding(.horizontal, 16)
.padding(.top, 6)
.padding(.bottom, 10)
.listRowInsets(EdgeInsets())
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
ForEach(viewModel.filteredJobs) { job in
NavigationLink(value: job) {
BuildRowView(job: job)
.equatable()
}
.contextMenu {
Button {
appState.copyToPasteboard(String(job.id), label: "job ID")
} label: {
Label("Copy Job ID", systemImage: "doc.on.doc")
}
if let note = job.note, !note.isEmpty {
Button {
appState.copyToPasteboard(note, label: "build note")
} label: {
Label("Copy Note", systemImage: "text.alignleft")
}
}
if !job.tags.isEmpty {
Button {
appState.copyToPasteboard(job.tags.joined(separator: ", "), label: "build tags")
} label: {
Label("Copy Tags", systemImage: "tag")
}
}
}
.swipeActions(edge: .leading, allowsFullSwipe: true) {
if swipeActionsEnabled, job.status.isCancellable {
Button {
Task {
await viewModel.cancelJob(job)
}
} label: {
Label("Cancel", systemImage: "xmark.circle")
}
.tint(.red)
}
}
.task {
await viewModel.loadMoreIfNeeded(currentItem: job)
}
}
if viewModel.isLoadingMore {
HStack {
Spacer()
ProgressView()
Spacer()
}
.listRowSeparator(.hidden)
}
}
}
.themedList()
.listStyle(.plain)
.listSectionSpacing(.compact)
.searchable(
text: $vm.searchText,
placement: .navigationBarDrawer(displayMode: .always),
prompt: "Search builds by job ID, tag, note, or status"
)
.searchSuggestions {
if viewModel.searchText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
RecentSearchSuggestions(
title: "Recent Build Searches",
entries: viewModel.recentSearches
) { query in
vm.searchText = query
} onClear: {
viewModel.clearRecentSearches()
}
}
}
.onSubmit(of: .search) {
let query = viewModel.searchText.trimmingCharacters(in: .whitespacesAndNewlines)
guard !query.isEmpty else { return }
viewModel.recordRecentSearch(query)
}
.overlay {
if viewModel.isLoading, viewModel.jobs.isEmpty {
SRHTLoadingStateView(message: "Loading builds…")
} else if let error = viewModel.error, viewModel.jobs.isEmpty {
SRHTErrorStateView(
title: "Couldn't Load Builds",
message: error,
retryAction: { await viewModel.loadJobs() }
)
} else if !viewModel.searchText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
viewModel.filteredJobs.isEmpty {
ContentUnavailableView(
"No Build Matches",
systemImage: "magnifyingglass",
description: Text("No builds matched “\(viewModel.searchText)”.")
)
} else if viewModel.jobs.isEmpty, viewModel.error == nil {
ContentUnavailableView(
"No Builds",
systemImage: "hammer",
description: Text("Your build jobs will appear here.")
)
}
}
.connectivityOverlay(hasContent: !viewModel.jobs.isEmpty) {
await viewModel.loadJobs()
}
.srhtErrorBanner(error: $vm.error)
.refreshable {
await viewModel.loadJobs()
}
}
}
private struct SubmitBuildSheet: View {
let viewModel: BuildListViewModel
let onSubmitted: (Int) -> Void
@Environment(\.dismiss) private var dismiss
@Bindable var viewModelBindable: BuildListViewModel
@State private var manifest = ""
@State private var tagsText = ""
@State private var note = ""
@State private var secrets = false
@State private var execute = true
@State private var visibility: Visibility = .public
init(viewModel: BuildListViewModel, onSubmitted: @escaping (Int) -> Void) {
self.viewModel = viewModel
self._viewModelBindable = Bindable(viewModel)
self.onSubmitted = onSubmitted
}
var body: some View {
NavigationStack {
Form {
Section("Build Manifest") {
TextField("Paste a build manifest", text: $manifest, axis: .vertical)
.font(.system(.body, design: .monospaced))
.lineLimit(12...24)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
}
Section("Build Options") {
TextField("Note (optional)", text: $note)
TextField("Tags (comma-separated, optional)", text: $tagsText)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
Picker("Visibility", selection: $visibility) {
Text("Public").tag(Visibility.public)
Text("Unlisted").tag(Visibility.unlisted)
Text("Private").tag(Visibility.private)
}
Toggle("Start build now", isOn: $execute)
Toggle("Allow build secrets", isOn: $secrets)
}
Section {
Text("You need a valid builds.sr.ht manifest and a token with BUILDS:RW.")
.font(.footnote)
.foregroundStyle(.secondary)
}
if let error = viewModel.error {
Section {
Label {
Text(error)
} icon: {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundStyle(.red)
}
.foregroundStyle(.red)
}
}
}
.navigationTitle("Submit Build")
.navigationBarTitleDisplayMode(.inline)
.onDisappear {
viewModelBindable.error = nil
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
viewModelBindable.error = nil
dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Button {
Task {
let tags = tagsText
.split(separator: ",")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
if let jobId = await viewModel.submitBuild(
manifest: manifest,
tags: tags,
note: note,
secrets: secrets,
execute: execute,
visibility: visibility
) {
onSubmitted(jobId)
}
}
} label: {
if viewModel.isSubmitting {
ProgressView()
.controlSize(.small)
} else {
Text("Submit Build")
}
}
.disabled(manifest.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.isSubmitting)
}
}
}
}
}
|