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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
import SwiftUI
struct PasteDetailView: View {
let paste: Paste
var onUpdated: ((Paste) -> Void)? = nil
var onDeleted: ((String) -> Void)? = nil
@Environment(AppState.self) private var appState
@Environment(\.dismiss) private var dismiss
@State private var viewModel: PasteDetailViewModel?
@State private var showVisibilitySheet = false
@State private var showDeleteConfirmation = false
@State private var showInfoSheet = false
@State private var showFileShareSheet = false
@State private var showShareUnavailableAlert = false
@State private var didCopyContents = false
@State private var copyResetTask: Task<Void, Never>?
@AppStorage(AppStorageKeys.wrapPasteFileLines) private var wrapLines = false
var body: some View {
Group {
if let viewModel {
content(viewModel)
} else {
SRHTLoadingStateView(message: "Loading paste…")
}
}
.navigationTitle(displayTitle)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItemGroup(placement: .topBarTrailing) {
if viewModel != nil {
Menu {
if let url = currentPaste.flatMap({ SRHTWebURL.paste(ownerCanonicalName: $0.user.canonicalName, pasteId: $0.id) }) {
ShareLink(item: url) {
Label("Share Link", systemImage: "link")
}
}
Button {
showVisibilitySheet = true
} label: {
Label("Change Visibility", systemImage: "eye")
}
Button(role: .destructive) {
showDeleteConfirmation = true
} label: {
Label("Delete Paste", systemImage: "trash")
}
} label: {
Image(systemName: "ellipsis.circle")
}
}
}
}
.sheet(isPresented: $showVisibilitySheet) {
if let viewModel, let currentPaste {
PasteVisibilitySheet(
currentVisibility: currentPaste.visibility,
isUpdating: viewModel.isUpdatingVisibility
) { visibility in
if let updated = await viewModel.updateVisibility(visibility) {
onUpdated?(updated)
showVisibilitySheet = false
}
}
}
}
.alert("Delete Paste?", isPresented: $showDeleteConfirmation) {
Button("Cancel", role: .cancel) {
// Alert dismissal is implicit; no additional action required.
}
Button("Delete", role: .destructive) {
Task {
if await viewModel?.deletePaste() == true {
onDeleted?(paste.id)
dismiss()
}
}
}
} message: {
Text("This paste will be permanently removed.")
}
.task {
if viewModel == nil {
let vm = PasteDetailViewModel(
pasteID: paste.id,
initialPaste: paste,
service: PasteService(client: appState.client)
)
viewModel = vm
await vm.loadPaste()
}
}
}
private var currentPaste: Paste? {
viewModel?.paste ?? paste
}
private var displayTitle: String {
if let filename = currentPaste?.files.first?.filename, !filename.isEmpty {
return filename
}
return "Paste \(paste.id)"
}
@ViewBuilder
private func content(_ viewModel: PasteDetailViewModel) -> some View {
@Bindable var vm = viewModel
if viewModel.isLoading, viewModel.paste == nil {
SRHTLoadingStateView(message: "Loading paste…")
} else if let error = viewModel.error, viewModel.paste == nil {
SRHTErrorStateView(
title: "Couldn't Load Paste",
message: error,
retryAction: { await viewModel.loadPaste() }
)
} else if let paste = viewModel.paste {
VStack(spacing: 0) {
fileHeaderBar(paste: paste, viewModel: viewModel)
Divider()
fileContentArea(viewModel: viewModel)
}
.safeAreaInset(edge: .bottom, spacing: 0) {
actionToolbar(viewModel: viewModel)
}
.srhtErrorBanner(error: $vm.error)
.sheet(isPresented: $showInfoSheet) {
PasteInfoSheet(paste: paste, selectedFile: viewModel.selectedFile)
}
.sheet(isPresented: $showFileShareSheet) {
if let contents = viewModel.selectedFileContents {
FileContentShareSheet(activityItems: [contents])
}
}
.alert("Share Unavailable", isPresented: $showShareUnavailableAlert) {
Button("OK", role: .cancel) {
// Alert dismissal is implicit; no additional action required.
}
} message: {
Text(SRHTShareTarget.file.fallbackMessage)
}
}
}
// MARK: - File Header Bar
private func fileHeaderBar(paste: Paste, viewModel: PasteDetailViewModel) -> some View {
VStack(alignment: .leading, spacing: 6) {
HStack(alignment: .firstTextBaseline, spacing: 8) {
let filename = viewModel.selectedFile.flatMap { $0.filename.flatMap { $0.isEmpty ? nil : $0 } }
Text(filename ?? "Untitled")
.font(.subheadline.monospaced())
.foregroundStyle(.primary)
.lineLimit(1)
Spacer(minLength: 4)
VisibilityBadge(visibility: paste.visibility)
}
HStack(spacing: 4) {
Text(paste.user.canonicalName)
.font(.caption)
.foregroundStyle(.secondary)
Text("·")
.font(.caption)
.foregroundStyle(.tertiary)
Text(paste.created.relativeDescription)
.font(.caption)
.foregroundStyle(.secondary)
if let hash = viewModel.selectedFile?.hash {
Text("·")
.font(.caption)
.foregroundStyle(.tertiary)
Text(hash.prefix(8))
.font(.caption.monospaced())
.foregroundStyle(.tertiary)
}
}
if paste.files.count > 1 {
Picker(
"File",
selection: Binding(
get: { viewModel.selectedFileHash ?? paste.files.first?.hash ?? "" },
set: { viewModel.selectFile(hash: $0) }
)
) {
ForEach(paste.files) { file in
Text(file.filename ?? String(file.hash.prefix(8)))
.tag(file.hash)
}
}
.pickerStyle(.segmented)
}
}
.padding(.horizontal)
.padding(.vertical, 10)
.background(.bar)
}
// MARK: - File Content Area
@ViewBuilder
private func fileContentArea(viewModel: PasteDetailViewModel) -> some View {
if let file = viewModel.selectedFile {
if viewModel.loadingFileHashes.contains(file.hash) && viewModel.selectedFileContents == nil {
SRHTLoadingStateView(message: "Loading contents…")
} else if let contents = viewModel.selectedFileContents {
CodeFileTextView(
text: contents,
fileName: file.filename ?? "",
wrapLines: wrapLines
)
} else {
ContentUnavailableView(
"Contents Unavailable",
systemImage: "doc.questionmark",
description: Text("This file's contents could not be loaded.")
)
}
} else {
ContentUnavailableView(
"No File Selected",
systemImage: "doc",
description: Text("Select a file to view its contents.")
)
}
}
// MARK: - Action Toolbar
private func actionToolbar(viewModel: PasteDetailViewModel) -> some View {
HStack(spacing: 0) {
toolbarButton(
title: didCopyContents ? "Copied" : "Copy All",
systemImage: didCopyContents ? "checkmark" : "doc.on.doc"
) {
if let contents = viewModel.selectedFileContents {
UIPasteboard.general.string = contents
didCopyContents = true
copyResetTask?.cancel()
copyResetTask = Task {
try? await Task.sleep(for: .seconds(2))
guard !Task.isCancelled else { return }
await MainActor.run { didCopyContents = false }
}
}
}
.disabled(viewModel.selectedFileContents == nil)
toolbarButton(
title: "Share",
systemImage: "square.and.arrow.up"
) {
if let contents = viewModel.selectedFileContents, !contents.isEmpty {
showFileShareSheet = true
} else {
showShareUnavailableAlert = true
}
}
.disabled(viewModel.selectedFileContents == nil)
toolbarButton(
title: wrapLines ? "Wrap On" : "Wrap Off",
systemImage: "text.word.spacing"
) {
wrapLines.toggle()
}
toolbarButton(
title: "Details",
systemImage: "info.circle"
) {
showInfoSheet = true
}
}
.padding(.horizontal, 8)
.padding(.top, 10)
.padding(.bottom, 8)
.background(.bar)
.overlay(alignment: .top) {
Divider()
}
}
private func toolbarButton(
title: String,
systemImage: String,
action: @escaping () -> Void
) -> some View {
Button(action: action) {
VStack(spacing: 4) {
Image(systemName: systemImage)
.font(.system(size: 17, weight: .semibold))
Text(title)
.font(.caption2)
.lineLimit(1)
}
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.foregroundStyle(.primary)
}
}
// MARK: - Paste Info Sheet
private struct PasteInfoSheet: View {
let paste: Paste
let selectedFile: PasteFile?
@Environment(\.dismiss) private var dismiss
var body: some View {
NavigationStack {
List {
Section("Paste") {
LabeledContent("ID", value: paste.id)
.themedRow()
LabeledContent("Owner", value: paste.user.canonicalName)
.themedRow()
LabeledContent("Created", value: paste.created.relativeDescription)
.themedRow()
LabeledContent("Visibility") {
VisibilityBadge(visibility: paste.visibility)
}
.themedRow()
if paste.files.count > 1 {
LabeledContent("Files", value: "\(paste.files.count)")
.themedRow()
}
}
if let file = selectedFile {
Section("File") {
if let filename = file.filename, !filename.isEmpty {
LabeledContent("Filename", value: filename)
.themedRow()
}
LabeledContent("Hash") {
Text(file.hash)
.font(.caption.monospaced())
.foregroundStyle(.secondary)
.textSelection(.enabled)
}
.themedRow()
}
}
}
.listStyle(.insetGrouped)
.themedList()
.navigationTitle("Details")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") { dismiss() }
}
}
}
}
}
// MARK: - Visibility Sheet
private struct PasteVisibilitySheet: View {
let currentVisibility: Visibility
let isUpdating: Bool
let onSave: (Visibility) async -> Void
@Environment(\.dismiss) private var dismiss
@State private var visibility: Visibility
init(currentVisibility: Visibility, isUpdating: Bool, onSave: @escaping (Visibility) async -> Void) {
self.currentVisibility = currentVisibility
self.isUpdating = isUpdating
self.onSave = onSave
_visibility = State(initialValue: currentVisibility)
}
var body: some View {
NavigationStack {
List {
ForEach(visibilityOptions, id: \.self) { option in
Button {
visibility = option
} label: {
HStack {
VStack(alignment: .leading, spacing: 2) {
Text(title(for: option))
.foregroundStyle(.primary)
Text(description(for: option))
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
if visibility == option {
Image(systemName: "checkmark")
.foregroundStyle(.tint)
}
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
}
.themedRow()
}
.listStyle(.insetGrouped)
.navigationTitle("Visibility")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") { dismiss() }
}
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
Task {
await onSave(visibility)
}
}
.disabled(isUpdating || visibility == currentVisibility)
}
}
.overlay {
if isUpdating {
ProgressView()
}
}
.themedList()
}
}
private var visibilityOptions: [Visibility] {
[.publicVisibility, .unlisted, .privateVisibility]
}
private func title(for visibility: Visibility) -> String {
switch visibility {
case .publicVisibility:
"Public"
case .unlisted:
"Unlisted"
case .privateVisibility:
"Private"
}
}
private func description(for visibility: Visibility) -> String {
switch visibility {
case .publicVisibility:
"Visible to everyone and listed on your profile."
case .unlisted:
"Visible to anyone with the URL, but not listed on your profile."
case .privateVisibility:
"Visible only to explicitly allowed viewers."
}
}
}
|